一、什么是规则引擎
最近在工作中,遇到一类上层业务规则多变的场景。所以研究了规则引擎相关的内容,在这里可以与大家一起探讨。规则引擎相关的概念和相关业内使用,可以参考以下两篇文章,分析的很到位,也指出了大体的方向和具体的实践:
《规则引擎概述》https://blog.csdn.net/express_wind/article/details/77141674
《从0到1:构建强大且易用的规则引擎》https://tech.meituan.com/maze_framework.html
二、规则引擎实践
规则引擎的实现有比较多的开源组件,比较出名的是Drools,属于重量级的实现。本文主要介绍一款轻量级的规则引擎实现:easyrules
1、什么是EasyRules?
首先EasyRule是一个规则引擎.这个名字由来是受到了Martin Fowler 的文章 Should I use a Rules Engine
You can build a simple rules engine yourself. All you need is to create a bunch of objects with conditions and actions, store them in a collection, and run through them to evaluate the conditions and execute the actions.
2、框架特点
- 轻量级类库和容易上手
- 基于POJO的开发与注解的编程模型
- 方便且适用于java的抽象的业务模型规则
- 支持从简单的规则创建组合规则
Useful abstractions to define business rules and apply them easily with Java
The ability to create composite rules from primitive ones
3、实践-FizzBuzz
循环打印 1 到 100,但如果是 5 的倍數就印 fizz,如果是 7 的倍數就印 buzz,如果同時是 5 和 7 的倍數就印 fizzzuzz,否则打印数字本身
不使用规则引擎
@Test
public void fizzbuzz() {
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();
}
使用Easy Rule
定义FizzRule
检查输入是否是fizz
@Rule
public class FizzRule {
private int input;
@Condition
public boolean isFizz() {
return input % 5 == 0;
}
@Action
public void printFizz() {
System.out.print("fizz");
}
public void setInput(int input) {
this.input = input;
}
@Priority
public int getPriority() {
return 1;
}
}
BuzzRule
检查输入是否是buzz
@Rule
public class BuzzRule {
private int input;
@Condition
public boolean isBuzz() {
return input % 7 == 0;
}
@Action
public void printBuzz() {
System.out.print("buzz");
}
public void setInput(int input) {
this.input = input;
}
@Priority
public int getPriority() {
return 2;
}
}
FizzBuzzRule
检查输入是否是fizzbuzz,这是一个组合规则,它由FizzRule和BuzzRule组成
public class FizzBuzzRule extends CompositeRule {
public FizzBuzzRule(Object... rules) {
for (Object rule : rules) {
addRule(rule);
}
}
@Override
public int getPriority() {
return 0;
}
}
NonFizzBuzzRule
@Rule
public class NonFizzBuzzRule {
private int input;
@Condition
public boolean isNotFizzNorBuzz() {
// can return true here
return input % 5 != 0 || input % 7 != 0;
}
@Action
public void printInput() {
System.out.print(input);
}
public void setInput(int input) {
this.input = input;
}
@Priority
public int getPriority() {
return 3;
}
}
执行
public class FizzBuzzWithEasyRules {
public static void main(String[] args) {
// create rules engine
RulesEngine fizzBuzzEngine =
RulesEngineBuilder.aNewRulesEngine()
.withSkipOnFirstAppliedRule(true) // 如果第一个规则满足条件,后面的规则将不再执行
.withSilentMode(true)
.build();
// create rules
FizzRule fizzRule = new FizzRule();
BuzzRule buzzRule = new BuzzRule();
FizzBuzzRule fizzBuzzRule
= new FizzBuzzRule(fizzRule, buzzRule);
NonFizzBuzzRule nonFizzBuzzRule = new NonFizzBuzzRule();
// register rules
fizzBuzzEngine.registerRule(fizzRule);
fizzBuzzEngine.registerRule(buzzRule);
fizzBuzzEngine.registerRule(fizzBuzzRule);
fizzBuzzEngine.registerRule(nonFizzBuzzRule);
// fire rules
for (int i = 1; i <= 100; i++) {
fizzRule.setInput(i);
buzzRule.setInput(i);
nonFizzBuzzRule.setInput(i);
fizzBuzzEngine.fireRules();
System.out.println();
}
}
}
4、相关文档
参考文献:https://www.jianshu.com/p/a8f1c3b789f2
来源:oschina
链接:https://my.oschina.net/u/3729778/blog/1863160