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.
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.