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

初探Drools--java端使用

程序员文章站 2022-05-07 21:55:22
...

初探Drools--java端使用

前言

本系列都是使用7.11版本

pom文件

		<!-- drools -->
        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-api</artifactId>
            <version>7.11.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.7.6</version>
        </dependency>
        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-ci</artifactId>
            <version>7.11.0.Final</version>
        </dependency>
        <!-- workbench打包好的jar -->
        <dependency>
            <groupId>com.myspace</groupId>
            <artifactId>ljlTest</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency> 
			<groupId>com.thoughtworks.xstream</groupId> 
			<artifactId>xstream</artifactId> 
			<version>1.4.10</version> 
		</dependency>

初始化工具类

public class DroolsTest {

	private static DroolsTest singleton = new DroolsTest();
	
	private KieContainer KieContainer;
	
	private KieBase kieBasek;
	
	public static Map<String, String> map = new HashMap<String, String>();
	
	public KieContainer getKieContainer() {
		return KieContainer;
	}

	public void setKieContainer(KieContainer KieContainer) {
		this.KieContainer = KieContainer;
	}

	public static DroolsTest getSingleton() {
		return singleton;
	}

	public KieBase getKieBasek() {
		return kieBasek;
	}

	private void setKieBasek(KieBase kieBasek) {
		this.kieBasek = kieBasek;
	}
	
	private DroolsTest(){
		KieServices ks = KieServices.Factory.get();
		ReleaseId releaseId = ks.newReleaseId( "com.myspace", "ljlTest", "1.0.0" );
		KieContainer kContainer = ks.newKieContainer(releaseId);
		KieBase kieBase = kContainer.getKieBase();
		setKieBasek(kieBase);
		setKieContainer(kContainer);
		KieScanner kScanner = ks.newKieScanner( kContainer );
		kScanner.start( 10000L );
		
	}
}

调用

		DroolsTest drools = DroolsTest.getSingleton();
		KieSession kieSession = drools.getKieContainer().newKieSession();
		Map ruleMap = new HashMap();
		kieSession.setGlobal("RuleName", ruleMap);
		kieSession.fireAllRules();
		ruleMap = (Map) kieSession.getGlobal("RuleName");
		kieSession.dispose();
相关标签: drools