簡(jiǎn)書著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請(qǐng)聯(lián)系作者獲得授權(quán)并注明出處诺擅。
語(yǔ)言: java
依賴:
<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-core</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-support</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-mvel</artifactId>
<version>3.2.0</version>
</dependency>
一市袖、使用場(chǎng)景
? ? ? ?在編寫代碼過(guò)程中,我們對(duì)于if...else...語(yǔ)句是相當(dāng)?shù)氖煜ち恕5侨绻麠l件分支比較多苍碟,比如有十幾個(gè)酒觅、幾十個(gè)、上百個(gè)微峰,甚至更多時(shí)舷丹,如果我們還堅(jiān)持在業(yè)務(wù)邏輯代碼中使用if...else...語(yǔ)句,那么這個(gè)文件中的代碼顯得十分不協(xié)調(diào)蜓肆,并且如果我們以后想再增加或減少規(guī)則時(shí)颜凯,還要來(lái)到這塊業(yè)務(wù)邏輯代碼中去修改if...else...語(yǔ)句,這給將來(lái)的維護(hù)帶來(lái)了一定的不便仗扬。
? ? ? ? 針對(duì)這種情況症概,我們可以考慮使用規(guī)則引擎來(lái)解決。規(guī)則引擎不只一種早芭,本文介紹的是easy rule規(guī)則引擎的使用彼城。對(duì)于上面的場(chǎng)景,easy rule會(huì)把if...else...中的邏輯從業(yè)務(wù)邏輯代碼中提取出來(lái)退个,并把這些邏輯存放在其他文件中募壕。這樣做以后,業(yè)務(wù)邏輯代碼看上去就清爽了很多语盈。同時(shí)如果將來(lái)我們想要修改這些規(guī)則時(shí)舱馅,直接去找這些存放規(guī)則的文件就行了。在easy rule中黎烈,存放規(guī)則有兩種方式习柠,一種是使用 .java 文件,另外一種是使用 .yml 文件照棋,下面我們分別介紹资溃。
二、使用實(shí)例
? ? ? ?假設(shè)我們有這樣一個(gè)場(chǎng)景:
? ? ? ? (1)如果一個(gè)數(shù)字可以被5整除烈炭,則輸出“fizz”溶锭;
? ? ? ? (2)如果一個(gè)數(shù)字可以被7整除,則輸出“buzz”符隙;
? ? ? ? (3)如果一個(gè)數(shù)字可以同時(shí)被5和7整除趴捅,則輸出“fizzbuzz”;
? ? ? ? (4)如果一個(gè)數(shù)字不滿足以上三個(gè)條件霹疫,則輸出這個(gè)數(shù)字本身拱绑。
1、不使用規(guī)則引擎的實(shí)現(xiàn)方式:
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");
else if ((i % 5) == 0) System.out.print("fizz");
else if ((i % 7) == 0) System.out.print("buzz");
else System.out.print(i);
System.out.println();
}
System.out.println();
}
}
2丽蝎、將規(guī)則存放在 .java 文件中:
? ? (1).java 規(guī)則文件內(nèi)容如下:
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 FizzBuzzRule(Object... 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, because this is the latest rule to trigger according to
// assigned priorities
// and in which case, 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)客戶端調(diào)用代碼如下:
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();
rules.register(new FizzRule());
rules.register(new BuzzRule());
rules.register(new RuleClass.FizzBuzzRule(new RuleClass.FizzRule(), new RuleClass.BuzzRule()));
rules.register(new NonFizzBuzzRule());
// 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);
? ? ? ? 這行代碼的作用是為接下來(lái)的RulesEngine的創(chuàng)建設(shè)置屬性猎拨。這里只設(shè)置了一個(gè)屬性skipOnFirstAppliedRule,意思是在之后執(zhí)行RuleClass中的規(guī)則時(shí),只要有一個(gè)規(guī)則被觸發(fā)红省,則當(dāng)前被傳進(jìn)來(lái)的Fact就不再判斷是否滿足其他規(guī)則的條件额各。
? ? ? ? 像這樣的屬性還有幾個(gè),我們?cè)诮酉聛?lái)的文章中將會(huì)講到吧恃。
注解2
? ? ? ? 我們要注意Facts的使用虾啦。Facts的用法很像Map,它是客戶端和規(guī)則文件之間通信的橋梁痕寓。在客戶端使用put方法向Facts中添加數(shù)據(jù)傲醉,在規(guī)則文件中通過(guò)key來(lái)得到相應(yīng)的數(shù)據(jù)。
3厂抽、將規(guī)則文件存放在 .yml 文件中:
(1).yml 規(guī)則文件內(nèi)容如下:
---
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"
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 itself otherwise"
priority: 3
condition: "number % 5 != 0 || number % 7 != 0"
actions:
- "System.out.println(number)"
? ? ? ? (2)客戶端調(diào)用代碼如下:
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 = MVELRuleFactory.createRulesFrom(new FileReader("fizzbuzz.yml"));
// fire rules
Facts facts = new Facts();
for (int i = 1; i <= 100; i++) {
facts.put("number", i);
fizzBuzzEngine.fire(rules, facts);
System.out.println();
}
}
}
執(zhí)行結(jié)果如下:
1
2
3
4
fizz
6
buzz
8
9
fizz
11
12
13
buzz
fizz
16
17
18
19
fizz
buzz
22
23
24
fizz
26
27
buzz
29
fizz
31
32
33
34
fizzbuzz
36
37
38
39
fizz
41
buzz
43
44
fizz
46
47
48
buzz
fizz
51
52
53
54
fizz
buzz
57
58
59
fizz
61
62
buzz
64
fizz
66
67
68
69
fizzbuzz
71
72
73
74
fizz
76
buzz
78
79
fizz
81
82
83
buzz
fizz
86
87
88
89
fizz
buzz
92
93
94
fizz
96
97
buzz
99
fizz