1 Star 1 Fork 0

MissJin / easy-rule-demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

easy-rule-demo

官网资料

demo

代码

package com.example.demo;

import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.core.DefaultRulesEngine;
import org.jeasy.rules.core.RuleBuilder;
import org.jeasy.rules.mvel.MVELRule;
import org.jeasy.rules.spel.SpELRule;
import org.jeasy.rules.support.composite.ActivationRuleGroup;
import org.jeasy.rules.support.composite.ConditionalRuleGroup;
import org.jeasy.rules.support.composite.UnitRuleGroup;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.expression.BeanResolver;

/**
 * 测试类:
 * 参考官网 @{https://github.com/j-easy/easy-rules}
 */
public class Test {
    public static void main(String[] args) {
        // define facts
        Facts facts = new Facts();
        facts.put("rain", true);

        // 方式(一):注解的形式
        WeatherRule weatherRule = new WeatherRule();
        facts.put("weatherRule", weatherRule);

        // 写法(二):链式
        Rule weatherRule2 = new RuleBuilder()
                .name("weather rule second")
                .priority(2)
                .description("if it rains then take an umbrella")
                .when(facts_ -> facts_.get("rain").equals(true))
                .then(facts_ -> System.err.println("second, It rains, take an umbrella!"))
                .build();
        // 写法(三.1):spEl表达式, 支持spring的el表达式
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MySpringAppConfig.class);
        BeanResolver beanResolver = new MySpringBeanResolver(applicationContext);
        Rule weatherRule3_1 = new SpELRule(beanResolver)
                .name("weather rule three 1")
                .priority(3)
                .description("if it rains then take an umbrella")
                .when("#{ ['rain'] == true }")
                .then("#{ @mySpringBean.print(\"three 1 1, It rains, take an umbrella!\") }");
        // 写法(三.2):mvEl表达式
        Rule weatherRule3_2 = new MVELRule()
                .name("weather rule three 2")
                .priority(3)
                .description("if it rains then take an umbrella")
                .when("rain == true")
                .then("System.err.println(\"three 2 1, It rains, take an umbrella!\")")
                .then("weatherRule.print(\"three 2 2, It rains, take an umbrella!\")");

        // (1) 每个规则独立运行
        Rules rules = new Rules();
        rules.register(weatherRule, weatherRule2, weatherRule3_1, weatherRule3_2);

        // (2) and 组合每个规则: 要么应用所有规则,要么不应用任何规则
        UnitRuleGroup myUnitRuleGroup = new UnitRuleGroup("myUnitRuleGroup", "要么应用所有规则,要么不应用任何规则");
        myUnitRuleGroup.addRule(weatherRule);
        myUnitRuleGroup.addRule(weatherRule2);
        Rules rulesAnd = new Rules();
        rulesAnd.register(myUnitRuleGroup);

        // (3) xor 组合每个规则: 它触发第一个适用规则,并忽略组中的其他规则
        ActivationRuleGroup activationRuleGroup = new ActivationRuleGroup("activationRuleGroup", "它触发第一个适用规则,并忽略组中的其他规则");
        activationRuleGroup.addRule(weatherRule);
        activationRuleGroup.addRule(weatherRule2);
        Rules rulesXor = new Rules();
        rulesXor.register(activationRuleGroup);

        // (4) condition priority:如果具有最高优先级的规则计算结果为true,则触发其余规则
        ConditionalRuleGroup conditionalRuleGroup = new ConditionalRuleGroup("conditionalRuleGroup", "如果具有最高优先级的规则计算结果为true,则触发其余规则");
        conditionalRuleGroup.addRule(weatherRule);
        conditionalRuleGroup.addRule(weatherRule2);
        Rules rulesPriority = new Rules();
        rulesPriority.register(conditionalRuleGroup);

        // fire rules on known facts
        RulesEngine rulesEngine = new DefaultRulesEngine();
        rulesEngine.fire(rules, facts);
        //rulesEngine.fire(rulesAnd, facts);
        //rulesEngine.fire(rulesXor, facts);
        //rulesEngine.fire(rulesPriority, facts);
    }
}

结果

D:\Java\jdk-11.0.11\bin\java.exe "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2021.2\lib\idea_rt.jar=60812:D:\Program Files\JetBrains\IntelliJ IDEA 2021.2\bin" -Dfile.encoding=UTF-8 -classpath D:\workspace-demo\easy-rule-demo\target\classes;D:\maven\aliyun_repo\org\springframework\boot\spring-boot-starter-web\2.6.3\spring-boot-starter-web-2.6.3.jar;D:\maven\aliyun_repo\org\springframework\boot\spring-boot-starter\2.6.3\spring-boot-starter-2.6.3.jar;D:\maven\aliyun_repo\org\springframework\boot\spring-boot\2.6.3\spring-boot-2.6.3.jar;D:\maven\aliyun_repo\org\springframework\boot\spring-boot-autoconfigure\2.6.3\spring-boot-autoconfigure-2.6.3.jar;D:\maven\aliyun_repo\org\springframework\boot\spring-boot-starter-logging\2.6.3\spring-boot-starter-logging-2.6.3.jar;D:\maven\aliyun_repo\ch\qos\logback\logback-classic\1.2.10\logback-classic-1.2.10.jar;D:\maven\aliyun_repo\ch\qos\logback\logback-core\1.2.10\logback-core-1.2.10.jar;D:\maven\aliyun_repo\org\apache\logging\log4j\log4j-to-slf4j\2.17.1\log4j-to-slf4j-2.17.1.jar;D:\maven\aliyun_repo\org\apache\logging\log4j\log4j-api\2.17.1\log4j-api-2.17.1.jar;D:\maven\aliyun_repo\org\slf4j\jul-to-slf4j\1.7.33\jul-to-slf4j-1.7.33.jar;D:\maven\aliyun_repo\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\maven\aliyun_repo\org\yaml\snakeyaml\1.29\snakeyaml-1.29.jar;D:\maven\aliyun_repo\org\springframework\boot\spring-boot-starter-json\2.6.3\spring-boot-starter-json-2.6.3.jar;D:\maven\aliyun_repo\com\fasterxml\jackson\core\jackson-databind\2.13.1\jackson-databind-2.13.1.jar;D:\maven\aliyun_repo\com\fasterxml\jackson\core\jackson-annotations\2.13.1\jackson-annotations-2.13.1.jar;D:\maven\aliyun_repo\com\fasterxml\jackson\core\jackson-core\2.13.1\jackson-core-2.13.1.jar;D:\maven\aliyun_repo\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.1\jackson-datatype-jdk8-2.13.1.jar;D:\maven\aliyun_repo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.1\jackson-datatype-jsr310-2.13.1.jar;D:\maven\aliyun_repo\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.1\jackson-module-parameter-names-2.13.1.jar;D:\maven\aliyun_repo\org\springframework\boot\spring-boot-starter-tomcat\2.6.3\spring-boot-starter-tomcat-2.6.3.jar;D:\maven\aliyun_repo\org\apache\tomcat\embed\tomcat-embed-core\9.0.56\tomcat-embed-core-9.0.56.jar;D:\maven\aliyun_repo\org\apache\tomcat\embed\tomcat-embed-el\9.0.56\tomcat-embed-el-9.0.56.jar;D:\maven\aliyun_repo\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.56\tomcat-embed-websocket-9.0.56.jar;D:\maven\aliyun_repo\org\springframework\spring-web\5.3.15\spring-web-5.3.15.jar;D:\maven\aliyun_repo\org\springframework\spring-beans\5.3.15\spring-beans-5.3.15.jar;D:\maven\aliyun_repo\org\springframework\spring-webmvc\5.3.15\spring-webmvc-5.3.15.jar;D:\maven\aliyun_repo\org\springframework\spring-aop\5.3.15\spring-aop-5.3.15.jar;D:\maven\aliyun_repo\org\springframework\spring-context\5.3.15\spring-context-5.3.15.jar;D:\maven\aliyun_repo\org\projectlombok\lombok\1.18.22\lombok-1.18.22.jar;D:\maven\aliyun_repo\org\springframework\spring-core\5.3.15\spring-core-5.3.15.jar;D:\maven\aliyun_repo\org\springframework\spring-jcl\5.3.15\spring-jcl-5.3.15.jar;D:\maven\aliyun_repo\org\jeasy\easy-rules-core\4.1.0\easy-rules-core-4.1.0.jar;D:\maven\aliyun_repo\org\slf4j\slf4j-api\1.7.33\slf4j-api-1.7.33.jar;D:\maven\aliyun_repo\org\jeasy\easy-rules-spel\4.1.0\easy-rules-spel-4.1.0.jar;D:\maven\aliyun_repo\org\jeasy\easy-rules-support\4.1.0\easy-rules-support-4.1.0.jar;D:\maven\aliyun_repo\com\fasterxml\jackson\dataformat\jackson-dataformat-yaml\2.13.1\jackson-dataformat-yaml-2.13.1.jar;D:\maven\aliyun_repo\org\springframework\spring-expression\5.3.15\spring-expression-5.3.15.jar;D:\maven\aliyun_repo\org\jeasy\easy-rules-mvel\4.1.0\easy-rules-mvel-4.1.0.jar;D:\maven\aliyun_repo\org\mvel\mvel2\2.4.10.Final\mvel2-2.4.10.Final.jar com.example.demo.Test
11:28:17.151 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@727803de
11:28:17.164 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
11:28:17.256 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
11:28:17.257 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
11:28:17.258 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
11:28:17.259 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
11:28:17.265 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'mySpringAppConfig'
11:28:17.270 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'mySpringBean'
11:28:17.397 [main] DEBUG org.jeasy.rules.core.DefaultRulesEngine - Engine parameters { skipOnFirstAppliedRule = false, skipOnFirstNonTriggeredRule = false, skipOnFirstFailedRule = false, priorityThreshold = 2147483647 }
11:28:17.398 [main] DEBUG org.jeasy.rules.core.DefaultRulesEngine - Registered rules:
11:28:17.399 [main] DEBUG org.jeasy.rules.core.DefaultRulesEngine - Rule { name = 'weather rule one', description = 'if it rains then take an umbrella', priority = '1'}
11:28:17.399 [main] DEBUG org.jeasy.rules.core.DefaultRulesEngine - Rule { name = 'weather rule second', description = 'if it rains then take an umbrella', priority = '2'}
11:28:17.399 [main] DEBUG org.jeasy.rules.core.DefaultRulesEngine - Rule { name = 'weather rule three 1', description = 'if it rains then take an umbrella', priority = '3'}
11:28:17.399 [main] DEBUG org.jeasy.rules.core.DefaultRulesEngine - Rule { name = 'weather rule three 2', description = 'if it rains then take an umbrella', priority = '3'}
11:28:17.399 [main] DEBUG org.jeasy.rules.core.DefaultRulesEngine - Known facts:
11:28:17.399 [main] DEBUG org.jeasy.rules.core.DefaultRulesEngine - Fact{name='weatherRule', value=com.example.demo.WeatherRule@146dfe6}
11:28:17.399 [main] DEBUG org.jeasy.rules.core.DefaultRulesEngine - Fact{name='rain', value=true}
11:28:17.399 [main] DEBUG org.jeasy.rules.core.DefaultRulesEngine - Rules evaluation started
11:28:17.400 [main] DEBUG org.jeasy.rules.core.DefaultRulesEngine - Rule 'weather rule one' triggered
11:28:17.401 [main] DEBUG org.jeasy.rules.core.DefaultRulesEngine - Rule 'weather rule one' performed successfully
11:28:17.402 [main] DEBUG org.jeasy.rules.core.DefaultRulesEngine - Rule 'weather rule second' triggered
11:28:17.402 [main] DEBUG org.jeasy.rules.core.DefaultRulesEngine - Rule 'weather rule second' performed successfully
11:28:17.424 [main] DEBUG org.jeasy.rules.core.DefaultRulesEngine - Rule 'weather rule three 1' triggered
first, It rains, take an umbrella!
second, It rains, take an umbrella!
spring bean ==>three 1 1, It rains, take an umbrella!
11:28:17.437 [main] DEBUG org.jeasy.rules.core.DefaultRulesEngine - Rule 'weather rule three 1' performed successfully
11:28:17.460 [main] DEBUG org.jeasy.rules.core.DefaultRulesEngine - Rule 'weather rule three 2' triggered
11:28:17.463 [main] DEBUG org.jeasy.rules.core.DefaultRulesEngine - Rule 'weather rule three 2' performed successfully
three 2 1, It rains, take an umbrella!
three 2 2, It rains, take an umbrella!

Process finished with exit code 0

空文件

简介

规则引擎easyRule使用,支持spring中bean的使用 展开 收起
Java
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/MissJin/easy-rule-demo.git
git@gitee.com:MissJin/easy-rule-demo.git
MissJin
easy-rule-demo
easy-rule-demo
master

搜索帮助