我想使用此Drools规则进行一些验证:
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
我首先创建了一个Statefull KieSession,它第一次按预期工作。但是,当我重新运行规则时,验证事实仍然在内存中,这不是我想要的。所以,我试图将这个例子调整为无状态的KieSession。
使用KieSession kSession
:
kSession.insert(req);
kSession.fireAllRules();
Collection<?> validations = kSession.getObjects(new ClassObjectFilter(Validation.class));
使用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");
statefull会话在Validation
集合中返回validations
对象,无状态KieSession返回空集合。为什么???
KIE / Drools:6.5.0.Final
Java EE 7,在WildFly 10.1.0上使用CDI
答案 0 :(得分:1)
找到它。我需要在运行fireAllRules命令时显式指定顺序。这有效:
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");