java 检查列表的所有元素(Drools Expert)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5106500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check all elements of a list (Drools Expert)
提问by John Manak
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.
我正在尝试在 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
回答by josh-cain
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:
我还找到了另一种 hack-y 方法来执行此操作,如果您想像 Geoffry 建议的那样使用 collect 将对象插入到工作内存中:
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
回答by Geoffrey De Smet
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:
SomeOtherType
如果您还没有将所有实例插入到工作内存中。如果您想检查所有 SomeOtherType 的颜色是否为红色,请尝试以下操作:
rule "Application eligible"
when
$app : Application()
forall( $x : SomeOtherType( application == $app )
SomeOtherType( this == $x, color == RED ) )
then
// application is eligible
end