3

It seems that actions added to choice pseudo-states are silently ignored. Doing this:

Builder builder = StateMachineBuilder.builder();
StateConfigurer states = builder.configureStates().withStates();
StateMachineTransitionConfigurer transitions = builder.configureTransitions();
StateConfigurer statesConfig = states.initial(INITIAL).states(EnumSet.allOf(StateType.class));
statesConfig.choice(StateType.CHOICE_STATE);

transitions.withChoice().source(StateType.CHOICE_STATE). //
    first(StateType.S1, someGuard). //
    last(StateType.S2);

states.state(StateType.CHOICE_STATE, someAction, null);

Results in someAction never being executed when CHOICE_STATE is entered.

Adding actions to transitions out of CHOICE_STATE (for example, to S1 or S2 above) is simply not permitted by the framework.

To get around this, we have implemented a state that precedes CHOICE_STATE. We are then free to add actions to this state, as usual. I was just wondering what is the reason for this limitation, or if there is some way of putting actions on a pseudo-state that I may have missed.

Marc
  • 1,812
  • 4
  • 23
  • 36

1 Answers1

4

That's because choice is a pseudostate which is supposed to be transitient so there should not be behavioural changed in that state itself.

Have you tried to define Action with a Transition which takes you into a choice state?

@Override
public void configure(StateMachineTransitionConfigurer<TestStates, TestEvents> transitions) throws Exception {
  transitions
    .withExternal()
      .source(TestStates.S1)
      .target(TestStates.S2)
      .event(TestEvents.E2)
      .action(externalTestAction());
}


@Bean
public Action<TestStates, TestEvents> externalTestAction() {
  return new TestAction();
}

Preceding state is a good workaround, especially it that is accompanied with a triggerless transition

I can try to see if in Spring Statemachine we could add feature to a transition(configurer for choice transition) itself. I created a ticket for this https://github.com/spring-projects/spring-statemachine/issues/108.

While we're mostly trying to follow UML model, spec is very vague in most parts and leave a lot of implementation specifics to the implementation itself.

Janne Valkealahti
  • 2,552
  • 1
  • 14
  • 12
  • 1
    Adding actions on entry into a choice pseudostate would be useful, but it would also be handy to be able to configure actions on the (outgoing) choice transitions. Do you have any plans to implement this possibility in a future release? – Marc Sep 14 '15 at 15:02
  • 1
    This is probably not a bad idea at all. I'll put that on my enhancement list. – Janne Valkealahti Sep 16 '15 at 01:45
  • @JanneValkealahti would you be able to provide an example of a triggerless transition? – Daniel Vilas-Boas Nov 30 '21 at 13:15