如何编写规则来匹配两个数组?

时间:2018-05-07 08:00:46

标签: java drools

我是新来的流口水所以非常感谢一些帮助。我有两个json文件,我必须应用一些规则。我已将json转换为java类,并将类插入到规则引擎中。

我必须编写一个规则来检查两个json文件中存在的数组是否相等。 InputFile和OutputFile分别是两个json的类。

假设第一个json中的数组包含[6,4,8],第二个json中的数组也包含[6,4,8],那么我编写的规则是为了检查数组是否相等

rule "EqualArray"
when
    $arrInInput : InputFile($inputArr : inputArray)
    $arrInOutput : OutputFile($outputArr : outputArray == $inputArr)
then
    System.out.println($inputArr+" and "+$outputArr+" Rule passed");
end

代码的输出是

6 and 6 Rule passed
4 and 4 Rule passed
8 and 8 Rule passed

我编写的规则是检查数组是否不相等

rule "Not EqualArray"
when
    $arrInInput : InputFile($inputArr : inputArray)
    $arrInOutput : OutputFile($outputArr : outputArray != $inputArr)
then
    System.out.println($inputArr+" and "+$outputArr+" Rule failed");
end

此规则的输出为

6 and 4 Rule failed
6 and 8 Rule failed
4 and 6 Rule failed
4 and 8 Rule failed
8 and 6 Rule failed
8 and 4 Rule failed

由于两个数组都相等,我不希望触发“Not EqualArray”规则。 我想要的是编写规则,以便我可以通过索引检查数组值。例如,如果两个数组的索引1,索引2和索引3匹配,则数组相等,如果数组的值与特定索引不匹配,则触发规则“Not EqualArray”。我也无法控制数组的大小。 json可以在数组中包含任意数量的值。

1 个答案:

答案 0 :(得分:0)

程序算法更容易用过程语言实现。我建议你在Java中实现逻辑(在类中或作为DRL中的函数),并从规则的LHS中调用该逻辑。