/// <summary> /// Evaluate the current Formula with a passed list of arguments. /// </summary> /// <param name="arguments">The name/value pairs of arguments.</param> /// <returns>An object representing the value of the Formula.</returns> public object Evaluate(IDictionary arguments) { if (resolutionType == FormulaResolutionType.NxBRE) { if (evaluator == null) { evaluator = Compilation.NewEvaluator(expression, DEFAULT_EXPRESSION_PLACEHOLDER, DEFAULT_NUMERIC_ARGUMENT_PATTERN, arguments); } return(evaluator.Run(arguments)); } else if (resolutionType == FormulaResolutionType.Binder) { if (formulaSignature == null) { formulaSignature = Parameter.BuildFormulaSignature(expression); } // if arguments have been passed in the formula signature, pass them to the binder as // a special entry in the arguments IDictionary (this approach has been preferred to modifying the // Compute method signature which would have broken this compatibility if (formulaSignature.Arguments.Count > 0) { arguments.Add(typeof(Parameter), formulaSignature.Arguments); } return(bob.Compute(formulaSignature.Name, arguments)); } else { throw new BREException("Formula evaluation mode not supported: " + resolutionType); } }
/// <summary> /// Instantiates a new Formula predicate. /// </summary> /// <param name="resolutionType">The type of resolution for the formula.</param> /// <param name="bob">The business object binder to use when evaluating the formula, or null.</param> /// <param name="expression">The expression value, i.e. the C# code source that should be computable.</param> /// <param name="evaluator">A precompiled evaluator, or null.</param> private Formula(FormulaResolutionType resolutionType, IBinder bob, string expression, IDictionaryEvaluator evaluator):base(expression) { this.resolutionType = resolutionType; this.bob = bob; this.expression = expression; this.evaluator = evaluator; this.functionSignature = null; }
/// <summary> /// Instantiates a new Formula predicate. /// </summary> /// <param name="resolutionType">The type of resolution for the formula.</param> /// <param name="bob">The business object binder to use when evaluating the formula, or null.</param> /// <param name="expression">The expression value, i.e. the C# code source that should be computable.</param> /// <param name="evaluator">A precompiled evaluator, or null.</param> private Formula(FormulaResolutionType resolutionType, IBinder bob, string expression, IDictionaryEvaluator evaluator) : base(expression) { this.resolutionType = resolutionType; this.bob = bob; this.expression = expression; this.evaluator = evaluator; this.formulaSignature = null; }
public void CompileThenChangeTypeError() { IDictionary values = new Hashtable(); values.Add("a", 4d); IDictionaryEvaluator compiledEvaluator = Compilation.NewEvaluator("5 + System.Math.Pow(2d, {var:a})", @"\{var:(?<1>[^}]*)\}", values); Assert.AreEqual(21d, compiledEvaluator.Run(values)); // change the type of a, the evaluation should fail values = new Hashtable(); values.Add("a", "4"); Assert.AreEqual(21d, compiledEvaluator.Run(values)); }
/// <summary> /// Evaluate the current Formula with a passed list of arguments. /// </summary> /// <param name="arguments">The name/value pairs of arguments.</param> /// <returns>An object representing the value of the Formula.</returns> public object Evaluate(IDictionary arguments) { if (resolutionType == FormulaResolutionType.NxBRE) { if (evaluator == null) evaluator = Compilation.NewEvaluator(expression, DEFAULT_EXPRESSION_PLACEHOLDER, DEFAULT_NUMERIC_ARGUMENT_PATTERN, arguments); return evaluator.Run(arguments); } else if (resolutionType == FormulaResolutionType.Binder) { if (functionSignature == null) functionSignature = Parameter.BuildFunctionSignature(expression, new object[0]); return bob.Compute(functionSignature, arguments); } else throw new BREException("Formula evaluation mode not supported: " + resolutionType); }
public void CompiledDictionaryExpressions() { IDictionary values = new Hashtable(); values.Add("a", 4d); // compile the evaluator a first time, using the values IDictionary as a template for // argument types IDictionaryEvaluator compiledEvaluator = Compilation.NewEvaluator("5 + System.Math.Pow(2d, {var:a})", @"\{var:(?<1>[^}]*)\}", values); // then use the evaluator Assert.AreEqual(21d, compiledEvaluator.Run(values)); // and use it again with different values values = new Hashtable(); values.Add("a", 3d); Assert.AreEqual(13d, compiledEvaluator.Run(values)); }
/// <summary> /// Evaluate the current Formula with a passed list of arguments. /// </summary> /// <param name="arguments">The name/value pairs of arguments.</param> /// <returns>An object representing the value of the Formula.</returns> public object Evaluate(IDictionary arguments) { if (resolutionType == FormulaResolutionType.NxBRE) { if (evaluator == null) evaluator = Compilation.NewEvaluator(expression, DEFAULT_EXPRESSION_PLACEHOLDER, DEFAULT_NUMERIC_ARGUMENT_PATTERN, arguments); return evaluator.Run(arguments); } else if (resolutionType == FormulaResolutionType.Binder) { if (formulaSignature == null) formulaSignature = Parameter.BuildFormulaSignature(expression); // if arguments have been passed in the formula signature, pass them to the binder as // a special entry in the arguments IDictionary (this approach has been preferred to modifying the // Compute method signature which would have broken this compatibility if (formulaSignature.Arguments.Count > 0) { // patch to BUG 1815223 submitted by Amitay Dolbo Hashtable formulaArguments = new Hashtable(arguments); formulaArguments.Add(typeof(Parameter), formulaSignature.Arguments); return bob.Compute(formulaSignature.Name, formulaArguments); } else { return bob.Compute(formulaSignature.Name, arguments); } } else { throw new BREException("Formula evaluation mode not supported: " + resolutionType); } }