我需要在运行时添加.drl文件(从S3桶),并提供它的Drools规则引擎(I need to ad

2019-11-05 10:43发布

无法在运行时通过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());
}

Answer 1:

在Drools的,现在一切都罐子导向。 所以,你需要重建KJar(这可能是一个“虚拟KJar”(KieModule)编写的只是在内存中的文件系统)与新创建的规则。 要查看示例,请如IncrementalCompilationTest类的Drools [1]。 如果你搜索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



文章来源: I need to add .drl files at runtime (From an S3 bucket) and supply it to drools rule engine