Drools – Stop Executing Current Agenda Group and All Rules

Sometimes, depends on your business rules in your application, there is a need to stop current agenda group or all rules from continuing to execute. It wont help setting a focus to another agenda group, since previous agenda will still remain in a stack. So in this post I want to show how to prevent rules in a particular agenda group from continuing to execute by clearing the agenda and also how to stop all rules totally.

Please note, in this example I am using agenda-groups to drive execution flow.

I created a short DRL file and a DSL template to demonstrate how clearing agenda and stopping of all rules can be achieved.

The DRL file:

expander template.dsl;

/*****************************************
 First default rule to kick in
******************************************/
rule "baserule"
salience 100000
auto-focus true
agenda-group "first-group"
when
      True
then

end

/*****************************************
 Second rule to kick in
******************************************/
rule "1"
salience 1000
agenda-group "first-group"
when
      True
then
      Goto agenda "second-group"
end

/*****************************************
 Third rule to kick in
******************************************/
rule "2"
salience 1000
agenda-group "second-group"
when
      True
then
      Do something
end

/*****************************************
 Fourth rule to kick in

 Current rule stops agenda "second-group"
 from continuing to execute, and the
 focus will be returned back to
 agenda "first-group"
******************************************/
rule "3"
salience 900
agenda-group "second-group"
when
      True
then
      Stop agenda "second-group" //clear current agenda
end

/*****************************************
 This rule never kicks in, since agenda
 "second-group" was cleared by rule "3"
******************************************/
rule "4"
salience 800
agenda-group "second-group"
when
      True
then
      Do something
end

/*****************************************
 Fifth rule to kick in

 After clearing agenda "second-group", the
 focus will be returned here
******************************************/
rule "5"
salience 900
agenda-group "first-group"
when
      True
then
      Do something
end

/*****************************************
 Sixth rule to kick in
 Current rule stops execution of all rules
******************************************/
rule "6"
salience 800
agenda-group "first-group"
when
      True
then
      Stop all
end

/*****************************************
 This rule never kicks in, since all rules
 execution was stopped by rule "6"
******************************************/
rule "7"
salience 700
agenda-group "first-group"
when
      True
then
      Do something
end

The DSL template:

[when]True=eval(true)
[then]Do something=
      System.out.println("Doing something");
[then]Stop all=drools.halt();
[then]Goto agenda "{agenda}"=
      drools.setFocus( "{agenda}" );
[then]Stop agenda "{agenda}"=
      drools.getWorkingMemory().
		clearAgendaGroup("{agenda}");

Basically what happens is: once the execution reaches rule “3″, I am calling for clearAgendaGroup() method to clear agenda, hence to remove it from the stack. As a result of the clearing, rule “4″ will never be executed.

When focus is returned back to agenda “first-group”, it lands on rule “5″. When the execution reaches rule “6″, I am calling for drools.halt() method to stop execution of all rules.

Keep in mind that in Drools 3 there is no halt() method. It came with Drools 4. If you want to achieve the above result of stopping all rules in Drools 3, you can call for drools.clearAgenda();

You noticed that I am using “drools” object and calling some methods on that object in my DSL template. Well “drools” object is actually KnowledgeHelper class, which is part of Drools library. It provides several APIs, for example method halt(), setFocus() and getWorkingMemory().

Keep in mind that I did not actually tested these particular DRL and DSL, but this example is based on real scenario that I have tested.

Comments/questions/flames are welcomed

Ouch!

Yesterday one of the developers that I work with lost all his work that he has done for the past two months! Apparently what happened was that the poor bagger did not commit anything to CVS for the past two months. So yesterday for some mysterious reason he has overridden his local changes with the outdated copy of the source code from CVS and tried to recompile everything. I don’t know why he did it.

I think he was almost crying telling me about the amount of work that was lost. During the conversation, it became obvious to me that for his luck, he had somewhere all the classes JARed as a backup (from before he has overridden the changes).

When I heard it, I said “Dude… you can decompile the classes! You can get the source code back” …

Ohhh… the look on his face after I said it – priceless …

Marshalling VS Serialization

The two terms are very very similar, and often treated the same. Quite few responds on various forums contradict each other, which made me a bit confused. So to make it clear for my self I decided to summarize what i understood from the resources I have read on the Internet:

Serialization:

  1. Technique of converting a state of an object (or object instance in different words) into a byte stream.
  2. To send object over the network, it needs to be serialized first, thus converted into to a sequence of bytes.

Marshalling:

  1. Technique of encoding an object to send it over the network and decoding it on the other end. Which is stub and skeleton mechanism, where stub is the local client and skeleton is the remote server.
  2. Besides the object state, marshalling also records codebases.
  3. Marshalling is used for sending objects through sockets. Off course objects must be serialized serialized before sending over the network.
  4. Marshalling is a way to create byte stream so that the copy of the original object can be recreated on the other side. When object is “unmarshalled” on the other side, object’s class definitions are loaded.
  5. Marshalling is a form of serialization, thats why I think some people treat teh two the same.

Thats it… I think… Did I miss something?