【问题标题】:Check all elements of a list (Drools Expert)检查列表的所有元素(Drools Expert)
【发布时间】:2011-07-03 15:39:18
【问题描述】:

我正在尝试在 Drools Expert 中编写规则。在规则的when 部分,我检查了Application 对象的一些属性。该对象包含一个列表,我想检查一堆规则是否适用于该列表中 SomeOtherType 的所有对象。只有当约束对该列表中的所有对象都有效时,该规则才会触发。

rule "Application eligible"
    when
        app : Application(
               some constrains
               & write some constraints for all objects in app.getList() (a method
               that returns a List<SomeOtherType> object)
        )
    then 
        // application is eligible
end

【问题讨论】:

    标签: java rules drools decision-tree rule-engine


    【解决方案1】:

    如果您还没有将所有SomeOtherType 实例也插入工作内存中。 如果您想检查所有 SomeOtherType 的颜色是否为红色,请尝试这样的操作:

    rule "Application eligible"
    when
        $app : Application()
        forall( $x : SomeOtherType( application == $app ) 
                SomeOtherType( this == $x, color == RED ) )
    then 
        // application is eligible
    end
    

    【讨论】:

    • 是否可以不将所有SomeOtherType 实例安装到工作内存中?例如使用forallfrom?
    • 是的,但是from 开始反向链接而不是前向链接 IIRC,这可能会产生性能影响(参见手册)。还有其他有趣的关键字可能会起作用,例如collect IIRC(另见手册)。
    • “SomeOtherType(application == $app)”中的“application”是什么意思?也许您的意思是“应用程序”? @Smet
    【解决方案2】:

    如果您想避免按照 Geoffry 的建议使用 collect 将对象插入工作内存,我还发现了另一种 hack-y 方法:

    rule "Person has all brothers"
      when
        $person : Person(siblings != null, siblings.size > 0) 
        List(size == siblings.size) from collect (Person(sex != null, sex == "m") from $person.siblings)
      then
        #Person has all brothers
      end
    

    【讨论】:

    • 为什么它是一种 hack-y 方式?
    猜你喜欢
    • 2012-03-15
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 2011-04-20
    • 2011-07-13
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    相关资源
    最近更新 更多