在drools中理解议程小组

Man*_*ani 15 drools

我尝试了一个示例,看看议程小组的工作原理.最初,我将ksession的重点放在议程组"ag1"上,并解除了规则.

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
Run Code Online (Sandbox Code Playgroud)

这是我得到的输出.

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
...
...
Run Code Online (Sandbox Code Playgroud)

我能理解输出的前5行直到"GoodBye 2".但由于焦点被设定为"ag2",它是如何回到"ag1"议程组的"GoodBye"规则并因此再次出现.

谢谢.

Eds*_*lli 23

议程小组像堆栈一样工作.将焦点设置为给定议程组时,该组将置于堆栈顶部.当引擎尝试触发下一次激活并且给定组中没有更多激活时,该组将从堆栈顶部移除,并且其下方的组再次获得焦点.

所以它是这样的(main是始终存在的默认组):

* 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
...
Run Code Online (Sandbox Code Playgroud)

循环重复.

如果在Eclipse IDE中使用审计日志,则很容易看到这种行为.

希望这可以帮助.