提问者:小点点

我需要在运行时添加. drl文件(从S3存储桶)并将其提供给drools规则引擎


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

共1个答案

匿名用户

在Drools中,现在一切都是面向jar的。所以你需要用新创建的规则重建你的KJar(它可能是一个只写在内存文件系统中的“虚拟KJar”(KieModule))。要查看示例,请查看例如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

相关问题