1. Overview
1.概述
Drools has support for managing business rules in a spreadsheet format.
Drools已支持以电子表格格式管理业务规则。
In this article, we’ll see a quick example of using Drools to manage business rules using an Excel file.
在这篇文章中,我们将看到一个使用Drools来管理业务规则的快速例子,使用的是Excel文件。
2. Maven Dependencies
2.Maven的依赖性
Let’s add the required Drools dependencies into our application:
让我们把所需的Drools依赖性添加到我们的应用程序中。
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-ci</artifactId>
<version>7.1.0.Beta2</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-decisiontables</artifactId>
<version>7.1.0.Beta2</version>
</dependency>
The latest version of these dependencies can be found at kie-ci and drools-decisiontables.
这些依赖的最新版本可以在kie-ci和drools-decisiontables找到。
3. Defining Rules in Excel
3.在Excel中定义规则
For our example, let’s define rules to determine discount based on customer type and the number of years as a customer:
对于我们的例子,让我们定义规则,根据客户类型和作为客户的年限来确定折扣。
- Individual customers with greater than 3 years get 15% discount
- Individual customers with less than 3 years get 5% discount
- All business customers get 20% discount
3.1. The Excel File
3.1.Excel文件
Let’s begin with creating our excel file as per the specific structure and keywords required by Drools:
让我们开始按照Drools所要求的特定结构和关键词来创建我们的excel文件。
For our simple example, we have used the most relevant set of keywords:
对于我们这个简单的例子,我们使用了最相关的一组关键词。
- RuleSet – indicates the beginning of the decision table
- Import – Java classes used in the rules
- RuleTable – indicates the beginning of the set of rules
- Name – Name of the rule
- CONDITION – the code snippet of the condition to be checked against the input data. A rule should contain at least one condition
- ACTION – the code snippet of the action to be taken if the conditions of the rule are met. A rule should contain at least one action. In the example, we are calling setDiscount on the Customer object
In addition, we have used the Customer class in the Excel file. So, let’s create that now.
此外,我们在Excel文件中使用了Customer类。所以,现在让我们来创建这个。
3.2. The Customer Class
3.2.客户类
As can be seen from the CONDITIONs and ACTION in the excel sheet, we are using an object of the Customer class for the input data (type and years) and to store the result (discount).
从excel表中的条件和行动可以看出,我们使用一个Customer类的对象来输入数据(type和years)并存储结果(discount)。
The Customer class:
Customer类。
public class Customer {
private CustomerType type;
private int years;
private int discount;
// Standard getters and setters
public enum CustomerType {
INDIVIDUAL,
BUSINESS;
}
}
4. Creating Drools Rule Engine Instance
4.创建Drools规则引擎实例
Before we can execute the rules that we have defined, we have to work with an instance of Drools rule engine. For that, we have to use Kie core components.
在我们执行我们所定义的规则之前,我们必须与Drools规则引擎的实例一起工作。为此,我们必须使用Kie的核心组件。
4.1. KieServices
4.1.KieServices
The KieServices class provides access to all the Kie build and runtime facilities. It provides several factories, services, and utility methods. So, let’s first get hold of a KieServices instance:
KieServices类提供对所有Kie构建和运行时设施的访问。它提供了几个工厂、服务和实用方法。因此,让我们首先掌握一个KieServices实例:
KieServices kieServices = KieServices.Factory.get();
Using the KieServices, we are going to create new instances of KieFileSystem, KieBuilder, and KieContainer.
使用KieServices,我们将创建KieFileSystem、KieBuilder和KieContainer的新实例。
4.2. KieFileSystem
4.2.KieFileSystem
KieFileSystem is a virtual file system. Let’s add our Excel spreadsheet to it:
KieFileSystem是一个虚拟文件系统。让我们把我们的Excel电子表格添加到其中。
Resource dt
= ResourceFactory
.newClassPathResource("com/baeldung/drools/rules/Discount.xls",
getClass());
KieFileSystem kieFileSystem = kieServices.newKieFileSystem().write(dt);
4.3. KieBuilder
4.3.KieBuilder
Now, build the content of the KieFileSystem by passing it to KieBuilder:
现在,将KieFileSystem的内容传递给KieBuilder,从而构建该内容。
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
If successfully built, it creates a KieModule (any Maven produced jar with a kmodule.xml in it, is a KieModule).
如果成功构建,它会创建一个KieModule(任何Maven生产的jar中含有kmodule.xml,就是一个KieModule)。
4.4. KieRepository
4.4.KieRepository
The framework automatically adds the KieModule (resulting from the build) to KieRepository:
该框架自动将KieModule(由构建产生)添加到KieRepository。
KieRepository kieRepository = kieServices.getRepository();
4.5. KieContainer
4.5.KieContainer
It is now possible to create a new KieContainer with this KieModule using its ReleaseId. In this case, Kie assigns a default ReleaseId:
现在可以用这个KieModule创建一个新的KieContainer,使用其ReleaseId。在这种情况下,Kie分配了一个默认的ReleaseId:。
ReleaseId krDefaultReleaseId = kieRepository.getDefaultReleaseId();
KieContainer kieContainer
= kieServices.newKieContainer(krDefaultReleaseId);
4.6. KieSession
4.6.KieSession
We can now obtain KieSession from the KieContainer. Our application interacts with the KieSession, which stores and executes on the runtime data:
我们现在可以从KieContainer获得KieSession。我们的应用程序与KieSession互动,它存储并执行运行时数据。
KieSession kieSession = kieContainer.newKieSession();
5. Executing the Rules
5.执行规则
Finally, it is time to provide input data and fire the rules:
最后,是提供输入数据和启动规则的时候了。
Customer customer = new Customer(CustomerType.BUSINESS, 2);
kieSession.insert(customer);
kieSession.fireAllRules();
6. Test Cases
6.测试案例
Let’s now add some test cases:
现在让我们添加一些测试案例。
public class DiscountExcelIntegrationTest {
private KieSession kSession;
@Before
public void setup() {
Resource dt
= ResourceFactory
.newClassPathResource("com/baeldung/drools/rules/Discount.xls",
getClass());
kSession = new DroolsBeanFactory().getKieSession(dt);
}
@Test
public void
giveIndvidualLongStanding_whenFireRule_thenCorrectDiscount()
throws Exception {
Customer customer = new Customer(CustomerType.INDIVIDUAL, 5);
kSession.insert(customer);
kSession.fireAllRules();
assertEquals(customer.getDiscount(), 15);
}
@Test
public void
giveIndvidualRecent_whenFireRule_thenCorrectDiscount()
throws Exception {
Customer customer = new Customer(CustomerType.INDIVIDUAL, 1);
kSession.insert(customer);
kSession.fireAllRules();
assertEquals(customer.getDiscount(), 5);
}
@Test
public void
giveBusinessAny_whenFireRule_thenCorrectDiscount()
throws Exception {
Customer customer = new Customer(CustomerType.BUSINESS, 0);
kSession.insert(customer);
kSession.fireAllRules();
assertEquals(customer.getDiscount(), 20);
}
}
7. Troubleshooting
7.故障排除
Drools converts the decision table to DRL. Due to that, dealing with errors and typos in the Excel file can be hard. Often the errors refer to the content of the DRL. So to troubleshoot, it helps to print and analyze the DRL:
Drools将决策表转换为DRL。由于这个原因,处理Excel文件中的错误和错别字会很困难。通常这些错误是指DRL的内容。因此,为了排除故障,有助于打印和分析DRL。
Resource dt
= ResourceFactory
.newClassPathResource("com/baeldung/drools/rules/Discount.xls",
getClass());
DecisionTableProviderImpl decisionTableProvider
= new DecisionTableProviderImpl();
String drl = decisionTableProvider.loadFromResource(dt, null);
8. Conclusion
8.结论
In this article, we have seen a quick example of using Drools to manage business rules in an Excel spreadsheet. We have seen the structure and the minimal set of keywords to be used in defining rules in an Excel file. Next, we have used Kie components to read and fire the rules. Finally, we wrote test cases to verify the results.
在这篇文章中,我们看到了一个使用Drools来管理Excel电子表格中的业务规则的快速例子。我们已经看到了在Excel文件中定义规则的结构和最基本的关键词集。接下来,我们使用Kie组件来读取和启动规则。最后,我们写了测试案例来验证结果。
As always, the example used in this article can be found in the Github project.
一如既往,本文中使用的例子可以在Github项目中找到。