Drools检测从一个阵列到另一个阵列的多个匹配

时间:2016-02-18 18:32:31

标签: java drools

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

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

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

提前谢谢

1 个答案:

答案 0 :(得分:0)

你写了一个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函数可以很容易地扩展到数组。