有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Drools:作为映射中的值访问ArrayList

我试图访问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函数不是type对象的一部分。如何获取arraylist值


共 (1) 个答案

  1. # 1 楼答案

    假设你的课程看起来像这样:

    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提取为对象,这意味着您必须先转换为list,然后才能使用get(#)方法

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