2.6 KiB
2.6 KiB
Quickstart: State Machine Activity
Build a StateMachine
- Add a
StateMachinewith a non-emptyInitialState. - Add uniquely named states. Each state can have an optional
EntryandExitactivity. - Add transitions in declaration order. Each transition names its source and target and can contain one optional
Trigger,Condition, andActionactivity. - Use
Sequencewhen a slot must run more than one activity; Trigger and Action are scalar activity slots, not arrays.
Execution contract
After the initial state's Entry completes, outbound transitions are considered in declaration order:
- Triggerless transitions are evaluated immediately.
- A missing Condition is true.
- A false triggerless Condition falls through to the next transition. If eventful alternatives exist, their triggers are scheduled.
- When a transition is accepted, execution is source
Exit→ transitionAction→ targetEntry. - Self-transitions run the same full Exit → Action → Entry lifecycle.
- Entering a state with no outbound transitions completes the StateMachine.
If every outbound transition is triggerless and false, the StateMachine remains active without scheduling a busy loop. Reachable triggerless cycles yield through Elsa's scheduler between transitions so they do not grow the synchronous call stack.
Durable and competing triggers
- A winning transition cancels distinct competing trigger work before source Exit begins.
- If an eventful Trigger completes but its Condition is false, Elsa re-arms that rejected Trigger and preserves already-active competitors. This is an intentional Elsa durability deviation from WF4's cancel-all-and-reschedule behavior.
- Inline StateMachine JSON cannot preserve WF4 shared Trigger object identity. Elsa 3.8 rejects shared Trigger instances and duplicate trigger activity IDs explicitly instead of guessing identity from IDs.
- Continuation tags use declaration-order transition identity. Reordering transitions while an instance is suspended is a compatibility-sensitive definition change.
These boundaries are part of the accepted StateMachine semantics ADR and are observable through validation and regression tests.
Validation
Run the focused StateMachine tests:
dotnet test test/unit/Elsa.Activities.UnitTests/Elsa.Activities.UnitTests.csproj --no-restore --filter FullyQualifiedName~StateMachine
The suite covers triggerless and eventful paths, missing and false conditions, lifecycle ordering, self-transitions, competing-trigger cancellation, identity rejection, persistence/resumption, and automatic-cycle scheduler yielding.