项目作者: IoTSharp

项目描述 :
RuleEngine
高级语言: C#
项目地址: git://github.com/IoTSharp/RuleEngine.git
创建时间: 2020-04-07T14:51:48Z
项目社区:https://github.com/IoTSharp/RuleEngine

开源协议:MIT License

下载


IoTSharp.RuleEngine

About IoTSharp.RuleEngine

IoTSharp.RuleEngine is a https://github.com/gsoulavy/RuleEngine fork, I’m will implement a rule engine for IoTSharp based on https://github.com/gsoulavy/RuleEngine, so, thanks to Gab Soulavy for the great work.

Purpose

This simple rule engine is a .NET Standard library 2.0, which uses Microsoft’s DLR DynamicExpressionParser in the background. The goal was to make a simple engine which is easy to use and compatible with many projects.

Use

Workflow Rules Engine

下面的示例实现了 在输入的数据中通过规则链MsgT_Exit_Waste 中的 STATION_NAME 不为空并且不以“某某某收费站”开头的数据输出, 这是个简单的例子 只有 Then , 没有Else

  1. Rule rule = new Rule()
  2. {
  3. RuleName = "data",
  4. ErrorType = ErrorType.Error,
  5. ErrorMessage = "未能找到数据",
  6. Expression = "f.MsgT_Exit_Waste!=null",
  7. ThenSelect = "MsgT_Exit_Waste",
  8. Then = new Rule()
  9. {
  10. Expression = "f.STATION_NAME!=null && !f.STATION_NAME.StartsWith(\"某某收费站\")",
  11. ThenSelect = "STATION_NAME",
  12. ErrorMessage = "收费站配置信息错误",
  13. RuleName = "moumou",
  14. RuleExpressionType = RuleExpressionType.LambdaExpression
  15. }
  16. };
  17. var engine = new RulesEngine(rule);
  18. var result = engine.Execute(Properties.Resources.MsgT_Exit_Waste);
  19. Assert.True(result.Result);
  20. Assert.Equal("新疆米东北主线站", result.Output);

Instantiating the kernel

The IKernel interface is implemented with Kerner in order to support Inverson Of Control.

  1. IKernel ruleEngine = new Kernel();
Role of the IRule

The engine is designed to use any object as a rule which implements IRule interface in order to make it easy to use with an ORM.

  1. public interface IRule
  2. {
  3. string Key { get; set; }
  4. string Expression { get; set; }
  5. }
  1. //...
  2. void AddRule(IRule rule);
  3. //...
Simple Validation

String expressions can be simply against the object passed to the engine.

Creating a fact:

  1. var fact = new Person {Age = 37, Income = 45000, NumberOfChildren = 3};

Validating the fact:

  1. var result = ruleEngine.Validate(fact, "(f.Age > 3 && f.Income > 100000) || f.NumberOfChildren > 5");
  2. // result = false
Validate All

More than one expreession can be added to the engine

  1. var rules = new List<Rule>
  2. {
  3. new Rule {Key = "1", Expression = "(f.Age > 3 && f.Income < 50000) || f.NumberOfChildren > 2"},
  4. new Rule {Key = "2", Expression = "(f.Age > 3 && f.Income > 100000) || f.NumberOfChildren > 5"}
  5. };
  6. ruleEngine.AddRules(rules);
  7. // Validate against all rules when no key passed
  8. var result = ruleEngine.ValidateAll(f);
  9. // result = false
  10. // Only validate against rules with the matching key
  11. var result = ruleEngine.ValidateAll(f, "1");
  12. // result = true
Validate Any

The calls are the same as the case of validate all, however it returns true if any case is true.

  1. var rules = new List<Rule>
  2. {
  3. new Rule {Key = "1", Expression = "(f.Age > 3 && f.Income < 50000) || f.NumberOfChildren > 2"},
  4. new Rule {Key = "2", Expression = "(f.Age > 3 && f.Income > 100000) || f.NumberOfChildren > 5"}
  5. };
  6. ruleEngine.AddRules(rules);
  7. // Validate against all rules when no key passed
  8. var result = ruleEngine.ValidateAny(f);
  9. // result = true
  10. // Only validate against rules with the matching key
  11. var result = ruleEngine.ValidateAny(f, "1");
  12. // result = true