easyrulesjava实例_规则引擎EasyRules使⽤实例(⼀)
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
语⾔: java
依赖:
org.jeasy
easy-rules-core
3.2.0
org.jeasy
easy-rules-support
3.2.0
org.jeasy
easy-rules-mvel
3.2.0
⼀、使⽤场景
在编写代码过程中,我们对于if...el...语句是相当的熟悉了。但是如果条件分⽀⽐较多,⽐如有⼗⼏个、⼏⼗个、上百个,甚⾄更多时,如果我们还坚持在业务逻辑代码中使⽤if...el...语句,那么这个⽂件中的代码显得⼗分不协调,并且如果我们以后想再增加或减少规则时,还要来到这块业务逻辑代码中去修改if...el...语句,这给将来的维护带来了⼀定的不便。
针对这种情况,我们可以考虑使⽤规则引擎来解决。规则引擎不只⼀种,本⽂介绍的是easy rule规则引擎的使⽤。对于上⾯的场景,easy rule会把if...el...中的逻辑从业务逻辑代码中提取出来,并把这些逻辑存放在其他⽂件中。这样做以后,业务逻辑代码看上去就清爽了很多。同时如果将来我们想要修改这些规则时,直接去找这些存放规则的⽂件就⾏了。在easy rule中,存放规则有两种⽅式,⼀种是使⽤
.java ⽂件,另外⼀种是使⽤ .yml ⽂件,下⾯我们分别介绍。
⼆、使⽤实例
假设我们有这样⼀个场景:
(1)如果⼀个数字可以被5整除,则输出“fizz”;
(2)如果⼀个数字可以被7整除,则输出“buzz”;
(3)如果⼀个数字可以同时被5和7整除,则输出“fizzbuzz”;
(4)如果⼀个数字不满⾜以上三个条件,则输出这个数字本⾝。
1、不使⽤规则引擎的实现⽅式:
public class FizzBuzz {
public static void main(String[] args) {
for(int i = 1; i <= 100; i++) {
if (((i % 5) == 0) && ((i % 7) == 0))
System.out.print("fizzbuzz");
el if ((i % 5) == 0) System.out.print("fizz");
el if ((i % 7) == 0) System.out.print("buzz");
el System.out.print(i);
System.out.println();
}
System.out.println();北京故宫介绍资料
}
}
2、将规则存放在 .java ⽂件中:
(1).java 规则⽂件内容如下:
public class RuleClass {
@Rule(priority = 1)
public static class FizzRule {
@Condition
public boolean isFizz(@Fact("number") Integer number) { return number % 5 == 0;
}
@Action
红豆沙馅的做法
public void printFizz() {
System.out.print("fizz");
}
}
@Rule(priority = 2)
public static class BuzzRule {
@Condition
public boolean isBuzz(@Fact("number") Integer number) { return number % 7 == 0;
}
@Action
public void printBuzz() {
System.out.print("buzz");
}
}
public static class FizzBuzzRule extends UnitRuleGroup { public rules) {
for (Object rule : rules) {
addRule(rule);
}
}
@Override
public int getPriority() {
return 0;
红豺}
}
@Rule(priority = 3)
前期物业管理合同public static class NonFizzBuzzRule {
@Condition
俄罗斯生育率
public boolean isNotFizzNorBuzz(@Fact("number") Integer number) {
// can return true, becau this is the latest rule to trigger according to
// assigned priorities
// and in which ca, the number is not fizz nor buzz
return number % 5 != 0 || number % 7 != 0;
}
@Action
public void printInput(@Fact("number") Integer number) {
System.out.print(number);
}
}
}
(2)客户端调⽤代码如下:
public class RuleClient {
public static void main(String[] args) {
// create a rules engine
RulesEngineParameters parameters = new RulesEngineParameters().skipOnFirstAppliedRule(true); RulesEngine fizzBuzzEngine = new DefaultRulesEngine(parameters);
// create rules
Rules rules = new Rules();
// fire rules
Facts facts = new Facts();
for (int i = 1; i <= 100; i++) {
facts.put("number", i);
fizzBuzzEngine.fire(rules, facts);
System.out.println();
并列式
}
}
}
代码注解:
注解1
RulesEngineParameters parameters = new RulesEngineParameters().skipOnFirstAppliedRule(true);
这⾏代码的作⽤是为接下来的RulesEngine的创建设置属性。这⾥只设置了⼀个属性skipOnFirstAppliedRule,意思是在之后执⾏RuleClass中的规则时,只要有⼀个规则被触发,则当前被传进来的Fact就不再判断是否满⾜其他规则的条件。
像这样的属性还有⼏个,我们在接下来的⽂章中将会讲到。
注解2
我们要注意Facts的使⽤。Facts的⽤法很像Map,它是客户端和规则⽂件之间通信的桥梁。在客户端使⽤put⽅法向Facts中添加数据,在规则⽂件中通过key来得到相应的数据。
3、将规则⽂件存放在 .yml ⽂件中:
(1).yml 规则⽂件内容如下:
---
name: "fizz rule"
description: "print fizz if the number is multiple of 5"
priority: 1
condition: "number % 5 == 0"
actions:
- "System.out.println(\"fizz\")"
---
name: "buzz rule"实习小结200字
description: "print buzz if the number is multiple of 7"
priority: 2
condition: "number % 7 == 0"
actions:
- "System.out.println(\"buzz\")"
---
name: "fizzbuzz rule"
description: "print fizzbuzz if the number is multiple of 5 and 7"
priority: 0
condition: "number % 5 == 0 && number % 7 == 0"
actions:
- "System.out.println(\"fizzbuzz\")"
---
name: "non fizzbuzz rule"
description: "print the number itlf otherwi"
priority: 3
condition: "number % 5 != 0 || number % 7 != 0"
actions:
- "System.out.println(number)"
(2)客户端调⽤代码如下:
圆谎
public class RuleClient {
public static void main(String[] args) throws FileNotFoundException {
// create a rules engine
RulesEngineParameters parameters = new RulesEngineParameters().skipOnFirstAppliedRule(true); RulesEngine fizzBuzzEngine = new DefaultRulesEngine(parameters);
// create rules
Rules rules = ateRulesFrom(new FileReader("l"));
// fire rules
Facts facts = new Facts();
for (int i = 1; i <= 100; i++) {
facts.put("number", i);
fizzBuzzEngine.fire(rules, facts);
System.out.println();
}
}
}
执⾏结果如下:
1
2
3