easy-rules json 文件格式rule定義


這次就不帖完整代碼了,只說明json 格式以及核心代碼調用

json 格式

  • 說明
    就是一個json 數組,每個rule 的格式參考如下,需要包含name,condition,priority,actions ,description 不是必須的
 
{
    "name": "3",
    "description": "3",
    "condition": "user.name.length<50",
    "priority": 3,
    "actions": [
      "System.out.println(\"default rule3 \")"
    ]
}
  • 一個參考多個rule 的json 文件
[{
  "name": "1",
  "description": "1",
  "priority": 1,
  "compositeRuleType": "UnitRuleGroup",
  "composingRules": [
    {
      "name": "2",
      "description": "2",
      "condition": "user.getAge()<28",
      "priority": 2,
      "actions": [
        "System.out.println(\"UnitRuleGroup not ok rule2 \")"
      ]
    },
    {
      "name": "3",
      "description": "3",
      "condition": "user.name.length<10",
      "priority": 3,
      "actions": [
        "System.out.println(\"UnitRuleGroup rule3 \")"
      ]
    },
    {
      "name": "4",
      "description": "4",
      "condition": "user.name.length<10",
      "priority": 4,
      "actions": [
        "System.out.println(\"UnitRuleGroup rule4 \")"
      ]
    },
    {
      "name": "5",
      "description": "5",
      "condition": "user.name.length<10",
      "priority": 5,
      "actions": [
        "System.out.println(\"UnitRuleGroup rule5 \");UserService.doAction4(user.userinfo)"
      ]
    }
  ]},
  {
    "name": "3",
    "description": "3",
    "condition": "user.name.length<50",
    "priority": 3,
    "actions": [
      "System.out.println(\"default rule3 \")"
    ]
  }
]

代碼引用規則

easy-rules 提供了json 格式的解析

  • easy-rules 代碼加載
MVELRuleFactory ruleFactory = new MVELRuleFactory(new JsonRuleDefinitionReader());
ParserContext context =new ParserContext();
context.addImport("UserService", UserService.class);
Rules yamlRules = ruleFactory.createRules(new FileReader(Launcher.class.getClassLoader().getResource("json-rule.json").getFile()),context);
DefaultRulesEngine rulesEngine = new DefaultRulesEngine();

參考資料

https://github.com/j-easy/easy-rules


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM