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
-
-
-
- m : Message( status == Message.HELLO, myMessage : message )
- then
-
-
- System.out.println( myMessage );
- m.setMessage( "Goodbye cruel world" );
- m.setStatus( Message.GOODBYE );
- update( m );
- end
-
- rule "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;
-
-
-
-
- public class DroolsTest {
-
- public static final void main(String[] args) {
- try {
-
- KnowledgeBase kbase = readKnowledgeBase();
- StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
- KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
-
- 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;
- }
-
- }
-
- }