[英]False evaluation in a choice resulting in state machine being paused
當選擇狀態內的評估返回false時,狀態機將暫停在選擇狀態,而不是移至下一個狀態。
代碼如下:
國家定義:
@Override
public void configure(StateMachineStateConfigurer<String, String>
states) throws Exception {
states
.withStates()
.initial("init")
.choice("S1Choice")
.state("S1")
.choice("S2Choice")
.state("S2")
.choice("S3Choice")
.state("S3")
.state("end");
}
過渡/選擇/動作:
@Override
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
transitions
.withExternal()
.source("init")
.target("S1Choice")
.event("start")
.and()
.withChoice()
.source("S1Choice")
.first("S1", new Guard<String, String>() {
public boolean evaluate(StateContext<String, String> context) {
System.out.println("In s1 choice");
/*Map<Object, Object> map = context.getExtendedState().getVariables();
return !map.containsKey("S1done");*/
return false;
}
})
.last("S2Choice")
.and()
.withLocal()
.source("S1")
.target("S2Choice")
.action(new Action<String, String>() {
public void execute(StateContext<String, String> context) {
Map<Object, Object> map = context.getExtendedState().getVariables();
System.out.println("Executing s1");
map.put("S1done", Boolean.TRUE);
}
})
.and()
.withChoice()
.source("S2Choice")
.first("S2", new Guard<String, String>() {
public boolean evaluate(StateContext<String, String> context) {
System.out.println("In s2 choice");
Map<Object, Object> map = context.getExtendedState().getVariables();
return !map.containsKey("S2done");
}
})
.last("S3Choice")
.and()
.withLocal()
.source("S2")
.target("S3Choice")
.action(new Action<String, String>() {
public void execute(StateContext<String, String> context) {
Map<Object, Object> map = context.getExtendedState().getVariables();
System.out.println("Executing s2");
map.put("S2done", Boolean.TRUE);
}
})
.and()
.withChoice()
.source("S3Choice")
.first("S3", new Guard<String, String>() {
public boolean evaluate(StateContext<String, String> context) {
System.out.println("In s3 choice");
Map<Object, Object> map = context.getExtendedState().getVariables();
return !map.containsKey("S3done");
}
})
.last("end")
.and()
.withLocal()
.source("S3")
.target("end")
.and()
.withLocal()
.source("end")
.target("init");
}
主班:
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(TasksConfig.class);
StateMachine<String, String> stateMachine = context.getBean(StateMachine.class);
stateMachine.start();
stateMachine.getExtendedState().getVariables().put("S1done", Boolean.TRUE);
stateMachine.sendEvent("start");
//stateMachine.stop();
}
以下是捕獲的輸出中的樣本:
工作項目狀態更改為init
INFO:開始S2 S1結束init S3 S1Choice S3Choice S2Choice / init /
在s1選擇中
如您所見,它停留在狀態-“ s1選擇”,而不是移動到新狀態-“ s2選擇”。
看起來您可能正在使用1.0.x,但鏈接的偽狀態存在問題(解決方法是將正常狀態置於這些選擇之間)。 這些已在1.1.x(下周發布1.1.0)中修復。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.