本文介绍了我需要在运行时(从S3存储桶中)添加.drl文件,并将其提供给drools规则引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Unable to add rules at runtime through ResourceFactory.newURLResource()Also , KnowledgeBase is deprecated or cannot be resolved

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

I have tried all the snippets from the documentation

公共类RuleRunner {

public class 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(){

public 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());
}

推荐答案

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

In Drools, now everything is jar oriented. So you need to rebuild your KJar (it could be a "virtual KJar" (KieModule) written just to the in-memory filesystem) with newly created rules. To see examples, please check e.g. IncrementalCompilationTest class in Drools [1]. You could find also other test classes with examples if you search the Drools codebase. E.g. this [2].

[1] "> https://github.com/kiegroup/drools/blob/221cb15cde3ab5ea7388281d2c96158117ee3e19/drools-test-coverage/test-compiler 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. #L31

[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

这篇关于我需要在运行时(从S3存储桶中)添加.drl文件,并将其提供给drools规则引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 02:04