【问题标题】:Drools: Accessing ArrayList as value in MapDrools:将 ArrayList 作为 Map 中的值访问
【发布时间】:2020-05-27 17:27:46
【问题描述】:

我正在尝试访问作为地图值的 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 值?

【问题讨论】:

    标签: java dictionary arraylist drools


    【解决方案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 可能会被提取为对象,这意味着您必须先转换为列表,然后才能使用 get(#) 方法.

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

    【讨论】:

    • 这是否意味着在 Map 和 Integer 之间只有一个 List() 行?
    • 像$heightList: ArrayList() from $height 这样的东西就足够了。一般来说,应该避免将地图作为规则输入......这在 Drools 4 中非常流行,并且已经被 Drools 5 劝阻。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多