规则引擎

查看代码

//定义规则
using Newtonsoft.Json;
using RulesEngine.Models;

//模拟用户的输入内容
var inputValue = new InputValue
{
    Score = 60
};

var rulesStr = @"[{
                    ""WorkflowName"": ""InputValueWorkflow"",
                    ""Rules"": [
                      {
                        ""RuleName"": ""11"",
                        ""ErrorMessage"": ""考试及格."",
                        ""ErrorType"": ""Error"",
                        ""RuleExpressionType"": ""LambdaExpression"",
                        ""Expression"": ""Score >=60""
                      },
                       {
                        ""RuleName"": ""22"",
                        ""ErrorMessage"": ""考试不及格"",
                         ""ErrorType"": ""Error"",
                        ""RuleExpressionType"": ""LambdaExpression"",
                        ""Expression"": ""Score<60""
                      }
                    ]
                  }] ";

//反序列化Json格式规则字符串
var workflowRules = JsonConvert.DeserializeObject<List<WorkflowRules>>(rulesStr);

//初始化规则引擎
var rulesEngine = new RulesEngine.RulesEngine(workflowRules.ToArray());

//使用规则进行判断,并返回结果
List<RuleResultTree> resultList = await rulesEngine.ExecuteAllRulesAsync("InputValueWorkflow", inputValue);

//返回结果并展示
foreach (var item in resultList)
{
    Console.WriteLine("验证成功:{0},消息:{1}", item.IsSuccess, item.ExceptionMessage);
}

//Console.ReadLine();

public class InputValue
{
    public double Score { get; set; }
}

posted on   隨風.NET  阅读(63)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2019-10-18 授权120天到期后的处理方式
2010-10-18 收缩数据库日志文件
2010-10-18 SQL Server 2008与SQL Server 2005共存时的问题

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示