Spring Boot + JBoss Drools Decision Table Example (2021) | CodeUsingJava








Spring Boot + JBoss Drools Decision Table Example (2021)


In previous tutorial we have implemented Spring Boot + JBoss Drools Hello World Example. We had created a drools rules file to specify all the rules regarding the interest rate to be calculated on deposit. The drools rules file was as follows-
import  com.codeusingjava.domain.DepositRequest;

rule "Interest Rate for Fixed deposit duration between 1 year and 2 years and age less than 60 years"
    when
        depositObject : DepositRequest( loanType=="Fixed" && durationInYear >= 1 && durationInYear < 2 && age < 60);
    then
        depositObject.setInterestRate("5%");
end;

rule "Interest Rate for Recurring deposit duration between 1 year and 2 years and age less than 60 years"
    when
        depositObject : DepositRequest( loanType=="Recurring" && durationInYear >= 1 && durationInYear < 2 && age < 60);
    then
        depositObject.setInterestRate("3%");
end;

Video Tutorial

As we had stated earlier the major advantage of using drools is that it separates code and the rules logic. So it is much easier for business analyst to understand the rules. But the drools file though comparatively easier to understand than java, still contains mvel syntax.
A much easier way to represent the drools rules is by using the decision table. The decision table is a table in excel sheet where we specify the drools rules in a particular format.
We will be modifying the Spring Boot + JBoss Drools Hello World Example we had created previously.
The maven project is as follows-
Spring Boot + Drools Decision Table Maven
In the pom.xml add the drools decision table dependency
<?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 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>2.4.2</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.codeusingjava</groupId>
	<artifactId>boot-drools</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>boot-drools</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
		<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-core</artifactId>
			<version></version>
		</dependency>
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-mvel</artifactId>
			<version></version>
		</dependency>
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-decisiontables</artifactId>
			<version></version>
		</dependency>

		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-compiler</artifactId>
			<version></version>
		</dependency>



		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>	
	
In the resources folder create the depositRules.xlsx file. We will be making use of the JBoss Drools Decision Table Documentation for specifying the rules using decision table.
  • The spreadsheet file consists of 2 areas - Rule Set Area and Rule Table Area
    Drools Decision Table Hello World Example
    1. Rule Set Area - defines all the DRL items except the rules.
    2. Rule Table Area - is the area where we define the actual decision table.
  • We will first be defining the Rule Set area. This will consist of
    1. RuleSet - Package name for the generated DRL file. This is optional and default value is rule_table
    2. Import - This imports the required Java classes. We need to import the DepositRequest class.
    3. Notes - Just a short description of the decision table.
    So the Rule Set area will be as follows -
    Rule Set area
  • Next we will be defining the Rule Table area. This will consist of
    1. RuleTable - Rule Table Name.
    2. NAME - Name of the rule.
    3. CONDITION - The rule condition.
    4. ACTION - Depending on the condition, the rule will get executed.
    So the Rule Table area will be as follows -
    Rule Table area
The final rule file is as follows. I have shared it in the downloads section below -
Spring Boot + Drools Decision Table Hello World Example
So in the above drools table we specify the same rules we had earlier specified in the drl file.
Next in the Drools configuration file we will be loading the rules from the xlsx file instead of the drl file.
package com.codeusingjava.bootdrools.configuration;

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DroolsConfiguration {

	private final KieServices kieServices = KieServices.Factory.get();

	@Bean
	public KieContainer kieContainer() {
		KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
		kieFileSystem.write(ResourceFactory.newClassPathResource("rules.xlsx"));
		KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);
		kb.buildAll();
		KieModule kieModule = kb.getKieModule();
		return kieServices.newKieContainer(kieModule.getReleaseId());
	}
}

Start the application. If we now hit the url - http://localhost:8080/getInterestRate?loanType=Fixed&age=55&durationInYear=1
Spring Boot + Drools Output

Download the code-

Spring Boot + Drools Decision Table Example