| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- using System;
- using System.Collections.Generic;
- using Abp.Dependency;
- using IwbZero.Expr;
- using IwbZero.IwbBase;
- using IwbZero.ToolCommon.StringModel;
- namespace WeEngine.Functions
- {
- public class ExprModule
- {
- private static IdCollection _functions;
- public static string Evaluate(string exprStr)
- {
- string str = exprStr;
- try
- {
- ExprObject exprObj = new ExprObject(exprStr, ExprElements.Expression);
- if (EvalExpr.Parse(exprObj, true))
- {
- str = Evaluate(exprObj);
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- return str;
- }
- public static string Evaluate(string exprStr, Dictionary<string, object> variableValueDic)
- {
- var exprStr2 = EvalExpr.TransVariable(exprStr, variableValueDic);
- var str = Evaluate(exprStr2);
- return str;
- }
- private static string Evaluate(ExprObject exprObj)
- {
- string str = "";
- if (exprObj.ChildCount == 0)
- {
- return exprObj.Expr;
- }
- EvaluateAllChildren(exprObj);
- ExprObject expr = null;
- for (int i = 0; i < exprObj.ChildCount; i++)
- {
- ExprObject child = exprObj.GetChild(i);
- if (child.IsOperator)
- {
- var @operator = child.GetOperator();
- ExprObject expr3 = exprObj.GetChild(i + 1);
- if (expr3 != null)
- {
- expr = @operator.Calculate(expr, expr3);
- i++;
- }
- }
- else if (expr == null)
- {
- expr = child;
- }
- }
- if (expr != null) str = expr.Expr;
- return str;
- }
- //public static string Evaluate(ExprObject exprObj, Dictionary<string, object> variableValueDic)
- //{
- // return Evaluate(exprObj.Expr,variableValueDic);
- //}
- private static void EvaluateAllChildren(ExprObject exprObj)
- {
- if (exprObj != null)
- {
- for (int i = 0; i < exprObj.ChildCount; i++)
- {
- ExprObject child = exprObj.GetChild(i);
- EvaluateAllChildren(child);
- if (child != null)
- {
- string str = null;
- switch (child.ElementType)
- {
- case ExprElements.Function:
- str = EvaluateFunction(child);
- break;
- case ExprElements.Variable:
- //str = TransSessionVariables(child.Expr);
- break;
- case ExprElements.Expression:
- str = Evaluate(child);
- break;
- }
- if (str != null)
- {
- child.Expr = str;
- child.ElementType = ExprElements.Value;
- }
- }
- }
- }
- }
- private static string EvaluateFunction(ExprObject exprObj)
- {
- string str = "";
- string functionId =exprObj.Expr.UAndT();
- if (functionId.IndexOf("IFUN", StringComparison.Ordinal) == 0)
- {
- return EvalExpr.EvaluateFunction(exprObj);
- }
- if (_functions == null)
- {
- _functions = new IdCollection();
- }
- ExprFunction function = (ExprFunction) _functions[functionId];
- if (function == null)
- {
- function = ExprFunction.LoadById(functionId);
- if (function != null)
- {
- _functions.Add(function);
- }
- }
- if (function != null)
- {
- str = function.Invoke(exprObj);
- }
- return str;
- }
- }
- public interface IExprModuleManager
- {
- string Evaluate(string exprStr);
- string Evaluate(string exprStr, Dictionary<string, object> variableValueDic);
- }
- public class ExprModuleManager: IExprModuleManager,ISingletonDependency
- {
- public string Evaluate(string exprStr)
- {
- return ExprModule.Evaluate(exprStr);
- }
- public string Evaluate(string exprStr, Dictionary<string, object> variableValueDic)
- {
- return ExprModule.Evaluate(exprStr,variableValueDic);
- }
- }
- public class NullExprModuleManager : IExprModuleManager
- {
- public static NullExprModuleManager Instance = new NullExprModuleManager();
- public string Evaluate(string exprStr)
- {
- return exprStr;
- }
- public string Evaluate(string exprStr, Dictionary<string, object> variableValueDic)
- {
- return exprStr;
- }
- }
- }
|