簡體   English   中英

Spring State Machine Builder with child state 拋出錯誤 - “初始 state 未設置”

[英]Spring State Machine Builder with child state throws error - "Initial state not set"

我正在嘗試使用 SpringStateMachine Builder 從 yml 文件配置 state 機器。 它適用於狀態和轉換,但是當我嘗試包含一些子狀態時,出現以下異常。

org.springframework.statemachine.config.model.MalformedConfigurationException:初始 state 未設置在 org.springframework.statemachine.config.model.verifier.BaseStructureVerifier.verify(BaseStructureVerifier.java:59)〜核心 [spring-statemachine .RELEASE.jar:2.1.3.RELEASE] 在 org.springframework.statemachine.config.model.verifier.CompositeStateMachineModelVerifier.verify(CompositeStateMachineModelVerifier.java:43) ~[spring-statemachine-core-2.1.38.920108.78:920108.78 2.1.3.RELEASE] 在 org.springframework.statemachine.config.AbstractStateMachineFactory.getStateMachine(AbstractStateMachineFactory.java:173) ~[spring-statemachine-core-2.1.3.RELEASE.jar:2.1.3.RELEASE] 在 org.在 org.springframework.stat emachine.config.StateMachineBuilder$Builder.build(StateMachineBuilder.java:155)~[spring-statemachine-core-2.1.3.RELEASE.jar:2.1.3.RELEASE]

這是我的 yml 文件

states:
   initial: INITIAL_STEP
   end: END_STEP
   states:
      INITIAL_STEP : 
      SECOND_STEP : 
         - SECOND_STEP_ONE
         - SECOND_STEP_TWO
         - SECOND_STEP_THREE
      END_STEP :

   transList:
      -
         source: INITIAL_STEP
         target: SECOND_STEP
         event: INITIAL_STEP_UP
         action: initialAction
      -
         source: SECOND_STEP
         target: END_STEP
         event: SECOND_STEP
         action: secondAction

這是我的 Spring State 機器 Class

@Configuration
public class MyStateMachineEngine {
    private static Logger logger = LogManager.getLogger(MyStateMachineEngine.class);

    @Autowired
    private StateConfig stateconfig;

    @Bean(name = "authStateMachine")
    @SessionScope(proxyMode = ScopedProxyMode.TARGET_CLASS)
    public StateMachine<String, String> buildStateEngine() {
        Builder<String, String> builder = StateMachineBuilder.builder();

        try {
            if (stateconfig != null) {

                Map<String,List<String>> stateListMap = stateconfig.getStates();
                List<TransProp> transList = stateconfig.getTransList();

                final StateConfigurer<String, String> stateConfigurer = builder.configureStates().withStates()
                        .initial(stateconfig.getInitial()).end(stateconfig.getEnd());

                stateListMap.keySet().forEach(state -> configureState(state,stateListMap.get(state), stateConfigurer));
                logger.info(transList.size());
                transList.forEach(trans -> addTransaction(builder, trans));

                builder.configureConfiguration().withConfiguration().autoStartup(true).beanFactory(null)
                        .machineId("OSTMACHINE");
            } else {
                logger.error("State initialization error please verify state config");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return builder.build();
    }

    private void configureState(String state, List<String> childStateList, StateConfigurer<String, String> stateConfigurer) {
        stateConfigurer.state(state);
        if(!childStateList.isEmpty()) {
            childStateList.forEach(childState -> stateConfigurer.parent(state).initial(childState).state(childState));
        }
    }

    private void addTransaction(Builder<String, String> builder, TransProp transProp) {
        try {
            if (null != transProp) {
                if (StringUtils.isBlank(transProp.getGuard())) {
                    builder.configureTransitions().withExternal().source(transProp.getSource())
                            .target(transProp.getTarget()).event(transProp.getEvent());
                } else {
                    builder.configureTransitions().withExternal().source(transProp.getSource())
                            .target(transProp.getTarget()).guard(new BaseGuard()).event(transProp.getEvent());
                }
            }

        } catch (Exception e) {
            logger.error("Error when configuring states ", e);
            new RuntimeException(e);
        }
    }

}

當我在本地系統上運行您的代碼時,我發現了問題。 結果表明問題出在您的 configureState 方法中,您已在該方法中完成了 state 配置,應該是這樣的:-

  private void configureState(String state, List<String> childStateList,
      StateConfigurer<String, String> stateConfigurer) {
    stateConfigurer.state(state);
    if (!childStateList.isEmpty()) {
      childStateList.forEach(childState -> {
        try {
          stateConfigurer.and().withStates().parent(state).initial(childState).state(childState);
        } catch (Exception e) {
          e.printStackTrace();
        }
      });
    }
  }

有關更多信息,您可以參考鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM