Search Tutorials

Drools Hello World Example | JavaInUse

JBoss Drools Hello World


Consider a Jewellery shop which needs to maintain business logic of calculating discount based on the type of Jewellery. This can be done in our Java Code. But such rules may change daily and need to be updated regularly in our code which is not good. Also we will always require a Developer for making such changes. So its a good practice to define this logic as rules in a Business Rule Management System. If tomorrow the discount is to be changed this can be done even by a non technical person.

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


This tutorial is explained in the below Youtube Video.

Lets Begin

Using drools we have to define the discounts offered on various jewellery products depending on the type. For example if the product is jewellery item offer a discount of 25% and so on. We will create Eclipse Maven project as follows-
The POM defined is as follows- Only a single dependency og Drools-compiler is required.
<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
Next define the Model class Product which defines the type of jewellery item.
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) { = 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.
package com.rule

import com.javainuse.model.Product

rule "Offer for Diamond"
		productObject: Product(type=="diamond")
rule "Offer for Gold"
		productObject: Product(type=="gold")

Drools employs a concept called Working Memory. Finally we define DroolsTest class to load the facts and the rules in the drools working memory and firing all the rules.
package com.javainuse.main;


import org.drools.compiler.compiler.DroolsParserException;
import org.drools.compiler.compiler.PackageBuilder;
import org.drools.core.RuleBase;
import org.drools.core.RuleBaseFactory;
import org.drools.core.WorkingMemory;

import com.javainuse.model.Product;

public class DroolsTest {

	public static void main(String[] args) throws DroolsParserException,
			IOException {
		DroolsTest droolsTest = new DroolsTest();

	public void executeDrools() throws DroolsParserException, IOException {

		PackageBuilder packageBuilder = new PackageBuilder();

		String ruleFile = "/com/rule/Rules.drl";
		InputStream resourceAsStream = getClass().getResourceAsStream(ruleFile);

		Reader reader = new InputStreamReader(resourceAsStream);
		org.drools.core.rule.Package rulesPackage = packageBuilder.getPackage();
		RuleBase ruleBase = RuleBaseFactory.newRuleBase();

		WorkingMemory workingMemory = ruleBase.newStatefulSession();

		Product product = new Product();


		System.out.println("The discount for the product " + product.getType()
				+ " is " + product.getDiscount());

On running get the output as-

Download Source Code

Download it - Drools Hello World