Drools:比较来自同一对象的 2 个数组列表

Drools: Compare 2 Arraylists from the same Object

我有一个 Class 并且这个 Class 构造了 7 个 ArrayLists:

public class Fruchtplanungsmodul {

    private ArrayList<Crops> fruchtliste1F;
    private ArrayList<Crops> fruchtliste2F;
    private ArrayList<Crops> fruchtliste3F;
    private ArrayList<Crops> fruchtliste4F;
    private ArrayList<Crops> fruchtliste5F;
    private ArrayList<Crops> fruchtliste6F;
    private ArrayList<Crops> fruchtliste7F;

    // Constructor
    public Fruchtplanungsmodul() {
        fruchtliste1F = new ArrayList<>();
        fruchtliste2F = new ArrayList<>();
        fruchtliste3F = new ArrayList<>();
        fruchtliste4F = new ArrayList<>();
        fruchtliste5F = new ArrayList<>();
        fruchtliste6F = new ArrayList<>();
        fruchtliste7F = new ArrayList<>();
    }
    functions for deleting Objects....
}

而且我想使用 Drools 规则在此列表中加载对象。 我确实将相同的 Objecttyps 加载到所有 Arraylists 中。例如,在这个规则中,我将 4 个 Objects Crops 加载到第一个 ArrayList 中。

rule "Körnerlegmunosen: Planung erste Feldrigkeit"
    when
        //$grund:         Grundbedingung(grundbedingung == 1)
        $feld:          Feldrigkeit(feldrigkeit1 == "Körnerleguminosen")
        $m:             Fruchtplanungsmodul()

    then

        Crops erbse = new Crops("Erbse", "Koernerleguminose","BF", "Hafer", "Silomais", "Sommerung", 6);
        Crops ackerbohne = new Crops("Ackerbohne" , "Koernerleguminose", "BF", "Silomais", "Wintergerste", "Sommerung", 4);
        Crops lupine = new Crops("Lupine", "Koernerleguminose", "BF", "Späte Kartoffel", "Winterroggen", "Sommerung", 4);
        Crops sojabohne = new Crops("Sojabohne", "Koernerleguminose", "BF", "Futterrübe", "winterroggen", "Sommerung", 3);

        insert(erbse);
        insert(ackerbohne);
        insert(lupine);
        insert(sojabohne);
        $m.addFrucht1(erbse);
        $m.addFrucht1(ackerbohne);
        $m.addFrucht1(lupine);
        $m.addFrucht1(sojabohne);
end

在其他规则中,我从 Class Fruchtplanungsmodul() 将不同的 Crops 加载到其他 ArrayLists。

我的问题是:有什么方法可以比较来自不同 ArrayList 的对象吗?

例如,ArrayList "fruchtliste2F" 有 4 个 Crops 类型的对象,ArrayList "fruchtliste3F" 也有 4 个 Crops 类型的对象。现在我需要检查一个规则是否在两个 ArrayLists 中有一个具有相同名称的对象。如果这是真的,规则应该从第二个列表中删除该对象。

感谢您的帮助! 菲利普

如果你要检查的数组是固定的(即你总是想检查数组#2 和数组#3),那么你可以这样做:

rule "Delete duplicated"
when 
  $f: Fruchtplanungsmodul()
  $c1: Crop() from $f.fruchtliste2F
  $c2: Crop(name == $c1.name) from $f.fruchtliste3F
then
  //If you want to remove the fact you have inserted too:
  delete($c2);

  //Remove the object from the array
  $f.deleteFrucht3($c2);
end

如果你想检查所有的数组,那么你要么创建上述规则的副本,要么创建一种方法让你的 java class 通过索引访问数组并创建一个通用的比较它们的规则。

希望对您有所帮助,