Spring Boot Drools Decision Table Example

Thameem Ansari
5 min readOct 31, 2021

--

Spring Boot Decision Table Example

Introduction

The drools decision table is another way of defining the user-friendly rules, and we can implement the drools decision table using the excel files with the spring boot framework.

Why Decision Table

We had seen in the previous chapter drools logic was implemented in .drl file. But if we look at the .drl file, any modification will require technical knowledge to build and run the application. As the .drl becomes more complicated it will become more difficult for non-programmers to do the modifications, also frequent changes to the drools file will be cumbersome. Hence this format, decision tables, is a very good option to use where something is going to be changed frequently by non-programmers.

Decision Table Structure

There are different components of a Decision table as mentioned below. The structure is similar to DRL file
1) Ruleset — Its similar to package from DRL file. This part contains the import statements, global variables, etc.
2) Import — To import comma separated Java packages that are used in rules.
3) Variables — One of more Global variables.

Rules configuration consists of below :
1) Rule Name — To specify the rule name
2) CONDITION — To specify the condition to execute for each rule.
3) ACTION — The action we are applying if the condition evaluation passes.

Technologies used

  • Spring boot version: 2.5.5
  • Drools
  • Java version 1.8
  • excel

Create a spring boot application

Create a spring boot application with the required dependencies.

Add the spring boot web starter dependency and the drools dependencies to the application’s pom XML configuration file.

pom.xml

<?xml version="1.0" encoding="UTF-8"?><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 http://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>2.4.3</version>
</parent>
<groupId>com.example.drools.decision.demo</groupId>
<artifactId>spring-boot-drools-decision-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-drools-decision-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<drools.version>7.59.0.Final</drools.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<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>
</dependencies><build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

Creating the Excel drools decision table

Instead of defining the drools rules using a DRL file, we can use the user-friendly excel file.

Create an excel file with the name customer-rules.xlsx and add the content as shown in the below image.

Add the spring boot drools configuration

Create a spring configuration class with the name DroolsConfig. The class configures the drools engine by loading the rules in the excel file.

@Configuration
public class DroolsConfig {
private static final String RULES_ORDER_DISCOUNT_XLS = "rules/customer-rules.xlsx";
private static final KieServices kieServices = KieServices.Factory.get();
@Bean
public KieContainer kieContainer() {
Resource dt = ResourceFactory.newClassPathResource(RULES_ORDER_DISCOUNT_XLS, getClass());
KieFileSystem kieFileSystem = kieServices.newKieFileSystem().write(dt);
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
KieModule kieModule = kieBuilder.getKieModule();
KieContainer kieContainer = kieServices.newKieContainer(kieModule.getReleaseId());
return kieContainer;
}
}

Here, we pass the file customer-rules.xlsx under the src/main/resources/rules directory to the KieFileSystem.

We are using the ResourceFactory class’s newClassPathResource() method to load the excel file to the drools rule engine.

Also, we are passing this resource to the KieFileSystem and then creating a KieModule using the KieBuilder.

Finally, we configure the KieContainer as the spring bean that we will use inside the service layer of the spring boot application.

Add the model classes

Create a java POJO class with the name OrderRequest.

We will receive the order request to the REST API, and we map the values to an instance of this class. Then we will pass this object to the rule for calculating the order discount.

@Getter
@Setter
public class OrderRequest {

private String customerNumber;
private Integer age;
private Integer amount;
private CustomerType customerType;
}

Create an enum with the name EmployeeType. We use this enum to define the supported employee type to decide the order discount by the drools rules.

public enum CustomerType {
LOYAL, NEW, DISSATISFIED;

public String getValue() {
return this.toString();
}
}

Create a java POJO class with the name OrderDiscount.

We use this class as a response object type. The REST API returns the response of this type to the API caller.

@Getter
@Setter
public class OrderDiscount {

private Integer discount = 0;
}

Add the service layer

Create a spring service class with the name OrderDiscountService.

This service passes the incoming order request object to the drools rules and returns the received response to the controller layer.

@Service
public class OrderDiscountService {

@Autowired
private KieContainer kieContainer;

public OrderDiscount getDiscount(OrderRequest orderRequest) {
OrderDiscount orderDiscount = new OrderDiscount();
KieSession kieSession = kieContainer.newKieSession();
kieSession.setGlobal("orderDiscount", orderDiscount);
kieSession.insert(orderRequest);
kieSession.fireAllRules();
kieSession.dispose();
return orderDiscount;
}
}

We are using the KieContainer bean instance to open a KieSession and execute the drools rules.

We are setting the global variable with the name orderDiscount, of type OrderDiscount class. This POJO class will hold the order discount value, and we also share the variable among multiple drool rules.

Add the REST endpoint

Create a spring REST API endpoint with the URL /get-discount.

We receive the OrderRequest object as the request and return back the OrderDiscount object as the response.

@RestController
public class OrderDiscountController {

@Autowired
private OrderDiscountService orderDiscountService;

@PostMapping("/get-discount")
public ResponseEntity<OrderDiscount> getDiscount(@RequestBody OrderRequest orderRequest) {
OrderDiscount discount = orderDiscountService.getDiscount(orderRequest);
return new ResponseEntity<>(discount, HttpStatus.OK);
}
}

Test the application

Start the spring boot application.

Invoke the http://localhost:9999/get-discount API and pass the order details as shown below.

We receive the response discount value as 20, as the age of the customer is < 20, amount > 10000, and the customer type is LOYAL.

We can also observe the applied discount in the console log as shown below.

Finally, change the request JSON field value to check for the different scenarios as shown below

Conclusion

In this article, we learned how to implement excel based drools rule table with the spring boot framework.

The example code is available on Github.

Happy coding..

--

--

Thameem Ansari

Technology Expert| Coder| Sharing Experience| Spring | Java | Docker | K8s| DevOps| https://reachansari.com