码迷,mamicode.com
首页 > 其他好文 > 详细

easy-rules yaml 组合模式rule 配置

时间:2020-04-13 00:23:19      阅读:395      评论:0      收藏:0      [点我收藏+]

标签:builder   conf   throws   out   simple   number   yaml   logs   name   

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

easy-rules yaml 组合模式rule 配置

标签:builder   conf   throws   out   simple   number   yaml   logs   name   

原文地址:https://www.cnblogs.com/rongfengliang/p/12688666.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!