Drools6.4.0结合Maven实例

1.为Eclipse配置Drools工具及环境搭建参考:http://blog.sina.com.cn/s/blog_4da7b8a90102vm45.html 

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 内容如下

 <?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>
(5)在rules文件夹中建立helloWorld.drl 内容如下:



   <?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>
 (6)建立实体类Message.java
      内容如下:
package com.sample;

public class Message
{

  
    //常量 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;  
      }  
        
}
 (7)建立测试类HelloWorldExample.java  内容如下:
    
	package com.sample;
	import org.kie.api.KieServices;
	import org.kie.api.runtime.KieContainer;
	import org.kie.api.runtime.KieSession;

	public class HelloWorldExample
	{

   	 public static final void main(final String[] args) {
       		KieServices ks = KieServices.Factory.get();
        	KieContainer kc = ks.getKieClasspathContainer();
        	KieSession ksession = kc.newKieSession("HelloWorldKS");
        	final Message message = new Message();
        	message.setMessage( "Hello World" );
       	 	message.setStatus( Message.HELLO );
       	 	ksession.insert(message);
       	 	ksession.fireAllRules();
       		ksession.dispose();
    	 }

       }
运行测试类结果如下:




 此实例所实现的功能比较简单,就是对message当中的status字段进行判断从而进行不同的操作。



你可能感兴趣的:(eclipse,maven,实例,drools)