Maven Drools6.4.0.Final和Spring 整合

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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.myproject.drools</groupId>
 <artifactId>droolsspring</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>droolsspring Maven Webapp</name>
 <url>http://maven.apache.org</url>
   <properties>
       <drools-version>6.4.0.Final</drools-version>
       <project.tomcat.version>8.0.0-RC5</project.tomcat.version>  
   </properties>
 <dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
	<dependency>
	    <groupId>org.kie</groupId>
	    <artifactId>kie-api</artifactId>
	    <version>${drools-version}</version>
	</dependency>
	<dependency>
	    <groupId>org.kie</groupId>
	    <artifactId>kie-internal</artifactId>
	    <version>${drools-version}</version>
	</dependency>
	<dependency>
	    <groupId>org.drools</groupId>
	    <artifactId>drools-core</artifactId>
	    <version>${drools-version}</version>
	</dependency>
	<dependency>
	    <groupId>org.drools</groupId>
	    <artifactId>drools-compiler</artifactId>
	    <version>${drools-version}</version>
	</dependency>
	<dependency>
	    <groupId>org.kie</groupId>
	    <artifactId>kie-spring</artifactId>
	    <version>${drools-version}</version>
	</dependency>
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-context</artifactId>
	    <version>3.2.12.RELEASE</version>
	</dependency>
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-test</artifactId>
	    <version>3.2.12.RELEASE</version>
	</dependency>
	<dependency>
	    <groupId>commons-logging</groupId>
	    <artifactId>commons-logging</artifactId>
	    <version>1.2</version>
	</dependency>
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-web</artifactId>
	    <version>4.3.0.RELEASE</version>
	</dependency>
	<dependency>  
      <groupId>org.apache.tomcat</groupId>  
      <artifactId>tomcat-servlet-api</artifactId>  
      <version>${project.tomcat.version}</version>  
      <scope>provided</scope>  
    </dependency> 
  </dependencies>
  <build>
    <finalName>droolsspring</finalName>
    <plugins>  
       <plugin>  
           <groupId>org.apache.maven.plugins</groupId>  
           <artifactId>maven-compiler-plugin</artifactId>  
           <version>3.1</version>  
           <configuration>  
               <source>1.8</source>  
               <target>1.8</target>  
           </configuration>  
       </plugin> 
       <plugin>  
            <groupId>org.apache.tomcat.maven</groupId>  
            <artifactId>tomcat7-maven-plugin</artifactId>  
            <version>2.2</version>  
            <configuration>  
                <url>http://localhost:8080/manager/text</url>  
                <path>/${project.artifactId}</path>  
            </configuration>  
        </plugin>  
        <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-resources-plugin</artifactId>  
                <configuration>  
                    <encoding>UTF-8</encoding>  
                </configuration>  
            </plugin>  
    </plugins>  
  </build>
</project>

MessageTest.java

package com.li.mysite.model;
/**Message
***/
public class MessageTest {
    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;
    }
}

KieTestService.java

package com.li.mysite.service;

/**KieTestService
 * 
 */
public interface KieTestService {
    void test();
}

KieTestServiceImpl.java

package com.li.mysite.service.impl;

import com.li.mysite.model.MessageTest;
import com.li.mysite.service.KieTestService;

import org.kie.api.command.Command;
import org.kie.internal.command.CommandFactory;
import org.kie.internal.runtime.StatelessKnowledgeSession;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

/**KieTestServiceImpl
 * 
 */
@Service("kieTestServiceImpl")
public class KieTestServiceImpl implements KieTestService{

    @Resource(name = "ksessionStateless")
    private StatelessKnowledgeSession ksessionStateless;

    public void test(){
        MessageTest message = new MessageTest();
        message.setMessage("Hello World");
        message.setStatus(MessageTest.HELLO);
        try {
        	ksessionStateless.setGlobal("MessageTest", message);
        	ksessionStateless.execute(message);
            System.out.println("已執行");
        } catch (LinkageError error) {
            System.out.println("" + error.getMessage());
        } catch (Exception ex) {
            System.out.println("" + ex);
        }
    }   
}

spring-config-drools.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"
	xmlns:kie="http://drools.org/schema/kie-spring"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
	http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 
	http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd 
	http://drools.org/schema/kie-spring http://drools.org/schema/kie-spring.xsd"
	default-lazy-init="true">
	
	<kie:kmodule id="kModule">
		<kie:kbase name="kbase" packages="rules">
			<kie:ksession name="ksessionStateless" type="stateless" />
		</kie:kbase>
	</kie:kmodule>
	<bean id="kiePostProcessor" class="org.kie.spring.KModuleBeanFactoryPostProcessor" />
</beans>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:cache="http://www.springframework.org/schema/cache"  
    xsi:schemaLocation="  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context.xsd  
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx.xsd  
    http://www.springframework.org/schema/jdbc  
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
    http://www.springframework.org/schema/cache  
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop.xsd  
    http://www.springframework.org/schema/util  
    http://www.springframework.org/schema/util/spring-util.xsd"> 
    <import resource="classpath:spring-config-drools.xml"></import>
    <bean id="kieTestService" class="com.li.mysite.service.impl.KieTestServiceImpl">
    </bean>
</beans>

#rule.drljava

package rules;
import com.li.mysite.model.MessageTest;
rule "Hello World"
    when
        messagetest:MessageTest(status == 0);
    then
        System.out.println("-------------- ");
end

drools.java

package com.li.mysite.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.li.mysite.service.KieTestService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"}) 
public class drools {
//	
	@Autowired
	public KieTestService kieTestService;
	
	@Test
	public void testDrools(){
		kieTestService.test();
	}

	public KieTestService getKieTestService() {
		return kieTestService;
	}
//
	public void setKieTestService(KieTestService kieTestService) {
		this.kieTestService = kieTestService;
	}
	
}
相關文章
相關標籤/搜索