我成功地使用drool fluent api成功地动态生成了drool文件,但是我的问题是我无法使用KieFileSystem API将该文件写入内存区域,这导致运行时异常找不到kie模块。一样
package com.nagarro;
全局事实。OutputData输出;
规则“ rule1”
什么时候
GenericEvent(cost> 500)
然后
output.setDiscount(10)
结束
线程“主”中的异常java.lang.RuntimeException:找不到KieModule:org.default:artifact:1.0.0-SNAPSHOT 在org.drools.compiler.kie.builder.impl.KieServicesImpl.newKieContainer(KieServicesImpl.java:97) 在main.Launcher.evaluate(Launcher.java:67) 在main.Launcher.main(Launcher.java:58)
我已经使用drool验证程序验证了drl文件的结构。
下面是我正在使用的代码。
public class Launcher {
public static void main(final String[] args) throws Exception {
/* PackageDescr pkg = DescrFactory.newPackage()
.name("org.drools.example").newImport().target("java.util.ArrayList").end()
.newImport().target("java.util.ArrayList").end()
.newImport().target("java.util.Vector").end()
.newRule().name("alert")
.lhs()
.and()
.pattern("Foo").id( "$foo", false ).constraint("bar==baz").constraint("x>y").end()
.not().pattern("Bar").constraint("a+b==c").end().end()
.end()
.end()
.rhs("System.out.println;"+"/n"+"System.out.println;").end()
.getDescr();*/
PackageDescr desc2 = DescrFactory
.newPackage()
.name("com.nagarro;")
.newGlobal().type("fact.OutputData").identifier("output;")
.end()
.newRule().name("rule1")
.lhs()
.pattern("GenericEvent").constraint("cost>500")
.end()
.end()
.rhs(" output.setDiscount(10)")
.end()
.getDescr();
DrlDumper dumper=new DrlDumper();
String drl=dumper.dump(desc2);
System.out.print(drl);
// verify(drl);
evaluate(drl, new OrderEvent());
}
static private void evaluate(final String drl, final Event event) throws Exception {
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
kieFileSystem.write("src/main/resources/rule.drl", drl);
kieServices.newKieBuilder(kieFileSystem).buildAll();
KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
StatelessKieSession statelessKieSession = kieContainer.getKieBase().newStatelessKieSession();
//HashMap<String,Object> outputMap = new HashMap<>();
OutputData outData = new OutputData();
statelessKieSession.getGlobals().set("output", outData);
HashMap<String, Object> inputMap = new HashMap<>();
inputMap.put("price", 1000);
GenericEvent evt = new GenericEvent();
evt.setInputmap(inputMap);
evt.setCost(1000);
statelessKieSession.execute(evt);
System.out.println(outData.getDiscount());
}
public static void verify(final String drl) {
// Create verifier builder [1]
VerifierBuilder vBuilder = VerifierBuilderFactory.newVerifierBuilder();
// Create verifier [2]
Verifier verifier = vBuilder.newVerifier();
// Add Resources you want to verify [3]
verifier.addResourcesToVerify(new ClassPathResource("generic.drl",Launcher.class),
ResourceType.DRL );
// Run the verification rules [4]
verifier.fireAnalysis();
// Get the result object [5]
VerifierReport result = verifier.getResult();
System.out.println(result);
// Print the the errors [6]
for(VerifierMessageBase base: result.getBySeverity( Severity.ERROR ) ){
System.out.println( base );
}
}
}
答案 0 :(得分:0)
要将drl作为文件写入,请使用:
kieFileSystem.write("src/main/resources/rule2.drl", kieServices.getResources().newReaderResource(
new StringReader(drl)));
用于验证drl
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem).buildAll();
// check there have been no errors for rule setup
Results results = kieBuilder.getResults();
if (results.hasMessages(Message.Level.ERROR)) {
System.out.println(results.getMessages());
throw new IllegalStateException("### errors ###");
}
KieContainer kieContainer = kieServices.newKieContainer( kieBuilder.getKieModule().getReleaseId());
KieSession kieSession = kieContainer.newKieSession();
对于评估规则:
kieSession.insert(inputData);
kieSession.setGlobal("output", outData);
kieSession.fireAllRules();
kieSession.dispose();