如下使用的是drools version 7.23.0
第一部分 测试代码 (删除、添加rule)
package com.test.drools.test;
import org.drools.core.impl.InternalKnowledgeBase;
import org.junit.Test;
import org.kie.api.runtime.KieSession;
import com.test.drools.bo.Message;
import com.test.drools.util.RuleEngineUtil;
public class TestDynamicRule{
String drl1 = "package rules;\n"
+ "import com.test.drools.bo.Message\n"
+ "rule \"test1\"\n"
+ "when\n"
+ "m : Message()\n"
+ "then \n"
+ "System.out.println(\"Rule \" + drools.getRule().getName() + \" is fired.\"); \n"
+ " System.out.println(\"----------Good\" + m);\n"
+ "end";;
String drl2 = "package rules;\n"
+ "import com.test.drools.bo.Message\n"
+ "rule \"test2\"\n"
+ "when\n"
+ "m : Message()\n"
+ "then \n"
+ "System.out.println(\"Rule \" + drools.getRule().getName() + \" is fired.\"); \n"
+ " System.out.println(\"----------Good 2\" + m);\n"
+ "end";
@Test
public void testDynamicRule()
{
InternalKnowledgeBase kBase = RuleEngineUtil.getKnowledgeBase();
RuleEngineUtil.loadRulesFromStringDrls(kBase, drl1);
KieSession kSession = kBase.newKieSession();
Message message1 = new Message();
message1.setMessage("Hello World 1");
message1.setStatus(Message.HELLO);
message1.setMessage("test");
kSession.insert(message1);
kSession.fireAllRules();
kBase.removeRule("rules", "test1");
RuleEngineUtil.loadRulesFromStringDrls(kBase, drl2);
Message message3 = new Message();
message3.setMessage("Good Bye 3");
message3.setStatus(Message.GOODBYE);
kSession.insert(message3);
kSession.fireAllRules();
kSession.dispose();
}
}
第二部分 Util代码
package com.test.drools.util;
import org.drools.core.impl.InternalKnowledgeBase;
import org.drools.core.impl.KnowledgeBaseFactory;
import org.kie.api.KieBase;
import org.kie.api.KieBaseConfiguration;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderConfiguration;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;
public class RuleEngineUtil {
public static InternalKnowledgeBase getKnowledgeBase() {
KieBaseConfiguration kBaseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
return (InternalKnowledgeBase)getKnowledgeBase(kBaseConfig);
}
public static KieBase getKnowledgeBase(KieBaseConfiguration kBaseConfig) {
KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kBaseConfig);
return kbase;
}
public static void loadRulesFromStringDrls(InternalKnowledgeBase kBase, String... stringDrls) {
loadRulesFromStringDrls(kBase, null, stringDrls);
}
public static void loadRulesFromStringDrls(InternalKnowledgeBase kBase, KnowledgeBuilderConfiguration kBuilderConf, String... stringDrls) {
if (kBuilderConf == null) {
kBuilderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
}
KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kBuilderConf);
for (String stringDrl : stringDrls) {
kBuilder.add(ResourceFactory.newByteArrayResource(stringDrl.getBytes()), ResourceType.DRL);
}
if (kBuilder.hasErrors()) {
System.out.println(kBuilder.getErrors().toString());
}
kBase.addPackages(kBuilder.getKnowledgePackages());
}
public static KieSession createKnowledgeSession(KieBase kBase) {
return kBase.newKieSession();
}
}
第三部分 bo代码
package com.test.drools.bo;
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 this.message;
}
public void setMessage(String message) {
this.message = message;
}
public int getStatus() {
return this.status;
}
public void setStatus(int status) {
this.status = status;
}
public String toString()
{
return "Message: [message = " + message
+ ", status = " + status;
}
}