spring-statemachine

Spring Statemachine: how to get transitions of choice states?


I configure a state machine thusly:

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

Then, in a different place, I'd like to get the choice transitions from this configuration. Whereas I can get the choice states easily enough by doing:

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

I have no idea how to get the list of transitions from these choice states (the "first", "then"s, and "last" above).

There appears to be no way to access this information. Am I right?


Solution

  • We finally solved this issue by introspecting the "choices" property of ChoicePseudoState, for which there is no accessor, for some reason:

        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);
    
    ...