Drools

Drools – Spring Integration

In this tutorial, we will create a Spring application and integrate it with JBoss Drools. As with the previous Drools examples, we will create an application for a mobile shop to calculate discounts based on the type of mobile model.

1. Tech Stack

We should have the following software to create/import a drools Project.

  1. Java 17 or higher version
  2. Eclipse IDE for Enterprise Java Developers

2. Create Example

The project structure is as follows

Download Spring with drools
Download counts: 2809
  1. After downloading, unzip the project.
  2. Go to Eclipse IDE -> File -> Import -> Maven -> Existing Maven project -> Select the project.

Define the model class Product.java as follows.

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;
	}
}

The pom.xml will be as follows. Added spring and drools related dependencies

<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>
	<groupId>com.hi.techpoints</groupId>
	<artifactId>drools-with-spring</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<java.version>17</java.version>
		<maven.compiler.target>17</maven.compiler.target>
		<maven.compiler.source>17</maven.compiler.source>
		<spring.version>6.0.8</spring.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-spring</artifactId>
			<version>5.4.0.Final</version>
		</dependency>

	</dependencies>

</project>

Define the rules.drl as follows

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

We define the stateless session for the rules.drl. Define the ProductServiceImpl as follows to get the Stateless session and execute the drools rules.

package com.hi.techpoints.service;

import org.drools.runtime.StatelessKnowledgeSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import com.hi.techpoints.model.Product;


@Component("ProductServiceImpl")
public class ProductServiceImpl {

	@Autowired
	private ApplicationContext applicationContext;

	public void calculateDiscount(Product product) {

		StatelessKnowledgeSession statelessKnowledgeSession = (StatelessKnowledgeSession) applicationContext
				.getBean("productSession");
		statelessKnowledgeSession.execute(product);
	}
}

Define the applicationContext for loading the spring beans.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
    <import resource="classpath:drools-context.xml"/>
    <context:component-scan base-package="com.hi.techpoints" />
    
</beans>

Finally create the product object and call the ProductServiceImpl CalculateDiscount method to fire the rules for discount.

package com.hi.techpoints;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hi.techpoints.model.Product;
import com.hi.techpoints.service.ProductServiceImpl;

public class RunDrools {

	public static void main(String args[]) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		ProductServiceImpl bean = ((ProductServiceImpl) applicationContext
				.getBean("ProductServiceImpl"));

		Product product = new Product();
		product.setModel("Pixel");

		bean.calculateDiscount(product);

		showDiscount(product);
	}

	private static void showDiscount(Product product) {
		System.out.println("The discount for the product (" + product.getModel() + ") is " + product.getDiscount());
	}

}

The output as

About the Author: Elavarasan PK

Technical Specialist, Intersoft Data Labs