我想使用以下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");


有状态会话在Validation集合中返回validations对象,而无状态KieSession返回空集合。为什么???

KIE /流口水:6.5.0。最终

Java EE 7,在WildFly 10.1.0上使用CDI

最佳答案

找到了。我需要在运行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");

09-26 21:05