Java Drools:如何迭代列表并添加到另一个列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20537506/
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
Drools: how to iterate list and add to another list
提问by Sandamali Vidanagamage
I am trying to iterate a ArrayList and add to another ArrayList using jboss drools inside the rule.
我正在尝试迭代一个 ArrayList 并在规则中使用 jboss drools 添加到另一个 ArrayList。
I have a pojo with the list.
我有一个包含列表的 pojo。
Class DroolsPojo{
List<String> answers;
//getters and setters
}
My pojo returning a list like {"a","b","c","","",""}. I want to iterate the list and want to add elements which are not equal to ""(not empty elements of the list).
我的 pojo 返回一个列表,如 {"a","b","c","","",""}。我想迭代列表并想要添加不等于“”的元素(不是列表的空元素)。
How can I do this with drools?
我怎么能用流口水做到这一点?
Is there any way to get the element count which is not equal to "" with the drools.
有什么办法可以让流口水的元素计数不等于“”。
My rule is like as follows.
我的规则如下。
rule "rule1"
when
dpojo:DroolsPojo(answers!=null)
then
List list = dpojo.getAnswers();
//want to iterate the list here
end
How to do this with drools?
如何用流口水做到这一点?
采纳答案by K.C.
So the rule just has to fire when the answers instance variable is not null? Using dialect mvel, something like this should work:
那么规则只需要在 answers 实例变量不为空时触发?使用方言 mvel,这样的事情应该可以工作:
package drools.xxx
dialect "mvel"
import drools.xxx.DroolsPojo
rule "rule1"
when
$dpojo : DroolsPojo(answers!=null)
$answersWithoutEmptyStrings : List() from collect ( String(length > 0) from $dpojo.answers )
then
insert($answersWithoutEmptyStrings)
end
Here I do the collect (iterating) in the when clause.
在这里,我在 when 子句中进行了收集(迭代)。
回答by laune
On the right hand side you just write plain old Java code:
在右侧,您只需编写普通的旧 Java 代码:
List list = dpojo.getAnswers();
for( Object obj: list ){
String s = (String)obj;
if( s.length() > 0 ){ ... }
}
The parser doesn't like generics (yet?) so you'll have to work around that.
解析器不喜欢泛型(还有?)所以你必须解决这个问题。