Load Call Multiple Drools File

In previous example you saw how to install Drools plugin in eclipse and write simple hello world application. To move one more step up today you will see how to load multiple Drools rules file, call method of java class to print pizza price.

  • Sample maven project final structure:

Load Call Multiple Drools File

  • pom.xml:
<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>
  • Sample.drl:
//created on: Feb 28, 2015
package com.javahonk.drools

//list any import classes here.

//declare any global variables here

rule "Hello World"
	when
		sampleReferenceObject: Sample( printMessage =="Pizza Price" )
	then
		sampleReferenceObject.showMessage();		
	end
  • printPizzaPrice.drl:
//created on: Feb 28, 2015
package com.javahonk.drools

//list any import classes here.


//declare any global variables here

rule "Print pizza price"

   when
		sampleReferenceObject: Sample( price > 10 )
	then
		PrintPizzaPrice printPizzaPrice = new PrintPizzaPrice();
		printPizzaPrice.printPizzaPrice(sampleReferenceObject);		
	end

  • Sample.java:
package com.javahonk.drools;

public class Sample {

	private String printMessage;
	private Integer price;

	public String getPrintMessage() {
		return printMessage;
	}

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

	public Integer getPrice() {
		return price;
	}

	public void setPrice(Integer price) {
		this.price = price;
	}

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

}
  • printPizzaPrice.java:
package com.javahonk.drools;

public class PrintPizzaPrice {

	public void printPizzaPrice(Sample helloWorld) {

		System.out.println("Cheez Pizza and Price: $" + helloWorld.getPrice());

	}

}
  • Test class to load multiple Drools file, execute rules 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;

import com.javahonk.drools.Sample;

public class MultipleDroolsFile {

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

	}

	public void executeHelloWorldRules() throws IOException, DroolsParserException {

		PackageBuilder packageBuilder = new PackageBuilder();
		
		String[] ruleFile = { "/com/javahonk/drools/Sample.drl",
				"/com/javahonk/drools/printPizzaPrice.drl" };
		for (String string : ruleFile) {
			// Convert rule file to InputStream
			InputStream resourceAsStream = getClass().getResourceAsStream(string);

			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();

		Sample helloWorld = new Sample();
		helloWorld.setPrintMessage("Pizza Price");
		
		helloWorld.setPrice(12);		
		
		//Insert and fire all rules until its empty
		workingMemory.insert(helloWorld);
		workingMemory.fireAllRules();

	}

}
  • Output:

Load Call Multiple Drools File

download  Download project:  DroolsHelloWorld

Leave a Reply

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