コード例 #1
0
ファイル: TestCompilation.cs プロジェクト: ilkerhalil/NxBRE
        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));
        }
コード例 #2
0
ファイル: Formula.cs プロジェクト: vkiktev/NxBRE
        /// <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);
            }
        }
コード例 #3
0
        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));
        }