Spring Boot + JBoss Drools Decision Table Example | CodeUsingJava








Spring Boot + JBoss Drools Decision Table Example


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;
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.xls file. The 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 xls 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("depositInterestRate.xls"));
		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