流口水在where子句中获得List的第一个元素 [英] Drools get first element of List in where clause

查看:97
本文介绍了流口水在where子句中获得List的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 规则 TestRule1 
的方言 java

$ c时:Collection( )
$ listCustObjs:来自collect的ArrayList()(来自$ c的CustomObject())

$ first:$ listCustObjs.get(0)//<-像这样的
$ otherObj:$ first.other //<--从列表

的第一个对象中取出其他元素,然后从
...
结束;


解决方案

您需要将$ listCustObjs对象转换为List然后只有您将能够使用列表方法。使用#运算符可以在流口水中进行投射。在此处。。 p>

此外,您可以直接使用 $ first之类的语句:$ listCustObjs.get(0)。在流口水中,所有计算或条件检查都是在事实对象上完成的。因此,在您的情况下,仅当您尝试将其获取到对象内部时,才可以从$ listCustObjs列表中获取该值。


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;

解决方案

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.

这篇关于流口水在where子句中获得List的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆