流口水在 where 子句中获取 List 的第一个元素



是否可以从 Drools 中的when子句获取列表的第一个元素? 如果我不知道列表中的对象字段值,而我只想检索第一个元素,我该怎么做?

rule "TestRule1"
dialect "java"
when
$c : Collection()
$listCustObjs : ArrayList() from collect (CustomObject() from $c)
$first : $listCustObjs.get(0) //<- something like this
$otherObj : $first.other // <- take other element from first object from the list
then
...
end;

您需要将$listCustObjs对象强制转换为List,然后只有您可以使用list方法。使用"#"运算符在口水中完成转换。查看此处。

此外,您可以直接使用类似$first : $listCustObjs.get(0).当所有计算或条件检查都在事实即对象上完成时,流口水。因此,在您的情况下,当您尝试在对象中获取值时$listCustObjs您只能从列表中获取值。

最新更新