Becuase of the commerical licensing issues with Jess, I wanted to look at an alternative source for my reasoning engines. As I mentioned in my last post on Jess, Drools is an open source rule engine at http://www.jboss.org/drools/ and implements the JSR94 API as well as an innovative semantics framework. A one minute tutorial is at http://legacy.drools.codehaus.org/1+Minute+DRL+Tutorial that shows DRL syntax, semantic modules-Base Semantic module and the Java semantic module. I like the example at http://www.theserverside.com/tt/articles/article.tss?l=Drools . I downloaded the binaries and attached both drools-core-4.0.7.jar and drools-compiler-4.0.7.jar as external jars to my project. The TestHelloWorldExample project in Eclipse is
import java.io.InputStreamReader;
import java.io.Reader;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.StatefulSession;
import org.drools.audit.WorkingMemoryFileLogger;
import org.drools.common.DefaultAgenda;
import org.drools.compiler.PackageBuilder;
import org.drools.event.DebugAgendaEventListener;
import org.drools.event.DebugWorkingMemoryEventListener;
import org.drools.event.DefaultAgendaEventListener;
import org.drools.event.DefaultWorkingMemoryEventListener;
import org.drools.rule.Package;
public class TestHelloWorldExample {
/**
*/
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
//read in the source
final Reader source = new InputStreamReader( TestHelloWorldExample.class.getResourceAsStream( "HelloWorld.drl" ) );
final PackageBuilder builder = new PackageBuilder();
//this wil parse and compile in one step
builder.addPackageFromDrl( source );
// Check the builder for errors
if ( builder.hasErrors() ) {
System.out.println( builder.getErrors().toString() );
throw new RuntimeException( "Unable to compile \"HelloWorld.drl\".");
}
//get the compiled package (which is serializable)
final Package pkg = builder.getPackage();
//add the package to a rulebase (deploy the rule package).
final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( pkg );
final StatefulSession session = ruleBase.newStatefulSession();
session.addEventListener( new DebugAgendaEventListener() );
session.addEventListener( new DebugWorkingMemoryEventListener() );
final WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger( session );
logger.setFileName( "log/helloworld" );
final Message message = new Message();
message.setMessage( "Hello World" );
message.setStatus( Message.HELLO );
session.insert( message );
session.fireAllRules();
logger.writeToDisk();
session.dispose();
}
public static class Message {
public static final int HELLO = 0;
public static final int GOODBYE = 1;
private String message;
private int status;
public Message() {
}
public String getMessage() {
return this.message;
}
public void setMessage(final String message) {
this.message = message;
}
public int getStatus() {
return this.status;
}
public void setStatus(final int status) {
this.status = status;
}
}
}
The input for the HelloWorld.drl given by:
package org.drools.examples
import org.drools.examples.TestHelloWorldExample.Message;
rule "Hello World"
dialect "mvel"
when
m : Message( status == Message.HELLO, message : message )
then
System.out.println( message );
modify ( m ) { message = "Goodbyte cruel world",
status = Message.GOODBYE };
System.out.println( message );
end
rule "Good Bye"
dialect "java"
when
Message( status == Message.GOODBYE, message : message )
then
System.out.println( message );
end
Combining these applications with the example provided above provides the foundation for using rules in an open source Java application.
Wednesday, March 4, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment