0
votes

I have a question about 2 Drools attributes - salience and no-loop

salience:

rule "Hello World"
    salience -10
    when
        $m : Message( status == Message.HELLO, $myMessage : message )
    then
        System.out.println( $myMessage );
        $m.setMessage( "Goodbye cruel world" );
        $m.setStatus( Message.GOODBYE );
        update( $m );
end

rule "GoodBye"
    when
        Message( status == Message.GOODBYE, $myMessage : message )
    then
        System.out.println( $myMessage );
end

We should expect the "GoodBye" rule to be fired first(since its salience is higher) however that doesn't happen and instead the "Hello World" rule gets fired up first and only then "GoodBye"

no-loop:

I understand that this attribute prevent from a rule to be executed to the same fact which will cause an infinite loop. My question is about an example about this attribute that I don't quite understand:

rule "interest calculation"
no-loop
when
$account : Account( )
then
modify($account) {
setBalance((long)($account.getBalance() * 1.03));
}
end

If there wasn't "no-loop" why that will cause an infinite loop?

1

1 Answers

2
votes

Re salience: Logic always beats salience. If Message.status is initially set to Message.HELLO, the other rule doesn't qualify and "Hello World" is executed.

Re no-loop: A modify/update simply means that re-evaluation of everything begins from scratch as far as the modified fact is concerned. So, Account is updated, goes back to square one, and reevaluation creates another activation of this trivially matching rule.