流口水地理解议程组
我尝试了一个示例来了解议程组是如何工作的。最初,我将 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”议程组的“再见”规则并因此递归的。
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
议程组的工作方式就像一个堆栈。当您将焦点设置到给定议程组时,该组将被放置在堆栈的顶部。当引擎尝试触发下一个激活并且给定组中不再有激活时,该组将从堆栈顶部删除,并且其下方的组再次获得焦点。
所以它是这样的(main 是始终存在的默认组):
并且循环重复。
如果您使用 Eclipse IDE 中的审核日志,这种行为很容易看到。
希望这有帮助。
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):
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.
由于您在计算规则期间更改了会话中的事实(我猜您的 Message 对象在您的事实中),因此会再次计算其他规则,而不取决于它们所属的议程组,以便更新 Drools 知识库。
您可以添加
no-loop true
来防止在rule
定义之后出现这种情况,我不太确定,但这是我在我的应用程序上注意到的行为,应该解决你的无限循环。顺便说一句,当事实发生变化时重新计算规则似乎是合乎逻辑的。
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 afterrule
definitionI'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.