亲爱的程序员,您好
在构建KnowledgeBase和KnowledgeSession之后,我尝试将DRL添加到KieFilesystem。但规则实际上不会触发。
你们可以试着帮助我吗?
感谢提前尝试每一个解决方案!
mycode的:
public class Playground {
private String newline = System.getProperty("line.separator");
private int MIN_VALUE = -2147483648;
public static void main(String[] args) {
Playground pg = new Playground();
pg.doRules();
}
private String generateDRLString() {
StringBuilder sb;
sb = new StringBuilder();
sb.append("package performance.drools;" + newline);
for (int i = start; i <= end; i++) {
sb.append("rule \"R" + i + "\"" + newline);
sb.append("when" + newline);
sb.append("then" + newline);
sb.append("System.out.println(\"rule" + i + " fired!\");" + newline);
sb.append("end" + newline);
}
return sb.toString();
}
KieServices kieServices;
KieContainer kieContainer;
KieBaseConfiguration kieBaseConf;
KieBase kieBase;
KieSession kieSession;
KieFileSystem kfs;
KieBuilder kieBuilder;
KieScanner kieScanner;
int start = MIN_VALUE;
int end = MIN_VALUE;
int count = 0;
private void init() {
kieServices = KieServices.Factory.get();
// configure module
KieModuleModel kieModuleModel = kieServices.newKieModuleModel();
KieBaseModel kieBaseModel1 = kieModuleModel.newKieBaseModel("KBase1 ")
.setDefault(true)
.setEqualsBehavior(EqualityBehaviorOption.EQUALITY)
.setEventProcessingMode(EventProcessingOption.CLOUD);
KieSessionModel kieSessionModel1 = kieBaseModel1
.newKieSessionModel("KSession1").setDefault(true)
.setType(KieSessionModel.KieSessionType.STATEFUL)
.setClockType(ClockTypeOption.get("realtime"));
// initialize module
kieBaseConf = kieServices.newKieBaseConfiguration();
kfs = kieServices.newKieFileSystem();
System.out.println(kieModuleModel.toXML());
kfs.writeKModuleXML(kieModuleModel.toXML());
// attach kfs to kieBuilder
kieBuilder = kieServices.newKieBuilder(kfs).buildAll();
assertEquals(0, kieBuilder.getResults()
.getMessages(Message.Level.ERROR).size());
kieContainer = kieServices.newKieContainer(kieServices.getRepository()
.getDefaultReleaseId());
kieBase = kieContainer.newKieBase(kieBaseConf);
kieSession = kieBase.newKieSession();
kieScanner = kieServices.newKieScanner(kieContainer);
}
private void addRules() {
kfs.write("src/main/resources/KBase1/ruleSet" + count + ".drl", generateDRLString());
count++;
kieBuilder.buildAll();
assertEquals(0, kieBuilder.getResults()
.getMessages(Message.Level.ERROR).size());
kieScanner.scanNow();
}
public void doRules() {
init();
start = 1;
end = 3;
addRules();
kieSession.insert(new Object());
start = 10;
end = 12;
addRules();
kieSession.fireAllRules();
System.out.println(kieSession.getFactCount());
}
}