code_junkie

Looking for simple rules-engine library in .NET

Nov 11th, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. class Rule
  2. {
  3. Expression Exp;
  4. Actions[] Actions;
  5. Run() { if(Exp.Evaluate()) { foreach(action in Actions) { action.Execute() ; } } }
  6. }
  7.  
  8. String result = ExpressionEvaluator.EvaluateToString("(2+5) < 8");
  9.  
  10. using System;
  11. using System.CodeDom.Compiler;
  12. using System.Globalization;
  13. using System.Reflection;
  14. using Microsoft.JScript;
  15.  
  16. namespace Common.Rule
  17. {
  18. internal static class ExpressionEvaluator
  19. {
  20. #region static members
  21. private static object _evaluator = GetEvaluator();
  22. private static Type _evaluatorType;
  23. private const string _evaluatorSourceCode =
  24. @"package Evaluator
  25. {
  26. class Evaluator
  27. {
  28. public function Eval(expr : String) : String
  29. {
  30. return eval(expr);
  31. }
  32. }
  33. }";
  34.  
  35. #endregion
  36.  
  37. #region static methods
  38. private static object GetEvaluator()
  39. {
  40. CompilerParameters parameters;
  41. parameters = new CompilerParameters();
  42. parameters.GenerateInMemory = true;
  43.  
  44. JScriptCodeProvider jp = new JScriptCodeProvider();
  45. CompilerResults results = jp.CompileAssemblyFromSource(parameters, _evaluatorSourceCode);
  46.  
  47. Assembly assembly = results.CompiledAssembly;
  48. _evaluatorType = assembly.GetType("Evaluator.Evaluator");
  49.  
  50. return Activator.CreateInstance(_evaluatorType);
  51. }
  52.  
  53. /// <summary>
  54. /// Executes the passed JScript Statement and returns the string representation of the result
  55. /// </summary>
  56. /// <param name="statement">A JScript statement to execute</param>
  57. /// <returns>The string representation of the result of evaluating the passed statement</returns>
  58. public static string EvaluateToString(string statement)
  59. {
  60. object o = EvaluateToObject(statement);
  61. return o.ToString();
  62. }
  63.  
  64. /// <summary>
  65. /// Executes the passed JScript Statement and returns the result
  66. /// </summary>
  67. /// <param name="statement">A JScript statement to execute</param>
  68. /// <returns>The result of evaluating the passed statement</returns>
  69. public static object EvaluateToObject(string statement)
  70. {
  71. lock (_evaluator)
  72. {
  73. return _evaluatorType.InvokeMember(
  74. "Eval",
  75. BindingFlags.InvokeMethod,
  76. null,
  77. _evaluator,
  78. new object[] { statement },
  79. CultureInfo.CurrentCulture
  80. );
  81. }
  82. }
  83. #endregion
  84. }
  85. }
  86.  
  87. BUSINESS RULES IN NATURAL LANGUAGE
  88.  
  89. Before
  90. If (Customer.Age > 50 && Customer.Status == Status.Active) {
  91. policy.SetDiscount(true, 10%);
  92. }
  93.  
  94. After (with Smart Rules)
  95. If Customer is older than 50 and
  96. the Customer Status is Active Then
  97. Apply 10 % of Discount
Add Comment
Please, Sign In to add comment