package com.sample;
import org.drools.core.SessionConfiguration;
import org.drools.core.base.RuleNameEqualsAgendaFilter;
import org.kie.api.KieServices;
import org.kie.api.builder.*;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.KieSessionConfiguration;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Test {
public Test() {
}
public void fireRules(Object fact) throws Exception{
KieServices ks = KieServices.Factory.get();
KieRepository kr = ks.getRepository();
KieFileSystem kfs = ks.newKieFileSystem();
KieSessionConfiguration conf= SessionConfiguration.newInstance();
String path ="test.drl";
String drl = new String(Files.readAllBytes(Paths.get(path)));
kfs.write("../resources/com/sample/rules/simple.drl",ks.getResources().newReaderResource( new StringReader(drl) ));
KieBuilder kb = ks.newKieBuilder(kfs);
kb.buildAll();
if (kb.getResults().hasMessages(Message.Level.ERROR)) {
throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
}
KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
KieSession kSession = kContainer.newKieSession(conf);
kSession.insert(fact);
Test t = new Test();
kSession.insert(t);
System.out.println(kSession.fireAllRules());
return;
}
}
上述函数没有触发规则
规则“规则01”
当eval(1==1)时,则system.out.println(“规则01工作”);结束
以上是规则文件。
我想在运行时定义规则,但是上面实现的代码没有触发规则。是否有其他方法来创建和部署规则。
提前谢谢
暂无答案!
目前还没有任何答案,快来回答吧!