Skip to content

Junction

Mikołaj Milewski edited this page Apr 1, 2025 · 1 revision

Overview

Junction is a type of Pseudostate used in State Machines to model unconditional or conditional branching. Unlike the Choice pseudostate, which evaluates conditions dynamically, Junction is typically used for static branching logic where transitions are evaluated in a predefined order.

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

As Junction is a Pseudostate and not a State, the State Machine cannot stop its flow on it. Therefore, an outgoing else Transition must be defined for each Junction to ensure that there will always be an alternative if all other conditions (Guards) are not met.

Definition

In UML, a Junction pseudostate is represented by a small black circle with multiple outgoing transitions:

stateDiagram-v2
[*] --> InitialState
InitialState --> Junction : TriggerEvent
Junction --> State1 : [condition1]
Junction --> State2 : [condition2]
Junction --> State3 : [else]
Loading

Equivalent Stateflows notation of a Junction pseudostate:

Lambda style

    /* fragment of State Machine definition */
    .AddInitialState("InitialState", b => b
        .AddTransition<TriggerEvent>("Junction")
    )
    .AddJunction("Junction", 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 Junction in the State Machine, a shortened notation with the default name can be used:

Lambda style

    /* fragment of State Machine definition */
    .AddInitialState("InitialState", b => b
        .AddTransition<TriggerEvent>(Junction.Name)
    )
    .AddJunction(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 Junction pseudostate and its Transitions are defined using string names and lambda functions for conditions.

Typed style

    public class JunctionCondition1 : IDefaultTransitionGuard
    {
        public Task<bool> GuardAsync()
        {
            return /* condition1 */;
        }
    }

    public class JunctionCondition2 : IDefaultTransitionGuard
    {
        public Task<bool> GuardAsync()
        {
            return /* condition2 */;
        }
    }

    /* fragment of State Machine definition */
    .AddInitialState<InitialState>(b => b
        .AddTransition<TriggerEvent, Junction>()
    )
    .AddJunction(b => b
        .AddTransition<JunctionCondition1, State1>()
        .AddTransition<JunctionCondition2, State2>()
        .AddElseTransition<State3>()
    )
    .AddState<State1>()
    .AddState<State2>()
    .AddState<State3>()

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

Junction class can be used as a target for typed style Transitions if State Machine contains just one Junction. If there are more Junctions, 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