Stop Drools from re-evaluating a rule

1,124 views
Skip to first unread message

Neel

unread,
Aug 13, 2017, 12:21:37 PM8/13/17
to Drools Usage
Hello,

I have just started using Drools and already comes up against a tricky situation. Let me describe my use case in a very abstract way in order to formulate the question.

(i). If there is already an object objA of type A, create another obj2A of type A.
(ii).If there are more than one object of type A, create an object objB of type B.

I have two rules in my drl file checking the two situation in working memory ( rule1 and rule2 respectively). ( I have a check in .java side so this does not ends up in infinite loop). In the rules I am only using Drools query, and creating the object in java side.

So at first I create an objA of type A and put it in the working memory. Now I am firing the rules.

fire 1) There will be match for rule1 only, and I will create another object of type A and put in the working memory.

fire 2) Now the situation arrives where both of the rule matches.

          *Here is my question*. Drools will check the rule1 again at fire 2, but I don't want this. I want it to check only rule2 and inform me if there is more than one                            object of type A in working memory. Is there a way to do this?

           After some search I come up with AgendaFilter. But after reading about how AgendaFilter works, I have understood it will still check/evaluate both rules, and only                restrict rule from firing according to my choice of filter. Is my understanding correct ? 

Kindly suggest me how can I achieve this.  Also let me know if I should provide some additional details. 

Many thanks in advance. 
        

José Filipe Neis

unread,
Aug 14, 2017, 9:30:11 AM8/14/17
to Drools Usage
Neel,

You can control your execution flow using AgendaFilter, doing something like: execute Rule 2. If nothing fires, execute Rule 1. If Rule 1 fires, execute Rule 2 again. Even I believe it will solve your problem, I don't like this approach as part of your rule semantics will be controlled by the code calling the rules.

I thought about another way using salience that will probably work too (needs testing):
  1. Declare your Rule 2 with a smaller salience than Rule 1 (so it will be executed first);
  2. When Rule 2 fires, insert your objB in session, as you planned;
  3. In Rule 1, check there's and objA, but also NOT an objB. This way, Rule 1 will only fire if Rule 2 has not fired yet.
I'm not sure if salience will interfere somehow in rules reevaluation, but I don't believe so, so it should work fine.

Hope it helps!

Filipe

Neel

unread,
Aug 18, 2017, 2:29:52 PM8/18/17
to Drools Usage
Hi Filipe,

Thanks for your reply.

However I had some misunderstanding about how Drools fire the rules.

I did some (ugly) coding to confirm that as long as as the facts are same in the working memory, Drools will not fire the same rule twice ( if you are working with the same session).

So in my question, the statement I wrote : ' Drools will check the rule1 again at fire 2', - This is wrong ! I should have checked it before posting. 

But now I need another help from you people. And I am posting another question ( with that ugly coding).

Thanks ,
Neel.
Reply all
Reply to author
Forward
0 new messages