Drools

Drools – Stateful Knowledge Session using KieSession

In previous chapter we implemented a simple drools project to execute simple rule. From Drools 6.0 onwards a new approach is used to create a Knowledge Base and a Knowledge Session. Knowledge base is an interface that manages a set of rules and processes. The main task of the knowledge base is to store and re-use rules because creation of rules is very expensive.Rules are contained inside the package org.drools.KnowledgeBase. These are commonly referred to as knowledge definitions or knowledge. Knowledge base provides methods for creating a Session.
The knowledge session can be of two types:

  1. Stateless Knowledge Session
  2. Stateful Knowledge Session

In this chapter we make use of KieSession to get stateful session. Stateful sessions are longer lived and allow iterative changes over time.

Create Example

Using drools we have to define the discounts offered on mobile products depending on the model. For example if the product is ‘Google Pixel’ model offer a discount of 30% and so on. We will create Eclipse Maven project as follows-

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

The POM defined is 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.hi.techpoints</groupId>
	<artifactId>drools-kie-session</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.compiler.source>1.8</maven.compiler.source>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.kie</groupId>
			<artifactId>kie-api</artifactId>
			<version>7.73.0.Final</version>
		</dependency>

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

		<dependency>
			<groupId>org.kie</groupId>
			<artifactId>kie-ci</artifactId>
			<version>7.73.0.Final</version>
		</dependency>

	</dependencies>

</project>

Next define the Model class Product which defines the type of mobile model item.

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

Now define the rules in the drl file. We will use the type property of the Product class for defining the rules for defining what action needs to be taken if a particular condition is met. The .drl file should be place in resources/com.rule folder.

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

The Drools 7.0 project consists of a meta data file META-INF/kmodule.xml. The file is located under the source folder as shown in below snapshot. A named session ksession-rule is created. The ksession rule is applicable for all drl file contained in the pacakge rules. Currently we have only one drl file of package rules.

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
   <kbase name="rules" packages="rules">
        <ksession name="ksession-rule"/>
    </kbase>
</kmodule>

Finally we define RunDrools class. Here load the facts and the rules in the drools working memory and firing all the rules.

package com.hi.techpoints;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

import com.hi.techpoints.model.Product;

public class RunDrools {

	public static final void main(String[] args) {
		try {
			KieServices ks = KieServices.Factory.get();
			KieContainer kContainer = ks.getKieClasspathContainer();
			KieSession kSession = kContainer.newKieSession("ksession-rule");

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

		    kSession.insert(product);
			kSession.fireAllRules();
			
			System.out.println("The discount for the mobile product " + product.getModel()
			+ " is " + product.getDiscount());

		} catch (Throwable t) {
			t.printStackTrace();
		}
	}

}

Output:

About the Author: Elavarasan PK

Technical Specialist, Intersoft Data Labs