如何使用流口水从自定义对象列表中查找任何属性的值,然后将其存储到变量

How to find value of any attribute from List of Custom Object using drool and then store it to variable

我有一个 POJO 实现:

public class Product{
String name;
String brand;
String company;
}

我的问题::

需要根据名称和公司从 worldOfProductList 中获取预期的产品(这两个字段都是已知的,以便可以应用过滤器)。 从该列表中获取一个特定产品后,需要将 "brand" 值存储到某个变量(可以是全局变量)中以供将来使用。

注意::截至目前,使用该过滤器只能获得唯一的产品。

我的方法是这样的:

[condition][]Get intended Product from list = $productList: java.util.LinkedList() from collect(Products(name="abc",company="com") from worldOfProductList)

这条语句运行正常,能够在 sysout 语句中获取我的值:

System.out.println("Brand is "+((Product)($productList.get(0)).getBrand());

但我需要在 DSL 中使用这个品牌值并存储到变量中。我尝试了以下组合但没有成功。

1.[condition][] Store Brand into variable=$brandVar: $productList[0].brand

2.[condition][] Store Brand into variable=Product($brandVar: $productList[0].brand)

3.[condition][] Store Brand into variable=$brandVar: ((Product)$productList.get(0)).getBrand()

如果你想在你的 DSL 中将它作为 2 个单独的条目,那么你可以这样做:

[condition][] Store Brand into variable= Product($brandVar: brand) from $productList

这将激活您在 $productList 中拥有的每个不同产品的规则。在规则的 RHS 中,您将有权访问 $brandVar

如果你只对品牌感兴趣,对产品本身不感兴趣,那么你也可以试试这样的:

[condition][]Get Brands from intended Product from list = $productList: java.util.Set() from accumulate(Products(name="abc",company="com", $brandVar: brand) from worldOfProductList, collectSet($brand))

希望对您有所帮助,