Search Tutorials


Drools Spring Integration Example | JavaInUse

Drools Tutorials- Integration with Spring

Overview

In this tutorial we will create a Spring MVC Application and integrate with JBoss Drools. Like previous drools examples we will create an application for a jewellery shop to calculate discount based on the type of jewellery. In another tutorial, we have integrated Spring Boot with Drools

JBoss Drools - Table of Contents

JBoss Drools Hello World JBoss Drools Hello World-Stateful Knowledge Session using KieSession JBoss Drools- Understanding Drools Decision Table using Simple Example Understand Drools Stateful vs Stateless Knowledge Session Drools Tutorials- Backward Chaining simple example Drools Tutorials- Understanding attributes salience, update statement and no-loop using Simple Example Drools Tutorials- Understanding Execution Control in Drools using Simple Example Drools Tutorials- Integration with Spring MVC Drools Tutorials- Integration with Spring Boot

Video

This tutorial is explained in the below Youtube Video.

Lets Begin

The project structure is as follows-
drools8_1
The pom.xml will be as follows-
<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>
  <groupId>com.javainuse</groupId>
  <artifactId>drools-spring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
   <properties>  
  <spring.version>3.1.1.RELEASE</spring.version>
 </properties>
 
 <dependencies>
 
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version></version>
  </dependency>
  
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version></version>
  </dependency>
  
  <dependency>
   <groupId>org.drools</groupId>
   <artifactId>drools-spring</artifactId>
   <version>5.4.0.Final</version>
  </dependency>
  
 </dependencies>
</project>
	
	

Define the model class Product.java as follows.
package com.javainuse.model;

public class Product {

	private String type;
	private int discount;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getDiscount() {
		return discount;
	}

	public void setDiscount(int discount) {
		this.discount = discount;
	}

}

Define the rules.drl as follows-
package rules
 
import com.javainuse.model.Product
rule "Offer for Diamond"
	when 
		productObject: Product(type=="diamond")
	then
		productObject.setDiscount(15);
	end
rule "Offer for Gold"
	when 
		productObject: Product(type=="gold")
	then
		productObject.setDiscount(25);
	end
	




We define the stateless session for the rules.drl.
<?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:drools="http://drools.org/schema/drools-spring"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://drools.org/schema/drools-spring http://drools.org/schema/drools-spring.xsd">

	<drools:kbase id="productBase">
		<drools:resources>
			<drools:resource type="DRL" source="classpath:rules/rules.drl" />
		</drools:resources>
	</drools:kbase>

	<drools:ksession id="productSession" name="productSession"
		type="stateless" kbase="productBase" />
</beans>
	

Define the ProductServiceImpl as follows to get the Stateless session and execute the drools rules.
package com.javainuse.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.javainuse.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.javainuse" />
</beans>
	

Finally create the product object and call the ProductServiceImpl CalculateDiscount method to fire the rules for discount.
package com.drools.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.javainuse.model.Product;
import com.javainuse.service.ProductServiceImpl;

public class DroolsSpringTest {

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

		Product product = new Product();
		product.setType("gold");

		bean.CalculateDiscount(product);

		showDiscount(product);
	}

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


drools8_2

Download Source Code

Download it - Drools Spring Integration example

JBoss Drools Hello World
JBoss Drools Hello World-Stateful Knowledge Session using KieSession
JBoss Drools- Understanding Drools Decision Table using Simple Example
Understand Drools Stateful vs Stateless Knowledge Session
Drools Tutorials- Backward Chaining simple example
Drools Tutorials- Understanding attributes salience, update statement and no-loop using Simple Example  
Drools Tutorials- Understanding Execution Control in Drools using Simple Example
Drools Interview Questions
Drools-Main Menu.