昰道 發表於 2021-2-6 00:17:00

C#规则引擎RulesEngine

<p>当编写应用程序时,经常性需要花费大量的时间与精力处理业务逻辑,往往业务逻辑的变化需要重构或者增加大量代码,对开发测试人员很不友好。</p>
<p>之前在这篇文章说过,可以使用脚本引擎来将我们需要经常变化的代码进行动态编译执行,自由度非常大,不过对应的需要资源也多。如果只是针对非常具体业务逻辑的变化,可以尝试使用RulesEngine对程序进行操作。</p>
<blockquote>
<p>下文使用了官方示例且部分内容翻译自说明文档</p>
</blockquote>
<h2 id="简介">简介</h2>
<p>RulesEngine是微软推出的规则引擎,规则引擎在很多企业开发中有所应用,是处理经常变动需求的一种优雅的方法。个人任务,规则引擎适用于以下的一些场景:</p>
<ul>
<li>输入输出类型数量比较固定,但是执行逻辑经常变化;</li>
<li>switch条件经常变化,复杂switch语句的替代;</li>
<li>会变动的,具有多种条件或者规则的业务逻辑;</li>
<li>规则自由度不要求特别高的场景。(这种情况建议使用脚本引擎)</li>
</ul>
<p>RulesEngine的规则使用JSON进行存储,通过lambda表达式方式表述规则(Rules)。</p>
<p>安装很方便,直接使用nuget进行安装:</p>
<pre><code class="language-powershell">install-pacakge RulesEngine
</code></pre>
<h2 id="规则定义">规则定义</h2>
<p>需要有Rules,有WorkflowName,然后还有一些属性。</p>
<pre><code class="language-json">[
{
    "WorkflowName": "Discount",
    "Rules": [
      {
      "RuleName": "GiveDiscount10",
      "SuccessEvent": "10",
      "ErrorMessage": "One or more adjust rules failed.",
      "ErrorType": "Error",
      "RuleExpressionType": "LambdaExpression",
      "Expression": "input1.country == \"india\" AND input1.loyalityFactor &lt;= 2 AND input1.totalPurchasesToDate &gt;= 5000 AND input2.totalOrders &gt; 2 AND input3.noOfVisitsPerMonth &gt; 2"
      }
    ]
}
]
</code></pre>
<blockquote>
<p>除了标准的<code>RuleExpressionType</code>,还可以通过定义Rules嵌套多个条件,下面是Or逻辑。</p>
</blockquote>
<pre><code class="language-json">{
"RuleName": "GiveDiscount30NestedOrExample",
"SuccessEvent": "30",
"ErrorMessage": "One or more adjust rules failed.",
"ErrorType": "Error",
"Operator": "OrElse",
"Rules":[
    {
    "RuleName": "IsLoyalAndHasGoodSpend",
    "ErrorMessage": "One or more adjust rules failed.",
    "ErrorType": "Error",
    "RuleExpressionType": "LambdaExpression",
    "Expression": "input1.loyalityFactor &gt; 3 AND input1.totalPurchasesToDate &gt;= 50000 AND input1.totalPurchasesToDate &lt;= 100000"
    },
    {
    "RuleName": "OrHasHighNumberOfTotalOrders",
    "ErrorMessage": "One or more adjust rules failed.",
    "ErrorType": "Error",
    "RuleExpressionType": "LambdaExpression",
    "Expression": "input2.totalOrders &gt; 15"
    }
]
}
</code></pre>
<h2 id="示例">示例</h2>
<p>可以从官方的代码库中下载示例,定义了上述规则,就可以直接开始用了。示例描述了这么一个应用场景:</p>
<p>根据不同的客户属性,提供不同的折扣。由于销售的情况变化较快,提供折扣的<strong>规则</strong>也需要经常变动。因此比较适用于规则引擎。</p>
<pre><code class="language-c#">public void Run()
{
    Console.WriteLine($"Running {nameof(BasicDemo)}....");
    //创建输入
    var basicInfo = "{\"name\": \"hello\",\"email\": \"abcy@xyz.com\",\"creditHistory\": \"good\",\"country\": \"canada\",\"loyalityFactor\": 3,\"totalPurchasesToDate\": 10000}";
    var orderInfo = "{\"totalOrders\": 5,\"recurringItems\": 2}";
    var telemetryInfo = "{\"noOfVisitsPerMonth\": 10,\"percentageOfBuyingToVisit\": 15}";

    var converter = new ExpandoObjectConverter();

    dynamic input1 = JsonConvert.DeserializeObject&lt;ExpandoObject&gt;(basicInfo, converter);
    dynamic input2 = JsonConvert.DeserializeObject&lt;ExpandoObject&gt;(orderInfo, converter);
    dynamic input3 = JsonConvert.DeserializeObject&lt;ExpandoObject&gt;(telemetryInfo, converter);

    var inputs = new dynamic[]
      {
            input1,
            input2,
            input3
      };
    //加载规则
    var files = Directory.GetFiles(Directory.GetCurrentDirectory(), "Discount.json", SearchOption.AllDirectories);
    if (files == null || files.Length == 0)
      throw new Exception("Rules not found.");

    var fileData = File.ReadAllText(files);
    var workflowRules = JsonConvert.DeserializeObject&lt;List&lt;WorkflowRules&gt;&gt;(fileData);
    //初始化规则引擎
    var bre = new RulesEngine.RulesEngine(workflowRules.ToArray(), null);

    string discountOffered = "No discount offered.";
    //执行规则
    List&lt;RuleResultTree&gt; resultList = bre.ExecuteAllRulesAsync("Discount", inputs).Result;
    //处理结果
    resultList.OnSuccess((eventName) =&gt; {
      discountOffered = $"Discount offered is {eventName} % over MRP.";
    });

    resultList.OnFail(() =&gt; {
      discountOffered = "The user is not eligible for any discount.";
    });

    Console.WriteLine(discountOffered);
}
</code></pre>
<h2 id="输入">输入</h2>
<p>输入一般来说是<code>IEnumerable&lt;dynamic&gt;</code>或者是匿名类型,上面实例展示的是由json反序列化形成的dynamic类型,对于程序生成的数据,使用匿名类型更加方便。</p>
<pre><code class="language-c#">var nestedInput = new {
                SimpleProp = "simpleProp",
                NestedProp = new {
                  SimpleProp = "nestedSimpleProp",
                  ListProp = new List&lt;ListItem&gt;
                  {
                        new ListItem
                        {
                            Id = 1,
                            Value = "first"
                        },
                        new ListItem
                        {
                            Id = 2,
                            Value = "second"
                        }
                  }
                }

            };
</code></pre>
<h2 id="命名空间">命名空间</h2>
<p>和脚本引擎一样,默认规则引擎只能访问System的命名空间。如果需要使用到稍微复杂一些的类型,可以自己定义类型或者函数。比如定义一个这样的函数:</p>
<pre><code class="language-c#">public static class Utils
{
    public static bool CheckContains(string check, string valList)
    {
      if (String.IsNullOrEmpty(check) || String.IsNullOrEmpty(valList))
            return false;

      var list = valList.Split(',').ToList();
      return list.Contains(check);
    }
}
</code></pre>
<p>需要使用的时候,先将类传递给RulesEngine:</p>
<pre><code class="language-c#">var reSettingsWithCustomTypes = new ReSettings { CustomTypes = new Type[] { typeof(Utils) } };
var engine = new RulesEngine.RulesEngine(workflowRules.ToArray(), null, reSettingsWithCustomTypes);
</code></pre>
<p>然后就可以直接在表达式中使用了。</p>
<pre><code class="language-c#">"Expression": "Utils.CheckContains(input1.country, \"india,usa,canada,France\") == true"
</code></pre>
<h2 id="规则参数">规则参数</h2>
<p>默认情况下,规则的输入使用的是类似<em>input1 input2</em>这样的形式,如果想直观一点,可以使用<code>RuleParameter</code>来进行封装具体的参数类型。</p>
<pre><code class="language-c#">RuleParameter ruleParameter = new RuleParameter("NIP", nestedInput);
var resultList = bre.ExecuteAllRulesAsync(workflow.WorkflowName, ruleParameter).Result;
</code></pre>
<h2 id="本地变量">本地变量</h2>
<p>如果表达式比较复杂的情况下,可以使用本地变量来进行分段处理,这对调试来说会比较方便。</p>
<p>本地变量的关键字为localParams,可以将中间的内容简单理解成<strong>var name = expression</strong></p>
<pre><code class="language-json">{
      "name": "allow_access_if_all_mandatory_trainings_are_done_or_access_isSecure",
      "errorMessage": "Please complete all your training(s) to get access to this content or access it from a secure domain/location.",
      "errorType": "Error",
      "localParams": [
          {
            "name": "completedSecurityTrainings",
            "expression": "MasterSecurityComplainceTrainings.Where(Status.Equals(\"Completed\", StringComparison.InvariantCultureIgnoreCase))"
          },
          {
            "name": "completedProjectTrainings",
            "expression": "MasterProjectComplainceTrainings.Where(Status.Equals(\"Completed\", StringComparison.InvariantCultureIgnoreCase))"
          },
          {
            "name": "isRequestAccessSecured",
            "expression": "UserRequestDetails.Location.Country == \"India\" ? ((UserRequestDetails.Location.City == \"Bangalore\" &amp;&amp; UserRequestDetails.Domain=\"xxxx\")? true : false):false"
          }
      ],
      "expression": "(completedSecurityTrainings.Any() &amp;&amp; completedProjectTrainings.Any()) || isRequestAccessSecured "
      }
</code></pre>
<h2 id="总结">总结</h2>
<p>使用规则引擎,可以将经常变动的业务逻辑独立摘出来,为我们编写动态、可拓展的程序提供了很大的便利。RulesEngine这个东西提供的API也比较简洁,上手非常简单。</p><br><br>
来源:https://www.cnblogs.com/podolski/p/14380360.html
頁: [1]
查看完整版本: C#规则引擎RulesEngine