Spring-Statemachine:访问 PseudoStates,被跳过

Spring-Statemachine: Access to PseudoStates, that are skipped

首先,我的主要目标是说明状态机图。

方法 StateMachineFactory<S, E>#getStateMachine() 构建了我的状态机的一个新实例。 StateMachine<S, E>有很多方法,比如getStates(): Collection<State<S, E>>getTransitions(): Collection<Transition<S,E>>

为了创建一个简单的状态机图,我需要所有与其转换相关的状态。上面的方法 getStates() 给了我所有的状态,但是方法 getTransitions() 没有给我所有的转换。

比如为了理解,我的意思。下面给出状态机图和对应的statemachinemachine

以下方法给出了我的结果:

如何从 Choice --> State_BChoice --> State_C 中获取剩余的转换?

Javdoc 将转换描述为状态机与状态更改关联的事物。这到底是什么意思?

最后但同样重要的是,我如何在不模拟整个触发事件的情况下通过我的状态机的状态和转换并记录守卫、转换和所有其他内容?

这更像是 3 个问题,而不是一个,但我们开始:

How can I get the remaining transistions from Choice --> State_B and Choice --> State_C ?

如果您想完全访问 StateMachineConfig,则必须查看 StateMachineModel。 一种方法是定义您自己的 StateMachineModelVerifier,调用 super(),然后从模型中读取您需要的任何内容:

@Override
public void configure(StateMachineConfigurationConfigurer<States, Events> config)
        throws Exception {
    config
        .withVerifier()
            .enabled(true)
            .verifier(cusomVerifier());
}

@Bean    
public StateMachineModelVerifier customVerifier() {
  return new CustomVerifier();
}

class CustomVerifier extends DefaultStateMachineModelVerifier {
  @Override
  public void verify(StateMachineModel model) {
    super.verify(model); // call the existing sanity check model verifier
    TransitionsData<S,E> transitions = model.getTransitionsData();
  }
}

通过TransitionsData你可以获得所有的选择等

The Javdoc describes a transitionas as something what a state machine associates with a state changes. What does this exactly mean?

进入选择或交汇点实际上不会触发状态转换。 这些是评估条件的伪状态,SM 根据这些条件决定下一步要转换到哪里。

例如在您的情况下,状态 A -> 选择 -> 状态 B 或状态 C

可能的转换是:

  • A -> B
  • A -> C

没有A -> 选择;选择 -> B;选择 -> C

Last but not least, how can i go through states and transistions of my state machine and logging the guards, transitions and all other stuff without mocking the whole triggering events?

我不完全理解你的问题,但请考虑:

  • 日志保护 - 在保护方法中实现日志记录;
  • 使用您自己的侦听器记录 SM 转换(参见下面的示例);

正在注册监听器

@Override
public void configure(StateMachineConfigurationConfigurer<States, Events> config) throws Exception {
  config.withConfiguration().listener(stateChangeListener());
}

private StateMachineListenerAdapter stateChangeListener() {
   return new StateMachineListenerAdapter<States, Events> () {

        //Override what's necessary, but particularly
        @Override
        public void stateChanged(State<S,E> from, State<S,E> to) {
            //logging
        }
   }
}

单步执行状态机

考虑使用 StateMachineTestPlan (official doc,其中包含代码示例)