我是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在数组中可以有任意数量的值。
发布于 2018-05-07 20:56:29
过程式算法更容易在过程式语言中实现。我建议您用Java实现逻辑(在类中或作为DRL中的函数),并从您的规则的LHS中调用该逻辑。
https://stackoverflow.com/questions/50209587
复制相似问题