ExprModule.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Collections.Generic;
  3. using Abp.Dependency;
  4. using IwbZero.Expr;
  5. using IwbZero.IwbBase;
  6. using IwbZero.ToolCommon.StringModel;
  7. namespace WeEngine.Functions
  8. {
  9. public class ExprModule
  10. {
  11. private static IdCollection _functions;
  12. public static string Evaluate(string exprStr)
  13. {
  14. string str = exprStr;
  15. try
  16. {
  17. ExprObject exprObj = new ExprObject(exprStr, ExprElements.Expression);
  18. if (EvalExpr.Parse(exprObj, true))
  19. {
  20. str = Evaluate(exprObj);
  21. }
  22. }
  23. catch (Exception e)
  24. {
  25. Console.WriteLine(e.Message);
  26. }
  27. return str;
  28. }
  29. public static string Evaluate(string exprStr, Dictionary<string, object> variableValueDic)
  30. {
  31. var exprStr2 = EvalExpr.TransVariable(exprStr, variableValueDic);
  32. var str = Evaluate(exprStr2);
  33. return str;
  34. }
  35. private static string Evaluate(ExprObject exprObj)
  36. {
  37. string str = "";
  38. if (exprObj.ChildCount == 0)
  39. {
  40. return exprObj.Expr;
  41. }
  42. EvaluateAllChildren(exprObj);
  43. ExprObject expr = null;
  44. for (int i = 0; i < exprObj.ChildCount; i++)
  45. {
  46. ExprObject child = exprObj.GetChild(i);
  47. if (child.IsOperator)
  48. {
  49. var @operator = child.GetOperator();
  50. ExprObject expr3 = exprObj.GetChild(i + 1);
  51. if (expr3 != null)
  52. {
  53. expr = @operator.Calculate(expr, expr3);
  54. i++;
  55. }
  56. }
  57. else if (expr == null)
  58. {
  59. expr = child;
  60. }
  61. }
  62. if (expr != null) str = expr.Expr;
  63. return str;
  64. }
  65. //public static string Evaluate(ExprObject exprObj, Dictionary<string, object> variableValueDic)
  66. //{
  67. // return Evaluate(exprObj.Expr,variableValueDic);
  68. //}
  69. private static void EvaluateAllChildren(ExprObject exprObj)
  70. {
  71. if (exprObj != null)
  72. {
  73. for (int i = 0; i < exprObj.ChildCount; i++)
  74. {
  75. ExprObject child = exprObj.GetChild(i);
  76. EvaluateAllChildren(child);
  77. if (child != null)
  78. {
  79. string str = null;
  80. switch (child.ElementType)
  81. {
  82. case ExprElements.Function:
  83. str = EvaluateFunction(child);
  84. break;
  85. case ExprElements.Variable:
  86. //str = TransSessionVariables(child.Expr);
  87. break;
  88. case ExprElements.Expression:
  89. str = Evaluate(child);
  90. break;
  91. }
  92. if (str != null)
  93. {
  94. child.Expr = str;
  95. child.ElementType = ExprElements.Value;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. private static string EvaluateFunction(ExprObject exprObj)
  102. {
  103. string str = "";
  104. string functionId =exprObj.Expr.UAndT();
  105. if (functionId.IndexOf("IFUN", StringComparison.Ordinal) == 0)
  106. {
  107. return EvalExpr.EvaluateFunction(exprObj);
  108. }
  109. if (_functions == null)
  110. {
  111. _functions = new IdCollection();
  112. }
  113. ExprFunction function = (ExprFunction) _functions[functionId];
  114. if (function == null)
  115. {
  116. function = ExprFunction.LoadById(functionId);
  117. if (function != null)
  118. {
  119. _functions.Add(function);
  120. }
  121. }
  122. if (function != null)
  123. {
  124. str = function.Invoke(exprObj);
  125. }
  126. return str;
  127. }
  128. }
  129. public interface IExprModuleManager
  130. {
  131. string Evaluate(string exprStr);
  132. string Evaluate(string exprStr, Dictionary<string, object> variableValueDic);
  133. }
  134. public class ExprModuleManager: IExprModuleManager,ISingletonDependency
  135. {
  136. public string Evaluate(string exprStr)
  137. {
  138. return ExprModule.Evaluate(exprStr);
  139. }
  140. public string Evaluate(string exprStr, Dictionary<string, object> variableValueDic)
  141. {
  142. return ExprModule.Evaluate(exprStr,variableValueDic);
  143. }
  144. }
  145. public class NullExprModuleManager : IExprModuleManager
  146. {
  147. public static NullExprModuleManager Instance = new NullExprModuleManager();
  148. public string Evaluate(string exprStr)
  149. {
  150. return exprStr;
  151. }
  152. public string Evaluate(string exprStr, Dictionary<string, object> variableValueDic)
  153. {
  154. return exprStr;
  155. }
  156. }
  157. }