无法在运行时通过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());
}
答案 0 :(得分: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