Drools规则引擎初学入门实例HelloWorld

2009-01-14 11:32(1)下载eclipse(www.eclipse.org),如果是一般的java开发,下载Eclipse IDE for Java Developers就行了,解压后即可使用;

(2)下载Drools(http://jboss.org/drools/downloads.html),目前最新版本是Drools 4.0.7 Binaries,下载后解压即可;

(3)之后下载eclipse的Drools插件,版本跟eclipse对应,目前有Drools 4.0.7 Eclipse 3.2 Workbench和Drools 4.0.7 Eclipse Europa 3.3 Workbench两种。

         Drools插件解压后,将里面的org.drools.eclipse_4.0.7.jar文件copy到eclipse的plugins目录中,重启eclipse,在工具栏可以看到一个  图标,这就是Drools的工作台,之后就可通过这个按钮创建Drools resource文件了。

(4)开始Hello World

Java文件: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;

/**
* 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 {
         
         //load up the rulebase
             RuleBase ruleBase = readRule();
             WorkingMemory workingMemory = ruleBase.newStatefulSession();
            
            //go !
             Message message = new Message();
             message.setMessage(  "Hello World" );
             message.setStatus( Message.HELLO );
             workingMemory.insert( message );
             workingMemory.fireAllRules();   
            
            
         } 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;
  public static final int GAME_OVER = 2;
  
  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;
   }
}
    
}
选择插件Drools按钮里的"New Rule resource",建立规则(rule)文件: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 );
   update( m );
end

rule "GoodBye"
no-loop true
when
   m : Message( status == Message.GOODBYE, message : message )
then
   System.out.println( message );
   m.setStatus(Message.GAME_OVER);
   m.setMessage("game over now!");
   update( m );
end

rule "game over"
when
   m : Message( status == Message.GAME_OVER)
then
   System.out.println( m.getMessage() );   
end
注意:文件要放在相应的包里,然后编译—执行,当时出现了错误,查找资料,还需要加载包,包括:

<1> Drools 4.0.7目录下的drools-core-4.0.7.jar,drools-compiler-4.0.7.jar

<2> Drools 4.0.7\lib目录下的antlr-runtime-3.0.jar,mvel-1.3.1-java1.4.jar

<3>以及eclipse\plugins目录下的org.eclipse.jdt.core_3.2.3.v_686_R32x.jar(不同版本,包名会稍有不同)。

重新运行,应该就不会有错了。执行结果如下:

Hello World
Goodbye cruel world
game over now!

你可能感兴趣的:(eclipse,jboss,ide,UP,Go)