欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[Drools Demos] 其 (一) : Quick Start

程序员文章站 2022-05-28 11:38:29
...

前言

基础配置: JDK 1.7 + Eclipse Mar + Maven

依赖: Drools插件(非必须)

本章主要对应项目内的drools-quickstart子模块.
Github地址: https://github.com/SeanYanxml/drools-train


Quick Start

Quick Start的基本操作步骤如下:

1 - pom.xml文件内导入所需依赖包:

	<!-- drools jars -->
	<properties>
		<drools-version>6.5.0.Final</drools-version>
	</properties>
	<dependencies>
		<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.drools</groupId>
			<artifactId>knowledge-api</artifactId>
			<version>${drools-version}</version>
		</dependency>

		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-decisiontables</artifactId>
			<version>${drools-version}</version>
		</dependency>
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-templates</artifactId>
			<!--templates 模板 -->
			<version>${drools-version}</version>
		</dependency>
		<dependency>
			<groupId>org.kie</groupId>
			<artifactId>kie-api</artifactId>
			<version>${drools-version}</version>
		</dependency>

		<!-- other jars -->
		
		<!-- test -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>
  </dependencies>

2 - 编写Message.java类(用于规则文件内传递数据)

package com.yanxml.drools.quickstart;

/**
 * The pojo for drl.
 * @author Sean
 * */

public class Message {
	public static final int HELLO = 0;
	public static final int GOODBYE = 1;
	
	private String message;
	private int status;
	
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}

}

3 - 编写kmodule.xml文件和QuickStart.drl规则文件

kmodule.xml

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
    <kbase name="rules" packages="com.yanxml.drools.quickstart">
    <!-- 特别注意这边的packages 千万不能写成package 不然会报错 -->
    <!-- name 是kbase的名字 packages 是drl文件的路径 也就是HelloWorld.drl文件的物理路径 是在src/main/test 
		目录下的 -->
	<!-- ksession 是ksession的名字 但是此处ksession的名字和 .drl规则文件的名称不是对应的 -->
	<!-- 生成之后和.class文件放在一起 找的是resources下的物理地址 -->	        <ksession name="ksession-rules"/>
    </kbase>
    <kbase name="dtables" packages="dtables">
        <ksession name="ksession-dtables"/>
    </kbase>
</kmodule>

QuickStart.drl

package com.yanxml.drools.quickstart

import com.yanxml.drools.quickstart.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

4 - 编写测试类DroolsBaseTest和QuickStartTest

DroolsBaseTest(基础测试类)

package com.yanxml.drools.quickstart;

import org.junit.Before;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;

public abstract class DroolsBaseTest {	
	
	protected KieServices kieServices;
	protected KieContainer kieContainer;
	
	@Before
	public void setUp(){
		kieServices = KieServices.Factory.get();
		kieContainer = kieServices.getKieClasspathContainer();
	}

}

QuickStartTest(QuickStart测试类)

package com.yanxml.drools.quickstart;

import org.junit.Test;
import org.kie.api.runtime.KieSession;


public class QuickStartTest extends DroolsBaseTest{
	
	@Test
	public void test(){
		KieSession kSession = kieContainer.newKieSession("ksession-rules");
		Message message = new Message();
		message.setMessage("Hello World");
		message.setStatus(Message.HELLO);
		kSession.insert(message);
		kSession.fireAllRules();
	}

}