查看代码
//定义规则
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; }
}