在条件下遍历Drools的列表 [英] Iterating over list in Drools' when condition

查看:126
本文介绍了在条件下遍历Drools的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的班级层次结构如下:

I have a class hierarchy as below:

class Order{
    List<Item> items;
    boolean eligibility;
    //geters and setters
}

class Item{
    boolean eligibility;
    //getters and setters
}

现在,我要做的是根据订单项目的合格状态设置其合格状态.如果订单中的所有项目的合格性均为假,则该订单的合格性应设置为"false".根据另一篇文章,我写了这样的规则:

Now, what I want to do is set the eligibility status of an order based on the eligibility status of its items. If all the items in an order has eligibility as false, then the order's eligibility should be set to false. Based on another post, I wrote a rule like this:

rule "Check order eligibility based on item eligibility"
dialect "java"
    when
        $order : Order($items : items)
            Boolean(booleanValue)       
            from accumulate(
                init(int eligibleItems = 0;),  
                action(for(Object item:$items) 
                        if(((Item)item).getEligibility()) eligibleItems++;
                    ),
                result(eligibleItems == 0)
            )
    then
        $order.setEligibility(false);
end

但是,当我运行此命令时,我总是得到不匹配的规则输入'eligibleItems'.在 when 条件内的列表上,我还能如何迭代?我正在使用Drools 6.2.0.

However, when I run this I keep getting mismatched input 'eligibleItems' in rule. How else can I iterate over a list inside a when condition? I'm using Drools 6.2.0.

推荐答案

要解释该错误,请查看规则的这一部分:

To explain the error, look at this part of the rule:

        Boolean(booleanValue)       
        from accumulate(
            init(int eligibleItems = 0;),  

使用init-action-result结果的 accumulate 的语法在关键字 accumulate 之后的右括号后要求一个 pattern .因此,语法错误.(您可以看到为什么说当您将 init 与模式类型名称进行匹配时显示它的原因:解析器无法在 eligibleItems 处继续.)

The syntax of an accumulate using init-action-result requires a pattern right after the opening parenthesis after the keyword accumulate. Hence the syntax error. (You can see the reason why it says when it says when you match init with a pattern type name: the parser can't continue at eligibleItems.)

可以尝试以更简单的方式编写规则,而不是尝试对其进行修复:

Rather than try and fix it, the rule can be written in a much simpler way:

rule "Check order eligibility based on item eligibility"
when
  $order : Order($items : items, eligibility )
  not Item( eligibility ) from $items
then
  modify( $order ){ setEligibility(false) }
end

它确定在订单中没有任何商品的合格性设置为true.万一您想使更改对IE可见,我已经使用了修改并添加了约束来避免循环.

It ascertains that there is no Item with eligibility set to true among the ones of an order. Just in case you want to make the change visible to the IE, I've used a modify and added a constraint to avoid looping.

这篇关于在条件下遍历Drools的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆