Take Control of Your Finances...
October 24, 2024
Drools is an open-source Business Rules Management Software (BRMS) written in Java that provides users with a variety of features like Business Rule Engine, Web authoring, Rules Management Application, and runtime support for Decision Model and Notation models.
It provides a rule engine which processes facts and produces output as a result of rules and facts processing. Centralization of business logic makes it possible to introduce changes fast and cheap.
Drools was released under Apache License 2.0 and it is compatible with any JVM and available in Maven Central Repository also.
It also bridges the gap between the Business and Technical teams by providing a facility for writing the rules in a format which is easy to understand.
The Drools tool helps you to separate and reason over logic and data found in business processes.
Drools are split into two parts:
Rules are parts of knowledge often expressed as, “When specific conditions occur, then do some tasks.”
The most crucial part of a rule is it’s when part. Once when part is satisfied, then the part is triggered.
Syntax:
When
<Condition is true>
Then
<Take desired Action>
Here are important features of Drool:
Drools Rule Engine is a rule-based approach to implement an Expert system in the Drools software. The Rule engine provides Expert systems which are knowledge-based systems that help you to make decisions like what to do and how to do it. It gathers knowledge into a knowledge base that can be used for reasoning.
Here, are prime reasons for using Drools rules engine:
Here, are drawbacks/ cons of using rules engine:
The Drools is an open-source business rule management system that can be integrated easily with the spring boot application.
The KIE project supports the integration of the drools with other technologies like the spring boot framework.
Create a spring boot application with the required dependencies. Add the spring boot starter web dependency. Also, include drools dependencies to the spring boot application’s pom XML configuration file.
Add drools version in properties tag as mentioned below.
<properties>
<java.version>17</java.version>
<drools.version>7.59.0.Final</drools.version>
</properties>
The below is the required dependencies of the pom.xml file.
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>${drools.version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>${drools.version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-decisiontables</artifactId>
<version>${drools.version}</version>
</dependency>
Configure the drools application
Create a configuration java class with the name DroolsConfig under config package.
Add the below configuration to the java class.
DroolsConfig.java
Create a model class with the name Order and define the below fields.
We receive this class as a request object to the rule engine and, we send these fields as the input to defined rules to set appropriate discount amount for the given customer order.
Order.java
Create the drools rules inside a file with the name customer-discount.drl. Add the file under the directory /src/main/resources/rules.
Customer-discount.drl
We need to import the models that are being used in the DRL file.
The DRL file can contain one or multiple rules. We can use the mvel syntax to specify the rules. Also, each rule can be described with a description using the rule keyword.
We can use when-then syntax to define the conditions for a rule.
Based on the input values of the Order request, we are adding discount to the result. If none of the defined rule is applied, then the default value will be returned as a discount.
Create a service java class with the name OrderService , and add the below content.
OrderService.java
We are injecting the KieContainer instance and creating a KieSession instance.
To pass the request object to the DRL file, we can use the insert() method.
Then we fire all the rules by calling the fireAllRules() method. and finally terminate the session by calling the dispose() method of the KieSession.
Create a REST API class with the name OrderController and add the below content.
The controller exposes a POST API with the endpoint /get-discount. The endpoint expects an Order object as request and returns the same Order object with calculated discount amount in response.
Run the spring boot application and access the REST API endpoint by sending the customer order request JSON.
For the VISA card type with price > 5000, we should get discount as 14 according to our defined rules.
Now, try with the different values as shown below.
For the VISA card type with price > 10000, we should get discount as 10 according to our defined rules.
In this article, we learned about drools rule system and how to create the drools rule engine using the spring boot framework.
We also learned how we can use the DRL files to define the business rules.
You can find project link here.