Drools中文网

基于java的功能强大的开源规则引擎

本站QQ群:
446150745

Drools 入门例子

本人参照drools-4.0.7-examples修改,可以到这个网址去下载:http://www.jboss.org/drools/downloads.html

1:下载4.0版eclipse : eclipse-java-ganymede-win32.zip并解压到D:/eclipse-java-ganymede-win32不需安装
2:下载drools插件http://www.jboss.org/drools/downloads.html Drools 4.0.7 Eclipse Europa 3.3 Workbench
3:解压drools,把其中的org.drools.eclipse_4.0.7.jar拷到eciplse的features目录下
4:启动eclipse,可以看到drools workbench了,创建简单的drools
   File->New->Project->Drools->Rule Project点击Next,命名我为HelloDrools,点击Finish。
5:运行Run As->Java Application->DroolsTest – com.sample
   报错org.drools.RuntimeDroolsException: Unable to load dialect ‘org.drools.rule.builder.dialect.java.JavaDialectConfiguration:java’
解决办法: 在运行图标->Run Configuration->Classpath->Add External JARs添加D:/eclipse-java-ganymede-win32/eclipse/plugins/org.eclipse.jdt.core_3.4.0.v_874.jar。
6:输出
Hello World
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

留下评论