Drools6 动态读取数据库规则

最近项目需要用到Drools,以前没接触过的,就只能百度资料了;查到的很多读库的规则写法都是drools6之前的,目前发现一种写法,只是实现了自定义规则,好像只能加载一个规则,还有很多问题希望大牛发现解答...

package com.sy.studio.data_server.dao;

import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.Results;
import org.kie.api.event.rule.DebugAgendaEventListener;
import org.kie.api.event.rule.DebugRuleRuntimeEventListener;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

public class Drools6toDB {

	public static void main(String[] args) {
		
		String rule = "package com.sy\r\n";
		rule += "import com.sy.studio.data_server.dao.Drools6toDB.Message;\r\n";
		rule += "rule \"Hello World\"\r\n";
		rule += "\twhen\r\n";
		rule += "m : Message( status == Message.HELLO, myMessage : message )";
		rule += "\tthen\r\n";
		rule += "\t\tSystem.out.println( myMessage );\r\n";
		rule += "\t\tm.setMessage(\"Goodbye cruel world\");\r\n";
		rule += "\t\tm.setStatus( Message.GOODBYE );;\r\n";
		rule += "\t\tupdate( m );;\r\n";
		rule += "end\r\n";
		
		rule += "rule \"GoodBye\"\r\n";
		rule += "\twhen\r\n";
		rule += "Message( status == Message.GOODBYE, myMessage : message )";
		rule += "\tthen\r\n";
		rule += "\t\tSystem.out.println( 2+\":\"+myMessage );\r\n";
		rule += "end\r\n";
		
		KieServices kieServices = KieServices.Factory.get();
	    KieFileSystem kfs = kieServices.newKieFileSystem();
	    kfs.write( "src/main/resources/rules/ruless.drl",rule.getBytes());
	    KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ).buildAll();
	    Results results = kieBuilder.getResults();
	    if( results.hasMessages( org.kie.api.builder.Message.Level.ERROR ) ){
	        System.out.println( results.getMessages() );
	        throw new IllegalStateException( "### errors ###" );
	    }
	    KieContainer kieContainer = kieServices.newKieContainer( kieServices.getRepository().getDefaultReleaseId() );
	    KieBase kieBase = kieContainer.getKieBase();
	    KieSession ksession = kieBase.newKieSession();
	    
	    Message message = new Message();
        message.setMessage("Hello World");
        message.setStatus(Message.HELLO);
        
        Message message2 = new Message();
		message2.setStatus(Message.HELLO);
		message2.setMessage("hi world!");
        ksession.addEventListener( new DebugAgendaEventListener() );
        ksession.addEventListener( new DebugRuleRuntimeEventListener() );
        ksession.insert(message);
        ksession.insert(message2);
        ksession.fireAllRules();
	}
    public static class Message {

        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;
        }

    }
	
}


你可能感兴趣的:(Drools6.4)