0

无法在运行时通过 ResourceFactory.newURLResource() 添加规则 此外,知识库已被弃用或无法解析

我已经尝试了文档中的所有片段

公共类 RuleRunner {

public RuleRunner() {
}

public void runRules(String[] rules,
                     Object[] facts) throws Exception {

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

    for ( int i = 0; i < rules.length; i++ ) {
        String ruleFile = rules[i];
        System.out.println( "Loading file: " + ruleFile );
        kbuilder.add( ResourceFactory.newClassPathResource( ruleFile,
                                                            RuleRunner.class ),
                      ResourceType.DRL );
    }

    Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();
    kbase.addKnowledgePackages( pkgs );
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

    for ( int i = 0; i < facts.length; i++ ) {
        Object fact = facts[i];
        System.out.println( "Inserting fact: " + fact );
        ksession.insert( fact );
    }

    ksession.fireAllRules();
}

}

公共 KieContainer getKieContainer() {

    String drlURL ="https://s3.amazonaws.com/somebucket4/rules.drl";
    KieServices kieServices = KieServices.Factory.get();
    KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
    kieFileSystem.write(ResourceFactory.newUrlResource(drlURL));
    KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
    kieBuilder.buildAll();
    KieModule kieModule = kieBuilder.getKieModule();
    return kieServices.newKieContainer(kieModule.getReleaseId());
}
4

1 回答 1

1

在 Drools 中,现在一切都是面向 jar 的。因此,您需要使用新创建的规则重建您的 KJar(它可能是仅写入内存文件系统的“虚拟 KJar”(KieModule))。要查看示例,请查看 Drools [1] 中的 IncrementalCompilationTest 类。如果您搜索 Drools 代码库,您还可以找到其他带有示例的测试类。例如这个[2]。

[1] https://github.com/kiegroup/drools/blob/221cb15cde3ab5ea7388281d2c96158117ee3e19/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/compiler/integrationtests/incrementalcompilation/IncrementalCompilationTest.java #L178
[2] https://github.com/kiegroup/drools/blob/a62949b27d35b423725da493bf53c252608438c7/drools-model/drools-model-compiler/src/test/java/org/drools/modelcompiler/IncrementalCompilationTest.java#L31

于 2019-01-15T08:53:36.253 回答