State diagrams: transition guards

State diagrams: transition guards

puml.online

Introduction

The easiest thing to get wrong in a state machine diagram is multiple transitions leaving the same state. If Payment can become either Paid or Payment failed, the arrows alone tell the reader nothing about which path is taken — unless you write down the condition on the arrow. That condition is the transition guard.

The UML specification writes guards in square brackets on the transition label: [amount <= 1000]. Both engines follow that convention, but they support it very differently: PlantUML draws a real UML diamond for the choice pseudostate, while Mermaid can only draw a plain rectangle. This article builds the same payment scenario in both, and flags a few gotchas you will actually hit.

PlantUML version

PlantUML fully supports the UML choice pseudostate. Declare it with the <<choice>> stereotype:

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

state "Cart" as Cart
state "Payment" as Payment
state "Paid" as Paid
state "Payment failed" as Failed

state decision <<choice>>

[*] --> Cart
Cart --> Payment : checkout()
Payment --> decision : authorize()
decision --> Paid : [amount <= 1000 and balance >= amount]
decision --> Failed : [amount > 1000 or balance < amount]

Paid --> [*]
Failed --> [*]
@enduml

The key points:

  • The guard goes straight on the transition label: decision --> Paid : [amount <= 1000 and balance >= amount]. Square brackets are the standard UML notation, and PlantUML renders them verbatim next to the arrow so the condition is readable at a glance.
  • and / or compose compound conditions. [amount <= 1000 and balance >= amount] reads like a complete business rule.
  • state decision <<choice>> declares a choice pseudostate, and it is drawn as a diamond — the standard UML symbol for “a decision happens here”. Note that the diamond carries no visible text: decision is only the state ID, stored in the SVG’s data-qualified-name attribute where a reader cannot see it. That is actually correct per the UML specification, where choice is an anonymous pseudostate.
  • Transition labels can also string event, guard, and action onto one line, following the UML event [guard] / action order — for example Payment --> Paid : submit [amount <= 1000] / approve(). This is the closest to the specification when you are drawing a full UML state machine.

One more parsing detail: <<choice>> must appear on the state declaration line. Writing it as a standalone line (decision <<choice>>) is a syntax error.

Mermaid version

Mermaid’s stateDiagram-v2 syntax is leaner, and the guard notation is exactly the same as PlantUML — square brackets on the transition label:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
stateDiagram-v2
state "Cart" as Cart
state "Payment" as Payment
state "Paid" as Paid
state "Payment failed" as Failed
state "Decision" as decision

[*] --> Cart
Cart --> Payment : checkout()
Payment --> decision : authorize()
decision --> Paid : [amount <= 1000 and balance >= amount]
decision --> Failed : [amount > 1000 or balance < amount]
Paid --> [*]
Failed --> [*]

Mermaid does accept the <<choice>> stereotype (state decision <<choice>> parses without error), but it still renders as an ordinary rectangle and the stereotype text never appears on the diagram. Naming it state "Decision" as decision is therefore far more useful — the reader can actually see the word “Decision” and understand what that node is for.

The two engines are complementary here: PlantUML gives you the correct shape but no label; Mermaid gives you a label but the wrong shape. With stateDiagram-v2 alone, neither can produce a diamond that also says “decision”.

Mermaid’s state diagram also supports direction LR (lay the diagram out left to right) and hide empty description (hide states that carry no description). Both parse cleanly.

Side-by-side

Aspect PlantUML Mermaid
Guard notation A --> B : [cond] A --> B : [cond] (identical)
Condition content Supports and / or compounds Same, rendered verbatim on the label
choice pseudostate state decision <<choice>> renders as a diamond Accepts <<choice>> but renders a plain rectangle; the stereotype text is not shown
Can the choice node carry a label No, the diamond is always empty Yes, state "Decision" as decision shows the text
Event + guard + action All on one line: submit [cond] / action() No compound syntax; put the action in a separate note
Non-ASCII state names state "支付中" as Payment state "支付中" as Payment
Layout direction State diagrams do not support direction LR (immediate 400 syntax error) Supports direction LR
Notes note right of X : text works note right of X : text works, but a second : inside the text fails to parse
Hide empty descriptions hide empty description Also supports hide empty description

Two gotchas that are worth knowing because they are easy to trip over:

  1. PlantUML state diagrams reject direction LR. Adding it makes PlantUML assume a sequence diagram and report a syntax error. To control direction you have to reorder the state declarations, or switch to a class diagram instead.
  2. Mermaid note text cannot contain another colon. note right of Idle : guard is balance > 0 is fine, but note right of Idle : guard: balance > 0 fails to parse — the parser treats the second colon as the error position. Use a different symbol when you need a separator inside a note.

Wrap-up

Guards themselves are not hard; the real difference is how deeply each engine models “where a condition is evaluated from.” PlantUML gives you complete UML semantics: a diamond choice, a compound event/guard/action label, a whole rule on a single line. Mermaid gives you the same handy bracket notation plus a clean state diagram syntax, but the merge point can only be an ordinary node.

The practical split is clear: when the diagram is for non-technical readers or has to go into a formal design document, use PlantUML — the diamond makes “a decision happens here” visible at a glance. When you just need to sketch a state flow inside a README or a Markdown doc, Mermaid’s stateDiagram-v2 is enough and skips a whole build-tool dependency. Either way, write the guard as a full boolean expression in brackets rather than a bare field name — [amount <= 1000 and balance >= amount] and [ok] differ by an order of magnitude in how much they tell the reader.

  • Title: State diagrams: transition guards
  • Author: puml.online
  • Created at : 2026-11-29 01:00:00
  • Updated at : 2026-12-05 02:08:58
  • Link: https://puml.online/blog/state-diagram-transition-guards-en/
  • License: This work is licensed under CC BY-NC-SA 4.0.
On this page
State diagrams: transition guards