Check all elements of a list (Drools Expert)

萝らか妹 提交于 2019-12-08 06:46:07

问题


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

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/5106500/check-all-elements-of-a-list-drools-expert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!