C#規(guī)則引擎RulesEngine的具體使用
當(dāng)編寫(xiě)應(yīng)用程序時(shí),經(jīng)常性需要花費(fèi)大量的時(shí)間與精力處理業(yè)務(wù)邏輯,往往業(yè)務(wù)邏輯的變化需要重構(gòu)或者增加大量代碼,對(duì)開(kāi)發(fā)測(cè)試人員很不友好。
之前在這篇文章說(shuō)過(guò),可以使用腳本引擎來(lái)將我們需要經(jīng)常變化的代碼進(jìn)行動(dòng)態(tài)編譯執(zhí)行,自由度非常大,不過(guò)對(duì)應(yīng)的需要資源也多。如果只是針對(duì)非常具體業(yè)務(wù)邏輯的變化,可以嘗試使用RulesEngine對(duì)程序進(jìn)行操作。
下文使用了官方示例且部分內(nèi)容翻譯自說(shuō)明文檔
簡(jiǎn)介
RulesEngine是微軟推出的規(guī)則引擎,規(guī)則引擎在很多企業(yè)開(kāi)發(fā)中有所應(yīng)用,是處理經(jīng)常變動(dòng)需求的一種優(yōu)雅的方法。個(gè)人任務(wù),規(guī)則引擎適用于以下的一些場(chǎng)景:
- 輸入輸出類(lèi)型數(shù)量比較固定,但是執(zhí)行邏輯經(jīng)常變化;
- switch條件經(jīng)常變化,復(fù)雜switch語(yǔ)句的替代;
- 會(huì)變動(dòng)的,具有多種條件或者規(guī)則的業(yè)務(wù)邏輯;
- 規(guī)則自由度不要求特別高的場(chǎng)景。(這種情況建議使用腳本引擎)
RulesEngine的規(guī)則使用JSON進(jìn)行存儲(chǔ),通過(guò)lambda表達(dá)式方式表述規(guī)則(Rules)。
安裝很方便,直接使用nuget進(jìn)行安裝:
install-pacakge RulesEngine
規(guī)則定義
需要有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" } ] } ]
除了標(biāo)準(zhǔn)的RuleExpressionType,還可以通過(guò)定義Rules嵌套多個(gè)條件,下面是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" } ] }
示例
可以從官方的代碼庫(kù)中下載示例,定義了上述規(guī)則,就可以直接開(kāi)始用了。示例描述了這么一個(gè)應(yīng)用場(chǎng)景:
根據(jù)不同的客戶屬性,提供不同的折扣。由于銷(xiāo)售的情況變化較快,提供折扣的規(guī)則也需要經(jīng)常變動(dòng)。因此比較適用于規(guī)則引擎。
public void Run() { Console.WriteLine($"Running {nameof(BasicDemo)}...."); //創(chuàng)建輸入 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<ExpandoObject>(basicInfo, converter); dynamic input2 = JsonConvert.DeserializeObject<ExpandoObject>(orderInfo, converter); dynamic input3 = JsonConvert.DeserializeObject<ExpandoObject>(telemetryInfo, converter); var inputs = new dynamic[] { input1, input2, input3 }; //加載規(guī)則 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<List<WorkflowRules>>(fileData); //初始化規(guī)則引擎 var bre = new RulesEngine.RulesEngine(workflowRules.ToArray(), null); string discountOffered = "No discount offered."; //執(zhí)行規(guī)則 List<RuleResultTree> resultList = bre.ExecuteAllRulesAsync("Discount", inputs).Result; //處理結(jié)果 resultList.OnSuccess((eventName) => { discountOffered = $"Discount offered is {eventName} % over MRP."; }); resultList.OnFail(() => { discountOffered = "The user is not eligible for any discount."; }); Console.WriteLine(discountOffered); }
輸入
輸入一般來(lái)說(shuō)是IEnumerable<dynamic>或者是匿名類(lèi)型,上面實(shí)例展示的是由json反序列化形成的dynamic類(lèi)型,對(duì)于程序生成的數(shù)據(jù),使用匿名類(lèi)型更加方便。
var nestedInput = new { SimpleProp = "simpleProp", NestedProp = new { SimpleProp = "nestedSimpleProp", ListProp = new List<ListItem> { new ListItem { Id = 1, Value = "first" }, new ListItem { Id = 2, Value = "second" } } } };
命名空間
和腳本引擎一樣,默認(rèn)規(guī)則引擎只能訪問(wèn)System的命名空間。如果需要使用到稍微復(fù)雜一些的類(lèi)型,可以自己定義類(lèi)型或者函數(shù)。比如定義一個(gè)這樣的函數(shù):
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); } }
需要使用的時(shí)候,先將類(lèi)傳遞給RulesEngine:
var reSettingsWithCustomTypes = new ReSettings { CustomTypes = new Type[] { typeof(Utils) } }; var engine = new RulesEngine.RulesEngine(workflowRules.ToArray(), null, reSettingsWithCustomTypes);
然后就可以直接在表達(dá)式中使用了。
"Expression": "Utils.CheckContains(input1.country, \"india,usa,canada,France\") == true"
規(guī)則參數(shù)
默認(rèn)情況下,規(guī)則的輸入使用的是類(lèi)似input1 input2這樣的形式,如果想直觀一點(diǎn),可以使用RuleParameter來(lái)進(jìn)行封裝具體的參數(shù)類(lèi)型。
RuleParameter ruleParameter = new RuleParameter("NIP", nestedInput); var resultList = bre.ExecuteAllRulesAsync(workflow.WorkflowName, ruleParameter).Result;
本地變量
如果表達(dá)式比較復(fù)雜的情況下,可以使用本地變量來(lái)進(jìn)行分段處理,這對(duì)調(diào)試來(lái)說(shuō)會(huì)比較方便。
本地變量的關(guān)鍵字為localParams,可以將中間的內(nèi)容簡(jiǎn)單理解成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 " }
總結(jié)
使用規(guī)則引擎,可以將經(jīng)常變動(dòng)的業(yè)務(wù)邏輯獨(dú)立摘出來(lái),為我們編寫(xiě)動(dòng)態(tài)、可拓展的程序提供了很大的便利。RulesEngine這個(gè)東西提供的API也比較簡(jiǎn)潔,上手非常簡(jiǎn)單。
到此這篇關(guān)于C規(guī)則引擎RulesEngine的具體使用的文章就介紹到這了,更多相關(guān)C規(guī)則引擎RulesEngine內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# HttpClient Post參數(shù)同時(shí)上傳文件的實(shí)現(xiàn)
這篇文章主要介紹了C# HttpClient Post參數(shù)同時(shí)上傳文件的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06C#中#define后面只加一個(gè)參數(shù)的解釋
今天小編就為大家分享一篇關(guān)于C#中#define后面只加一個(gè)參數(shù)的解釋?zhuān)【幱X(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04深入探討C#中的const、readonly關(guān)鍵字
這篇文章主要介紹了深入探討C#中的const、readonly關(guān)鍵字,本文可以幫助你深刻理解這兩個(gè)關(guān)鍵字,而且是面試中最可能面試到的問(wèn)題哦,需要的朋友可以參考下2014-08-08DevExpress GridControl實(shí)現(xiàn)根據(jù)RowIndex和VisibleColumnsIndex來(lái)獲取單元格
這篇文章主要介紹了DevExpress GridControl實(shí)現(xiàn)根據(jù)RowIndex和VisibleColumnsIndex來(lái)獲取單元格值,需要的朋友可以參考下2014-08-08C#代替go采用的CSP并發(fā)模型實(shí)現(xiàn)
這篇文章主要為大家介紹了C#代替go采用的CSP并發(fā)模型的輕松實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能
這篇文章主要介紹了C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能,需要的朋友可以參考下2017-12-12C#實(shí)現(xiàn)排列組合算法完整實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)排列組合算法的完整實(shí)例,文中實(shí)例主要展示了排列循環(huán)方法和排列堆棧方法,需要的朋友可以參考下2014-09-09