What is Drools?
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:
- Authoring – It involves the development of rules files.
- Runtime – It involves the creation of working memory and handling the activation.
What is Rule?
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.
<Condition is true>
<Take desired Action>
Features of Drool
Here are important features of Drool:
- Helps you to separate application from dynamic logic
- Declarative Programming
- Centralization of Knowledge
- Speed and Scalability
- Separate logic from application
- Understandable rules
Drools Rule Engine
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.
Why use Drools Rule Engine?
Here, are prime reasons for using Drools rules engine:
- Rules are easy to understand for developers and business analysts.
- Rules are easily maintainable.
- Rule Engine uses a Rete algorithm which states that the performance of the engine never depends on the number of rules.
- Rules can be modified and deployed without bringing down the application.
- Externalizes the business logic from the comparatively static codebase.
- Rules are developed in less complicated formats so the business analyst can easily read and verify a group of rules.
- Rules allow you to create a warehouse of knowledge which is executable in form.
Disadvantages of Rules Engine
Here, are drawbacks/ cons of using rules engine:
- Lots of learning effort requires by developers to know this method of programming
- Rule engine is not a secure method to troubleshoot issues.
- Needs to understand the working of rule engine to consumes more memory
- There are a wide set of rules for a complex branching.
Spring Boot Drools Rule Engine Example
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
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.
The below is the required dependencies of the pom.xml file.
Configure the drools application
Create a configuration java class with the name DroolsConfig under config package.
Add the below configuration to the java class.
Add the model class
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.
Define the drools rules
Create the drools rules inside a file with the name customer-discount.drl. Add the file under the directory /src/main/resources/rules.
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.
Add the service layer
Create a service java class with the name OrderService , and add the below content.
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.
Add the REST API
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.
Testing the application
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.