Exemple #1
0
        public double Calculate(string formulaText, IDictionary <string, double> variables)
        {
            if (string.IsNullOrEmpty(formulaText))
            {
                throw new ArgumentNullException("formulaText");
            }

            if (variables == null)
            {
                throw new ArgumentNullException("variables");
            }

            if (!caseSensitive)
            {
                variables = EngineUtil.ConvertVariableNamesToLowerCase(variables);
            }
            VerifyVariableNames(variables);

            // Add the reserved variables to the dictionary
            foreach (ConstantInfo constant in ConstantRegistry)
            {
                variables.Add(constant.ConstantName, constant.Value);
            }

            if (IsInFormulaCache(formulaText, null, out var function))
            {
                return(function(variables));
            }
            else
            {
                Operation operation = BuildAbstractSyntaxTree(formulaText, new ConstantRegistry(caseSensitive));
                function = BuildFormula(formulaText, null, operation);
                return(function(variables));
            }
        }
Exemple #2
0
        public double Calculate(string formulaText, Dictionary <string, double> variables)
        {
            if (formulaText == null)
            {
                throw new ArgumentNullException("formulaText");
            }

            if (variables == null)
            {
                throw new ArgumentNullException("variables");
            }

            variables = EngineUtil.ConvertVariableNamesToLowerCase(variables);
            VerifyVariableNames(variables);

            // Add the reserved variables to the dictionary
            foreach (ConstantInfo constant in ConstantRegistry)
            {
                variables.Add(constant.ConstantName, constant.Value);
            }

            if (IsInFormulaCache(formulaText))
            {
                Func <Dictionary <string, double>, double> formula = executionFormulaCache[formulaText];
                return(formula(variables));
            }
            else
            {
                Operation operation = BuildAbstractSyntaxTree(formulaText);
                Func <Dictionary <string, double>, double> function = BuildFormula(formulaText, operation);

                return(function(variables));
            }
        }