package org.drools.examples.helloworld;
import java.io.UnsupportedEncodingException;
import org.drools.core.impl.InternalKnowledgeBase;
import org.drools.core.impl.KnowledgeBaseFactory;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderError;
import org.kie.internal.builder.KnowledgeBuilderErrors;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;
public class MainTest {
public static void main(String[] args) {
//rule,rule2可以放在数据库中,有个唯一code和他们对于,代码要执行规则的时候,根据code从数据库获取出来就OK了,这样自己开发的规则管理系统那边对数据库里的规则进行维护就行了
String rule = "package org.drools.examples.helloworld\r\n";
rule += "import org.drools.examples.helloworld.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 org.drools.examples.helloworld\r\n";
rule += "import org.drools.examples.helloworld.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";
KieSession 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);
}
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
kBase.addPackages(kb.getKnowledgePackages());
kSession = kBase.newKieSession();
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();
}
}
}
maven pom.xml 核心内容
0.0.1-SNAPSHOT war UTF-8 4.3.12.RELEASE 7.5.0.Final 1.8.4 1.7.12 3.16 org.apache.poi poi-ooxml ${poi.version} org.apache.poi poi-scratchpad ${poi.version} org.apache.poi poi-ooxml-schemas ${poi.version} org.drools drools-compiler ${drools.version} org.kie kie-spring ${drools.version} org.springframework spring-context ${org.springframework-version} commons-logging commons-logging org.springframework spring-webmvc ${org.springframework-version} org.springframework spring-core ${org.springframework-version} org.springframework spring-oxm ${org.springframework-version} org.springframework spring-beans ${org.springframework-version} org.springframework spring-tx ${org.springframework-version} org.springframework spring-jdbc ${org.springframework-version} org.springframework spring-webmvc ${org.springframework-version} org.springframework spring-aop ${org.springframework-version} org.springframework spring-test ${org.springframework-version} org.slf4j slf4j-api ${org.slf4j-version} org.slf4j slf4j-log4j12 ${org.slf4j-version} runtime log4j log4j 1.2.15 javax.mail mail javax.jms jms com.sun.jdmk jmxtools com.sun.jmx jmxri runtime javax.servlet javax.servlet-api 3.1.0 provided com.alibaba fastjson 1.2.44 com.google.protobuf protobuf-java 2.6.1 commons-io commons-io 1.4 com.google.code.gson gson 2.8.2 org.apache.maven.plugins maven-compiler-plugin 2.3.2 1.6 1.6