public void success_of_nothing_has_a_shortcut() { var result = PartialResult.Success(); Assert.That(result.State, Is.EqualTo(PartialResultState.Success)); Assert.That(result.ResultData, Is.EqualTo(Nothing.Instance)); }
public void adding_an_error_and_a_warning_results_in_an_error() { var result_1 = PartialResult <int> .Warning("warn") .Failure("err") .Failure(new Exception("ex")); var result_2 = PartialResult <int> .Failure("fail") .Warning("warn"); var result_3 = PartialResult <int> .Failure(new Exception("fail")) .Warning("warn"); Assert.That(result_1.State, Is.EqualTo(PartialResultState.Failed)); Assert.That(result_2.State, Is.EqualTo(PartialResultState.Failed)); Assert.That(result_3.State, Is.EqualTo(PartialResultState.Failed)); Assert.That(string.Join(",", result_1.CauseMessages), Is.EqualTo("warn,err,ex")); Assert.That(string.Join(",", result_2.CauseMessages), Is.EqualTo("fail,warn")); Assert.That(string.Join(",", result_3.CauseMessages), Is.EqualTo("fail,warn")); }
/// <summary> /// Add the specified values to the result if they are within the top-N. /// </summary> /// <param name="values">The collection of values to add.</param> /// <param name="result">The result.</param> protected void AddToResult(ICollection values, PartialResult result) { int maxSize = Results; int curSize = result.Count; object elemFirst = null; foreach (object value in values) { if (value == null) { continue; } if (curSize < maxSize) { result.Add(value); } else { if (elemFirst == null) { elemFirst = result.First; } if (Comparer.Compare(value, elemFirst) > 0) { result.Add(value); result.RemoveFirst(); elemFirst = null; } } ++curSize; } }
public PartialResult getPartialResult() { List <Coalition> coalitions = calculator.getPartialResult(); if (coalitions != null) // if there was result yet { List <string> coals = new List <string>(); List <int> profits = new List <int>(); foreach (var coalition in coalitions) { coals.Add(coalition.ToString() + " "); profits.Add(Convert.ToInt32(coalition.getMaximumValue())); } PartialResult result = new PartialResult(); result.Coalitions = coals; result.Profits = profits; result.End = false; return(result); } else // if there was no result { if (!calculatorThread.IsAlive) //if thread is done { PartialResult result = new PartialResult(); result.End = true; return(result); } else { return(null); } } }
public void can_create_a_success_result_and_read_it_back() { var result = PartialResult.Success(1); Assert.That(result.IsSuccess, Is.True, "Should have been success, but was not"); Assert.That(result.IsFailure, Is.False, "Should NOT have been failure, but was"); Assert.That(result.State, Is.EqualTo(PartialResultState.Success), $"Should have been 'complete', but was '{result.State}'"); Assert.That(result.ResultData, Is.EqualTo(1), "Stored data was incorrect"); }
public static PartialResult CreatePartialResult(IEnumerable <double> values) { PartialResult result = new PartialResult { Time = Math.Round(values.Average(), 3), Deviation = Math.Round(StatisticsUtil.StandardDeviation(values), 3) }; return(result); }
public void adding_success_and_failure_results_in_failure() { var result_1 = PartialResult <int> .Success(1).Failure("err"); var result_2 = PartialResult.Success(2).Failure("fail"); var result_3 = PartialResult <int> .Failure("fail").Success(1); Assert.That(result_1.State, Is.EqualTo(PartialResultState.Failed)); Assert.That(result_2.State, Is.EqualTo(PartialResultState.Failed)); Assert.That(result_3.State, Is.EqualTo(PartialResultState.Failed)); }
/// <summary> /// Aggregate the results of the parallel aggregations, producing a /// partial result logically representing the partial aggregation. The /// returned partial result will be further {@link /// ParallelAwareAggregator#aggregateResults aggregated} to produce /// the final result. /// </summary> /// <param name="colPartialResults"> /// The partial results to agregate. /// </param> /// <returns> /// An aggregattion of the collection of partial results. /// </returns> public virtual object AggregatePartialResults(ICollection colPartialResults) { PartialResult resultMerged = new PartialResult(Comparer); foreach (PartialResult resultPartial in colPartialResults) { AddToResult(resultPartial.Results, resultMerged); } return(resultMerged); }
/// <summary> /// Process a set of <see cref="IInvocableCacheEntry"/> objects /// in order to produce an aggregated result. /// </summary> /// <param name="entries"> /// A collection of read-only <b>IInvocableCacheEntry</b> /// objects to aggregate. /// </param> /// <returns> /// The aggregated result from processing the entries. /// </returns> public virtual object Aggregate(ICollection entries) { PartialResult resultPartial = new PartialResult(Comparer); IConverter converter = new ExtractingConverter(Extractor); AddToResult(ConverterCollections.GetCollection(entries, converter, converter), resultPartial); return(IsParallel ? (object)resultPartial : (object)FinalizeResult(resultPartial)); }
public void can_change_the_contained_type_of_a_failure_result_to_help_error_propagation() { var expected = new Exception("hello"); var original = PartialResult <int> .Failure(expected); // ReSharper disable SuggestVarOrType_Elsewhere PartialResult <string> propagated_1 = original.PropagateFailure <string>(); PartialResult <float> propagated_2 = propagated_1.PropagateFailure <float>(); // ReSharper restore SuggestVarOrType_Elsewhere Assert.That(propagated_2.Causes.Single(), Is.EqualTo(expected)); }
public void failure_of_nothing_has_a_shortcut() { var result_1 = PartialResult.Failure("err"); var result_2 = PartialResult.Failure(new Exception("err")); Assert.That(result_1.State, Is.EqualTo(PartialResultState.Failed)); Assert.That(result_2.State, Is.EqualTo(PartialResultState.Failed)); Assert.That(string.Join(",", result_1.CauseMessages), Is.EqualTo("err")); Assert.That(string.Join(",", result_2.CauseMessages), Is.EqualTo("err")); Assert.That(result_1.ResultData, Is.EqualTo(Nothing.Instance)); Assert.That(result_2.ResultData, Is.EqualTo(Nothing.Instance)); }
public void warning_of_nothing_has_a_shortcut() { var result_1 = PartialResult.Warning("warn"); var result_2 = PartialResult.Warning(new Exception("warn")); Assert.That(result_1.State, Is.EqualTo(PartialResultState.Warning)); Assert.That(result_2.State, Is.EqualTo(PartialResultState.Warning)); Assert.That(string.Join(",", result_1.CauseMessages), Is.EqualTo("warn")); Assert.That(string.Join(",", result_2.CauseMessages), Is.EqualTo("warn")); Assert.That(result_1.ResultData, Is.EqualTo(Nothing.Instance)); Assert.That(result_2.ResultData, Is.EqualTo(Nothing.Instance)); }
public static ResultViewModel PrepareResult(String name, PartialResult result1, PartialResult result10, PartialResult result20, PartialResult result50, PartialResult result100) { ResultViewModel result = new ResultViewModel() { Name = name, Result1 = result1, Result10 = result10, Result20 = result20, Result50 = result50, Result100 = result100 }; return(result); }
public async Task <PartialResult <T> > QuerySegmented(T low, T high, int take = 20, string ct = null) { var lowKey = low.Key(); var highKey = high.Key(); var iter = this.db.NewIterator(ReadOptions.Default); if (!string.IsNullOrEmpty(ct)) { iter.Seek(ct); } else { iter.Seek(lowKey); } List <T> result = new List <T>(); int count = 0; for (/* pass */; iter.Valid() && iter.Key() < highKey; iter.Next()) { result.Add(this.Deserializer(iter.Value().ToArray())); count++; if (count == take) { break; } } if (!iter.Valid() || iter.Key() >= highKey) { var partialResult = new PartialResult <T>() { Result = result, ContinuationToken = null }; return(await Task.FromResult(partialResult)); } else { var partialResult = new PartialResult <T>() { Result = result, ContinuationToken = iter.Key().ToString() }; return(await Task.FromResult(partialResult)); } }
public void can_create_a_warning_result_with_data_and_more_than_one_error_cause() { var result_1 = PartialResult .WithData("data 1") .Warning("string warning") .Warning(new Exception("exception warning")); var result_2 = PartialResult <string> .Warning("String warning") .WithData("data 2"); var result_3 = PartialResult <string> .Warning(new Exception("exception warning")) .WithData("data 3"); // Assert on multiple causes Assert.That(result_1.Causes.Count, Is.EqualTo(2), "Wrong cause count"); Assert.That(result_2.Causes.Count, Is.EqualTo(1), "Wrong cause count"); Assert.That(result_3.Causes.Count, Is.EqualTo(1), "Wrong cause count"); // Assert on the rest of the result Assert.That(result_1.State, Is.EqualTo(PartialResultState.Warning), "Wrong state"); Assert.That(result_2.State, Is.EqualTo(PartialResultState.Warning), "Wrong state"); Assert.That(result_3.State, Is.EqualTo(PartialResultState.Warning), "Wrong state"); Assert.That(result_1.IsFailure, Is.True, "Failure state wrong"); Assert.That(result_2.IsFailure, Is.True, "Failure state wrong"); Assert.That(result_3.IsFailure, Is.True, "Failure state wrong"); Assert.That(result_1.IsSuccess, Is.False, "Success state wrong"); Assert.That(result_2.IsSuccess, Is.False, "Success state wrong"); Assert.That(result_3.IsSuccess, Is.False, "Success state wrong"); Assert.That(result_1.ResultData, Is.EqualTo("data 1")); Assert.That(result_2.ResultData, Is.EqualTo("data 2")); Assert.That(result_3.ResultData, Is.EqualTo("data 3")); }
/// <summary> /// Create a result from a partial result. /// </summary> /// <typeparam name="TInput"></typeparam> /// <typeparam name="TOutput"></typeparam> /// <param name="state"></param> /// <param name="parser"></param> /// <param name="part"></param> /// <returns></returns> public static IResult <TOutput> Result <TInput, TOutput>(this IParseState <TInput> state, IParser <TInput, TOutput> parser, PartialResult <TOutput> part) { if (part.Success) { return(Success(state, parser, part.Value !, part.Consumed, part.Location)); } return(Fail(state, parser, part.ErrorMessage !, part.Location)); }
private PartialResult AddRoll(PartialResult result) => new(
/// <summary> /// Finalize the partial aggregation result. /// </summary> /// <param name="result"> /// The final aggregation result. /// </param> /// <returns> /// The finalized partial aggregation result. /// </returns> protected object[] FinalizeResult(PartialResult result) { object[] results = CollectionUtils.ToArray(result.Results); Array.Reverse(results); return(results); }
internal void Add(PartialResult partialResult) { Results.Add(partialResult); }
} // returns remaining valid region of target public bool Equals(PartialResult <S1> that) { return(this.value1.Equals(that.value1) && this.index2 == that.index2); }
/// <summary> /// Parses input tokens supplied by the <i>lexer</i> and returns the result. /// </summary> /// <param name="lexer">The ILexer which will supply the Parser with tokens.</param> /// <returns>An object containing the value returned for the root nonterminal.</returns> /// <exception cref="ParsingException">when the lexer's output doesn't conform to the grammar's rules or /// when the lexer throws a ParsingException.</exception> public object Parse(ILexer lexer, object userObject) { Stack <PartialResult> resultStack = new Stack <PartialResult>(); Stack <int> stateStack = new Stack <int>(); //počáteční stav stateStack.Push(0); bool done = false; Token nextToken = lexer.GetNextToken(); int state = stateStack.Peek(); while (!done && (lexer.HasTokens || ((state >= 0) && (parseTable[state, nextToken.SymbolCode].ActionType != ParserActionType.Fail)))) { ParserAction nextAction = parseTable[state, nextToken.SymbolCode]; switch (nextAction.ActionType) { case ParserActionType.Shift: resultStack.Push(new PartialResult(nextToken.Value, symbolNames[nextToken.SymbolCode], nextToken.LineNumber, nextToken.ColumnNumber)); stateStack.Push(nextAction.Argument); state = stateStack.Peek(); nextToken = lexer.GetNextToken(); break; case ParserActionType.Reduce: //podle informací o patřičném přepisovacím pravidle odebereme ze zásobníků //příslušný počet prvků ProductionOutline production = productions[nextAction.Argument]; Stack <PartialResult> constituents = new Stack <PartialResult>(); for (int i = 0; i < production.NumRHSSymbols; i++) { constituents.Push(resultStack.Pop()); stateStack.Pop(); } state = stateStack.Peek(); // We take the values of the constituents and compute the value of the composite // element using the relevant action. This new result replaces the old ones on the // result stack; the accompanying state for the state stack is found in the goto // table of the parser. The line and column number for the new result are taken from // the first constituent. If the nonterminal has no constituents, we take the position // of the upcoming token. int numConstituents = constituents.Count; object[] constituentValues = new object[numConstituents]; int[] constituentLines = new int[numConstituents]; int[] constituentColumns = new int[numConstituents]; for (int i = 0; i < numConstituents; i++) { PartialResult constituent = constituents.Pop(); constituentValues[i] = constituent.Value; constituentLines[i] = constituent.LineNumber; constituentColumns[i] = constituent.ColumnNumber; } object result = actions[nextAction.Argument](constituentValues, constituentLines, constituentColumns, userObject); if (numConstituents > 0) { resultStack.Push(new PartialResult(result, symbolNames[production.LHSSymbol], constituentLines[0], constituentColumns[0])); } else { resultStack.Push(new PartialResult(result, symbolNames[production.LHSSymbol], nextToken.LineNumber, nextToken.ColumnNumber)); } stateStack.Push(gotoTable[state, production.LHSSymbol - numTerminals]); state = stateStack.Peek(); //přepisovací pravidlo 0 je vždy $start ::= <start-symbol> $end a provedení //redukce podle tohoto pravidla znamená, že jsme načetli i $end a podařilo //se nám vstup rozparsovat, tudíž končíme; //pokud by se nám snažil lexer ještě nějaké tokeny vrazit, tak to ohlásíme if (nextAction.Argument == 0) { done = true; } break; case ParserActionType.Fail: StringBuilder expectedTerminals = new StringBuilder(); for (int terminal = 0; terminal < numTerminals; terminal++) { if (parseTable[state, terminal].ActionType != ParserActionType.Fail) { expectedTerminals.Append(", "); expectedTerminals.Append(symbolNames[terminal]); } } throw new ParsingException(string.Format("Unexpected terminal {0}({1}) encountered by the parser, expected one of the following terminals: {2}.", symbolNames[nextToken.SymbolCode], nextToken.Value, expectedTerminals.ToString(2, expectedTerminals.Length - 2)), nextToken.LineNumber, nextToken.ColumnNumber); } } if (lexer.HasTokens) { //lexer nám chce něco říct, ale my už jsme hotovi; tohle by se s naším lexerem nemělo nikdy //stát, jelikož po tom, co vrátí $end, už další tokeny nenabízí nextToken = lexer.GetNextToken(); throw new ParsingException(string.Format("There are additional symbols in the input string starting with terminal {0}({1}).", symbolNames[nextToken.SymbolCode], nextToken.Value), nextToken.LineNumber, nextToken.ColumnNumber); } else if ((resultStack.Count == 1) && (resultStack.Peek().SymbolName == "$start")) { //vše je, jak má být return(resultStack.Pop().Value); } else if (resultStack.Count == 0) { throw new ParsingException("There were no symbols in the input.", nextToken.LineNumber, nextToken.ColumnNumber); } else { //tohle znamená, že parser nebyl ještě se vstupem spokojen a očekává další symboly StringBuilder symbolsOnStack = new StringBuilder(); foreach (PartialResult stackItem in resultStack.Reverse()) { symbolsOnStack.Append(", "); symbolsOnStack.Append(stackItem.SymbolName); } throw new ParsingException("The entire input was reduced to more than one symbol: " + symbolsOnStack.ToString(2, symbolsOnStack.Length - 2) + ". Input text was probably incomplete.", nextToken.LineNumber, nextToken.ColumnNumber); } }