-
Notifications
You must be signed in to change notification settings - Fork 1
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, State Machine can't stop its flow on it. Therefore, outgoing else Transition must be defined for each Choice to ensure that there will always be an alternative if all other conditions (Guards) are not met.
In UML, a Choice pseudostate is represented by a diamond shape with multiple outgoing transitions:
stateDiagram-v2
[*] --> InitialState
InitialState --> Choice : TriggerEvent
Choice --> State1 : [condition1]
Choice --> State2 : [condition2]
Choice --> State3 : [else]
Equivalent Stateflows notation of a Choice pseudostate:
/* fragment of State Machine definition */
.AddInitialState("InitialState", b => b
.AddTransition<TriggerEvent>("Choice")
)
.AddChoice("Choice", b => b
.AddTransition("State1", b => b
.AddGuard(b => /* condition1 */)
)
.AddTransition("State2", b => b
.AddGuard(b => /* condition2 */)
)
.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:
/* 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(b => /* condition1 */)
)
.AddTransition("State2", b => b
.AddGuard(b => /* condition2 */)
)
.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.
public class ChoiceCondition1 : IDefaultTransitionGuard
{
public Task<bool> GuardAsync()
{
return /* condition1 */;
}
}
public class ChoiceCondition2 : IDefaultTransitionGuard
{
public Task<bool> GuardAsync()
{
return /* condition2 */;
}
}
/* 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
Choiceclass 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.
Home page Support Code licensed under an MIT-style License. Documentation licensed under CC BY 4.0. © by Mikołaj Milewski, 2025
Overview
Installation
Behaviors
State Machines
Building blocks
States
State
Composite State
Orthogonal State
Final State
Pseudostates
Choice
Junction
Fork
Join
Transitions
Transition
Default Transition
Internal Transition
Concepts
Evaluation of Transitions
Activities
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
Data Store Node
Structured Activity Node
Iterative Activity Node
Parallel Activity Node
Flows
Data Flow
Control Flow
Concepts
Implicit fork and join
Actions