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

(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

  1. package com.sample;
  2. import java.io.InputStreamReader;
  3. import java.io.Reader;
  4. import org.drools.RuleBase;
  5. import org.drools.RuleBaseFactory;
  6. import org.drools.WorkingMemory;
  7. import org.drools.compiler.PackageBuilder;
  8. import org.drools.rule.Package;
  9. /**
  10. * This is a sample file to launch a rule package from a rule source file.
  11. */
  12. public class DroolsTest {
  13.     public static final void main(String[] args) {
  14.         try {
  15.          
  16.          //load up the rulebase
  17.              RuleBase ruleBase = readRule();
  18.              WorkingMemory workingMemory = ruleBase.newStatefulSession();
  19.             
  20.             //go !
  21.              Message message = new Message();
  22.              message.setMessage(  "Hello World" );
  23.              message.setStatus( Message.HELLO );
  24.              workingMemory.insert( message );
  25.              workingMemory.fireAllRules();   
  26.             
  27.             
  28.          } catch (Throwable t) {
  29.              t.printStackTrace();
  30.          }
  31.      }
  32.     /**
  33.       * Please note that this is the "low level" rule assembly API.
  34.       */
  35. private static RuleBase readRule() throws Exception {
  36.   //read in the source
  37.    Reader source = new InputStreamReader( DroolsTest.class.getResourceAsStream( "/Sample.drl" ) );
  38.   
  39.   //optionally read in the DSL (if you are using it).
  40.   //Reader dsl = new InputStreamReader( DroolsTest.class.getResourceAsStream( "/mylang.dsl" ) );
  41.   //Use package builder to build up a rule package.
  42.   //An alternative lower level class called "DrlParser" can also be used...
  43.   
  44.    PackageBuilder builder = new PackageBuilder();
  45.   //this wil parse and compile in one step
  46.   //NOTE: There are 2 methods here, the one argument one is for normal DRL.
  47.    builder.addPackageFromDrl( source );
  48.   //Use the following instead of above if you are using a DSL:
  49.   //builder.addPackageFromDrl( source, dsl );
  50.   
  51.   //get the compiled package (which is serializable)
  52.    Package pkg = builder.getPackage();
  53.   
  54.   //add the package to a rulebase (deploy the rule package).
  55.    RuleBase ruleBase = RuleBaseFactory.newRuleBase();
  56.    ruleBase.addPackage( pkg );
  57.   return ruleBase;
  58. }
  59. public static class Message {
  60.   public static final int HELLO = 0;
  61.   public static final int GOODBYE = 1;
  62.   public static final int GAME_OVER = 2;
  63.   
  64.   private String message;
  65.   
  66.   private int status;
  67.   
  68.   public String getMessage() {
  69.    return this.message;
  70.    }
  71.   
  72.   public void setMessage(String message) {
  73.    this.message = message;
  74.    }
  75.   
  76.   public int getStatus() {
  77.    return this.status;
  78.    }
  79.   
  80.   public void setStatus( int status ) {
  81.    this.status = status;
  82.    }
  83. }
  84.     
  85. }

选择插件Drools按钮里的"New Rule resource",建立规则(rule)文件:Sample.drl

  1. package com.sample
  2. import com.sample.DroolsTest.Message;
  3. rule "Hello World"
  4. when
  5.    m : Message( status == Message.HELLO, message : message )
  6. then
  7.    System.out.println( message );
  8.    m.setMessage( "Goodbye cruel world" );
  9.    m.setStatus( Message.GOODBYE );
  10.    update( m );
  11. end
  12. rule "GoodBye"
  13. no-loop true
  14. when
  15.    m : Message( status == Message.GOODBYE, message : message )
  16. then
  17.    System.out.println( message );
  18.    m.setStatus(Message.GAME_OVER);
  19.    m.setMessage("game over now!");
  20.    update( m );
  21. end
  22. rule "game over"
  23. when
  24.    m : Message( status == Message.GAME_OVER)
  25. then
  26.    System.out.println( m.getMessage() );   
  27. 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!

java代码就不说了, 主要说明一下rule文件, 熟悉一下rule语法:
1:规则的主体结构:
rule "name"
    ATTRIBUTES
    when
        LHS
    then
        RHS
end

其中:
LHS(left hand side)是规则的条件部分, RHS(right hand side)是条件满足后的自行块.

2: 条件的书写:
m : Message( status == Message.HELLO, message : message )
意思是:
在working Memory中, 如果有一个Message的instance满足如下条件:
它的status熟悉等于Message.HELLO.
满足条件的Message instance 用变量 m表示, 它的message属性用message变量表示, 以便在RHS中使用.
 
3: 条件满足后的自行块(RHS)
 
   System.out.println( message );
  m.setMessage( "Goodbye cruel world" );
  m.setStatus( Message.GOODBYE );
  update( m );
用到了在LHS中 定义的变量: m, message
update( m )表示把此Message instance在 working memory中更新.这便会触发名称为"GoodBye"的rule, 进而触发名称为"game over"的rule.

 

以下是drools提供的demo, 稍微整理了下, 可直接在eclipse中import进来

http://lcllcl987.iteye.com/blog/255404
(前提: 下载并安装Drool的eclipse插件:Drools 4.0.7 Eclipse Workbench for 3.2 )
建议首先看看:
FibonacciExample: Fibonacci数列的drools实现.
GolfingExample: 著名的golf智力问题的drools实现,golf.drl的注释就是此问题的文字描述.
ShoppingExample: 一个shopping打折的demo, 超过100元, 有10元的折扣.
StateExampleUsingAgendGroup: By setting dynamic to TRUE, Drools will use JavaBean, PropertyChangeListeners so you don't have to call update().
StateExampleWithDynamicRules: 动态加载rule的一个demo(加载StateExampleUsingSalience.drl后在加载StateExampleDynamicRule.drl).

TroubleTicketExample:
使用了duration规则特性: 将指定的规则延迟一定时间运行
规则引擎是在另外一个线程中进行的,因此使用了duration特性可以将指定的规则延迟一定时间运行.
但是要注意的是主线程中也需要做相应的延时,否则主线程结束后不会再处理子线程的结果
salience 和 duration特性影响rule的执行顺序.
其次demo还定义了一个rule function.

 

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