Search code examples
droolsinference

Inference not working


I'm relatively new to Drools. I have those rules :

import models.Demarche;
declare IsMarque
 demarche : Demarche
end 

rule "Add type Marque taxe"
salience 2
no-loop true
lock-on-active true
when
    $d : Demarche( typeDemarche == "Marque" )
then
    modify($d){
        listTaxes.put(14, 1)
    }
    insert( new IsMarque( $d ) );
    System.out.println("infer marque");
end

And :

rule "Add Marque Polynesie extention taxe"
no-loop true
lock-on-active true
when
    $d : Demarche( extPolynesie == true)
    IsMarque(demarche == $d)
then
    $d.listTaxes.put(17, 1);
    System.out.println("marque");
end



rule "Add Not Marque Polynesie extention taxe"
no-loop true
lock-on-active true
when
    $d : Demarche( extPolynesie == true)
    not(IsMarque(demarche == $d))
then

    System.out.println("not marque");
end

For the rules 2 or 3, nothing happens. One of them should be true, but nothing is printed, as if the infered IsMarque cannot be evaluated. If I comment the IsMaqrue evaluation this is working, i can see the messages printed in the console. Any idea?


Solution

  • This rule needs to be rewritten as

    rule "Add type Marque taxe"
    when
        $d : Demarche( typeDemarche == "Marque", $t:  listTaxes)  // I AM GUESSING HERE
        not IsMarque(demarche == $d)
    then
        modify($t){
            put(14, 1)
        }
        insert( new IsMarque( $d ) );
        System.out.println("infer marque");
    end
    

    And no no-loop true and lock-on-active true if you have shown everything there is.

    Note that you could write the first rule also as

    rule "Add type Marque taxe"
    when
        $d : Demarche( typeDemarche == "Marque")
    then
        $d.getListTaxes().put(14, 1);
        insert( new IsMarque( $d ) );
        System.out.println("infer marque");
    end
    

    if (and only if) no other rule as CEs referring to the listTaxes property. Since you have used this "dirty update" in rule "Add Marque Polynesie extention taxe", it seems to be OK.

    Please post complete rules - #1 doesn't compile.