Search code examples
droolsbusiness-rules

how to make a list after comparing two Lists


I'm trying to collect some objects in drools using this code:

rule "Evalua Anexo 10" salience 300 when
  jul: ArchJuliano()
  reg551: Registro551( fraccAne10 : getFraccion() ) from jul.getListReg551() 
    exists ( Anexo10( getFraccion() == fraccAne10 ) from jul.getListFraccAne10() )
then
  // get pojo reg551   
end

The above rule will fire once for each Registro551 when fraccAne10 exists in jul.getListFraccAne10. However, I want to get a list of Registro551 instead only the object.

rule "Evalua Anexo 10" salience 300 when
  jul: ArchJuliano()
  listOfReg551: List() from collect (
   Registro551( fraccAne10 : getFraccion() ) from jul.getListReg551() 
   exists ( Anexo10( getFraccion() == fraccAne10 ) from jul.getListFraccAne10() )
  )
then
  // trying to get List<Registro551>
  // fires error: mismatched input 'exists' in rule name-of-rule
end

facts:

public class Anexo10 {
  private String fraccion;
  // getters and setters

public class Registro551 {
  private String fraccion;
  // getters and setters

public class ArchJuliano {
  private List<Anexo10> listFraccAne10;
  private List<Registro551> listReg551;
  // getters and setters

thank you very much.


Solution

  • rule "Evalua Anexo 10"
    when
      jul: ArchJuliano( $lAne: listFraccAne10, $lReg: listReg551 )
      accumulate( Anexo10( $fA: fraccion ) from $lAne;
                  $fraccionsA: collectSet($fA) )
      accumulate( Registro551( $fR: fraccion memberOf $fraccionsA ) from $lReg;
                  $fraccionsR: collectList($fR) )
    then
      ...List<Registro551> $fraccionsR...
    end
    

    (I haven't tested this.)