Drools入门-----------环境搭建,分析Helloworld
Drools官网:http://www.jboss.org/drools
?
Drools and jBPM?consist out of several projects:(Drools软件包提供的几个部分的功能)
Drools Guvnor (Business Rules Manager)?(规则集管理器)
Drools Expert (rule engine)?? ? ? ? ? ? ? ? ? ? ?(规则引擎) ? ?
jBPM?5 (process/workflow)?? ? ? ? ? ? ? ? ? ? ??(工作流)
Drools Fusion (event processing/temporal reasoning)???(规则集引擎搜索与规划) ? ?
Drools Planner (automated planning)?? ? ???(决策表) ?
-----------------------------------------------
Eclipse3.5安装Drools5.2.0.Final插件
到Drools下载页面(现在是http://www.jboss.org/drools/downloads.html)
-下载并解压Drools:drools-distribution-5.2.0.Final.zip??(解压目录随意)
-下载并解压插件:droolsjbpm-tools-distribution-5.2.0.Final.zip(解压目录随意)
- 打开 Eclipse.
- 点 "Help"—"Install new software..."
- 点击 "Add..." ?
- 在name中输入"DroolsLocaluUpdateSite"(名字随意起)
- 点击 "Local..." 并且选择"......./binaries/org.drools.updatesite" (此目录在刚刚解压的插件中)
-?点击OK
- 选择所以插件。
-下面就是点击next,finish
?
插件安装完后还需在Eclipse中添加Drools的运行环境:
-?点击"Windows"—"Preferencess"
- 在窗口的树形菜单中选择"Drools"—"Installed Drools Runtimes"
- 在右边的空的Drools运行库列表中,点击"Add"
- 在弹出窗口中name输入DroolsRuntime(随意起个名字),在Path选择"......./drools-distribution-5.2.0.Final\drools-distribution-5.2.0.Final\binaries"?(此目录在刚刚解压的Drools中),
- 点击"OK"
- 点击"OK"
-----------------------------------------------
HelloWorld例子
?
- "New"—"Project"—"Drools Project"—输入项目名—"Next"
- 这里显示可以选择要生成的样例文件的类型(3种类型的6种样例文件:规则引擎样例的规则文件和java测试类;决策表开发的决策表文件和java测试类;工作流的流程文件和java测试类)。勾选规则引擎样例的规则文件和java测试类,点击"Next"
?
- 进入到Drools运行时库的选择界面。环境搭建中已经设置了默认的运行时库,这里加载。然后选择编译代码所使用的规则引擎版本,这里选择"Drools 5.1 or above",点击"Finish"
Sample.drl :规则引擎的样例文件,该文件根据Drools规则语言语法编写
?
package com.sample import com.sample.DroolsTest.Message; rule "Hello World" when //LHS块 //在working Memory中,如果存在一个Message对象满足他的status属性等于Message.HELLO条件, //就把满足条件的Message实例用变量m表示,他的message属性用myMessage表示,以便在RHS中使用。 m : Message( status == Message.HELLO, myMessage : message ) then //RHS块 //在LHS中满足条件就运行RHS块。用到了LHS中的变量m,myMessage System.out.println( myMessage ); m.setMessage( "Goodbye cruel world" ); m.setStatus( Message.GOODBYE ); update( m );//表示把此Message实例在 working memory中更新.这便会触发名称为"GoodBye"的rule, endrule "GoodBye" when Message( status == Message.GOODBYE, myMessage : message ) then System.out.println( myMessage );end
?
DroolsTest.java:java测试类,用来读取Sample.drl 并运行,该文件使用Drools规则引擎的API进行编写
?
package com.sample;import org.drools.KnowledgeBase;import org.drools.KnowledgeBaseFactory;import org.drools.builder.KnowledgeBuilder;import org.drools.builder.KnowledgeBuilderError;import org.drools.builder.KnowledgeBuilderErrors;import org.drools.builder.KnowledgeBuilderFactory;import org.drools.builder.ResourceType;import org.drools.io.ResourceFactory;import org.drools.logger.KnowledgeRuntimeLogger;import org.drools.logger.KnowledgeRuntimeLoggerFactory;import org.drools.runtime.StatefulKnowledgeSession;/** * This is a sample class to launch a rule. */public class DroolsTest { public static final void main(String[] args) { try { // load up the knowledge base KnowledgeBase kbase = readKnowledgeBase(); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();//创建会话 KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test"); // go ! Message message = new Message(); message.setMessage("Hello World"); message.setStatus(Message.HELLO); ksession.insert(message);//插入 ksession.fireAllRules();//执行规则 logger.close();//关闭 } catch (Throwable t) { t.printStackTrace(); } } private static KnowledgeBase readKnowledgeBase() throws Exception { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();//创建规则构建器 kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"), ResourceType.DRL);//加载规则文件,并增加到构建器 KnowledgeBuilderErrors errors = kbuilder.getErrors(); if (errors.size() > 0) {//编译规则过程中发现规则是否有错误 for (KnowledgeBuilderError error: errors) {System.out.println("规则中存在错误,错误消息如下:"); System.err.println(error); } throw new IllegalArgumentException("Could not parse knowledge."); } KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();//创建规则构建库 kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());//构建器加载的资源文件包放入构建库 return kbase; } public static class Message { public static final int HELLO = 0; public static final int GOODBYE = 1; 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; } }}
?
?
1 楼 bewithme 2012-03-13 顶一个,分析得不错。