我需要帮助使用 ng 文件系统为动态生成的 DRL 文件实施 Drool Fluent ApI

I need help for implementing Drool Fluent ApI for dynamically generated DRL file usi ng Kie file system

我成功地使用 drool fluent 动态生成了 drool 文件 api.But 我的问题是我无法使用 KieFileSystem 在内存区域写入这个文件 API.That 导致运行时异常不能找到 kie module.Below 是一样的

包com.nagarro;

全局fact.OutputData输出;

规则"rule1" 什么时候 通用事件(成本>500)
然后 output.setDiscount(10) 结束

线程异常 "main" 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 verifier 验证了 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 );
        }
    }

}

要将 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();