最近由于项目用到drools,所以只有一边学习一边开发,首先先安装好插件搭好开发环境。
下载drools eclipse插件,地址为:
http://www.jboss.org/drools/downloads.html
解压用links方式安装插件,如果
features目录和
plugins目录上层没有eclipse文件夹,就要建一个。
在Eclipse目录下面新建一个links文件夹,里面建一个XXX.link,内容为:path=F://plugins/drools
启动Eclipse,在window----prefernces那里如果看不到又drools,就说明插件安装不成功。
Eclipse3.4安装drools插件遇到识别不了插件,解决方案如下:
F:\eclipse\configuration\org.eclipse.equinox.simpleconfigurator下面
bundles.info最后加上以下几句:
org.guvnor.tools,5.0.1,file:plugins\org.guvnor.tools_5.0.1.jar,4,false
org.eclipse.webdav,3.0.101,file:plugins\org.eclipse.webdav_3.0.101.jar,4,false
org.drools.eclipse,5.0.1,file:plugins\org.drools.eclipse_5.0.1.jar,4,false
org.drools.eclipse.task,5.0.1,file:plugins\org.drools.eclipse.task_5.0.1.jar,4,false
保存即可。
再次启动Eclipse就可以成功安装插件,window----prefernces---drools插件,配置drools的依赖包环境,这些很简单,省略。
新建一个drools工程,就会看到一个附带的helloworld的例子,执行java文件即可执行测试。
Sample.drl
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
1package com.sample;
2
3import org.drools.KnowledgeBase;
4import org.drools.KnowledgeBaseFactory;
5import org.drools.builder.KnowledgeBuilder;
6import org.drools.builder.KnowledgeBuilderError;
7import org.drools.builder.KnowledgeBuilderErrors;
8import org.drools.builder.KnowledgeBuilderFactory;
9import org.drools.builder.ResourceType;
10import org.drools.io.ResourceFactory;
11import org.drools.logger.KnowledgeRuntimeLogger;
12import org.drools.logger.KnowledgeRuntimeLoggerFactory;
13import org.drools.runtime.StatefulKnowledgeSession;
14
15/** *//**
16 * This is a sample class to launch a rule.
17 */
18public class DroolsTest {
19
20 public static final void main(String[] args) {
21 try {
22 // load up the knowledge base
23 KnowledgeBase kbase = readKnowledgeBase();
24 StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
25 KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
26 // go !
27 Message message = new Message();
28 message.setMessage("Hello World");
29 message.setStatus(Message.HELLO);
30 ksession.insert(message);
31 ksession.fireAllRules();
32 logger.close();
33 } catch (Throwable t) {
34 t.printStackTrace();
35 }
36 }
37
38 private static KnowledgeBase readKnowledgeBase() throws Exception {
39 KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
40 kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"), ResourceType.DRL);
41 KnowledgeBuilderErrors errors = kbuilder.getErrors();
42 if (errors.size() > 0) {
43 for (KnowledgeBuilderError error: errors) {
44 System.err.println(error);
45 }
46 throw new IllegalArgumentException("Could not parse knowledge.");
47 }
48 KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
49 kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
50 return kbase;
51 }
52
53 public static class Message {
54
55 public static final int HELLO = 0;
56 public static final int GOODBYE = 1;
57
58 private String message;
59
60 private int status;
61
62 public String getMessage() {
63 return this.message;
64 }
65
66 public void setMessage(String message) {
67 this.message = message;
68 }
69
70 public int getStatus() {
71 return this.status;
72 }
73
74 public void setStatus(int status) {
75 this.status = status;
76 }
77
78 }
79
80}
输出:
Hello World
Goodbye cruel world-------------------------------------------------------------------------------------------------
PS:本博客文章,如果没有注明是有“转”字样,属于本人原创。如果需要转载,务必注明作者和文章的详细出处地址,否则不允许转载,多谢合作!
posted on 2010-01-17 21:08
apple0668 阅读(6009)
评论(2) 编辑 收藏 所属分类:
drools