1. 概述
本文将介绍Java中最流行的几款规则引擎。在关键业务系统中,将业务逻辑硬编码在源代码里会变得难以维护。通过规则引擎将业务逻辑与源代码分离,能显著简化开发和维护工作。
Java领域的规则引擎大多实现JSR94标准(即Java Rule API规范)。
2. Drools
Drools 是一款业务规则管理系统(BRMS)解决方案。它可与jBPM(业务流程管理工具)集成,用于标准化流程、事件活动、任务等。
想深入了解?可以阅读这篇Drools入门指南和与Spring集成教程。
3. OpenL Tablets
OpenL Tablets 是基于Excel决策表的业务规则管理系统和引擎。由于使用的表格格式业务人员熟悉,它架起了业务人员与开发者之间的桥梁。
下面通过Excel决策表演示框架工作原理。首先添加依赖(基于org.openl.core和org.openl.rules模块):
<dependency>
<groupId>org.openl</groupId>
<artifactId>org.openl.core</artifactId>
<version>5.26.5</version>
</dependency>
<dependency>
<groupId>org.openl.rules</groupId>
<artifactId>org.openl.rules</artifactId>
<version>5.26.5</version>
</dependency>
创建User POJO类:
public class User {
private String name;
// getters and setters
}
定义规则执行结果的枚举:
public enum Greeting {
// ...
}
Case类封装User对象和推导结果所需的变量:
public class Case {
// Variables to infer outcomes
// getters and setters
}
IRule接口包含Excel文件注入的规则方法:
public interface IRule {
void helloUser(Case aCase, final Response response);
}
Response类处理规则执行结果:
public class Response {
private String result;
private Map<String, String> map = new HashMap<>();
}
触发规则执行的主类:
public class Main {
private IRule instance;
public static void main(String[] args) {
Main rules = new Main();
// setup user and case here
rules.process(aCase);
}
public void process(Case aCase) {
EngineFactory<IRule> engineFactory = new RulesEngineFactory<IRule>(
getClass().getClassLoader()
.getResource("openltablets/HelloUser.xls"), IRule.class);
instance = engineFactory.newEngineInstance();
instance.helloUser(aCase, new Response());
}
}
4. Easy Rules
Easy Rules是轻量级Java规则引擎,提供基于POJO的框架定义业务规则。它通过组合模式可从简单规则构建复杂规则。
与传统规则引擎不同,Easy Rules不使用XML或DSL文件分离规则,而是通过注解注入业务逻辑。
✅ 优势:开发者可轻松创建和维护业务逻辑完全分离的应用
❌ 局限:未实现JSR94标准,业务逻辑需直接编写Java代码
下面看"Hello World"示例。添加easy-rules-core依赖:
<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-core</artifactId>
<version>4.1.0</version>
</dependency>
创建规则类:
@Rule(name = "Hello World rule", description = "Always say hello world")
public class HelloWorldRule {
@Condition
public boolean when() {
return true;
}
@Action
public void then() throws Exception {
System.out.println("hello world");
}
}
主程序:
public class Launcher {
public static void main(String... args) {
// create facts
Facts facts = new Facts();
// create rules
Rules rules = new Rules();
rules.register(new HelloWorldRule());
// create a rules engine and fire rules on known facts
RulesEngine rulesEngine = new DefaultRulesEngine();
rulesEngine.fire(rules, facts);
}
}
5. RuleBook
RuleBook是Java 8 lambda和责任链模式驱动的规则框架,采用简单BDD(行为驱动开发)方式定义规则。
类似多数规则引擎,RuleBook使用"Facts"(事实)概念作为规则输入数据。允许规则修改事实状态,后续规则可读取修改后的结果。当输入输出类型不同时,使用"Decisions"处理。
支持通过Java DSL与Spring集成。
下面看"Hello World"示例。添加rulebook-core依赖:
<dependency>
<groupId>com.deliveredtechnologies</groupId>
<artifactId>rulebook-core</artifactId>
<version>0.12</version>
</dependency>
创建规则:
public class HelloWorldRule {
public RuleBook<Object> defineHelloWorldRules() {
return RuleBookBuilder
.create()
.addRule(rule -> rule.withNoSpecifiedFactType()
.then(f -> System.out.print("Hello ")))
.addRule(rule -> rule.withNoSpecifiedFactType()
.then(f -> System.out.println("World")))
.build();
}
}
主程序:
public static void main(String[] args) {
HelloWorldRule ruleBook = new HelloWorldRule();
ruleBook
.defineHelloWorldRules()
.run(new FactMap<>());
}
6. 总结
本文介绍了几个用于业务逻辑抽象的知名规则引擎库。各引擎特点对比:
引擎 | 核心特点 | 适用场景 |
---|---|---|
Drools | 完整BRMS方案,支持BPM集成 | 企业级复杂规则系统 |
OpenL Tablets | Excel决策表驱动 | 业务人员频繁修改规则的场景 |
Easy Rules | 注解驱动,轻量级 | 简单规则快速实现 |
RuleBook | Lambda+责任链,BDD风格 | Java 8项目链式规则处理 |
⚠️ 选用建议:
- 企业级复杂系统优先考虑Drools
- 需业务人员参与维护选OpenL
- 简单场景快速开发用Easy Rules
- 函数式风格选RuleBook
完整示例代码见GitHub仓库。