【问题标题】:Drools: Compare 2 Arraylists from the same ObjectDrools:比较来自同一对象的 2 个数组列表
【发布时间】:2019-03-18 15:25:05
【问题描述】:

我有一个类,这个类构造了 7 个 ArrayList:

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-rules 加载这个列表中的对象。 我确实将相同的 Objecttypes 加载到所有 Arraylist 中。例如,在这条规则中,我将 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

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

我的问题是:有没有办法比较来自不同 ArrayLists 的对象?

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

感谢您的帮助! 菲利普

【问题讨论】:

    标签: java arraylist compare drools


    【解决方案1】:

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

    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 类通过索引访问数组并创建一个通用规则来比较它们。

    希望对你有帮助,

    【讨论】:

      猜你喜欢
      • 2022-11-19
      • 1970-01-01
      • 2023-03-10
      • 2022-06-26
      • 2018-01-25
      • 1970-01-01
      • 2017-05-12
      • 2022-01-11
      • 2022-10-16
      相关资源
      最近更新 更多