logo

Get in touch

Awesome Image Awesome Image

Java Spring Boot March 20, 2023

Drools Rule Management System with Spring Boot

Written by Mahipalsinh Rana

38,179

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. 

Syntax: 

When
<Condition is true>
Then
<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.

<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 

drool configuration

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.

Order.java 

Ordered Java

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.

Customer-discount.drl

Customer discount

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.

OrderService.java 

Ordered Service

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. 

Order Controller

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.

Database

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. 

DataBase 1

Conclusion

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.

With over 20 years of industry experience, our CTO is a distinguished expert in Liferay Enterprise DXP, AI/ML, and a broad array of cutting-edge technologies including Enterprise Search, ElasticSearch, Java, Spring Boot, .NET, Microservices, Python (Django, FastAPI, Flask), ReactJs, and NodeJs. As a seasoned Technical Architect, he has a proven track record of leading complex, large-scale projects across diverse industries. In addition to his technical expertise, he is an International Trainer and an accomplished author, sharing his deep knowledge with professionals worldwide. Recently, he reviewed the highly-regarded Elasticsearch 8.x Cookbook - Fifth Edition published by Packt, contributing his insights to this essential resource for developers and architects. His leadership, combined with a passion for innovation, ensures that our technology solutions are always at the forefront of the industry.

Bringing Software Development Expertise to Every
Corner of the World

United States

India

Germany

United Kingdom

Canada

Singapore

Australia

New Zealand

Dubai

Qatar

Kuwait

Finland

Brazil

Netherlands

Ireland

Japan

Kenya

South Africa