Exemplo n.º 1
0
        public string parseVariableFormula(List<Variable> allVariables, List<Variable> treeVariables)
        {
            if (treeVariables.Contains(this))
                return "Recursive variable reference detected.";

            treeVariables.Add(this);

            Dictionary<string, Variable> variableNames = new Dictionary<string, Variable>();
            foreach (Variable var in allVariables)
            {
                if (variableNames.ContainsKey(var.VariableName))
                    variableNames[var.VariableName] = null;
                else
                    variableNames.Add(var.VariableName, var);
            }

            dotMath.EqCompiler eq = new dotMath.EqCompiler(variableFormula, true);

               // eq.AddFunction(new CPow());

            try
            {
                eq.Compile();
            }
            catch (Exception e)
            {
                return e.Message;
            }

            string[] usedVariables = eq.GetVariableList();

            if (usedVariables != null)
            {
                if (usedVariables.Length != 0)
                {

                    for (int i = 0; i < usedVariables.Length; i++)
                    {

                        if (!variableNames.ContainsKey(usedVariables[i]))
                        {
                            VariableValue = 0;
                            return "Unable to parse formula. There is no variable named " + usedVariables[i] + ".";
                        }

                        if (variableNames[usedVariables[i]] == null)
                        {
                            VariableValue = 0;
                            return "Unable to parse formula. There are multiple variables named " + usedVariables[i] + ".";
                        }

                        Variable currentVar = variableNames[usedVariables[i]];

                        if (currentVar.DerivedVariable)
                        {
                            string err = currentVar.parseVariableFormula(allVariables, treeVariables);
                            if (err != null)
                            {
                                VariableValue = 0;
                                return err;
                            }
                        }

                        eq.SetVariable(usedVariables[i], currentVar.VariableValue);
                    }
                }
            }

            try
            {
                this.VariableValue = eq.Calculate();
            }
            catch (Exception e)
            {
                VariableValue = 0;
                return e.Message;
            }

            treeVariables.Remove(this);

            return null;
        }
Exemplo n.º 2
0
        private string parseVariableFormula(List <Variable> allVariables, List <Variable> treeVariables)
        {
            if (treeVariables.Contains(this))
            {
                return(EQUATION_ERROR_RECURSIVE);
            }

            treeVariables.Add(this);

            Dictionary <string, Variable> variableNames = new Dictionary <string, Variable>();

            foreach (Variable var in allVariables)
            {
                if (variableNames.ContainsKey(var.VariableName))
                {
                    variableNames[var.VariableName] = null;
                }
                else
                {
                    variableNames.Add(var.VariableName, var);
                }
            }

            dotMath.EqCompiler eq = new dotMath.EqCompiler(variableFormula, true);

            // eq.AddFunction(new CPow());


            try
            {
                eq.Compile();
            }
            catch (Exception e)
            {
                return(e.Message);
            }

            string[] usedVariables = eq.GetVariableList();

            if (usedVariables != null)
            {
                for (int i = 0; i < usedVariables.Length; i++)
                {
                    if (!variableNames.ContainsKey(usedVariables[i]))
                    {
                        VariableValue = 0;
                        return("Unable to parse formula. There is no variable named " + usedVariables[i] + ".");
                    }

                    if (variableNames[usedVariables[i]] == null)
                    {
                        VariableValue = 0;
                        return("Unable to parse formula. There are multiple variables named " + usedVariables[i] + ".");
                    }

                    Variable currentVar = variableNames[usedVariables[i]];

                    if (currentVar.DerivedVariable)
                    {
                        string err = currentVar.parseVariableFormula(allVariables, treeVariables);
                        if (err != null)
                        {
                            VariableValue = 0;
                            return(err);
                        }
                    }

                    eq.SetVariable(usedVariables[i], currentVar.VariableValue);
                }
            }

            try
            {
                this.VariableValue = eq.Calculate();
            }
            catch (Exception e)
            {
                VariableValue = 0;
                return(e.Message);
            }

            treeVariables.Remove(this);

            return(null);
        }