检查列表的所有元素(Drools Expert)

发布于 2024-10-18 17:50:26 字数 504 浏览 3 评论 0原文

我正在尝试在 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

I'm trying to write rules in Drools Expert. In the when part of the rule, I check some properties of an Application object. This object contains a List and I would like to check if a bunch of rules apply to all objects of SomeOtherType in this list. The rule should fire only when the constraints are valid for ALL objects in that list.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻梦旅人 2024-10-25 17:50:26

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

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

Insert all your SomeOtherType instances into the working memory too if you haven't already.
Then try something like this if you want to check that all SomeOtherType's have the color RED:

rule "Application eligible"
when
    $app : Application()
    forall( $x : SomeOtherType( application == $app ) 
            SomeOtherType( this == $x, color == RED ) )
then 
    // application is eligible
end
素衣风尘叹 2024-10-25 17:50:26

如果您想避免必须按照 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

I also found another kind of hack-y way to do this if you want to get around having to insert your object into working memory using collect as Geoffry suggested:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文