In this tutorial, we will learn how to integrate the Drools rule engine and implement a simple rule engine service using spring boot. We will use the DRL rules by creating the .drl file inside the spring boot application.
1. Tech Stack
We should have the following software to create/import a drools Project.
- Spring boot version: 3.1.1
- Drools
- Java version 1.8
2. Create a spring boot application
The project structure is as follows
Download counts: 57
- After downloading, unzip the project.
- Go to Eclipse IDE -> File -> Import -> Maven -> Existing Maven project -> Select the project.
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.
The below is the complete content of the pom.xml file.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.hi.techpoints</groupId>
<artifactId>drools-with-spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<drools.version>7.49.0.Final</drools.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-mvel</artifactId>
<version>${drools.version}</version>
</dependency>
</dependencies>
</project>
Configure the drools application in SpringBootDroolsApplication class
package com.hi.techpoints;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieModule;
import org.kie.api.runtime.KieContainer;
import org.kie.internal.io.ResourceFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SpringBootDroolsApplication {
private final KieServices kieServices = KieServices.Factory.get();
public static void main(String[] args) {
SpringApplication.run(SpringBootDroolsApplication.class, args);
}
@Bean
public KieContainer kieContainer() {
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
kieFileSystem.write(ResourceFactory.newClassPathResource("rules.drl"));
KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);
kb.buildAll();
KieModule kieModule = kb.getKieModule();
return kieServices.newKieContainer(kieModule.getReleaseId());
}
}
The SpringBootDroolsApplication class defines a spring bean KieContainer. The KieContainer is used to build the rule engine by loading the rule files under the application’s /resources folder.
We create the KieFileSystem instance and configure the rule engine and load the DRL file from the application’s resources directory.
Also, we can use the KieBuilder instance to build the drools module. We can use the KieSerive singleton instance to create the KieBuilder instance.
Finally, we use KieService to create a KieContainer and configure it as a spring bean.
Model class Product which defines the type of mobile model item.
package com.hi.techpoints.model;
public class Product {
private String model;
private int discount;
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
}
We will use the type property of the Product class for defining the rules for defining what action needs to be taken if a particular condition is met. The .drl file should be place in resources folder.
import com.hi.techpoints.model.Product
rule "Offer for Iphone"
when
productObject: Product(model=="Iphone")
then
productObject.setDiscount(20);
end
rule "Offer for Pixel"
when
productObject: Product(model=="Pixel")
then
productObject.setDiscount(30);
end
Service class for fire the rules
package com.hi.techpoints.service;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hi.techpoints.model.Product;
@Service
public class MobileShopService {
@Autowired
private KieContainer kieContainer;
public Product getProductDiscount(Product product) {
KieSession kieSession = kieCntainer.newKieSession();
kieSession.insert(product);
kieSession.fireAllRules();
kieSession.dispose();
return product;
}
}
Rest controller for call service method
package com.hi.techpoints.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.hi.techpoints.model.Product;
import com.hi.techpoints.service.MobileShopService;
@RestController
public class MobileShopController {
@Autowired
private MobileShopService mobileShopService;
@RequestMapping(value = "/getDiscount", method = RequestMethod.GET, produces = "application/json")
public Product getQuestions(@RequestParam(required = true) String model) {
Product product = new Product();
product.setModel(model);
mobileShopService.getProductDiscount(product);
return product;
}
}
Output
Based on ‘model’ query param, it will return discount.