Search code examples
drools

Rules fired upon fact insert - Drools


I am very new to Drools, and started with the basics. Here is a setup;

  1. Have a few rule files
  2. Rules in different files belong to different Agenda groups
  3. All the rules operate on the same fact
  4. In my Unit Test, I obtain a particular "Agenda Group", set Focus (session.setFocus)
  5. I insert the fact (Here is where I notice the rules within the other agenda group are getting evaluated even though the focus is not on them)

  6. I fireAllRules()

Shouldn't the rules pertaining to the particular agenda group that is in focus be fired and not all rules from all the groups?


Solution

  • It is a fundamental law in many Rule Based Systems - especially those that follow Rete or similar algorithms - that the evaluation of conditions ("t when", "left hand side") happens whenever there are changes in Working Memory: inserts, updates or deletes. In contrast, rule firing or the execution of consequences ("then", "right hand side") happens after fireAllRules or fireUntilHalt has been called.

    The firing of rules - more precisely: of activations of rules - can be controlled in several ways. Here, it is indeed the focussed agenda group that limits what can be executed by the engine.

    Keep this in mind: evaluation is not equal to execution.