Drools get first element of List in where clause

泪湿孤枕 提交于 2020-01-24 15:08:42

问题


Is it possible to get first element of list from when clause in Drools? If I don't know Object fields values inside the list and I want just to retrieve first element, how can I do this?

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;

回答1:


You need to cast the $listCustObjs object to List then only you will be able to use list methods. Casting is done in drools by using '#' operator. Check here.

Also, you can directly use a statement like $first : $listCustObjs.get(0). In drools when all the computation or conditional check is done on Fact i.e object. So in your case, you can only get the value from $listCustObjs list when you will try to get it inside an object.



来源:https://stackoverflow.com/questions/59085312/drools-get-first-element-of-list-in-where-clause

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