Skip to content

State Machine Choice

Mikołaj Milewski edited this page Oct 27, 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.

Overview
Installation
Runtimes
   Orleans runtime
Concepts
   Events and Structural Events
   Messaging Patterns
   Validation
   Scheduling
   Observability
Behaviors
   Behavior Values
   Dependency Injection
   Behavioral Groups
   State Machines
       Concepts
           State Identity
           In-State Event Deferrals
           Evaluation of Transitions
           Exception Handling
       Building blocks
           Regions
           States
               State
               Composite State
               Orthogonal State
               Final State
           Pseudostates
               Choice
               Junction
               Fork
               Join
               History
           Transitions
               Transition
               Default Transition
               Internal Transition
   Activities
       Concepts
           Exception Handling
           Implicit Fork and Join
           Tokens
       Building blocks
           Nodes
               Action Node
               Decision Node
               Merge Node
               Initial Node
               Final Node
               Input Node
               Output Node
               Fork Node
               Join Node
               Accept Event Action Node
               Send Event Action Node
               Publish Event Action Node
               Time Event Action Node
               Data Store Node
               Structured Activity Node
               Iterative Activity Node
               Parallel Activity Node
           Flows
               Data Flow
               Control Flow
   Actions
   Entities
       Fields
       Mutations
       Projections
Extensions
   ClearScript
   FluentValidation
   JSON definitions
   Minimal APIs
   OneOf
   OpenTelemetry
   PlantUML
   Scheduling

Clone this wiki locally