问题
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