我正在尝试访问ArrayList的元素,该元素是地图的值。

例如:

{“ height”:[-10,20]},我试图将单个值设为“ -10”,以便在when条件下进行比较。

现在我正在做:

rule "test"
when
    Params(tol: tolerance)    //recieving the Map
    eval( tol.get("height").get(0) < 0 )
then
   ...
end


它说get函数不是Object类型的一部分。我如何获得arraylist的值?

最佳答案

假设您的课程如下所示:

class Params {
  private Map<String, List<Integer>> tolerance;
  public Map<String, List<Integer>> getTolerance() { return this.tolerance; }
}


然后,您应该能够构建如下规则:

rule "test"
when
  // get the Tolerance map and assign to $tolerance
  Params( $tolerance: tolerance )

  // get the 'height' list from the $tolerance map, assign to $height
  Map( $height: this["height"] ) from $tolerance

  // Check if the first integer in the $height list is negative
  Integer( this < 0 ) from $height.get(0)
then
   ...
end


this[ key ]语法仅适用于地图。根据您配置Drools的方式以及所使用的Drools版本的年龄,可能会将$height提取为一个对象,这意味着您必须先转换为列表,然后再使用get(#)方法。 。

尽可能避免使用eval,因为Drools编译器无法优化这些调用。

10-08 01:22