- <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1.爲Eclipse配置Drools工具及環境搭建參考:http://blog.sina.com.cn/s/blog_4da7b8a90102vm45.html </span>
2.環境搭建完畢後即可建立一個實際的Drools與maven結合的實例。
(1)建立一個maven項目:
按照上圖的步驟建立一個maven項目
(2)在pom.xml文件中引入依賴包
打開如圖所示配置文件輸入如下配置數據
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>com.sample</groupId>
- <artifactId>Drools-test</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>Drools :: Sample Maven Project</name>
- <description>A sample Drools Maven project</description>
- <properties>
- <runtime.version>6.4.0.Final</runtime.version>
- </properties>
- <repositories>
- <repository>
- <id>jboss-public-repository-group</id>
- <name>JBoss Public Repository Group</name>
- <url>http://repository.jboss.org/nexus/content/groups/public/</url>
- <releases>
- <enabled>true</enabled>
- <updatePolicy>never</updatePolicy>
- </releases>
- <snapshots>
- <enabled>true</enabled>
- <updatePolicy>daily</updatePolicy>
- </snapshots>
- </repository>
- </repositories>
- <dependencies>
- <dependency>
- <groupId>org.kie</groupId>
- <artifactId>kie-api</artifactId>
- <version>${runtime.version}</version>
- </dependency>
- <dependency>
- <groupId>org.kie</groupId>
- <artifactId>kie-spring</artifactId>
- <version>${runtime.version}</version>
- </dependency>
- <dependency>
- <groupId>org.drools</groupId>
- <artifactId>drools-core</artifactId>
- <version>${runtime.version}</version>
- </dependency>
- <dependency>
- <groupId>org.drools</groupId>
- <artifactId>drools-decisiontables</artifactId>
- <version>${runtime.version}</version>
- </dependency>
- <dependency>
- <groupId>org.jbpm</groupId>
- <artifactId>jbpm-test</artifactId>
- <version>${runtime.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-nop</artifactId>
- <version>1.7.6</version>
- </dependency>
- </dependencies>
- </project>
確定依賴包成功加載後(不成功就是什麼clean呀,maven->update project什麼的。。。。)
(3)如下圖所示建立文件夾和包結構
(4)在META-INF中建立kmodule.xml 內容如下
- <span style="font-family: Arial, Helvetica, sans-serif;"> <?xml version="1.0" encoding="UTF-8"?> </span>
- <kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://jboss.org/kie/6.0.0/kmodule">
- <kbase name="HelloWorldKB" packages="rules">
- <ksession name="HelloWorldKS"/>
- </kbase>
- </kmodule>
- <pre name="code" class="html"><pre name="code" class="html"><span style="font-size: 14px;">(5)在rules文件夾中建立helloWorld.drl 內容如下:</span></pre>
- <pre></pre>
- <span style="font-size:14px; font-family:Arial,Helvetica,sans-serif"></span>
- <pre></pre>
- <pre name="code" class="html" style="font-size: 14px;"><pre name="code" class="html"> <?xml version="1.0" encoding="UTF-8"?>
- <kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://jboss.org/kie/6.0.0/kmodule">
- <kbase name="HelloWorldKB" packages="rules">
- <ksession name="HelloWorldKS"/>
- </kbase>
- </kmodule></pre><pre name="code" class="html"> (6)建立實體類Message.java</pre><pre name="code" class="html"> 內容如下:<span style="font-family: Arial, Helvetica, sans-serif;">package com.sample;</span><pre name="code" class="java"><span style="white-space:pre"> </span>public class Message
- <span style="white-space:pre"> </span>{
-
-
- //常量 HELLO 值0
- public static final int HELLO = 0;
- //常量 GOODBYE 值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;
- }
-
- }</pre><pre name="code" class="java"> (7)建立測試類HelloWorldExample.java <span style="font-family: Arial, Helvetica, sans-serif;"> 內容如下:</span></pre><pre name="code" class="java"> <pre name="code" class="java"><span style="white-space:pre"> </span>package com.sample;
- <span style="white-space:pre"> </span>import org.kie.api.KieServices;
- <span style="white-space:pre"> </span>import org.kie.api.runtime.KieContainer;
- <span style="white-space:pre"> </span>import org.kie.api.runtime.KieSession;
-
- <span style="white-space:pre"> </span>public class HelloWorldExample
- <span style="white-space:pre"> </span>{
-
- <span style="white-space:pre"> </span> public static final void main(final String[] args) {
- <span style="white-space:pre"> </span>KieServices ks = KieServices.Factory.get();
- <span style="white-space:pre"> </span>KieContainer kc = ks.getKieClasspathContainer();
- <span style="white-space:pre"> </span>KieSession ksession = kc.newKieSession("HelloWorldKS");
- <span style="white-space:pre"> </span>final Message message = new Message();
- <span style="white-space:pre"> </span>message.setMessage( "Hello World" );
- <span style="white-space:pre"KieSession ksession = kc.newKieSession("HelloWorldKS");
- <span style="white-space:pre"> </span>final Message message = new Message();
- <span style="white-space:pre"> </span>message.setMessage( "Hello World" );
- <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>message.setStatus( Message.HELLO );
- <span style="white-space:pre">