Goozo
Goozo

Reputation: 956

How to check the size of a set in drools rules

I have the following rule in drool

rule klFor2001 salience 30
when
 exists( Vgdokfag( Fagkode() == "FOR2001", ( Merknadkode() != "FAM13" ||
                   Merknadparameter() in ("FO1010", "FO1030", "FO1040"))))
 vgs: HashSet() from collect(Vgdokfag( Fagkode() contains "UPF",
                                 (Merknadkode() == "FAM13" ||
                                 Merknadparameter() in ("FO1010","FO1030", "FO1040"))) )
                                      
then
  insert(getKlIdr20xx("KL_IDR2001",true));
end

So what I am trying to do is to

  1. check if there is a Vgdokfag with For2001 as Fagkode and with either Merknadkode not equal to "FAM13" or Merknadparameter is one of the following strings ("FO1010","FO1030", "FO1040").
  2. check that there exist at least two VgdokFag facts which has "UPF" in their "Fagkode" and either Merknadkode is equal to "FAM13" or Merknadparameter is one of the following strings ("FO1010","FO1030", "FO1040").

I do not know how to do the second one. I am trying to get all facts with the given conditions and then some how check if the size of the set is bigger than 2, but for some reason I cannot write something like this

eval(vgs.size() > 2)

I am sure that I am doing something wrong here, can someone please help me.

cheers,

es

Upvotes: 0

Views: 380

Answers (1)

Esteban Aliverti
Esteban Aliverti

Reputation: 6322

You were really close. You can add the size constraint straight into the HashSet:

vgs: HashSet(size >= 2) from collect(...)

Upvotes: 3

Related Questions