drools初接触

Drool是jboss的规则引擎, 老早就想看看了, 前断时间忙的一直没有时间.现在抽空学习一下.记录一下学习的心得体会, 也算给今年一个交代.


一. 环境配置.
最快的方法:
下载Drool的eclipse插件:
http://www.jboss.org/drools/downloads.html

此插件功能不错, 写rule很方便, 并且能创建 drool project, 所有依赖
的jar包都有包含,

二: hello world
晦涩的理论先放一边, 跑一个hello world再说:

  1. packagecom.sample;
  2. importjava.io.InputStreamReader;
  3. importjava.io.Reader;
  4. importorg.drools.RuleBase;
  5. importorg.drools.RuleBaseFactory;
  6. importorg.drools.WorkingMemory;
  7. importorg.drools.compiler.PackageBuilder;
  8. importorg.drools.rule.Package;
  9. /**
  10. *Thisisasamplefiletolauncharulepackagefromarulesourcefile.
  11. */
  12. publicclassDroolsTest{
  13. publicstaticfinalvoidmain(String[]args){
  14. try{
  15. //loaduptherulebase
  16. RuleBaseruleBase=readRule();
  17. WorkingMemoryworkingMemory=ruleBase.newStatefulSession();
  18. //go!
  19. Messagemessage=newMessage();
  20. message.setMessage("HelloWorld");
  21. message.setStatus(Message.HELLO);
  22. workingMemory.insert(message);
  23. workingMemory.fireAllRules();
  24. }catch(Throwablet){
  25. t.printStackTrace();
  26. }
  27. }
  28. /**
  29. *Pleasenotethatthisisthe"lowlevel"ruleassemblyAPI.
  30. */
  31. privatestaticRuleBasereadRule()throwsException{
  32. //readinthesource
  33. Readersource=newInputStreamReader(DroolsTest.class.getResourceAsStream("/Sample.drl"));
  34. //optionallyreadintheDSL(ifyouareusingit).
  35. //Readerdsl=newInputStreamReader(DroolsTest.class.getResourceAsStream("/mylang.dsl"));
  36. //Usepackagebuildertobuilduparulepackage.
  37. //Analternativelowerlevelclasscalled"DrlParser"canalsobeused...
  38. PackageBuilderbuilder=newPackageBuilder();
  39. //thiswilparseandcompileinonestep
  40. //NOTE:Thereare2methodshere,theoneargumentoneisfornormalDRL.
  41. builder.addPackageFromDrl(source);
  42. //UsethefollowinginsteadofaboveifyouareusingaDSL:
  43. //builder.addPackageFromDrl(source,dsl);
  44. //getthecompiledpackage(whichisserializable)
  45. Packagepkg=builder.getPackage();
  46. //addthepackagetoarulebase(deploytherulepackage).
  47. RuleBaseruleBase=RuleBaseFactory.newRuleBase();
  48. ruleBase.addPackage(pkg);
  49. returnruleBase;
  50. }
  51. publicstaticclassMessage{
  52. publicstaticfinalintHELLO=0;
  53. publicstaticfinalintGOODBYE=1;
  54. publicstaticfinalintGAME_OVER=2;
  55. privateStringmessage;
  56. privateintstatus;
  57. publicStringgetMessage(){
  58. returnthis.message;
  59. }
  60. publicvoidsetMessage(Stringmessage){
  61. this.message=message;
  62. }
  63. publicintgetStatus(){
  64. returnthis.status;
  65. }
  66. publicvoidsetStatus(intstatus){
  67. this.status=status;
  68. }
  69. }
  70. }

rule文件:
Sample.drl

  1. packagecom.sample
  2. importcom.sample.DroolsTest.Message;
  3. rule"HelloWorld"
  4. when
  5. m:Message(status==Message.HELLO,message:message)
  6. then
  7. System.out.println(message);
  8. m.setMessage("Goodbyecruelworld");
  9. m.setStatus(Message.GOODBYE);
  10. update(m);
  11. end
  12. rule"GoodBye"
  13. no-looptrue
  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("gameovernow!");
  20. update(m);
  21. end
  22. rule"gameover"
  23. when
  24. m:Message(status==Message.GAME_OVER)
  25. then
  26. System.out.println(m.getMessage());
  27. end
  28. java代码就不说了,简单解释一下rule文件:
  29. rule"HelloWorld"
  30. when
  31. m:Message(status==Message.HELLO,message:message)
  32. then
  33. System.out.println(message);
  34. m.setMessage("Goodbyecruelworld");
  35. m.setStatus(Message.GOODBYE);
  36. update(m);
  37. end

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,Blog,Go)