예제 #1
0
        public static string[] ExtractVariablesFromEquation(string equation)
        {
            string[] varList = null;

            if (EquationConversion.MakeEquationTree(equation) != null)
            {
                varList = EquationConversion.GetVariableList();
            }

            return(varList);
        }
예제 #2
0
        /*
         * ConvertAndCheckInputs
         * -----------------------------------------------------------------------------------
         * Returns an Integer code denoting the success of the parsing and conversion process.
         * Codes:
         * -3 -> Equation Parsing failed
         * -2 -> Variable Matching failed
         * -1 -> Equation Parser Initialization failed
         *  0 -> Success
         */
        public static int ConvertAndCheckInputs(string eqString, string varList, OperatorStruct[] operators, string[][] terminators, string lineDelimiter, string fieldDelimiter)
        {
            string[]   equationVars;
            List <int> eqVarIndex = new List <int>();

            string[]   intervalVars;
            List <int> ivVarIndex = new List <int>();

            string[]   intervalVars2;
            List <int> removeIndex = new List <int>();
            int        index;
            int        success = 0;
            string     message = "";

            EquationConversion.ResetEquationConversion();
            EquationConversion.ConfigureParser(operators, terminators);

            if (EquationConversion.IsReady())
            {
                equationTreeRoot = EquationConversion.MakeEquationTree(eqString);

                if (equationTreeRoot != null)
                {
                    if (varList != "")
                    {
                        intervalList = IntervalConversion.ConvertToIntervals(varList, lineDelimiter, fieldDelimiter);
                    }

                    intervalVars = GetVariableNamesFromIntervals(intervalList);
                    equationVars = EquationConversion.GetVariableList();

                    // Find matching variable names from the interval structures and the equation
                    for (int i = 0; i < equationVars.Length; i++)
                    {
                        index = Array.IndexOf(intervalVars, equationVars[i]);
                        if (index > -1)
                        {
                            ivVarIndex.Add(index);
                            eqVarIndex.Add(i);
                        }
                    }

                    // Found fewer matching entries in the equation
                    if (ivVarIndex.Count < intervalVars.Length)
                    {
                        message += "Warning: Extraneous variables found in interval list (";

                        // Sort list and reverse so that it is in descending order -> enables correct
                        // behaviour for RemoveName(string[], int)
                        ivVarIndex.Sort();
                        ivVarIndex.Reverse();

                        foreach (int v in ivVarIndex)
                        {
                            intervalVars = RemoveName(intervalVars, v);
                        }

                        foreach (string vName in intervalVars)
                        {
                            message += vName + ", ";
                        }
                        frm_Main.UpdateLog(message.Substring(0, message.Length - 2) + ")." + Environment.NewLine);

                        // Find and remove extra intervals from the list
                        intervalVars2 = GetVariableNamesFromIntervals(intervalList);
                        for (int i = 0; i < intervalVars.Length; i++)
                        {
                            index = Array.IndexOf(intervalVars2, intervalVars[i]);
                            if (index > -1)
                            {
                                removeIndex.Add(index);
                            }
                        }

                        removeIndex.Sort();
                        removeIndex.Reverse();
                        foreach (int r in removeIndex)
                        {
                            intervalList = RemoveInterval(intervalList, r);
                        }
                    }

                    // Found fewer matching entries in the variable list
                    if (eqVarIndex.Count < equationVars.Length)
                    {
                        // This is a fail state
                        success = -2;

                        message = "Error: Cannot find intervals for variable name(s): ";

                        foreach (int v in eqVarIndex)
                        {
                            equationVars = RemoveName(equationVars, v);
                        }

                        foreach (string vName in equationVars)
                        {
                            message += vName + ", ";
                        }
                        frm_Main.UpdateLog(message.Substring(0, message.Length - 2) + "." + Environment.NewLine);
                    }
                }
                else
                {
                    success = -3;
                }
            }
            else
            {
                frm_Main.UpdateLog("Error: Equation parser could not be configured.");
                success = -1;
            }

            return(success);
        }