1. Overview
1.概述
In this article, we’ll go through some of the most popular rule engines for Java.
在这篇文章中,我们将介绍一些最流行的Java规则引擎。
Within mission-critical applications, the process of maintaining business logic within the source code can become too complicated. Business rules can be used to ease the development and maintenance by separating business logic from the source code.
在关键任务的应用程序中,在源代码中维护业务逻辑的过程可能变得过于复杂。业务规则可以通过将业务逻辑从源代码中分离出来,来缓解开发和维护工作。
In the Java world, most of the rules engines libraries implement JSR94 standard known as Java Rule API Engine.
在Java世界中,大多数规则引擎库都实现了JSR94标准,即Java Rule API Engine。
2. Drools
2.Drools
Drools is a Business Rules Management System (BRMS) solution. Drools can integrate with jBPM, a Business Process Management tool for standardization of process, events activities, tasks, etc.
Drools是一个业务规则管理系统(BRMS)解决方案。Drools可以与jBPM集成,jBPM是一个业务流程管理工具,用于实现流程、事件活动、任务等的标准化。
If you want to read more, an introduction to Drools is available here, along with an article on integration with Spring.
如果您想阅读更多内容,可以在这里找到Drools的介绍,以及一篇关于与Spring集成的文章。
3. OpenL Tablets
3.OpenL平板电脑
OpenL Tablets is a business rules management system and a business rules engine based on Excel decision tables. Since the format of tables used by this framework is familiar to business users, it bridges the gap between business users and developers.
OpenL Tablets是一个业务规则管理系统和一个基于Excel决策表的业务规则引擎。由于该框架所使用的表格格式是商业用户所熟悉的,因此它在商业用户和开发人员之间架起了桥梁。
Here is a simple example of how the framework works by using an Excel file containing decision tables. First, let’s import its dependencies which rely on org.openl.core and org.openl.rules modules:
下面是一个简单的例子,通过使用一个包含决策表的Excel文件来说明该框架如何工作。首先,让我们导入它的依赖项,它依赖于org.openl.core和org.openl.rules模块。
<dependency>
<groupId>org.openl</groupId>
<artifactId>org.openl.core</artifactId>
<version>5.19.4</version>
</dependency>
<dependency>
<groupId>org.openl.rules</groupId>
<artifactId>org.openl.rules</artifactId>
<version>5.19.4</version>
</dependency>
Now, a User POJO:
现在,一个User POJO。
public class User {
private String name;
// getters and setters
}
And an enum that will represent the outcome of the applied rules:
还有一个枚举,将代表应用规则的结果。
public enum Greeting {
// ...
}
The Case class wraps the User object with variables that lead to outcomes:
Case类将User对象与导致结果的变量包装起来。
public class Case {
// Variables to infer outcomes
// getters and setters
}
The interface IRule contains the rule injected by the Excel file:
接口IRule包含由Excel文件注入的规则。
public interface IRule {
void helloUser(Case aCase, final Response response);
}
The Response class handles the return of the applied rule:
Response类处理应用规则的返回。
public class Response {
private String result;
private Map<String, String> map = new HashMap<>();
}
The main class, which triggers the rule execution:
主类,触发规则的执行。
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
4.简易规则
Easy Rules is a simple Java rules engine providing a lightweight and POJO based framework to define business. It can create complex rules from primitive ones by using the composite pattern.
Easy Rules是一个简单的Java规则引擎,提供了一个轻量级和基于POJO的框架来定义业务。它可以通过使用复合模式从原始的规则中创建复杂的规则。
This framework, in contrast to the most traditional rules engines, doesn’t make use of XML files or any Domain Specific Language files to segregate rules from the application. It uses annotation-based classes and methods for injecting business logic into the application.
这个框架与最传统的规则引擎相比,没有利用XML文件或任何特定领域的语言文件来将规则与应用隔离开来。它使用基于注解的类和方法,将业务逻辑注入到应用程序中。
Easy Rules can be handy for developers to create and maintain applications with business logic that’s entirely separated from the application itself. On the other hand, as this framework doesn’t implement the JSR94 standard and the business logic has to be coded straight to Java code.
对于开发者来说,Easy Rules可以方便地创建和维护具有完全与应用程序本身分离的业务逻辑的应用程序。另一方面,由于这个框架没有实现JSR94标准,业务逻辑必须直接编码为Java代码。
Here we provide a “Hello, world” example. Let’s import the required dependencies based on the easy-rules-core module:
这里我们提供一个 “你好,世界 “的例子。让我们根据easy-rules-core模块来导入所需的依赖项。
<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-core</artifactId>
<version>3.0.0</version>
</dependency>
Next, we create a class that defines a rule:
接下来,我们创建一个定义规则的类。
@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");
}
}
Finally, we create the main class:
最后,我们创建主类。
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
5.规则书》
RuleBook is a Java framework that leverages Java 8 lambdas and the Chain of Responsibility Pattern to define rules using simple BDD approach.
RuleBook是一个Java框架,利用Java 8 lambdas和责任链模式,用简单的BDD方法定义规则。
Like most rules engines, RuleBook makes use of the concept of “Facts”, which is data supplied to rules. RuleBook allows rules to modify the state of facts, which then can be read and modified by rules further down the chain. For those rules that read in data (Facts) of one type and output a result of a different type, RuleBook has Decisions.
像大多数规则引擎一样,RuleBook使用了”Facts“的概念,它是提供给规则的数据。RuleBook允许规则修改事实的状态,然后这些事实可以被更多的规则读取和修改。对于那些读入一种类型的数据(Facts)并输出另一种类型的结果的规则,RuleBook有Decisions。
RuleBook can be integrated with Spring using Java DSL.
RuleBook可以使用Java DSL与Spring集成。
Here, we provide a simple “Hello, world” example using RuleBook. Let’s add its dependency which is relying on the rulebook-core module:
在这里,我们使用RuleBook提供了一个简单的 “Hello, world “例子。让我们添加它的依赖性,它依赖于rulebook-core模块。
<dependency>
<groupId>com.deliveredtechnologies</groupId>
<artifactId>rulebook-core</artifactId>
<version>0.6.2</version>
</dependency>
Now, we create the rule:
现在,我们创建规则。
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();
}
}
Finally, the main class:
最后是主类。
public static void main(String[] args) {
HelloWorldRule ruleBook = new HelloWorldRule();
ruleBook
.defineHelloWorldRules()
.run(new FactMap<>());
}
6. Conclusion
6.结论
In this quick article, we’ve discussed some well-known libraries that provide engines for business logic abstraction.
在这篇短文中,我们讨论了一些著名的库,它们为业务逻辑抽象提供了引擎。
As always, examples from this article are available on our GitHub repository.
一如既往,本文的例子可在我们的GitHub 仓库中找到。