Skip to content

5.4.2.3.1. Choice

Mikołaj Milewski edited this page Nov 12, 2026 · 1 revision

Choice

Choice is a type of Pseudostate used in State Machines to model conditional branching. It allows the State Machine to evaluate multiple conditions and transition to different target states based on those conditions.

All Transitions coming out from Choice are Default Transitions - they are not explicitly triggered by an Event.

As Choice is a Pseudostate and not a State, a State Machine cannot stop its flow on it. Add an outgoing else transition when the model requires a fallback if no guarded branch is enabled.

Definition

In UML, a Choice pseudostate is represented by a diamond shape with multiple outgoing transitions:

stateDiagram-v2
state Choice <<choice>>
[*] --> InitialState
InitialState --> Choice : TriggerEvent
Choice --> State1 : [condition1]
Choice --> State2 : [condition2]
Choice --> State3 : [else]
Loading

Equivalent Stateflows notation of a Choice pseudostate:

Lambda style

Definition fragment (inside the builder callback):

    /* fragment of State Machine definition */
    .AddInitialState("InitialState", b => b
        .AddTransition<TriggerEvent>("Choice")
    )
    .AddChoice("Choice", b => b
        .AddTransition("State1", b => b
            .AddGuard(c => true)
        )
        .AddTransition("State2", b => b
            .AddGuard(c => false)
        )
        .AddElseTransition("State3")
    )
    .AddState("State1")
    .AddState("State2")
    .AddState("State3")

If there is just one Choice in State Machine, shortened notation with default name can be used:

Unnamed pseudostate shorthand

Definition fragment (inside the builder callback):

    /* fragment of State Machine definition */
    .AddInitialState("InitialState", b => b
        .AddTransition<TriggerEvent>(Choice.Name)
    )
    .AddChoice(b => b // no name provided here, default name is used
        .AddTransition("State1", b => b
            .AddGuard(c => true)
        )
        .AddTransition("State2", b => b
            .AddGuard(c => false)
        )
        .AddElseTransition("State3")
    )
    .AddState("State1")
    .AddState("State2")
    .AddState("State3")

Using lambda style means that the Choice Pseudostate and its Transitions are defined using string names and lambda functions for conditions.

Typed style

    public class ChoiceCondition1 : IDefaultTransitionGuard
    {
        public Task<bool> GuardAsync()
        {
            return Task.FromResult(true);
        }
    }

    public class ChoiceCondition2 : IDefaultTransitionGuard
    {
        public Task<bool> GuardAsync()
        {
            return Task.FromResult(false);
        }
    }

Definition fragment (inside the builder callback):

    /* fragment of State Machine definition */
    .AddInitialState<InitialState>(b => b
        .AddTransition<TriggerEvent, Choice>()
    )
    .AddChoice(b => b
        .AddTransition<ChoiceCondition1, State1>()
        .AddTransition<ChoiceCondition2, State2>()
        .AddElseTransition<State3>()
    )
    .AddState<State1>()
    .AddState<State2>()
    .AddState<State3>()

Using typed style here means that Choice Pseudostate is represented by a class, in the example predefined Choice class is used.

Choice class can be used as a target for typed style Transitions if State Machine contains just one Choice. If there are more Choices, there is an important concern of State identity to be considered.

Clone this wiki locally