具有子状态的Spring State Machine Builder引发错误-“未设置初始状态”

来源:爱站网时间:2021-09-25编辑:网友分享
我正在尝试使用SpringStateMachine Builder从yml文件配置状态机。它可以很好地与状态和过渡配合使用,但是当我尝试包含一些子状态时,我得到了...

问题描述


我正在尝试使用SpringStateMachine Builder从yml文件配置状态机。它可以很好地与状态和过渡配合使用,但是当我尝试包含一些子状态时,出现了以下异常。

org.springframework.statemachine.config.model.MalformedConfigurationException:未设置初始状态在org.springframework.statemachine.config.model.verifier.BaseStructureVerifier.verify(BaseStructureVerifier.java:59)〜[spring-statemachine-core-2.1.3.RELEASE.jar:2.1.3.RELEASE]在org.springframework.statemachine.config.model.verifier.CompositeStateMachineModelVerifier.verify(CompositeStateMachineModelVerifier.java:43)〜[spring-statemachine-core-2.1.3.RELEASE.jar: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.springframework.statemachine.config.AbstractStateMachineFactory.getStateMachine(AbstractStateMachineFactory.java:143)〜[spring-statemachine-core-2.1.3.RELEASE.jar:2.1.3.RELEASE]在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 State Machine类

@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 buildStateEngine() {
        Builder builder = StateMachineBuilder.builder();

        try {
            if (stateconfig != null) {

                Map> stateListMap = stateconfig.getStates();
                List transList = stateconfig.getTransList();

                final StateConfigurer 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 childStateList, StateConfigurer stateConfigurer) {
        stateConfigurer.state(state);
        if(!childStateList.isEmpty()) {
            childStateList.forEach(childState -> stateConfigurer.parent(state).initial(childState).state(childState));
        }
    }

    private void addTransaction(Builder 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方法中,在该方法中已完成状态配置,应该是这样的:-

  private void configureState(String state, List childStateList,
      StateConfigurer 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

上一篇:在活动之间保存SeekBar,Android

下一篇:无法反序列化对象。没有定义无参数的构造函数。如果您使用的是ProGuard,请确保未剥离这些构造函数[重复]

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载