2016-05-17 87 views
0

狀態機在選擇狀態內的評估返回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(); 
} 

以下是樣本從ou tput的捕獲:

工作項狀態變化到初始化

信息:開始S2 S1結束初始化S3 S1Choice S3Choice S2Choice /初始化/

在S1的選擇

正如你所看到的它在狀態 - 「s1選擇」中停頓,而不是轉到新的狀態 - 「s2選擇」。

回答

0

看起來你可能會使用1.0.x,它有鏈接的僞狀態問題(解決方法是在這些選擇之間放置正常狀態)。這些固定在1.1.x(1.1.0下週發佈)。

+0

是的。升級版本後,它工作正常!謝謝!! – ShankaraNarayanan