easy-rules yaml 组合模式rule 配置

easy-rules 支持组合模式,但是一般我们看到的都是基于代码的,以下是一个基于yaml配置的组合模式rule 的试用

项目结构

  • pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dalong</groupId>
  <artifactId>myruleapps</artifactId>
  <version>1.0-SNAPSHOT</version>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <encoding>UTF-8</encoding>
    <java.version>1.8</java.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>8</source>
          <target>8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <packaging>jar</packaging>
  <dependencies>
    <dependency>
      <groupId>org.jeasy</groupId>
      <artifactId>easy-rules-core</artifactId>
      <version>3.4.0</version>
    </dependency>
    <dependency>
      <groupId>org.jeasy</groupId>
      <artifactId>easy-rules-mvel</artifactId>
      <version>3.4.0</version>
    </dependency>
    <dependency>
      <groupId>org.jeasy</groupId>
      <artifactId>easy-rules-spel</artifactId>
      <version>3.4.0</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.7.25</version>
    </dependency>
  </dependencies>
</project>
  • rule yaml 文件
    rules-com.yml
 
---
name: "1"
description: "1"
priority: 1
compositeRuleType: "UnitRuleGroup"
composingRules:
  - name: "2"
    description: "2"
    condition: "user.getAge()<29"
    priority: 2
    actions:
      - "System.out.println(\"UnitRuleGroup rule2 \")"
  - name: "3"
    description: "3"
    condition: "user.name.length<10"
    priority: 3
    actions:
      - "System.out.println(\"UnitRuleGroup rule3 \")"
---
name: "3"
description: "3"
condition: "user.name.length<50"
priority: 3
actions:
  - "System.out.println(\"default rule3 \")"

代码集成

 
import org.jeasy.rules.api.*;
import org.jeasy.rules.core.DefaultRulesEngine;
import org.jeasy.rules.core.InferenceRulesEngine;
import org.jeasy.rules.core.RuleBuilder;
import org.jeasy.rules.mvel.MVELRuleFactory;
import org.jeasy.rules.support.YamlRuleDefinitionReader;
import org.mvel2.ParserContext;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import static com.dalong.DecreaseTemperatureAction.decreaseTemperature;
import static com.dalong.HighTemperatureCondition.itIsHot;
import static com.dalong.AppendUserName.appendUserName;
import static com.dalong.NameTooShort.nameTooShort;
public class Launcher {
    public static void main(String[] args) throws Exception {
       app4();
    }
    // 加载方式没变
    private static void app4() throws Exception {
        MVELRuleFactory ruleFactory = new MVELRuleFactory(new YamlRuleDefinitionReader());
        ParserContext context =new ParserContext();
        context.addImport("UserService", UserService.class);
        Rules yamlRules = ruleFactory.createRules(new FileReader(Launcher.class.getClassLoader().getResource("rules-com.yml").getFile()),context);
        DefaultRulesEngine rulesEngine = new DefaultRulesEngine();
        Facts facts = new Facts();
        MyRule<User> rule = new MyRule<User>();
        Map<Object,Object> userinfo = new HashMap<>();
        userinfo.put("name2","dalong");
        userinfo.put("age2",27);
        yamlRules.register(rule);
        User user =new User("dalong",27);
        user.setUserinfo(userinfo);
        facts.put("user",user);
        rulesEngine.registerRuleListener(new MyRulesListener());
        rulesEngine.registerRulesEngineListener(new MyRuleEngineListener());
        rulesEngine.fire(yamlRules, facts);
        if(rule.isExecuted()){
            User userResult= rule.getResult();
            System.out.println("result from final ruls"+userResult.toString());
        }
    }
}
  • 运行效果
四月 12, 2020 11:30:46 下午 com.dalong.MyRuleEngineListener beforeEvaluate
信息: -----------------beforeEvaluate-----------------
四月 12, 2020 11:30:46 下午 com.dalong.MyRuleEngineListener beforeEvaluate
信息: org.jeasy.rules.api.Rules@78c03f1f [ { user : User{name='dalong', age=27, userinfo={name2=dalong, age2=27}} } ]
四月 12, 2020 11:30:46 下午 com.dalong.MyRulesListener afterEvaluate
信息: -----------------afterEvaluate-----------------
四月 12, 2020 11:30:46 下午 com.dalong.MyRulesListener beforeExecute
信息: -----------------beforeExecute-----------------
四月 12, 2020 11:30:46 下午 com.dalong.MyRulesListener onSuccess
信息: -----------------onSuccess-----------------
四月 12, 2020 11:30:46 下午 com.dalong.MyRulesListener afterEvaluate
信息: -----------------afterEvaluate-----------------
四月 12, 2020 11:30:46 下午 com.dalong.MyRulesListener beforeExecute
信息: -----------------beforeExecute-----------------
四月 12, 2020 11:30:46 下午 com.dalong.MyRulesListener onSuccess
信息: -----------------onSuccess-----------------
四月 12, 2020 11:30:46 下午 com.dalong.MyRulesListener afterEvaluate
信息: -----------------afterEvaluate-----------------
四月 12, 2020 11:30:46 下午 com.dalong.MyRulesListener beforeExecute
信息: -----------------beforeExecute-----------------
四月 12, 2020 11:30:46 下午 com.dalong.MyRulesListener onSuccess
信息: -----------------onSuccess-----------------
四月 12, 2020 11:30:46 下午 com.dalong.MyRuleEngineListener afterExecute
信息: -----------------afterExecute-----------------
四月 12, 2020 11:30:46 下午 com.dalong.MyRuleEngineListener afterExecute
信息: org.jeasy.rules.api.Rules@78c03f1f [ { user : User{name='dalong', age=27, userinfo={name2=dalong, age2=27}} } ]
UnitRuleGroup rule2 
UnitRuleGroup rule3 
default rule3 
my rule has been executed
result from final rulsUser{name='dalong', age=27, userinfo={name2=dalong, age2=27}}
Process finished with exit code 0

说明

其他组合模式的rule,配置一样

参考资料

https://github.com/j-easy/easy-rules/blob/master/easy-rules-support/src/main/java/org/jeasy/rules/support/AbstractRuleDefinitionReader.java

posted on   荣锋亮  阅读(2708)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-04-12 Introducing Outflux: a smart way out of InfluxDB
2019-04-12 使用outflux 导入influxdb 的数据到timescaledb
2019-04-12 edgedb 强大的对象关系数据库
2018-04-12 elixir grpc 试用
2018-04-12 elixir 调用erlang 代码
2018-04-12 Elixir's keyword lists as option parameters
2015-04-12 BigPipe 了解

导航

< 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
点击右上角即可分享
微信分享提示