【今日推荐】:为什么一到面试就懵逼!>>> easy-rules mvel yaml 格式规则配置&&试用二——rule 数据返回结果-LMLPHP

默认上easy-rules 的数据是没有返回值的,同时对于基于yaml 配置方式更是如此,官方给予我们了一种解决方法
就是基于pojo添加返回字段,但是基于yaml 配置的方式就不方便了,实际上我们可以通过组合的模式——添加一个
最总解决的rule(使用pojo)同时配置一个比较大的优先级,同时这个rule,永远都是执行的就可以解决了,以下是
一个实践


项目结构

详细的可以参考https://www.cnblogs.com/rongfengliang/p/12686702.html

  • 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>
  • yaml规则
---
name: "1"
description: "1"
condition: "user.getAge()>26"
priority: 1
actions:
  - "user.age=user.age-2;System.out.println(user.userinfo);"
---
name: "2"
description: "2"
condition: "user.getAge()<26"
priority: 2
actions:
  - "UserService.doAction1(user)"
---
name: "3"
description: "3"
condition: "user.name.length<10"
priority: 3
actions:
  - "UserService.doAction2(user)"
---
name: "4"
description: "4"
condition: "user.name.length<40"
priority: 4
actions:
  - "System.out.println(\"rule4 \"+user.userinfo)"
---
name: "5"
description: "5"
condition: "user.name.length<30"
priority: 5
actions:
  - "UserService.doAction4(user.userinfo)"
---
name: "6"
description: "6"
condition: "user.name.length<30"
priority: 6
actions:
  - "System.out.println(\"rule6 \"+user.userinfo)"
---
name: "7"
description: "7"
condition: "user.name.length<30"
priority: 7
actions:
  - "UserService.doAction4(user)"
---
name: "8"
description: "8"
condition: "user.name.length<30"
priority: 8
actions:
  - "System.out.println(\"rule8 \"+user)"
  • 最后必须执行的rule
    MyRule.java 优先级数字很大,最后执行
 
package com.dalong;
import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Fact;
import org.jeasy.rules.annotation.Rule;
import org.jeasy.rules.api.Facts;
@Rule(name = "100", description = "my rule description", priority = 100)
public class MyRule<T> {
    private boolean executed;
    private T result;
    @Condition
    public boolean when(@Fact("user") T fact) {
        //my rule conditions
        return true;
    }
    @Action(order = 1)
    public void then(@Fact("user") T facts) throws Exception {
        try {
            System.out.println("my rule has been executed");
            result = (T)facts; // assign your result here
            executed = true;
        } catch (Exception e) {
            // executed flag will remain false if an exception occurs
            throw e;
        }
    }
    public boolean isExecuted() {
        return executed;
    }
    public T getResult() {
        return result;
    }
}

代码集成

  • 入口
package com.dalong;
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.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 {
       app3();
    }
    private static void app3() 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.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());
        }
    }
}
 

说明

以及上参考资料的一个实践,实际中还是有点用的

参考资料

https://github.com/j-easy/easy-rules/wiki/faqs
https://www.cnblogs.com/rongfengliang/p/12686702.html

04-13 16:32