示例#1
0
        public static bool CheckCanEvaluate(string strExpression, ref byte nResult, ref double dValue, ref string pErr)
        {
            string tmpexpression = "";
            string tmpreturn = "";

            tmpreturn = strExpression.Substring(0, strExpression.IndexOf(":="));
            tmpexpression = strExpression.Substring(strExpression.IndexOf(":=") + 2);
            EvaluationEngine.Parser.Token token = new EvaluationEngine.Parser.Token(tmpexpression);
            EvaluationEngine.Evaluate.Evaluator eval = new EvaluationEngine.Evaluate.Evaluator(token);
            string result = "";
            if (eval.Evaluate(out result, out pErr) == true)
            {
                dValue = Convert.ToDouble(result.ToString());
                if (!ht.ContainsKey(tmpreturn))
                {
                    ht.Add(tmpreturn, dValue);
                }
                nResult = 1;
                return true;
            }
            else
            {
                for (int i = 0; i < token.Variables.Count; i++)
                {
                    #region 如果返回false,表示所需要的变量没有准备好
                    if (!ht.ContainsKey(token.Variables[i].VariableName))
                    {
                        return false;
                    }
                    else
                    {
                        token.Variables[i].VariableValue = ht[token.Variables[i].VariableName].ToString();
                    }
                    #endregion
                }
                EvalExpression(strExpression, ref nResult, ref dValue, ref pErr);
                if (nResult == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
        }
示例#2
0
 public static void EvalExpression(string strExpression, ref byte nResult, ref double dValue, ref string pErr)
 {
     string tmpexpression = "";
     string tmpreturn = "";
     tmpreturn = strExpression.Substring(0, strExpression.IndexOf(":="));
     tmpexpression = strExpression.Substring(strExpression.IndexOf(":=") + 2);
     EvaluationEngine.Parser.Token token = new EvaluationEngine.Parser.Token(tmpexpression);
     EvaluationEngine.Evaluate.Evaluator eval = new EvaluationEngine.Evaluate.Evaluator(token);
     string result = "";
     if (eval.Evaluate(out result, out pErr) == true)
     {
         dValue = Convert.ToDouble(result.ToString());
         if (!ht.ContainsKey(tmpreturn))
         {
             ht.Add(tmpreturn, dValue);
         }
         nResult = 1;
     }
     else
     {
         for (int i = 0; i < token.Variables.Count; i++)
         {
             token.Variables[i].VariableValue = ht[token.Variables[i].VariableName].ToString();
         }
         if (eval.Evaluate(out result, out pErr) == true)
         {
             dValue = Convert.ToDouble(result.ToString());
             if (!ht.ContainsKey(tmpreturn))
             {
                 ht.Add(tmpreturn, dValue);
             }
             nResult = 1;
         }
         else
         {
             nResult = 0;
         }
     }
 }
        /// <summary>
        /// 计算表达式
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="processParams"></param>
        /// <returns></returns>
        public object CalculateExpression(string expression, Dictionary<string, object> processParams)
        {
            if (string.IsNullOrEmpty(expression))
            {
                return null;
            }
            if (processParams != null)
            {
                throw new ArgumentException("processParams is not supported in EvaluationEngineScript!");
            }

            EvaluationEngine.Parser.Token token = new EvaluationEngine.Parser.Token(expression);
            EvaluationEngine.Evaluate.Evaluator eval = new EvaluationEngine.Evaluate.Evaluator(token);
            string ErrorMsg = string.Empty;
            string result = string.Empty;
            try
            {
                if (eval.Evaluate(out result, out ErrorMsg) == false)
                {
                    throw new InvalidOperationException("Error evaluating the tokens: " + ErrorMsg + System.Environment.NewLine
                        + expression);
                }
            }
            catch (Exception)
            {
                throw;
            }
            if (!string.IsNullOrEmpty(result) && result.Length >= 2)
            {
                if (result[0] == '\"' && result[result.Length - 1] == '\"')
                {
                    result = result.Substring(1, result.Length - 2);
                }
            }
            return result;
        }
示例#4
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="strExpression"></param>
 /// <param name="nResult"></param>
 /// <param name="dValue"></param>
 /// <param name="pErr"></param>
 /// <param name="ls">返回strExpression表达式中的所有变量</param>
 /// <param name="ls1">返回:=左边的变量(其实质是最后所得的结果)</param>
 public static void GetMasterVariables(string strExpression, ref byte nResult, ref double dValue, ref string pErr, ref Dictionary<string, double> ls, ref Dictionary<string, double> ls1)
 {
     string tmpexpression = "";
     string tmpreturn = "";
     tmpreturn = strExpression.Substring(0, strExpression.IndexOf(":="));
     tmpexpression = strExpression.Substring(strExpression.IndexOf(":=") + 2);
     EvaluationEngine.Parser.Token token = new EvaluationEngine.Parser.Token(tmpexpression);
     EvaluationEngine.Evaluate.Evaluator eval = new EvaluationEngine.Evaluate.Evaluator(token);
     string result = "";
     if (eval.Evaluate(out result, out pErr) == true)
     {
         if (!ls1.Keys.Contains(tmpreturn))
         {
             ls1.Add(tmpreturn, 0);
         }
     }
     else
     {
         for (int i = 0; i < token.Variables.Count; i++)
         {
             if (!ls.Keys.Contains(token.Variables[i].VariableName))
             {
                 ls.Add(token.Variables[i].VariableName, 0);
             }
         }
         if (!ls1.Keys.Contains(tmpreturn))
         {
             ls1.Add(tmpreturn, 0);
         }
     }
 }
        internal void FilterIt(GridFilterInfo filter)
        {
            try
            {
                // first for rowExpression
                foreach (MyGrid grid in m_grids)
                {
                    foreach (Xceed.Grid.DataRow row in grid.DataRows)
                    {
                        row.Visible = true;
                    }
                }

                if (!string.IsNullOrEmpty(filter.RowExpression))
                {
                    string[] expressions = filter.RowExpression.Split(new char[] {';'},
                                                                      StringSplitOptions.RemoveEmptyEntries);
                    foreach (string expression in expressions)
                    {
                        int idx = expression.IndexOf(':');
                        if (idx == -1)
                        {
                            throw new ArgumentException("Invalid expression format");
                        }
                        string gridName = expression.Substring(0, idx).Trim();
                        string realExpression = expression.Substring(idx + 1).Trim();

                        MyGrid grid = FindGridByName(gridName);
                        if (grid == null)
                        {
                            throw new ArgumentException("Invalid Grid Name");
                        }

                        EvaluationEngine.Parser.Token token = new EvaluationEngine.Parser.Token(realExpression);
                        MatchCollection mc = m_regex.Matches(realExpression);

                        foreach (Xceed.Grid.DataRow row in grid.DataRows)
                        {
                            Dictionary<string, string> variables = new Dictionary<string, string>();

                            foreach (Match m in mc)
                            {
                                variables[m.Groups[1].Value] = m.Groups[0].Value;
                            }

                            foreach (KeyValuePair<string, string> kvp in variables)
                            {
                                if (row.Cells[kvp.Key] == null)
                                {
                                    throw new ArgumentException("Invalid Grid Cell's Name");
                                }
                                else
                                {
                                    if (row.Cells[kvp.Key].ParentColumn.DataType == typeof (string))
                                    {
                                        token.Variables[kvp.Value].VariableValue =
                                            row.Cells[kvp.Key].GetDisplayText() == null
                                                ? string.Empty
                                                : row.Cells[kvp.Key].GetDisplayText();
                                    }
                                    else
                                    {
                                        token.Variables[kvp.Value].VariableValue = row.Cells[kvp.Key].Value == null
                                                                                       ? string.Empty
                                                                                       : row.Cells[kvp.Key].Value.
                                                                                             ToString();
                                    }
                                }
                            }

                            EvaluationEngine.Evaluate.Evaluator eval = new EvaluationEngine.Evaluate.Evaluator(token);

                            string ErrorMsg = "";
                            string result = "";
                            if (eval.Evaluate(out result, out ErrorMsg) == false)
                            {
                                throw new InvalidOperationException("Error evaluating the tokens: " + ErrorMsg + System.Environment.NewLine
                                    + expression);
                            }

                            row.Visible = (result == "true");
                        }
                    }
                }

                // Then for columnExpression
                foreach (MyGrid grid in m_grids)
                {
                    foreach (Xceed.Grid.Column column in grid.Columns)
                    {
                        column.Fixed = false;
                    }
                }
                if (!string.IsNullOrEmpty(filter.ColumnExpression))
                {
                    string[] expressions = filter.ColumnExpression.Split(new char[] {';'},
                                                                         StringSplitOptions.RemoveEmptyEntries);
                    foreach (string expression in expressions)
                    {
                        int idx = expression.IndexOf(':');
                        if (idx == -1)
                        {
                            throw new ArgumentException("Invalid expression format");
                        }
                        string gridName = expression.Substring(0, idx).Trim();
                        string realExpression = expression.Substring(idx + 1).Trim();

                        MyGrid grid = FindGridByName(gridName);
                        grid.BeginInit();

                        if (grid == null)
                        {
                            throw new ArgumentException("Invalid Grid Name");
                        }

                        EvaluationEngine.Parser.Token token = new EvaluationEngine.Parser.Token(realExpression);
                        MatchCollection mc = m_regex.Matches(realExpression);

                        foreach (Xceed.Grid.Column column in grid.Columns)
                        {
                            Dictionary<string, string> variables = new Dictionary<string, string>();

                            foreach (Match m in mc)
                            {
                                if (m.Groups[0].Value != "$ColumnName$")
                                {
                                    throw new NotSupportedException("ColumnExpression Format is invalid !");
                                }
                                variables[m.Groups[1].Value] = m.Groups[0].Value;
                            }

                            foreach (KeyValuePair<string, string> kvp in variables)
                            {
                                token.Variables[kvp.Value].VariableValue = column.FieldName;
                            }

                            EvaluationEngine.Evaluate.Evaluator eval = new EvaluationEngine.Evaluate.Evaluator(token);

                            string ErrorMsg = "";
                            string result = "";
                            if (eval.Evaluate(out result, out ErrorMsg) == false)
                            {
                                throw new InvalidOperationException("Error evaluating the tokens: " + ErrorMsg + System.Environment.NewLine
                                    + expression);
                            }

                            column.Fixed = (result == "true");
                        }
                        grid.EndInit();
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionProcess.ProcessWithNotify(ex);
            }
        }