本人参照drools-4.0.7-examples修改,可以到这个网址去下载:http://www.jboss.org/drools/downloads.html
Goodbye cruel world
文档中说还需要GEF,不过上面的4.0版的eclipse中已经有了,其他版本不知道,在这看Help->Software Update->Installed Software。
首先要导入的包:其中foxtrot-2.0.jar ,jgoodies-forms-1.0.4.jar非必须在其他例子要用到
HelloWorldExample .Java
package org.drools.examples;
import java.io.FileReader;
import java.io.Reader;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.StatefulSession;
import org.drools.compiler.PackageBuilder;
import org.drools.event.DebugAgendaEventListener;
import org.drools.event.DebugWorkingMemoryEventListener;
import org.drools.rule.Package;
/**
* This is a sample file to launch a rule package from a rule source file.
*/
public class HelloWorldExample {
public static void main(String[] args) throws Exception {
String filepath = “bin/rules/org/drools/examples/HelloWorld.drl”;
final Reader source = new FileReader(filepath);
final PackageBuilder builder = new PackageBuilder();
// this will 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 Message message = new Message();
message.setMessage(“Hello World”);
message.setStatus(Message.HELLO);
session.insert(message);
session.fireAllRules();
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;
}
}
}
HelloWorld.drl
package org.drools.examples
import org.drools.examples.HelloWorldExample.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
Warning: count(): Parameter must be an array or an object that implements Countable in /www/wwwroot/www.drools.org.cn/wp-includes/class-wp-comment-query.php on line 405