Spring Boot with Drools Engine

In this blog we would see how Spring boot easily integrates with Drools Rule Engine.

Praveen G. Nair
CodeX
5 min readApr 10, 2022

--

Spring Boot and Drools Rule Engine Integration

Rule engines are most commonly used integration pattern when we have to implement most complex business rules.

The Drools is an open-source Business Rule Management System (BRMS) that can be integrated easily with many applications. It is written in 100% pure Java, runs on any JVM and is available in the Maven Central repository too.

We would today explore and implement an integration with Spring boot.

KIE (Knowledge Is Everything) is an umbrella project introduced to bring the related technologies together under one roof. Hence KIE is the main core component which integrates with Spring boot.

In this blog, we will see how to integrate the Drools rule engine and implement a demo rule engine service using spring boot.

We would also use the DRL (Drools Rule Language) rules by creating the .drl file inside the spring boot application.

1. Add maven dependency

Let’s create a basic spring boot application and add below drools dependency to pom.xml.

2. Configure the Drools Application

Create a configuration java class with the name DroolsConfig.java and add the below configuration to the java class.

The config class creates a spring bean KieContainer to build the rule engine by loading the rule files under the application’s /resources folder. We create the KieFileSystem instance and load the DRL file from the application’s resources directory.

Finally, we use KieService and KieBuilder to create a KieContainer and configure it as a spring bean.

3. Create the model classes

Create Pojo class with the name CustomerRequest and define the below fields.

We receive this class as a request object to the rule engine and also we send these fields as the input to defined rules to derive the customerType for the given customer request.

Also define a java enum with the name CustomerCategory.java as shown below. The enum holds the customer categories and based on the value, the rule engine derives the customer type.

Finally, create a response POJO class with the name CustomerType as shown below.

4. Define Drools Rules

Create a drools rules file with the name customer-category.drl and place the file under the directory /src/main/resources/rules.

We need to import the models that are being used in the DRL file. We are also using a global parameter with the name customerType. The global parameter can be shared between multiple rules.

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.

Then we define when-then syntax to specify the conditions for a rule. Based on the input values of the Customer request, we are deriving the customerType to the result.

5. Add the controller and Service layer

Create a service java class with the name CustomerCategorizeService, and add the below content.

Now we are injecting the KieContainer instance and creating a KieSession instance. We are also setting a global parameter of type CustomerType, that will hold the rules execution result.

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.

expose a POST API with the endpoint /api/getCustomerType. The endpoint expects an CustomerRequest object and returns the CustomerType response. controller content looks like below :

6. Add Swagger Config (Optional)

Add swagger config to the application to get a swagger page.

Execute some tests using below curl or run via swagger.

  1. if numberOfOrders == 0 then customerType = SUSPENDED

2. if age > 0 and numberOfOrders ! = 0 then customerType = KIDS

3. if age>50 and numberOfOrder!=0 then customerType = SENIOR_CITIZEN

Summary

In this blog, we have learned how to create the drools rule engine using the spring boot framework. We have also seen how we can use the DRL files to define the business rules.

Hope you have enjoyed the learning. I have pushed the entire code into github.
If you don’t want to miss similar contents follow and subscribe to my medium handle. Happy coding !!

--

--

Praveen G. Nair
CodeX

I am a Software Developer and a Technologist. Interested in all cool stuffs of software development, Machine Learning and Cloud. https://praveeng-nair.web.app/