大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
今天就跟大家聊聊有关如何在C#中使用RulesEngine规则引擎,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
创新互联建站长期为1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为凤城企业提供专业的成都网站建设、做网站,凤城网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。简介
RulesEngine是微软推出的规则引擎,规则引擎在很多企业开发中有所应用,是处理经常变动需求的一种优雅的方法。个人任务,规则引擎适用于以下的一些场景:
输入输出类型数量比较固定,但是执行逻辑经常变化;
switch条件经常变化,复杂switch语句的替代;
会变动的,具有多种条件或者规则的业务逻辑;
规则自由度不要求特别高的场景。(这种情况建议使用脚本引擎)
RulesEngine的规则使用JSON进行存储,通过lambda表达式方式表述规则(Rules)。
安装很方便,直接使用nuget进行安装:
install-pacakge RulesEngine
需要有Rules,有WorkflowName,然后还有一些属性。
[ { "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 <= 2 AND input1.totalPurchasesToDate >= 5000 AND input2.totalOrders > 2 AND input3.noOfVisitsPerMonth > 2" } ] } ]
除了标准的RuleExpressionType,还可以通过定义Rules嵌套多个条件,下面是Or逻辑。
{ "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 > 3 AND input1.totalPurchasesToDate >= 50000 AND input1.totalPurchasesToDate <= 100000" }, { "RuleName": "OrHasHighNumberOfTotalOrders", "ErrorMessage": "One or more adjust rules failed.", "ErrorType": "Error", "RuleExpressionType": "LambdaExpression", "Expression": "input2.totalOrders > 15" } ] }
可以从官方的代码库中下载示例,定义了上述规则,就可以直接开始用了。示例描述了这么一个应用场景:
根据不同的客户属性,提供不同的折扣。由于销售的情况变化较快,提供折扣的规则也需要经常变动。因此比较适用于规则引擎。
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(basicInfo, converter); dynamic input2 = JsonConvert.DeserializeObject (orderInfo, converter); dynamic input3 = JsonConvert.DeserializeObject (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[0]); var workflowRules = JsonConvert.DeserializeObject >(fileData); //初始化规则引擎 var bre = new RulesEngine.RulesEngine(workflowRules.ToArray(), null); string discountOffered = "No discount offered."; //执行规则 List
resultList = bre.ExecuteAllRulesAsync("Discount", inputs).Result; //处理结果 resultList.OnSuccess((eventName) => { discountOffered = $"Discount offered is {eventName} % over MRP."; }); resultList.OnFail(() => { discountOffered = "The user is not eligible for any discount."; }); Console.WriteLine(discountOffered); }
输入一般来说是IEnumerable
var nestedInput = new { SimpleProp = "simpleProp", NestedProp = new { SimpleProp = "nestedSimpleProp", ListProp = new List{ new ListItem { Id = 1, Value = "first" }, new ListItem { Id = 2, Value = "second" } } } };
和脚本引擎一样,默认规则引擎只能访问System的命名空间。如果需要使用到稍微复杂一些的类型,可以自己定义类型或者函数。比如定义一个这样的函数:
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); } }
需要使用的时候,先将类传递给RulesEngine:
var reSettingsWithCustomTypes = new ReSettings { CustomTypes = new Type[] { typeof(Utils) } }; var engine = new RulesEngine.RulesEngine(workflowRules.ToArray(), null, reSettingsWithCustomTypes);
然后就可以直接在表达式中使用了。
"Expression": "Utils.CheckContains(input1.country, \"india,usa,canada,France\") == true"
默认情况下,规则的输入使用的是类似input1 input2这样的形式,如果想直观一点,可以使用RuleParameter来进行封装具体的参数类型。
RuleParameter ruleParameter = new RuleParameter("NIP", nestedInput); var resultList = bre.ExecuteAllRulesAsync(workflow.WorkflowName, ruleParameter).Result;
如果表达式比较复杂的情况下,可以使用本地变量来进行分段处理,这对调试来说会比较方便。
本地变量的关键字为localParams,可以将中间的内容简单理解成var name = expression
{ "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\" && UserRequestDetails.Domain=\"xxxx\")? true : false):false" } ], "expression": "(completedSecurityTrainings.Any() && completedProjectTrainings.Any()) || isRequestAccessSecured " }
看完上述内容,你们对如何在C#中使用RulesEngine规则引擎有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。