2011-07-29 4 views
14

나는 의제 그룹이 어떻게 작동하는지보기 위해 샘플 예제를 시도했다. 처음에 ksession의 초점을 agenda-group "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"로 설정 되었기 때문에 어떻게 ag1a 그룹의 "GoodBye"규칙으로 돌아가 다시 돌아 왔는가?

감사합니다.

답변

22

일정 그룹은 스택처럼 작동합니다. 특정 의제 그룹에 포커스를 설정하면 해당 그룹이 스택 맨 위에 배치됩니다. 엔진이 다음 활성화를 시작하려고 시도하고 주어진 그룹에 활성화가 더 이상 없으면 그 그룹은 스택 맨 위에서 제거되고 그 아래 그룹은 다시 포커스를받습니다.

* 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에서 감사 로그를 사용하면 이러한 종류의 동작을 매우 쉽게 볼 수 있습니다.

희망이 도움이됩니다.

+0

이 감사 로그를 활성화하는 방법은 무엇입니까? –

+0

session.addEventListener (new DebugAgendaEventListener())를 살펴보십시오. – Topera

1

규칙을 계산하는 동안 세션에서 사실을 변경 했으므로 (메시지 객체는 귀하의 사실에 맞춰 짐) 규칙을 계산하는 동안 다른 규칙이 속한 의제 그룹에 의존하지 않고 다시 계산되어 업데이트됩니다 Drools 지식 기반.

당신은 rule 정의

나는 확실히 조용히 아니에요 후 라인에이를 방지하기 위해 no-loop true을 추가 할 수 있습니다,하지만 난 내 응용 프로그램에 발견하고 그래서 당신의 무한 루프를 해결해야하는 행동입니다. 그런데 사실이 바뀌면 규칙을 다시 계산하는 것이 논리적 인 것처럼 보입니다.