【问题标题】:How can I reset state machine to sub state machine?如何将状态机重置为子状态机?
【发布时间】:2022-01-02 06:29:08
【问题描述】:

你能帮我解决这个问题吗?

我有员工{ Start, S1 , S1_1_start, S1_1_end , S1_2_start, S1_2_end, S2, End } 的状态。

public void configure(StateMachineStateConfigurer<EmployeeStates, EmployeeEvents> states) throws Exception {
     states
     .withStates()
         .initial(EmployeeStates.Start)
         .fork(EmployeeStates.FORK)
         .state(EmployeeStates.S1)
         .join(EmployeeStates.JOIN)
         .state(EmployeeStates.S2)
         .end(EmployeeStates.End)
     .and()
     .withStates()
         .parent(EmployeeStates.S1)
         .initial(EmployeeStates.S1_1_start)
         .end(EmployeeStates.S1_1_end)
     .and()
     .withStates()
         .parent(EmployeeStates.S1)
         .initial(EmployeeStates.S1_2_start)
         .end(EmployeeStates.S1_2_end);
}

我需要将机器状态重置为从 [S1, , S1_1_start, S1_2_end] 开始,并将我的事件添加到 S2

这是重置我的机器的正确方法吗?

StateMachine<EmployeeStates, EmployeeEvents> sm = this.factory.getStateMachine();
sm.stopReactively().subscribe();
sm.getStateMachineAccessor().doWithAllRegions(sma -> {
    sma.addStateMachineInterceptor(employeeStateChangeInterceptor);
    StateMachineContext<EmployeeStates,EmployeeEvents> ctx1 = new DefaultStateMachineContext(EmployeeStates.S1_1_start,null,null,null,null);
    StateMachineContext<EmployeeStates,EmployeeEvents> ctx2 = new DefaultStateMachineContext(EmployeeStates.S1_2_end,null,null,null,null);
    List<StateMachineContext<EmployeeStates,EmployeeEvents>> list = List.of(ctx1,ctx2);
    sma.resetStateMachineReactively(new DefaultStateMachineContext<EmployeeStates, EmployeeEvents>(list,EmployeeStates.S1, null,null, null,null)).subscribe();
});

在日志中我看到它是错误的,它首先将状态更改为预期状态,但随后又为子状态再次执行此操作:(

state changed from [S1, , S1_1_start ,S1_2_end ]  to ObjectState [S2]
state changed from [ S1_1_start ]  to ObjectState [S1_1_end]

【问题讨论】:

  • 只是一个提示...这里很多可以帮助您解决问题的人都是专业人士。 “Can u”和“Wanna”不是专业用语。它们会损害您获得答案的机会。也许英语不是你的主要语言。如果是这样,不完美的单词选择是可以理解的。但仍然值得学习正确的形式。
  • 对不起,我的英语不好,这不是我的母语
  • 不需要道歉。我不能像你那样用英语以外的任何语言发帖。正如我所说,这只是一个小费......
  • 我只知道我在过去 15 分钟内读到的有关 Spring 状态机的内容,所以这是一个猜测。看起来您的逻辑正在将您设置的 both 上下文应用到 each 区域。您可能想尝试向区域添加名称(使用.region() 方法)。然后使用这些来确定在每种情况下应用哪个上下文。无论如何,builder API 不是最好的,文档也不清楚。我希望你能得到答案。

标签: java spring spring-statemachine


【解决方案1】:

这就是我以非反应方式从一些数据中重新水化(恢复)状态机的方式

public static StateMachine restoreMachine(
      final StateMachine<States, Events> stateMachine,
      final States currentState,
      final ExtendedState extendedState) {
    stateMachine
        .getStateMachineAccessor()
        .doWithAllRegions(
            sma ->
                sma.resetStateMachine(
                    new DefaultStateMachineContext<>(
                        currentState,
                        Events.E1,
                        null,
                        extendedState,
                        null,
                        "1")));
    return stateMachine;
  }

您希望机器恢复到哪个状态?我的猜测是恢复复合状态可能会有所不同。还注意到您每次恢复机器时都设置了拦截器?也许那是不必要的

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多