zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    java 数组声明方法
    python 什么叫迭代
    Golang生成区间随机整数
    Golang字符串格式化
    Golang中map的三种声明方式和简单实现增删改查
    Golang实现二分查找法
    Golang实现冒泡排序法
    Golang切片的三种简单使用方式及区别
    Golang获取int数组里的最大值和下标
    Golang数组注意细节
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/12693489.html
Copyright © 2011-2022 走看看