State diagrams: internal transitions
简介 / Introduction
The easiest kind of transition to get wrong in a state machine is an arrow pointing back at its own state. The UML specification splits these into two categories: an external self-transition (A --> A) is drawn as a loop arrow that leaves the state and re-enters it, so the exit and entry actions both fire; an internal transition never leaves the state at all — it is a self-trigger happening inside the state, and it fires neither exit nor entry. The UML specification requires the latter to be written inside the state’s rounded rectangle rather than drawn as an arrow.
The two engines differ sharply in how much of that they can express: PlantUML can draw both, Mermaid can only draw the external self-transition. This article builds the same “background job running” scenario in each, and flags three gotchas you will actually hit.
PlantUML 版
PlantUML maps the two kinds of transition onto two completely different syntaxes: a same-name link is an external self-transition, while a description line inside a composite state is an internal transition.
1 | @startuml |
A few key points:
- A same-name link is an external self-transition.
Running --> Running : tick / refreshrenders as a loop arrow with the label beside it. The SVG geometry makes the distinction directly measurable: that label sits outside the state box, while the in-boxheartbeat / keep alivesits inside it (the rectangle isx=7, y=87, w=76, h=52.6; the in-box text lands at(12, 131)and the loop label at(124, 117)). - A description line inside the box is an internal transition. Written as
Running : heartbeat / keep alivewithin the braces ofstate "Running" as Running { ... }, the text is rendered directly inside the rounded rectangle — exactly the UML notation for an internal transition. You can write several lines in the same box and PlantUML lays them out as a list. - You can also write entry / do / exit inside the box.
Running : entry / init(),Running : do / periodic refresh, andRunning : exit / cleanup()are all valid in-box description lines, corresponding to the state’s entry action, ongoing behaviour, and exit action. note right of Running : ...attaches a side note explaining that the box text is an internal transition, so readers do not mistake it for part of the state name.- PlantUML state diagrams do not support
direction LR. Adding it returns an HTTP 400 syntax error (the engine parses it as a sequence diagram). To control direction you have to reorder the state declarations, or switch to a class diagram.
Note how tolerant PlantUML is about colons: a transition label may contain another colon (poll : refresh()), and note right of X : guard: retry < 3 renders fine. This is the exact opposite of Mermaid’s behaviour below.
Mermaid 版
Mermaid’s stateDiagram-v2 syntax is leaner, and the external self-transition is written exactly the same way as in PlantUML:
1 | stateDiagram-v2 |
Running --> Running : tick / refresh is rendered as a self-referencing edge, with both the arrow and its label.
But Mermaid cannot express a UML internal transition. Writing the same description inside a composite state fails to parse:
1 | stateDiagram-v2 |
The error is Group nodes can only have label. Remove the additional description for node [Running]. In other words, a Mermaid composite state accepts only one label plus any number of named child states — it rejects description lines entirely, so there is no Mermaid syntax that corresponds to the UML internal transition notation.
Two more places where Mermaid is the opposite of PlantUML:
- Mermaid state diagrams support
direction LR, while PlantUML does not. This difference shows up in the comparison table of both articles, in opposite directions. - Mermaid labels and notes cannot contain a second ASCII colon.
note right of Running : guard: retry < 3is a hard parse error — the parser treats the second colon as a syntax error position. To write a condition in a note, use a different symbol, such asguard = retry < 3.
Also note that an empty composite state, state "R" as R { }, fails in Mermaid with No such shape: roundedWithTitle. A composite state needs at least one named child state.
对比 / Side-by-side
| Aspect | PlantUML | Mermaid |
|---|---|---|
| External self-transition | A --> A : label, rendered as a loop arrow |
Identical syntax, rendered as a self edge |
| Internal transition (written inside the state box) | state "A" as A { A : label }, text rendered inside the rounded rectangle |
Not supported: a composite state allows only a label and named child states; any description line is a parse error |
| entry / do / exit inside the box | Supports A : entry / ..., A : do / ..., A : exit / ... |
Not supported — same Group nodes can only have label error |
| Layout direction | State diagrams do not support direction LR (HTTP 400 syntax error) |
Supports direction LR |
Three gotchas you will actually hit:
- Mermaid cannot draw a UML internal transition. This is the central conclusion of the article. If your design document explicitly distinguishes “exits and re-enters” from “stays inside the state”, Mermaid cannot satisfy it — switch to PlantUML, or fall back to annotating outside the diagram which self-loop is internal.
- Mermaid composite states cannot contain any description line, not even a harmless-looking
Running : tick. Only named child states are allowed; this is a hard limitation of the parser, not a matter of how strict the grammar is. - Mermaid labels and notes cannot contain a second ASCII colon, while PlantUML allows it freely. Remember to replace that second colon before writing
note right of X : guard: retry < 3.
小结 / Wrap-up
Internal transitions are the UML feature that looks identical to an ordinary transition but means something quite different: an external self-transition exits and re-enters the state, firing exit / entry; an internal transition never leaves the state. PlantUML makes that distinction explicit — a same-name link and an in-box description line are two separate syntaxes, and you can tell them apart at a glance in the output. Mermaid offers only the former, so it cannot show the in-box semantics and can only add a note beside the diagram to explain itself.
The practical choice is clear: when the semantics must follow UML strictly (for example, feeding a modelling tool, or conveying “this self-trigger does not reset the timer”), use PlantUML; when the diagram just lives inside a document for readers to look at and Mermaid’s styling is more consistent, use Mermaid and accept that the self-loop will be read as an external transition. Either way, label explicitly which self-transitions are internal — readers cannot know from the arrow alone whether exit / entry fire.
- Title: State diagrams: internal transitions
- Author: puml.online
- Created at : 2026-11-30 01:00:00
- Updated at : 2026-12-05 02:08:58
- Link: https://puml.online/blog/state-diagram-internal-transitions-en/
- License: This work is licensed under CC BY-NC-SA 4.0.