在 Drools Expert 6.5 中对字符串进行计数和分组,使用 accumulate 或 collect 关键字

Counting and Grouping Strings in Drools Expert 6.5, with accumulate or collect keyword

如何使用 drools 规则计算列表中 Vs 的百分比。如果 V 的百分比为 80%,则触发流口水规则。

我有一个只有两个值的 ArrayList:V 和 N。

List<String> list = new ArrayList<>();

如何用drools规则统计V的个数,每个V和N都为1,得到V和N的总和

在java

  Map<String, Long> counted = list.stream()
             .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    System.out.println("Counted: "+counted);

上面的表达式打印

Counted: {V=4, N=15}

获取值后,会有一个drols规则表达式,如果出现80%,则计数V出现打印"ok"否则打印不允许。

这是一个计算列表中 Vs 百分比的规则:

rule "countVN"
when
  $vn: ArrayList( $s: size )
  accumulate( String( toString == "V" ) from $vn;
              $cnt: count( 1 );
              $cnt.doubleValue()/$s >= 0.8 )
then
  System.out.println( "80% or more" );
end

这仅在 >= 80% 的情况下触发。如果无论如何都需要结果,请使用

  ...
  accumulate( String( toString == "V" ) from $vn;
              $cnt: count( 1 ))
then
  if( $cnt.doubleValue()/$s >= 0.8 ) ...