C# RulesEngine 规则引擎:从入门到看懵( 五 )

笔者这里的示例是,每个规则只计算自己的折扣,也就是每个 Rule 都是独立的,下一个 Rule 不会在上一个 Rule 结果上计算 。
< 18 : 0.9VIP: 0.9如果是折扣可以叠加,那么就是 0.9*0.9 ,最终可以拿到 0.81 的折扣 。
如果折扣不能叠加,只能选择最佳的优惠,那么就是 0.9
使用自定义函数自定义函数有两种静态函数和实例函数两种,我们可以在 Expression 中调用预先写好的函数 。
下面讲解如何在 Rule 中调用自定义的函数 。
静态函数自定义静态函数:
public static bool CheckAge(int age){return age >= 18;}注册类型:
ReSettings reSettings = new ReSettings{CustomTypes = new[] { typeof(Program) }};var bre = new RulesEngine.RulesEngine(Workflows: workflows.ToArray(), reSettings: reSettings);使用静态函数:
[{ "WorkflowName": "Test", "Rules": [{"RuleName": "CheckAge","Expression": "Program.CheckAge(buyer.Age) == true" }]}]完整代码:
static async Task Main(){// 定义var rulesStr = "[{\"WorkflowName\":\"Test\",\"Rules\":[{\"RuleName\":\"CheckAge\",\"Expression\":\"Program.CheckAge(buyer.Age) == true\"}]}]";var workflows = JsonConvert.DeserializeObject<List<Workflow>>(rulesStr)!;ReSettings reSettings = new ReSettings{CustomTypes = new[] { typeof(Program) }};var bre = new RulesEngine.RulesEngine(Workflows: workflows.ToArray(), reSettings: reSettings);List<RuleResultTree> resultList = await bre.ExecuteAllRulesAsync("Test", new Buyer{Age = 16});foreach (var item in resultList){Console.WriteLine("规则名称:{0},验证结果:{1}", item.Rule.RuleName, item.IsSuccess);}}public static bool CheckAge(int age){return age >= 18;}实例函数定义实例函数:
public bool CheckAge(int age){return age >= 18;}通过 RuleParameter 参数的方式,传递实例:
var rp1 = new RuleParameter("p", new Program());通过参数的名称调用函数:
[{ "WorkflowName": "Test", "Rules": [{"RuleName": "CheckAge","Expression": "p.CheckAge(buyer.Age) == true" }]}]完整代码:
static async Task Main(){// 定义var rulesStr = "[{\"WorkflowName\":\"Test\",\"Rules\":[{\"RuleName\":\"CheckAge\",\"Expression\":\"p.CheckAge(buyer.Age) == true\"}]}]";var workflows = JsonConvert.DeserializeObject<List<Workflow>>(rulesStr)!;var rp1 = new RuleParameter("p", new Program());var bre = new RulesEngine.RulesEngine(Workflows: workflows.ToArray());List<RuleResultTree> resultList = await bre.ExecuteAllRulesAsync("Test", new Buyer{Age = 16}, rp1);foreach (var item in resultList){Console.WriteLine("规则名称:{0},验证结果:{1}", item.Rule.RuleName, item.IsSuccess);}}public bool CheckAge(int age){return age >= 18;}自定义执行器自定义执行器就是 OnSuccessOnFailure 这部分的自定义执行代码,相比静态函数、实例函数,使用自定义执行器,可以获取 Rule 的一些数据 。
"Actions": {"OnSuccess": {"Name": "MyCustomAction","Context": {"customContextInput": "0.9"}}}自定义一个执行器,执行器需要继承 ActionBase
public class MyCustomAction : ActionBase{public override async ValueTask<object> Run(ActionContext context, RuleParameter[] ruleParameters){var customInput = context.GetContext<string>("customContextInput");return await ValueTask.FromResult(new object());}}定义 ReSettings,并在构建规则引擎时,传递进去:
var b = new Buyer{Age = 16};var reSettings = new ReSettings{CustomActions = new Dictionary<string, Func<ActionBase>>{{"MyCustomAction", () => new MyCustomAction() }}};var bre = new RulesEngine.RulesEngine(workflows.ToArray(), reSettings);List<RuleResultTree> resultList = await bre.ExecuteAllRulesAsync("Test", b);

经验总结扩展阅读