Skip to content
mikolaj-milewski edited this page Mar 25, 2025 · 6 revisions

Overview

State (sometimes referred to as Simple State) represents stable configuration of State Machine.

Simple State consist of:

  • Name, uniquely identifying State within State Machine,
  • Parent State, if State is a substate of Composite State or Orthogonal State,
  • OnEntry, logic that is executed when State is entered,
  • OnExit, logic that is executed when State is exited,
  • Set of outgoing Transitions.

Definition

In UML, Simple State is represented by rounded rectangle:

stateDiagram-v2
SimpleState --> AnotherState
Loading

If there is any logic within State, it is described textually below State name:

stateDiagram-v2
SimpleState<br/>/onEntry<br/>/onExit --> AnotherState
Loading

Equivalent Stateflows notation of States:

Lambda style

    /* fragment of State Machine definition */
    .AddState("SimpleState", b => b
        .AddOnEntry(async c => /* logic here */)
        .AddOnExit(async c => /* logic here */)
        .AddDefaultTransition("AnotherState")
    )
    .AddState("AnotherState")

Using lambda style here means that State is referred to by string name and each bit of State logic is implemented as lambda function passed as parameters to AddOn* methods in State definition.

Typed style

    public class SimpleState : IStateEntry, IStateExit
    {
        public async Task OnEntryAsync()
        {
            /* logic here */
        }

        public async Task OnExitAsync()
        {
            /* logic here */
        }
    }

    public class AnotherState : IState
    { }

    /* fragment of State Machine definition */
    .AddState<SimpleState>(b => b
        .AddDefaultTransition<AnotherState>()
    )
    .AddState<AnotherState>()

Using typed style here means that State is a class that is referenced in typed overloads of Add* methods in State Machine definition.

Class can be used as State if it implements at least one interface from IState* family:

  • IState - simply represents State,
  • IStateEntry - represents State with OnEntry logic implemented as OnEntryAsync() method,
  • IStateExit - represents State with OnExit logic implemented as OnExitAsync() method,
  • IStateDefinition - represents State with additional Build() method that allows to extend its definition, for example to define Transitions within State class.

State logic may also be split into several classes like in the example:

    public class SimpleState : IState
    { }

    public class SimpleEntry : IStateEntry
    {
        public async Task OnEntryAsync()
        {
            /* logic here */
        }
    }

    public class SimpleExit : IStateExit
    {
        public async Task OnExitAsync()
        {
            /* logic here */
        }
    }

    public class AnotherState : IState
    { }

    /* fragment of State Machine definition */
    .AddState<SimpleState>(b => b
        .AddOnEntry<SimpleEntry>()
        .AddOnExit<SimpleExit>()

        .AddDefaultTransition<AnotherState>()
    )
    .AddState<AnotherState>()

This technique enables reusing of State's logic, but there is an important concern of State identity to be considered.

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

Clone this wiki locally