State diagrams: entry/exit actions

State diagrams: entry/exit actions

puml.online

简介 / Introduction

A state machine models an object’s lifecycle as a set of states and transitions between them. An entry action fires automatically when entering a state; an exit action fires when leaving it. This pattern is useful for initialization, resource acquisition, logging, and cleanup. This article uses the same example in both PlantUML and Mermaid, then compares the two directly.

PlantUML 版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@startuml
hide empty description

[*] --> Idle : entry / call initialize()
Idle --> Running : start()
Running --> Idle : stop() / exit / call cleanup()
Running --> Done : entry / call finalize() / exit

state Running {
[*] --> Working
Working --> Paused : pause() / exit
Paused --> Working : resume() / entry / call resume()
Working --> [*] : complete()
}
@enduml

Mermaid 版

1
2
3
4
5
6
7
8
9
10
11
12
13
stateDiagram-v2
[*] --> Idle: entry/ initialize()
Idle --> Running: start()
Running --> Done: entry/ finalize() exit/
Running --> Paused: pause()
Paused --> Running: entry/ resume()

state Running {
[*] --> Working
Working --> Paused: pause()
Paused --> Working: entry/ resume()
Working --> [*]: complete()
}

对比 / Side-by-side

Feature PlantUML Mermaid
entry action syntax entry / call initialize() entry/ initialize()
exit action syntax exit / call cleanup() exit/
Composite / nested state state X { ... } directly state X { ... } wrapper
Actions on transition label stop() / exit / call cleanup() not supported; exit only on transition line
entry for composite entry state X : entry / ... state X { [*] --> S : entry/ ... }
Hide empty descriptions hide empty description no special directive needed

小结 / Wrap-up

PlantUML’s entry/exit actions can be placed both in state declarations and on transition labels, offering greater flexibility for complex state machines. Mermaid’s syntax is more concise, but transition labels can only carry a single entry/ or exit/ marker. Both clearly express the entry/exit semantics; choose based on the level of detail your project requires.

  • Title: State diagrams: entry/exit actions
  • Author: puml.online
  • Created at : 2026-09-07 09:00:00
  • Updated at : 2026-09-07 01:06:15
  • Link: https://puml.online/blog/state-diagram-entry-exit-en/
  • License: This work is licensed under CC BY-NC-SA 4.0.