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
loading...
Related posts:
- Drools – Working with Stateless Session
Drools (now it is also called JBoss Rules) is an amazing open source framework which allows you to create business rules management system... - Drools – Tutorial on Writing DSL Template
Few months ago I wrote a post that describes an example that uses source DRL in conjunction with DSL template. In the current... - Rule Engine Stress Testing
I came across a blog by a company called Illation. What those guys do is compare performance of several rule engines available on... - JBoss Clustering – Shared State Across Cluster Partition
Did you know that if you have a JBoss cluster, HA singletons service beans on each can share a common memory state? State... - Brainteaser Drools: Testing Objects
This can be a hard one, since it requires from you to be familiar with Drools. Consider the condition side of the following...
