private void UpdateRange(AbstractFragment range, AbstractPatch patch) { var start = Math.Max(patch.StartPosition, range.StartPosition); var end = Math.Min(patch.EndPosition, range.EndPosition); for (var position = start; position <= end; position++) { range.SetValue(position, this.updateFunc(patch, position)); } }
/// <inheritdoc/> public void RestorePatch(AbstractPatch patch) { var forwardRestoredSamples = this.GetForwardArray(patch); var backwardRestoredSamples = this.GetBackwardArray(patch); var joinedRestoredSamples = this.ApplyWindowFunction( forwardRestoredSamples, backwardRestoredSamples); patch.SetInternalArray(joinedRestoredSamples); patch.ErrorLevelAtStart = this.GetErrorLevelAtStart(patch); patch.ConnectionError = this.GetConnectionError( forwardRestoredSamples, backwardRestoredSamples); patch.ErrorLevelAfterEnd = this.GetErrorLevelAfterEnd(patch); }
private void UninstallPatch(PatchId patchId) { using (var transaction = this.context.CreateTransaction()) { AbstractPatch patch = AbstractPatch.LoadById(patchId, context); var patchInstallInfo = transaction.ExecuteReader( String.Format( "select {1} from {0} where {2} = {4} and {3} = {5} for update", transaction.EscapeName(this.context.PatchesTable), transaction.EscapeName("ROLLBACK_DATA"), transaction.EscapeName("VERSION"), transaction.EscapeName("NAME"), transaction.MarkParam("pversion"), transaction.MarkParam("pname") ), new Dictionary <string, object> { { "pversion", patchId.version }, { "pname", patchId.name }, } ).Single(); patch.Rollback(transaction, XDocument.Parse(patchInstallInfo["ROLLBACK_DATA"])); int affectedRows = transaction.ExecuteNonQuery( String.Format( "delete from {0} where {1} = {3} and {2} = {4}", transaction.EscapeName(this.context.PatchesTable), transaction.EscapeName("VERSION"), transaction.EscapeName("NAME"), transaction.MarkParam("pversion"), transaction.MarkParam("pname") ), new Dictionary <string, object> { { "pversion", patchId.version }, { "pname", patchId.name }, } ); if (affectedRows != 1) { throw new ApplicationException("Unable to install patch; are you trying to run two patchers simultaneously?"); } transaction.Commit(); } }
/// <inheritdoc/> public double GetErrorLevel(int position, AbstractPatch anotherPatch) { var errors = this.predictionErrPatcher.GetRange( position - this.normCalculator.InputDataSize, this.normCalculator.InputDataSize, anotherPatch); var normalError = this.normCalculator.GetResult(errors); var inputSamples = this.inputPatcher.GetRange( position - this.predictor.InputDataSize, this.predictor.InputDataSize, anotherPatch); var errorAtPosition = Math.Abs(this.predictor.GetForward(inputSamples) - this.inputPatcher.GetValue(position)); return(errorAtPosition / normalError); }
private AbstractPatch BestOf(List <AbstractPatch> patches) { const double ErrorAtStartWeight = 0.33; const double ConnectionErrorWeight = 0.33; const double ErrorAtEndWeight = 0.33; var maxErrorAtStart = patches.Select(p => p.ErrorLevelAtStart).Max(); var minErrorAtStart = patches.Select(p => p.ErrorLevelAtStart).Min(); var maxConnectionError = patches.Select(p => p.ConnectionError).Max(); var minConnectionError = patches.Select(p => p.ConnectionError).Min(); var maxErrorAtEnd = patches.Select(p => p.ErrorLevelAfterEnd).Max(); var minErrorAtEnd = patches.Select(p => p.ErrorLevelAfterEnd).Min(); AbstractPatch bestPatch = null; var bestPatchError = 0.0; foreach (var patch in patches) { var errorAtStartCoef = ErrorAtStartWeight * (patch.ErrorLevelAtStart - minErrorAtStart) / (maxErrorAtStart - minErrorAtStart); var connectionErrorCoef = ConnectionErrorWeight * (patch.ConnectionError - minConnectionError) / (maxConnectionError - minConnectionError); var errorAtEndCoef = ErrorAtEndWeight * (patch.ErrorLevelAfterEnd - minErrorAtEnd) / (maxErrorAtEnd - minErrorAtEnd); var patchError = errorAtStartCoef + connectionErrorCoef + errorAtEndCoef; if (bestPatch is null || patchError < bestPatchError) { bestPatch = patch; bestPatchError = patchError; } } return(bestPatch); }
/// <summary> /// Gets array containing sequence of patched samples of specified length /// starting from the specified position. /// </summary> /// <param name="start">Start position of range.</param> /// <param name="length">Length of range.</param> /// <param name="anotherPatch">One more optional AbstractPatch /// that is not in the Collection yet.</param> /// <returns>Array of patched samples.</returns> public double[] GetRange(int start, int length, AbstractPatch anotherPatch = null) { var range = new ArrayFragment( this.immutableArray, start, length); var patches = this.patchCollection.GetPatchesForRange(range); foreach (var patch in patches) { this.UpdateRange(range, patch); } if (anotherPatch != null) { this.UpdateRange(range, anotherPatch); } return(range.GetInternalArray()); }
private double GetErrorLevelAfterEnd(AbstractPatch patch) => (this.detector.GetErrorLevel(patch.EndPosition + 1, patch) + this.detector.GetErrorLevel(patch.EndPosition + 2, patch) + this.detector.GetErrorLevel(patch.EndPosition + 3, patch)) / 3;
private double GetErrorLevelAtStart(AbstractPatch patch) => this.detector.GetErrorLevel(patch.StartPosition, patch);
private void InstallPatch(PatchId patchId) { AbstractPatch patch = AbstractPatch.LoadById(patchId, this.context); if (!patch.DoesSupportEnvironment(this.context.EnvironmentName)) { this.context.console.Report(" (skipping because of unsupported environment) "); return; } using (var transaction = this.context.CreateTransaction()) { bool patchExistsInDB; XDocument rollbackData; var patchDBData = transaction.ExecuteReader( string.Format( "select {1} from {0} where {2} = {3} and {4} = {5}", transaction.EscapeName(this.context.PatchesTable), transaction.EscapeName("STATUS"), transaction.EscapeName("VERSION"), transaction.MarkParam("pversion"), transaction.EscapeName("NAME"), transaction.MarkParam("pname") ), new Dictionary <string, object> { { "pversion", patchId.version }, { "pname", patchId.name }, } ).ToList(); if (patch is PersistentPatch) { bool reinstall; if (patchDBData.Any()) { var patchInfo = patchDBData.Single(); switch (patchInfo["STATUS"]) { case STATUS_INSTALLED: throw new ApplicationException("Patch is already installed"); case STATUS_INSTALLING: reinstall = true; break; default: throw new ApplicationException(string.Format("Unknown status {0}", patchInfo["STATUS"])); } } else { reinstall = false; } if (reinstall) { patchExistsInDB = true; } throw new NotImplementedException("Persistent patch installation is not implemented yet"); } else { if (patchDBData.Any()) { throw new ApplicationException("Patch is already installed"); } patchExistsInDB = false; rollbackData = patch.Apply(transaction); } //System.Threading.Thread.Sleep(1000); int affectedRows = transaction.ExecuteNonQuery( String.Format( patchExistsInDB ? "update {0} set {5} = {6}, {7} = {8} where {1} = {2} and {3} = {4}" : "insert into {0}({1}, {3}, {5}, {7}) values({2}, {4}, {6}, {8})" , transaction.EscapeName(this.context.PatchesTable), transaction.EscapeName("VERSION"), transaction.MarkParam("pversion"), transaction.EscapeName("NAME"), transaction.MarkParam("pname"), transaction.EscapeName("ROLLBACK_DATA"), transaction.MarkParam("prollbackdata"), transaction.EscapeName("STATUS"), transaction.MarkParam("pstatus") ), new Dictionary <string, object> { { "pversion", patchId.version }, { "pname", patchId.name }, { "prollbackdata", rollbackData.ToString() }, { "pstatus", STATUS_INSTALLED }, } ); if (affectedRows != 1) { throw new ApplicationException("Unable to install patch; are you trying to run two patchers simultaneously?"); } transaction.Commit(); } }