drools 6学习--动态加载规则

    

        学习用的是drools 6。之前的2个drools例子的规则都是为规则文件中预先写好的,kmodule.xml指定文件路径,kieSeesion会将指定的文件夹下的所有规则文件都加载,这样会有一个麻烦,就是当规则改变了,那就得重写规则文件,然后重新打包部署项目,这会很麻烦,就有人想了,能否把规则保存在数据库中(这样可以通过页面进行更新数据库规则就OK了),然后项目加载规则的时候从数据库读取。drools也提供有这功能的,下面写了个小例子记录下

drools 6学习--动态加载规则_第1张图片

pom.xml


  4.0.0
  com.fei.drools
  drools-test2
  0.0.1-SNAPSHOT
  
    
  
  
    org.drools
    drools-core
    6.4.0.Final


    org.drools
    drools-compiler
    6.4.0.Final



    org.drools
    knowledge-api
    6.4.0.Final

  
  

Message.java

package com.fei.drools;

public class Message {

	public static final int HELLO = 0;
	public static final int GOODBYE = 1;
	@org.kie.api.definition.type.Label("消息")
	private String msg = "test";
	private int status;

	public Message() {
		super();
	}

	public String getMsg() {
		return this.msg;
	}

	public void setMsg(String message) {
		this.msg = message;
	}

	public int getStatus() {
		return this.status;
	}

	public void setStatus(int status) {
		this.status = status;
	}
	
}

MainTest.java

package com.fei.drools;

import java.io.UnsupportedEncodingException;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;


public class MainTest {

	public static void main(String[] args) {
		//rule,rule2可以放在数据库中,有个唯一code和他们对于,代码要执行规则的时候,根据code从数据库获取出来就OK了,这样自己开发的规则管理系统那边对数据库里的规则进行维护就行了
		String rule = "package com.fei.drools\r\n";
		rule += "import com.fei.drools.Message;\r\n";
		rule += "rule \"rule1\"\r\n";
		rule += "\twhen\r\n";
		rule += "Message( status == 1, myMessage : msg )";
		rule += "\tthen\r\n";
		rule += "\t\tSystem.out.println( 1+\":\"+myMessage );\r\n";
		rule += "end\r\n";
		
		
		String rule2 = "package com.fei.drools\r\n";
		rule += "import com.fei.drools.Message;\r\n";
		
		rule += "rule \"rule2\"\r\n";
		rule += "\twhen\r\n";
		rule += "Message( status == 2, myMessage : msg )";
		rule += "\tthen\r\n";
		rule += "\t\tSystem.out.println( 2+\":\"+myMessage );\r\n";
		rule += "end\r\n";
		
		
		StatefulKnowledgeSession kSession = null;
		try {
			
			
			KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
			//装入规则,可以装入多个
			kb.add(ResourceFactory.newByteArrayResource(rule.getBytes("utf-8")), ResourceType.DRL);
			kb.add(ResourceFactory.newByteArrayResource(rule2.getBytes("utf-8")), ResourceType.DRL);
			
			KnowledgeBuilderErrors errors = kb.getErrors();
			for (KnowledgeBuilderError error : errors) {
				System.out.println(error);
			}
			KnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
			kBase.addKnowledgePackages(kb.getKnowledgePackages());
			
			kSession = kBase.newStatefulKnowledgeSession();

			
			Message message1 = new Message();
			message1.setStatus(1);
			message1.setMsg("hello world!");
			
			Message message2 = new Message();
			message2.setStatus(2);
			message2.setMsg("hi world!");
			
			kSession.insert(message1);
			kSession.insert(message2);
			kSession.fireAllRules();

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} finally {
			if (kSession != null)
				kSession.dispose();
		}

	}
}


执行结果

drools 6学习--动态加载规则_第2张图片



你可能感兴趣的:(drools规则引擎)