Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

understanding agenda-group in drools

Tags:

drools

I tried a sample example to see how agenda-group works. Initially I set the focus of ksession to agenda-group "ag1" and fired the rules.

package com.sample

import com.sample.DroolsTest.Message;

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

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

rule "GoodBye"
  agenda-group "ag1"
    when
        m : Message( status == Message.GOODBYE, myMessage : message )
    then
        System.out.println( "GoodBye" );
        drools.setFocus("ag2");
        System.out.println("comeon man");
        m.setStatus(com.sample.DroolsTest.Message.HELLO);
        update(m);
end

rule "GoodBye 2"
  agenda-group "ag2"
    when
        Message( status == Message.GOODBYE, myMessage : message )
    then
        System.out.println( "GoodBye 2" );
end

This is the output I got.

Hello World
GoodBye
comeon man
Hello World 2
GoodBye 2
GoodBye
comeon man
Hello World 2
GoodBye 2
GoodBye
comeon man
Hello World 2
GoodBye 2
GoodBye
comeon man
Hello World 2
GoodBye 2
GoodBye
comeon man
Hello World 2
...
...

I could understand the first 5 lines of the output till "GoodBye 2". But since the focus was set to "ag2", how did it go back to "ag1" agenda-group's "GoodBye" rule and hence recursed.

Thanks.

like image 246
Manish Mulani Avatar asked Jul 29 '11 07:07

Manish Mulani


People also ask

What is agenda-group in drools?

By default “MAIN” agenda-group is pushed to drools stack using “PRIORITY” or “SALIENCE” and then while execution they pop one at a time and execute. Quote from documentation — “Agenda groups are known as “modules” in CLIPS terminology.

What are the different execution control statements in drools?

In this tutorial we will try to understand the execution controls statements in Drools agenda-group, ruleflow-group and activation-group.

How do you call one rule from another rule in drools?

Well, the answer is we can't call a Rule from another Rule. Drools matches the Rules with incoming data/facts, and if the data satisfies the Rule condition, it stores the data in an Agenda. It might be possible for the same data or facts to be matched by different rules, so it stores matching facts in an Agenda.

What is drools BRMS?

Drools is a business rule management system (BRMS) with a forward and backward chaining inference based rules engine, more correctly known as a production rule system, using an enhanced implementation of the Rete algorithm.


2 Answers

Since you changed facts in the session (your Message object is in your facts i guess) during the computation of a rule, other rules are computed again, not depending on the agenda-group they belong to, in order to update the Drools knowledge base.

You could add no-loop true to prevent this on the line after rule definition

I'm not quiet sure, but it's the behavior i noticed on my app and should so resolve your infinite loop. By the way, it seems logical to compute again rules when facts change.

like image 45
Nicholas Avatar answered Oct 21 '22 15:10

Nicholas


Agenda groups work like a stack. When you set the focus to a given agenda group, that group is placed on top of the stack. When the engine tries to fire the next activation and there are no more activations in a given group, that group is removed from the top of the stack and the group below it receives the focus again.

So it goes like this (main is the default group that is always present):

* STACK: [MAIN, ag1]

Hello Word fires and activates both "GoodBye" rules
GoodBye fires, activates both "Hello World" rules and sets the focus to "ag2"

* STACK: [MAIN, ag1, ag2]

Hellow World 2 fires, cancels the "Hello World 1" rule and activates both "GoodBye" rules
GoodBye 2 fires because ag2 has the focus

* There are no more activations in ag2 to fire, so ag2 is removed from the stack
* STACK: [MAIN, ag1]
* The "GoodBye" rule is still active in ag1, so it fires

GoodBye fires, activates both "Hello World" rules and sets the focus to "ag2"

* STACK: [MAIN, ag1, ag2]

Hellow World 2 fires, cancels the "Hello World 1" rule and activates both "GoodBye" rules
...

And the loop repeats.

This kind of behavior is very easy to see if you use the audit log in the Eclipse IDE.

Hope this helps.

like image 195
Edson Tirelli Avatar answered Oct 21 '22 16:10

Edson Tirelli