State diagram

Allows you to describe the behavior of a single object.
Also, a state diagram is used by analysts to describe the sequence of transitions of an object from one state to another.
The state diagram shows all possible states in which the object may be, as well as the process of changing states as a result of external influence.

1. Basic elements

The main elements of the state diagram are “State” and “Transition”. The state diagram has similar semantics to the activity diagram, with the only difference that the activity here is replaced by a state, and transitions symbolize actions. Thus, in a state diagram, an activity symbolizes the state in which the object is in an extended amount of time, while the action is momentary.

Initial pseudostate

he initial state of the system in the diagram is indicated as follows: 

Transition

Transition means moving from one state to another. 

State (simple and compound)

Indicates the actions performed by the system (may include possible options) leading to the results observed by the actors. 

Final state

Allows you to define the boundaries of systems or subsystems.

2. Syntax 

To display the initial and final state, use [*]. To indicate transitions use  ->

Код скопирован
  • @startuml
    [*] –> State1
    State1 –> [*]
    State1 -> State2
    State2 –> [*]
    @enduml

The state keyword is used to indicate the state. 

If you want to show the state as a simple field, then use hide empty description

Код скопирован
  • @startuml
    hide empty description
    [*] –> State1
    State1 –> [*]
    State1 -> State2
    State2 –> [*]
    @enduml

Compound states can be declared using the state {…} operator: 

Код скопирован
  • @startuml
    [*] –> NotShooting
    state NotShooting {
    [*] –> Idle
    Idle –> Configuring : EvConfig
    Configuring –> Idle : EvConfig }
    @enduml

Using the operator - or ||, it is possible to declare parallel substates inside a compound state. 

Код скопирован
  • @startuml
    [*] –> Active
    state Active {
    [*] -> NumLockOff
    NumLockOff –> NumLockOn : EvNumLockPressed
    NumLockOn –> NumLockOff : EvNumLockPressed

    [*] -> CapsLockOff
    CapsLockOff –> CapsLockOn : EvCapsLockPressed
    CapsLockOn –> CapsLockOff : EvCapsLockPressed
    }
    @enduml

To display transition arrows horizontally, use the -> operator. To set a different direction, use:

  • -down-> (default arrow)
  • -right-> or ->
  • -left->
  • -up->
Код скопирован
  • @startuml 
    [*] -up-> First 
    First -right-> Second 
    Second –> Third 
    Third -left-> Last 
    @enduml 

Branching and merging arrows can be done using the keywords <<fork>> and <<join>>:

Код скопирован
  • @startuml
    state fork_state <<fork>>
    [*] –> fork_state
    fork_state –> State2
    fork_state –> State3
    state join_state <<join>>
    State2 –> join_state
    State3 –> join_state
    join_state –> State4
    State4 –> [*]
    @enduml

Notes

You can add notes to the state using special keywords: note left of, note right of, note top of, note bottom of

You can create notes that are not tied to any object. 

Notes can also be attached to compound states. 

Код скопирован
  • @startuml
    [*] –> off
    note left of off : this is a short\nnote
    note “This is a floating note” as N1
    state off {
    off –> blinking : turn_on_off
    }
    state blinking {
    [*] –> blink_on
    blinking –> off : turn_on_off
    state blink_on {
    blink_on –> blink_off : [1.5s]
    blink_on : enter/turn_led_on
    blink_on : exit/turn_led_off
    }
    state blink_off {
    blink_off –> blink_on : [0.75s]
    }
    }
    note right of blinking: This is a note on a composite state
    @enduml