Spring Statemachine:如何获得选择状态的转换?

时间:2015-08-28 14:56:39

标签: spring-statemachine

我这样配置状态机:

    StateMachineTransitionConfigurer transitions = builder.configureTransitions();
    transitions.withChoice().source(CHOICE). //
    first(A, aGuard). //
    then(B, bGuard). //
    last(C);

然后,在另一个地方,我想从这个配置中获得选择转换。虽然我可以通过以下方式轻松获得选择状态:

    for (final State smState : stateMachine.getStates()) {
        if (smState.getPseudoState() != null && smState.getPseudoState().getKind() == PseudoStateKind.CHOICE) {
// smState is a choice state
        }
    }

我不知道如何从这些选择状态中获取转换列表("第一个","然后" s和"最后"上面)

似乎无法访问此信息。我是对的吗?

2 个答案:

答案 0 :(得分:0)

状态机有:

Collection<Transition<S,E>> getTransitions()

来自Transition您需要检查来源或目标

State<S,E> getSource();
State<S,E> getTarget()

答案 1 :(得分:0)

我们最终通过内省ChoicePseudoState的“choices”属性解决了这个问题,因为某些原因,没有访问者:

    final Field choicesField = ChoicePseudoState.class.getDeclaredField("choices");
    choicesField.setAccessible(true);

    for (final State<SessionStateType, SessionEvent> smState : stateMachine.getStates()) {
        if (smState.getPseudoState() != null && smState.getPseudoState().getKind() == PseudoStateKind.CHOICE) {
            final ChoicePseudoState<SessionStateType, SessionEvent> choice = ((ChoicePseudoState<SessionStateType, SessionEvent>) smState
                .getPseudoState());
            final List<ChoiceStateData<SessionStateType, SessionEvent>> choices = (List<ChoiceStateData<SessionStateType, SessionEvent>>) choicesField
                .get(choice);

...