我有一个 POJO 实现:
public class Product{
String name;
String brand;
String company;
}
我的问题::
需要根据名称和公司从 worldOfProductList 中获取预期产品(这两个字段都是已知的,以便可以应用过滤器)。 从该列表中获取一种特定产品后,需要将“品牌”值(value)存储到某个变量(可以是全局变量)中以供将来使用。
注意::截至目前,使用该过滤器仅获取唯一的产品。
我的方法是这样的:
[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
中每个不同产品的规则。在规则的右侧,您将有权访问 $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))
希望对你有帮助
关于java - 如何使用 drool 从自定义对象列表中查找任何属性的值,然后将其存储到变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38107299/