/// <summary> /// Converts this instance to a fixed array reference. /// </summary> public FixedArrayRef AsFixed() { LiteralReference lr = ArrayExpr as LiteralReference; if (lr == null) { return(null); } object obj; if (!lr.ReferencedObject.IsConst(out obj)) { return(null); } Array array = obj as Array; if (array == null) { return(null); } long[] constIndices = new long[Indices.Length]; IEvaluator eval = new DefaultEvaluator(); for (int i = 0; i < Indices.Length; i++) { Expression index = Indices[i]; if (!index.IsConst()) { constIndices = null; break; } constIndices[i] = TypeConversions.ToLong(index.Eval(eval)); } return(new FixedArrayRef(lr.ReferencedObject, array, constIndices)); }
public static T Evaluate <T>(RPNExpression expression) { try { var context = new RPNContext(expression); while (context.CanMove) { context.MoveNext(); if (DefaultEvaluator.Evaluate(context)) { continue; } if (BasicEvaluator.Evaluate(context)) { continue; } if (MathEvaluator.Evaluate(context)) { continue; } if (LogicEvaluator.Evaluate(context)) { continue; } if (StringEvaluator.Evaluate(context)) { continue; } if (RegexEvaluator.Evaluate(context)) { continue; } if (DateTimeEvaluator.Evaluate(context)) { continue; } if (ControlEvaluator.Evaluate <T>(context)) { continue; } context.Stack.Push(context.Current); } return(context.GetResult <T>()); } catch (RPNException) { throw; } catch { throw new ParsingException(expression.Expression); } }
public void Evaluate_GivenValidRules_ReturnsCorrectResultSet() { var compiler = new DefaultCompiledRuleStore(new DefaultRuleStore()); var ruleDtos = new List <RuleDto> { new RuleDto { Name = "Holding NAV within 10 of Fund NAV", RuleExpression = "ensure holding.GetNav() is within 10 of fund.Nav", TargetTypeName = "Holding" }, new RuleDto { Name = "Holding NAV is greater than Fund NAV", RuleExpression = "ensure holding.Nav is greaterThan fund.GetNav()", TargetTypeName = "Holding" }, new RuleDto { Name = "Holding NAV is equal to Fund NAV", RuleExpression = "ensure holding.Nav is equalTo fund.Nav", TargetTypeName = "Holding" } }; foreach (var ruleDto in ruleDtos) { compiler.AddRule <DefaultInterpreterTests.Holding, DefaultInterpreterTests.Fund>(ruleDto); } var sut = new DefaultEvaluator(compiler); var holding = new DefaultInterpreterTests.Holding { Nav = 25 }; var fund = new DefaultInterpreterTests.Fund { Nav = 20 }; var expected = new ResultSet { Status = ResultStatus.Fail, Results = new List <Result> { new Result { RuleName = "Holding NAV within 10 of Fund NAV", RuleExpression = "ensure holding.GetNav() is within 10 of fund.Nav", Status = ResultStatus.Pass }, new Result { RuleName = "Holding NAV is greater than Fund NAV", RuleExpression = "ensure holding.Nav is greaterThan fund.GetNav()", Status = ResultStatus.Pass }, new Result { RuleName = "Holding NAV is equal to Fund NAV", RuleExpression = "ensure holding.Nav is equalTo fund.Nav", Status = ResultStatus.Fail } } }; var actual = sut.Evaluate(holding, fund); Assert.That(actual, Is.EqualTo(expected)); }