JBoss Drools Hello World

JBoss Drools Hello World

Jboss Drools: It’s a Business Rules Management System (BRMS) which provide solution to core Business Business Rules Engine (BRE) rules management application. Generally to develop application we do write logic inside the code using loop and if else statement. This way there is no centralize point to check or change business logic because logic could be scattered in many classes objects and chances are same business logic duplicated in many classes. Drools provide solution to this problem where all business logic can be maintain on one location and can be change any time without affecting application code. To start learning Drools its required to first set up and run simple application which prints some message. Here we will set up simple Hello World application and print message on console.

  • Create maven project DroolsHelloWorld. Below is final project structure:

JBoss Drools Hello World

  • To run Drools project you will have to add drools-compiler run-time dependency in pom.xml as below:
<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javahonk</groupId>
	<artifactId>DroolsHelloWorld</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>DroolsHelloWorld Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-compiler</artifactId>
			<version>6.0.1.Final</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>DroolsHelloWorld</finalName>
	</build>
</project>
  • Because it’s first application so its important to see how we do create Drools file. Install Drools eclipse plugin using this tutorial. To create drools file right click folder (“\src\main\resources\com\javahonk”) –> New –> Other

JBoss Drools Hello WorldJBoss Drools Hello WorldJBoss Drools Hello World

  • Sample.drl file will be created with default template. Copy paste below content in it:
//created on: Feb 28, 2015
package com.javahonk

//list any import classes here.

//declare any global variables here

rule "Hello World"
	when
		helloWorldReferenceObject: HelloWorld( printMessage =="Java Honk Drools Hello World!" )
	then
		helloWorldReferenceObject.showMessage();
	end
  
  • Create HelloWorld.java in com.javahonk package:
package com.javahonk;

public class HelloWorld {

	private String printMessage;

	public String getPrintMessage() {
		return printMessage;
	}

	public void setPrintMessage(String printMessage) {
		this.printMessage = printMessage;
	}

	public void showMessage() {
		System.out.println(printMessage);
	}

}
  • Create test class HelloWorldTest.java inside com.javahonk package to load and print message on console:
package com.javahonk;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

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;

public class HelloWorldTest {

	public static void main(String[] args) throws DroolsParserException, IOException {
		
		HelloWorldTest helloWorldTest = new HelloWorldTest();
		helloWorldTest.executeHelloWorldRules();

	}

	public void executeHelloWorldRules() throws IOException, DroolsParserException {

		PackageBuilder packageBuilder = new PackageBuilder();
		
		String ruleFile = "/com/javahonk/Sample.drl";
		//Convert rule file to InputStream
		InputStream resourceAsStream = getClass().getResourceAsStream(ruleFile);
		
		Reader reader = new InputStreamReader(resourceAsStream);
		packageBuilder.addPackageFromDrl(reader);
		org.drools.core.rule.Package rulesPackage = packageBuilder.getPackage();		
		RuleBase ruleBase = RuleBaseFactory.newRuleBase();
		ruleBase.addPackage(rulesPackage);
		
		//Create new WorkingMemory session for this RuleBase. By default the RuleBase retains a weak reference to returned WorkingMemory
		WorkingMemory workingMemory = ruleBase.newStatefulSession();

		HelloWorld helloWorld = new HelloWorld();
		helloWorld.setPrintMessage("Java Honk Drools Hello World!");
		
		//Insert and fire all rules until its empty
		workingMemory.insert(helloWorld);
		workingMemory.fireAllRules();

	}

}
  • To run right click HelloWorldTest.java –> Run As –> Java Application you will see below hello world message on console:

JBoss Drools Hello World

  • For more details visit Drools official site here  

download  Download project:  DroolsHelloWorld

2 thoughts on “JBoss Drools Hello World”
  1. Please, I cannot to do this example because I got the following error:

    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 org.drools.core.RuleBase cannot be resolved…
    import org.drools.compiler.PackageBuilder cannot be resolved…

    Could you help me?

Leave a Reply

Your email address will not be published. Required fields are marked *