我尝试了一个示例来了解议程组的工作原理。最初我将 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”议程组的“再见”规则并因此递归。
谢谢。
最佳答案
议程组就像一个堆栈。当您将焦点设置到给定的议程组时,该组将放置在堆栈顶部。当引擎尝试触发下一个激活并且给定组中没有更多激活时,该组将从堆栈顶部移除,并且其下方的组再次获得焦点。
所以它是这样的(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/