Getting Started with JBoss Drools

1. Download Drools installations.

2. If you are using Eclipse the best way is to download the drools plugins for eclipse, which would create a new project type in the Eclipse enviorment.

3.Create a new Java project and add the jars from the drools directory to the project.

4.Create a new rules file with .drl file extension, having the following contents

Sample.drl

package com.sample

import com.sample.DroolsTest.Message;

rule “Hello World”
when
m : Message( status == Message.HELLO, message : message )
then
System.out.println( message );
m.setMessage( “Goodbye cruel world” );
m.setStatus( Message.GOODBYE );
System.out.println( “Testing message again” );
update( m );
end

rule “GoodBye”
no-loop true
when
m : Message( status == Message.GOODBYE, message : message )
then
System.out.println( message );
m.setMessage( message );

end

5. Next we write the Rule Loader and Sample application

DroolsTest.java

package com.sample;

import java.io.InputStreamReader;
import java.io.Reader;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.WorkingMemory;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;
import org.drools.spi.Activation;
import org.drools.spi.AgendaFilter;

/**
* This is a sample file to launch a rule package from a rule source file.
*/
public class DroolsTest {

public static final void main(String[] args) {
try {
//Loading the Rules
RuleBase ruleBase = readRule();
WorkingMemory workingMemory = ruleBase.newStatefulSession();
Message message = new Message();
message.setMessage(  “Hello World” );
message.setStatus( Message.HELLO );
workingMemory.insert( message );

//To run all the rules please un-comment the following line
//            workingMemory.fireAllRules();

//To run a specific rule
AgendaFilter filter = new AgendaFilter()
{
public boolean accept(Activation activation)
{
if (activation.getRule().getName().equals(“Hello World”))
{
return true;
}

return false;
}
};
workingMemory.fireAllRules(filter);

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

/**
* Please note that this is the “low level” rule assembly API.
*/
private static RuleBase readRule() throws Exception {
//read in the source
Reader source = new InputStreamReader( DroolsTest.class.getResourceAsStream( “/Sample.drl” ) );

//optionally read in the DSL (if you are using it).
//Reader dsl = new InputStreamReader( DroolsTest.class.getResourceAsStream( “/mylang.dsl” ) );

//Use package builder to build up a rule package.
//An alternative lower level class called “DrlParser” can also be used…

PackageBuilder builder = new PackageBuilder();

//this wil parse and compile in one step
//NOTE: There are 2 methods here, the one argument one is for normal DRL.
builder.addPackageFromDrl( source );

//Use the following instead of above if you are using a DSL:
//builder.addPackageFromDrl( source, dsl );

//get the compiled package (which is serializable)
Package pkg = builder.getPackage();

//add the package to a rulebase (deploy the rule package).
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( pkg );
return ruleBase;
}

public static class Message {
public static final int HELLO = 0;
public static final int GOODBYE = 1;

private String message;

private int status;

public String getMessage() {
return this.message;
}

public void setMessage(String message) {
this.message = message;
}

public int getStatus() {
return this.status;
}

public void setStatus( int status ) {
this.status = status;
}
}

}

In my case I am running “Hello World” Rule. Hope everything would work accordingly, for you also.

I would try to embed Drools with Spring, which I am trying to integrate with one of our projects on GWT.

Leave a Comment