UML シーケンス図

Schematex の sequence エンジンを使って、UML 2.5.1 §17 準拠のシーケンス図をテキスト DSL から生成する方法を解説します。12 種類の複合フラグメント演算子と ref フレームに完全対応。

シーケンス図について

シーケンス図は、参加者が時間の経過とともにメッセージを交換する様子を示します。ライフラインは上から下へ、メッセージは参加者間で左から右へ流れ、縦軸が順序を表します。UML 2.5.1 §17(インタラクション) で定義されており、API コールフロー、認証ハンドシェイク、分散プロトコルを明確にするためにエンジニアが最もよく使う図です。誰が誰を呼び出し、どの順序で、何が返ってくるかを表現します。

Schematex は完全な UML 記法を実装しており、生成向けに Mermaid の sequenceDiagram 方言も受け付けます。Mermaid コードをそのまま貼り付けてレンダリングできます。sequenceDiagram ヘッダーではMermaid の矢印の意味(->> 同期呼び出し、-->> 返答、-) 非同期)を使用し;ネイティブの sequence "Title" ヘッダーでは従来の Schematex の意味(->> = 非同期)を保持します。多くのツールが alt/opt/loop/par の共通部分で止まるのに対し、Schematex はすべての12 種類の複合フラグメント演算子と ref インタラクション使用フレームを実装しています。インタラクションが本格的になったとき、プロフェッショナルが必要とするのはこれらの部分だからです。usecase(システムスコープ、メッセージ順序ではない)、state(1 つのオブジェクトのモード、オブジェクト間のメッセージではない)、bpmn(組織プロセス、コールシーケンスではない)とは異なります。

sequence·§
↘ preview
100%
Sequence Diagram — Login flow 4 participants, 8 messages, 1 combined fragments. Login flow User Web App Auth DB alt [credentials valid] [invalid] submit(credentials) verify(credentials) SELECT user row token 200 OK 401 error session cookie set
UTF-8 · LF · 21 lines · 432 chars✓ parsed·1.3 ms·7.9 KB SVG

1. はじめての図

すべてのドキュメントは sequence キーワードとオプションの "title" で始まります。参加者は宣言不要で、メッセージに初めて登場した時点でライフラインになります。

sequence
  Alice -> Bob : Authentication Request
  Bob --> Alice : Authentication Response

ライフラインは初出順に左から右に並びます。-> は同期呼び出し(実線、塗りつぶし矢じり);--> は返答(破線、開き矢じり)。: の後がメッセージラベルです。


2. 参加者

参加者を明示的に宣言して種別の設定、エイリアスの付与、順序の固定を行います。

sequence
  actor User
  participant Web as "Web App"
  boundary LoginUI
  control Auth
  entity Account
  database DB
  collections Sessions
  queue Events
種別レンダリング
participant(デフォルト)角丸分類ボックス
actor棒人間
boundary / control / entityJacobson ロバストネスアイコン(⊢◯ / 矢印付き◯ / 下線付き◯)
databaseシリンダー
collections / queue種別を «ステレオタイプ» として表示したボックス
  • as "Label" で表示名を設定します(クォート省略可;「…」 CJK クォート受け入れ)。
  • カスタムステレオタイプは二重山括弧または ASCII 山括弧を宣言の後ろに付けます。デフォルトラベルを上書きします:actor Printer «system»participant Bus as "Event Bus" <<service>>

3. メッセージ

矢印トークンが UML のセマンティクスを決定します。

DSL意味レンダリング
A -> B同期呼び出し実線、塗りつぶし矢じり
A ->> B非同期シグナル実線、開き矢じり
A --> B返答 / リターン破線、開き矢じり
A -x B消失メッセージ塗りつぶし円で終わる線
o-> B発見メッセージ塗りつぶし円から始まる線
A -> Aセルフメッセージ同じライフラインに戻る曲線ループ

矢印前後のスペースは省略可能(A->B でも動作)で、ラベルは自由テキストです。戻り値も表現できます:例 aHotel -> aHotel : available(roomId, date): isRoom


4. アクティベーション(実行仕様)

ライフライン上の細いバーがアクティブ状態を示します。明示的な文または矢印への + / - サフィックスで開閉します。

sequence
  participant Client
  participant Server
  Client ->+ Server : request()
  Server ->> Server : validate()
  Server -->- Client : response

+ を矢印の後に付けると受信者が到着時にアクティベートされ;- を付けると送信者がメッセージ送信後にディアクティベートされます。明示的な形式は activate X / deactivate X です。同一ライフライン上の重複するバーは水平オフセットで入れ子になります。


5. オブジェクトの生成と破棄

sequence
  participant Factory
  Factory -> *Worker : «create»
  Factory -> Worker : work()
  destroy Worker

受信者の前に * を付けるとメッセージがインスタンス化します。新しいライフラインのヘッドは上部ではなく到着行に描画されます。destroy X でライフラインを ✕ で終了し、時間軸を止めます。


6. 複合フラグメント

複合フラグメントはある領域を囲むラベル付きフレームです。Schematex は UML InteractionOperatorKind のフルセットを実装しています。

演算子意味オペランド
alt選択肢(if/else-if/else)else、それぞれガード付き
optガードが成立する場合のみ実行1 つ、ガード付き
loop繰り返し1 つ;ガードは (min,max) でも可
par並行オペランドand
break例外的な終了1 つ、ガード付き
criticalアトミック / インターリーブなし1 つ
seq / strict弱い / 厳密なシーケンスand
neg無効なトレース(色付きでレンダリング)1 つ
ignore / considerメッセージセットフィルター {m1, m2}1 つ
assert唯一有効な継続1 つ
sequence
  actor User
  participant API
  User -> API : GET /resource
  alt [authorized]
    API --> User : 200 + body
  else [forbidden]
    API --> User : 403
  end

ガードは演算子の後(および else の後)に [括弧] で記述します。フラグメントは入れ子にでき、内側のフレームは親の中にきれいに収まるよう自動でインセットされます。


7. インタラクション使用(ref

インライン展開の代わりに別のインタラクションを参照します。UML が大きな図を合成可能に保つ方法です。

sequence
  participant A
  participant B
  ref over A, B : Establish session
  A -> B : poll()

ref over <lifelines> : Name は指定されたライフラインをまたぐフレームボックスを描画します。多くのツールがこれを省略していますが、実際のシステムで長いフローを分解する方法がこれです。


8. ノート、区切り、不変条件、番号付け

sequence
  autonumber 1 1
  actor Shopper
  participant Cart
  == Phase 1: review ==
  Shopper -> Cart : view items
  note over Cart : cart persisted in Redis
  state Cart : ready
  • note over A / note over A, B / note left of A / note right of A — 折り角アノテーション。
  • == text == — 全幅のセクション区切り。
  • state X : text — ライフライン上の状態不変条件カプセル。
  • autonumber [start] [step] — すべてのメッセージに増分番号を付ける。

9. 文法(EBNF)

diagram      = "sequence" [ string ] NEWLINE statement*
statement    = participant | message | activation | note
             | fragment | ref | divider | invariant | destroy | autonumber

participant  = kind IDENT ("as" label)? stereotype?
kind         = "participant" | "actor" | "boundary" | "control"
             | "entity" | "database" | "collections" | "queue"
stereotype   = "«" TEXT "»" | "<<" TEXT ">>"

message      = IDENT? act? arrow act? ("*")? IDENT? (":" TEXT)?
arrow        = "->" | "->>" | "-->" | "-x" | "o->"
act          = "+" | "-"
activation   = ("activate" | "deactivate") IDENT

fragment     = ("alt"|"opt"|"loop"|"par"|"break"|"critical"|"seq"
               |"strict"|"neg"|"ignore"|"consider"|"assert") guard? NEWLINE
                 statement* (("else"|"and") guard? NEWLINE statement*)* "end"
guard        = "[" TEXT "]" | "(" NUMBER ("," NUMBER)? ")"
ref          = "ref" "over" IDENT ("," IDENT)* ":" TEXT
note         = "note" ("over"|"left of"|"right of") IDENT ("," IDENT)? ":" TEXT
divider      = "==" TEXT "=="
invariant    = "state" IDENT ":" TEXT
destroy      = "destroy" IDENT
autonumber   = "autonumber" NUMBER? NUMBER?

10. 標準準拠

Schematex は OMG UML 2.5.1 §17(インタラクション) 記法に準拠しています:同期(塗りつぶし)と非同期(開き)と返答(破線)の矢じり、発見 / 消失メッセージのエンドポイント、実行仕様バー、ガード付きオペランドを持つ 12 種類すべての複合フラグメント演算子、ref インタラクション使用フレーム、boundary/control/entity 用の Jacobson 分析クラスアイコン。ゲート、コリージョン、時間 / 期間制約は v0.1 のスコープ外(新しいレイアウトプリミティブが必要)であり、後のリリースで追跡される予定です。完全な仕様は docs/reference/33-SEQUENCE-STANDARD.md を参照してください。


関連サンプル

サンプルギャラリーからすぐに使えるシナリオ:

sequence·§ OMG UML 2.5.1 §17 (Interactions)
Sequence Diagram — Login flow 4 participants, 8 messages, 1 combined fragments. Login flow User Web App Auth DB alt [credentials valid] [invalid] submit(credentials) verify(credentials) SELECT user row token 200 OK 401 error session cookie set
Login flow with an alt fragment
An authentication handshake across an actor, a Jacobson control object, and a database lifeline — synchronous calls open activation bars, a dashed reply returns the row, and an alt combined fragment splits the valid/invalid branches. A note records the session-cookie side effect.
business & operations
sequence·§ OMG UML 2.5.1 §17 (Interactions)
Sequence Diagram — OAuth 2.0 Authorization Code 5 participants, 16 messages, 1 combined fragments. OAuth 2.0 Authorization Code User Browser Web App Auth Server User Store alt [token exchange ok] [exchange failed] click "Sign in" GET /login 302 → Auth Server GET /authorize consent screen approve 302 + auth code GET /callback?code POST /token (code) load user profile access + refresh token Set-Cookie: session signed in 401 Unauthorized retry
OAuth 2.0 authorization-code login
The canonical browser-based sign-in handshake as a UML sequence diagram — redirect to the auth server, user consent, code-for-token exchange, and an alt fragment for the success vs. failure branch, with activation bars tracking who is busy at each step.
software & it
sequence·§ OMG UML 2.5.1 §17 (Interactions)
Sequence Diagram 2 participants, 3 messages, 0 combined fragments. Factory Worker «create» external trigger fire-and-forget
Object lifecycle — create, found, lost, destroy
The four UML interaction lifecycle markers in one short diagram — a create message draws its arrow to the new participant's box, a found message starts from a filled circle, a lost message ends at one, and destroy terminates a lifeline with an ✕.
education
sequence·§ OMG UML 2.5.1 §17 (Interactions)
Sequence Diagram — Order processing (saga) 6 participants, 13 messages, 2 combined fragments. Order processing (saga) Customer API Gateway Order Service Payment Service Inventory Service «queue» Event Bus par alt [payment captured && stock reserved] [payment failed] ref Validate cart & price POST /orders createOrder() charge(card) reserve(items) paid reserved OrderConfirmed 201 Created confirmation release(items) OrderCancelled 402 Payment Required declined
Microservices order saga
A distributed order-processing saga showing the parts of UML sequence notation that generic tools omit — a ref interaction-use frame, a par fragment for concurrent service calls, asynchronous event-bus messages, and an alt fragment with a compensating rollback on payment failure.
software & it

Found this useful?

Schematex is free, fully open source, and zero-dependency. A star helps other developers discover it.