Spring 带有子状态的状态机生成器抛出错误 - "Initial state not set"

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

我正在尝试使用 SpringStateMachine Builder 从 yml 文件配置状态机。 它适用于状态和转换,但是当我尝试包含一些子状态时,出现以下异常。

org.springframework.statemachine.config.model.MalformedConfigurationException: Initial state not set at org.springframework.statemachine.config.model.verifier.BaseStructureVerifier.verify(BaseStructureVerifier.java:59) ~[spring-statemachine-core-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.statemachine.config.model.verifier.CompositeStateMachineModelVerifier.verify(CompositeStateMachineModelVerifier.java:43) ~[spring-statemachine-core-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.statemachine.config.AbstractStateMachineFactory.getStateMachine(AbstractStateMachineFactory.java:173) ~[spring-statemachine-core-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.statemachine.config.AbstractStateMachineFactory.getStateMachine(AbstractStateMachineFactory.java:143) ~[spring-statemachine-core-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.statemachine.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状态机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();
        }
      });
    }
  }

更多信息请参考Link