gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:29:15 26 4
gpt4 key购买 nike

我是流口水的新手,所以我们将不胜感激。我有两个 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 数组中可以有任意数量的值。

最佳答案

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

关于java - 如何编写规则来匹配两个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50209587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com