规则引擎入门
关于规则引擎,我们在工作中应该会经常遇到,例如我们对不同的用户给不同的折扣。前一段时间在网上闲逛,发现一个很简单的规则引擎,一下是学习笔记。
在使用之前,我们要先导入 jar 包:
<dependency> <groupId>org.jeasy</groupId> <artifactId>easy-rules-core</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>org.jeasy</groupId> <artifactId>easy-rules-mvel</artifactId> <version>3.3.0</version> </dependency>
一. 使用零配置的方式:
1. 规则引擎入口:
package cn.bridgeli.demo.rule; import org.jeasy.rules.api.Facts; import org.jeasy.rules.api.Rules; import org.jeasy.rules.api.RulesEngine; import org.jeasy.rules.core.DefaultRulesEngine; import org.jeasy.rules.core.RulesEngineParameters; import org.junit.Test; /** * @author bridgeli */ public class ThreeEightRuleTest { @Test public void testRule() { /** * 创建规则执行引擎 * 注意: skipOnFirstAppliedRule意思是,只要匹配到第一条规则就跳过后面规则匹配 */ RulesEngineParameters parameters = new RulesEngineParameters().skipOnFirstAppliedRule(true); RulesEngine rulesEngine = new DefaultRulesEngine(parameters); //创建规则 Rules rules = new Rules(); rules.register(new EightRule()); rules.register(new ThreeRule()); rules.register(new ThreeEightRuleUnitGroup(new EightRule(), new ThreeRule())); rules.register(new OtherRule()); Facts facts = new Facts(); for (int i = 1; i <= 50; i++) { //规则因素,对应的name,要和规则里面的@Fact 一致 facts.put("number", i); //执行规则 rulesEngine.fire(rules, facts); System.out.println(); } } }
这个是判断 1- 50 里面,哪些是 3 的倍数、哪些是 8 的倍数、哪些是 3 和 8 的倍数。
2. 各种规则的实现:
package cn.bridgeli.demo.rule; import org.jeasy.rules.annotation.Action; import org.jeasy.rules.annotation.Condition; import org.jeasy.rules.annotation.Fact; import org.jeasy.rules.annotation.Priority; import org.jeasy.rules.annotation.Rule; /** * @author bridgeli */ @Rule(name = "被8整除") public class EightRule { @Condition public boolean isEight(@Fact("number") int number) { return number % 8 == 0; } @Action public void eightAction(@Fact("number") int number) { System.out.print(number + " is eight"); } @Priority public int getPriority() { return 2; } } package cn.bridgeli.demo.rule; import org.jeasy.rules.annotation.Action; import org.jeasy.rules.annotation.Condition; import org.jeasy.rules.annotation.Fact; import org.jeasy.rules.annotation.Priority; import org.jeasy.rules.annotation.Rule; /** * @author bridgeli */ @Rule(name = "被3整除", description = "number如果被3整除,打印:number is three") public class ThreeRule { /** * 条件判断注解:如果return true, 执行Action * * @param number * @return */ @Condition public boolean isThree(@Fact("number") int number) { return number % 3 == 0; } /** * 执行方法注解 * * @param number */ @Action public void threeAction(@Fact("number") int number) { System.out.print(number + " is three"); } /** * 优先级注解:return 数值越小,优先级越高 * * @return */ @Priority public int getPriority() { return 1; } } package cn.bridgeli.demo.rule; import org.jeasy.rules.annotation.Rule; import org.jeasy.rules.support.UnitRuleGroup; /** * @author bridgeli */ @Rule(name = "被3和8同时整除", description = "这是一个组合规则") public class ThreeEightRuleUnitGroup extends UnitRuleGroup { public ThreeEightRuleUnitGroup(Object... rules) { for (Object rule : rules) { addRule(rule); } } @Override public int getPriority() { return 0; } } package cn.bridgeli.demo.rule; import org.jeasy.rules.annotation.Action; import org.jeasy.rules.annotation.Condition; import org.jeasy.rules.annotation.Fact; import org.jeasy.rules.annotation.Priority; import org.jeasy.rules.annotation.Rule; /** * @author bridgeli */ @Rule(name = "既不被3整除也不被8整除", description = "打印number自己") public class OtherRule { @Condition public boolean isOther(@Fact("number") int number) { return number % 3 != 0 || number % 8 != 0; } @Action public void printSelf(@Fact("number") int number) { System.out.print(number); } @Priority public int getPriority() { return 3; } }
二. MVEL 的方式
除了上面的方式我们还可以通过 MVEL 的方式实现。我们首先也是看入口
1. 规则入口:
package cn.bridgeli.demo.rule; 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.mvel.MVELRule; import org.jeasy.rules.mvel.MVELRuleFactory; import org.jeasy.rules.support.YamlRuleDefinitionReader; import org.junit.Test; import java.io.FileReader; /** * @author bridgeli */ public class ShopTest { @Test public void testMvel() throws Exception { //创建规则1 Rule ageRule = new MVELRule() .name("age rule") .description("Check if person's age is > 18 and marks the person as adult") .priority(1) .when("person.age > 18") .then("person.setAdult(true); System.out.println(\"Shop: OK, you are allowed to buy alcohol\");"); //创建规则2 MVELRuleFactory ruleFactory = new MVELRuleFactory(new YamlRuleDefinitionReader()); Rule alcoholRule = ruleFactory.createRule(new FileReader("src/main/resources/alcohol-rule.yml")); Rules rules = new Rules(); rules.register(ageRule); rules.register(alcoholRule); //创建规则执行引擎,并执行规则 RulesEngine rulesEngine = new DefaultRulesEngine(); System.out.println("Tom: Hi! can I have some Vodka please?"); //创建一个Person实例(Fact) Person person = new Person(); person.setName("Tom"); // person.setAge(20); person.setAge(18); Facts facts = new Facts(); facts.put("person", person); rulesEngine.fire(rules, facts); } }
这个是根据年龄判断是一个人是否可以在超市买酒的判断。
2. yml 配置文件
name: "alcohol rule" description: "children are not allowed to buy alcohol" priority: 2 condition: "person.isAdult() == false" actions: - "System.out.println(\"Shop: Sorry, you are not allowed to buy alcohol\");"
这里面需要一个 person 对象,其实很简单
3. person 对象
package cn.bridgeli.demo.rule; import lombok.Data; /** * @author bridgeli */ @Data public class Person { private String name; private boolean adult; private int age; }
简单使用了一个 lombok 插件,相信大家都知道这是什么,也可以不用,使用 get、set 方法,所以不做介绍了。
通过这两个雷子,我们就可以随心所用的使用规则引擎了,消除我们代码中的各种 if、else 判断,体现了设计模式的开闭原则。
参考资料:本来是要列出来的,但是原网站挂掉了。。。
全文完,如果本文对您有所帮助,请花 1 秒钟帮忙点击一下广告,谢谢。
作 者: BridgeLi,https://www.bridgeli.cn
原文链接:http://www.bridgeli.cn/archives/686
版权声明:非特殊声明均为本站原创作品,转载时请注明作者和原文链接。
作 者: BridgeLi,https://www.bridgeli.cn
原文链接:http://www.bridgeli.cn/archives/686
版权声明:非特殊声明均为本站原创作品,转载时请注明作者和原文链接。
近期评论