How do I know if a guard rejected a transistion

1.4k views Asked by At

I have transitions configured as in the reference documentation:

public void configure(StateMachineTransitionConfigurer<States, Events> transitions)
        throws Exception {
    transitions
        .withExternal()
            .source(States.SI).target(States.S1)
            .event(Events.E1)
            .guard(guard1())
            .and()
        .withExternal()
            .source(States.S1).target(States.S2)
            .event(Events.E1)
            .guard(guard2())
            .and()
        .withExternal()
            .source(States.S2).target(States.S3)
            .event(Events.E2)
            .guardExpression("extendedState.variables.get('myvar')");
}

If the state machine is in state States.S1 and I send event Events.E1, how do I know if guard2() rejected the transition?

I'm currently checking the state of the state machine: if it's still in States.S1 then I know the event was rejected. Is this the "right" way to handle rejection by a guard?

Edit:

After reading Janne's comment I realized what I'm trying to do is probably an incorrect use of a guard. It seems that guards should be used only to determine which state the machine should transition to and not whether or not a state should be entered. If it was the latter, i.e. the transition was outright rejected, then the state machine would be in no state. I should have been tipped off by what the code allows me to do and by having a state machine-centric mindset when coding the workflow. That's what I get for rushing before a holiday!

0

There are 0 answers