I want to do some validation using this Drools rule:
rule "Test"
when
MyRequest(paymentTerm == PaymentTerm.MONTH);
then
Validation val = new Validation();
val.setIsValid(true);
val.setDescription("It's a monthly term!");
insert(val);
end
I first created a Statefull KieSession, which works as expected, the first time. However, when I rerun the rule, the validation facts are still in Memory, which is not what I want. So, I tried to adjust the example to a Stateless KieSession.
Using KieSession kSession
:
kSession.insert(req);
kSession.fireAllRules();
Collection<?> validations = kSession.getObjects(new ClassObjectFilter(Validation.class));
Using StatelessKieSession kSession
:
List<Command> cmds = new ArrayList<>();
cmds.add(CommandFactory.newInsert(req, "request"));
cmds.add(CommandFactory.newGetObjects(new ClassObjectFilter(Validation.class), "validations"));
ExecutionResults results = kSession.execute(CommandFactory.newBatchExecution(cmds));
Collection<?> validations = (Collection<?>) results.getValue("validations");
The statefull session returns a Validation
object in the validations
collection and the stateless KieSession returns an empty collection. Why???
KIE / Drools: 6.5.0.Final
Java EE 7, using CDI on WildFly 10.1.0
Found it. I need to explicitly specify the order when the fireAllRules command is run. This works:
List<Command> cmds = new ArrayList<>();
cmds.add(CommandFactory.newInsert(req, "request"));
cmds.add(CommandFactory.newFireAllRules());
cmds.add(CommandFactory.newGetObjects(new ClassObjectFilter(Validation.class), "validations"));
ExecutionResults results = kSession.execute(CommandFactory.newBatchExecution(cmds));
Collection<?> validations = (Collection<?>) results.getValue("validations");