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 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 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 variableValueDic); } public class ExprModuleManager: IExprModuleManager,ISingletonDependency { public string Evaluate(string exprStr) { return ExprModule.Evaluate(exprStr); } public string Evaluate(string exprStr, Dictionary 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 variableValueDic) { return exprStr; } } }