Drools 检测从一个数组到另一个数组的多个匹配项

Drools detect multiple matches from one array to another

我在对象颜色中有两个数组。

private String[] colorArray = {"blue", "red", "yellow", "green"};
private String[] myColors = {"blue", "red", "purple"};

我希望能够检测到另一个数组中是否有多个匹配项。

rule "check if inside array"
    when
        Colors($colors: getMyColors())
        $color: String() from $colors
        Colors(getColorArray() contains $color)
    then
        System.out.println("Color found once in array");
end

所以对于我的示例,我想检测是否在 colorArray 中找到 "blue" 和 "red"。我不知道是否有办法用 LHS 自动增加变量,这样我就可以检测到找到的 2 种颜色。

提前致谢

你写了一个 DRL 函数:

function int cardInter( Collection a, Collection b ){
    ArrayList x = new ArrayList( a );
    x.retainAll( b );
    return x.size();
}

并称它为:

rule "check if inside array"
when
    Colors($a: myColors, $b: colorArray )
    eval( cardInter( $a, $b ) > 1 )
then
    System.out.println( "Color found more than once" );
end

大多数时候,数组比任何集合都更难。请注意,DRL 函数可以很容易地扩展到数组。