http://justsee.iteye.com/blog/1198259
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,
- end
- rule "GoodBye"
- when
- Message( status == Message.GOODBYE, myMessage : message )
- then
- System.out.println( myMessage );
- end
-
- 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,
- 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;
- /**
- * This is a sample class to launch a rule.
- */
- public class DroolsTest {
- public staticfinal 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 staticclass Message {
- public staticfinal int HELLO =0;
- public staticfinal 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;
- }
- }
- }opy
- 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;
- }
- }
- }
DroolsTest.rar (5.8 KB)
http://sunxboy.iteye.com/blog/1464919
drools中,支持的常见文件类型有drl(drools的规则语言),xls,cvs,rf(规则流文件),drt(drools rule template),在简单的应用中,一般只会使用drl和xls的文件,为了方便业务人员和维护人员能够配置规则文件,一般的规则文件都是由程序员做成execl文件,这样一来,添加相关的配置就很傻瓜了。但是这样做会影响到程序运行的效率,我们做一个测试。
规则文件:base.xls 包含了 rule.xls和simple.xls;在rule.xls中包括了credit.xls,为了读取credit.xls中的规则对象,则需要对base.xls ,rule.xls,credit.xls三个execl文件解析,读取规则对象结果合计耗时:
读取规则文件耗时——————–13484毫秒
在drools5.0中,org.drools.decisiontable.SpreadsheetCompiler这个类可以把xls文件流转化为drl的String对象,通过FileWriter来写成drl文件,相关的转换代码是:
public String drlString(String file,String toDir) throws Exception{
SpreadsheetCompiler sc = new SpreadsheetCompiler();
File f = new File(file);
String fileName = f.getName();
String name = fileName.substring(0,fileName.lastIndexOf("."));
FileInputStream xlsStream = new FileInputStream(f);
String drlFileTmp = sc.compile(xlsStream, InputType.XLS);
drlFileTmp.replaceAll(".xls", ".drl");
File drlFile = new File(toDir + "/" + name + ".drl");
FileWriter writer = new FileWriter(drlFile);
writer.write(drlFileTmp);
writer.close();
return drlFileTmp;
}
把execl解析成为drl文件后,读取规则对象结果合计耗时:
读取规则文件耗时——————--7579毫秒
从数据上看,几乎节省了一半的时间开销。当然,7.6秒仍然不是一个可以接受的数字,需要继续优化。我们需要在系统打版本的时候,就把xls规则文件转换为drl规则文件,然后我们在系统启动的时候,把规则对象通过oscache缓存在内存中。如果没有该对象则重新从规则文件中读取,有的话直接从内存读取,这样一来,读取规则对象消耗的时间几乎为0。这里把xls转化为drl文件只是为了加速系统的启动。
http://xiaoyanzi1205.iteye.com/blog/611936
我们公司服务器是websphere,所以我测试的时候决定用Tomcat6.0测试,以便以后部署到服务器上。
Tomcat6.0要求:
1.jdk5.0以上,而且必须是sun公司的(IBM的jdk发布时会报错);
2.下载所需的jar包,网址:http://download.jboss.org/drools/dependencies/jboss_jsf_libs.zip,解压后放到TOMCAT_HOME/lib下。
3.删除掉drools-guvnor.war里lib下面自带的el-api.jar(因为Tomcat6.0已经自带了,其他Tomcat版本不清楚)。
然后把drools-guvnor.war直接扔到Tomcat的webapps下,启动Tomcat即可。
访问BRMS:http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/Guvnor.html
username:admin
password:null
结束。