问题
I have a session in Drools 6.3.x containing e few million facts.
I'm building an interactive application that, among other things, allows the user to filter the facts as he wants. In my mind, this filter is basically a pattern. Once the facts are filtered, the logic is applied to produce the desired result.
Due to the interactive nature of the application, I'd rather not to wait for the user input to build a KieBase
, derive a KieSession
, load the few million facts and fire the rules all the times.
Ideally, I'd like to create a KieBase
containing the logic of the application once, derive a KieSession
once, load all the facts once, and the inject/remove rules on the fly depending on the user input. How can I do that in Drools 6.3? I know for sure it was possible with Drools 5.
Some code to contextualise my question. This snippet shows how to set up the Drools session:
KieHelper helper = new KieHelper();
String location = "/drools/logic.drl";
InputStream stream = getClass().getResourceAsStream(location);
Resource resource = ResourceFactory.newInputStreamResource(stream);
helper.addResource(resource, ResourceType.DRL);
Results results = helper.verify();
if (results.hasMessages(Message.Level.ERROR)) {
System.out.println(results.getMessages());
System.exit(0);
}
KieBase base = helper.build();
KieSession session = base.newKieSession();
for (Source source : sources) {
for (Object fact : source.getFacts()) {
session.insert(fact);
}
}
session.fireAllRules();
As far as I can see, the KieBase
doesn't provide any way to add new rules (only ways to remove them). So I don't know how to proceed from the following snippet, if not initialising the KieSession
from scratch...
String rule = "package boot\n" +
"\n" +
"rule \"Stamp\"\n" +
"when\n" +
"\t$o: Object()\n" +
"then\n" +
"\tSystem.out.println($o);\n" +
"end\n";
InputStream ruleStream = new ByteArrayInputStream(rule.getBytes());
Resource ruleResource = ResourceFactory.newInputStreamResource(ruleStream);
helper.addResource(ruleResource, ResourceType.DRL);
Any suggestion?
来源:https://stackoverflow.com/questions/36502310/how-to-dynamically-change-the-rules-of-a-given-drools-6-3-session