我尝试了一个示例示例,以查看议程小组的工作方式。最初,我将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


这是我得到的输出。

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
...
...


我可以理解输出的前5行,直到“ GoodBye 2”为止。但是,由于焦点设置为“ ag2”,因此它如何回到议程组“ ag1”的“ GoodBye”规则,因此又重新出现。

谢谢。

最佳答案

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

就像这样(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
...


和循环重复。

如果您在Eclipse IDE中使用审核日志,这种行为非常容易发现。

希望这可以帮助。

关于drools - 了解流口水中的议程小组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6870192/

10-09 00:03