问题
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
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();
}
}
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());
}
回答1:
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-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
来源:https://stackoverflow.com/questions/54186476/i-need-to-add-drl-files-at-runtime-from-an-s3-bucket-and-supply-it-to-drools