Krad213
Krad213

Reputation: 9

Spring state machine skips initial states

I have an issue with Spring state machine:

I have hierarchy of states, let it be like this:

{
    S1 - initial
    S2
    S3 {
        S3S1 - initial
        S3S2
    }
}

Currently I'm in state S2 and want to transfer to S3S2, my assumption was that inital state action should be called in this transition, but it doesn't. For example S3 is represents working with some modal window, and in state S3S1 I should initialize this window, then after that, in S3S2 I can work with some controls inside this window, currently I have no way to do this by defining single transition from S2 to S3S2 (please correct me if I'm wrong).

I found this very old issue #71 (https://github.com/spring-projects/spring-statemachine/issues/71) in github, it states "Spec mentions that machine should always enter its initial state but this this is a bit awkward if user wants to enter directly into a state in a submachine.", but in my understanding of State Machine idea it makes perfect sense if you don't go via initial states why do you need hierarchy of states at all, you can define everything on same level, in my opinion initial states should prepare the environment for other states in same region to work. So I don't think initial states should ever be skipped, or in worst case this behavior should be optional.

Upvotes: 0

Views: 1026

Answers (1)

Janne Valkealahti
Janne Valkealahti

Reputation: 2646

Some parts of the uml specification mentions that a composite state is entered via its initial state, but wording is slightly misleading as that refers to a case where transition terminates in a particular composite state itself, and after that initial state(aka default entry) is used. Transition directly into other substate is called explicit entry. There's also controlled entry via entry point, which is another way to enter submachines.

If you need to do some init logic before S3S2, then you could try to do it as entry action in its parent state S3. If you do transition from S2 to S3S2, order what's happening is, exit S2, enter S3, enter S3S2.

Upvotes: 0

Related Questions