ステートダイアグラム

ステートダイアグラムは、システムまたはオブジェクトが存在しうるさまざまな状態、およびそれらの状態間の遷移を視覚的に表現します。これらは、システムが時間の経過とともに異なるイベントにどのように応答するかを捉え、システムの動的な動作をモデリングする上で不可欠です。ステートダイアグラムはシステムのライフサイクルを描写し、その挙動を理解、設計、最適化するのを容易にします。

PlantUML を使用してステートダイアグラムを作成することは、いくつかの利点を提供します:
  • テキストベースの言語:手描きの手間なく、迅速に状態と遷移を定義し、視覚化します。
  • 効率と一貫性:ダイアグラムの作成を効率的に行い、簡単なバージョンコントロールを確保します。
  • 多様性:さまざまなドキュメンテーションプラットフォームと統合し、複数の出力フォーマットをサポートします。
  • オープンソース&コミュニティサポート強力なコミュニティにサポートされ、絶えずその強化に貢献し、貴重なリソースを提供します。

簡単なステート

ステート図の始点と終点は、 [*] で示します。

矢印は、 --> で示します。

🎉 Copied!

@startuml

[*] --> State1
State1 --> [*]
State1 : this is a string
State1 : this is another string

State1 -> State2
State2 --> [*]

@enduml

ステートの表現を変える

hide empty descriptionを使用し、ステート枠をシンプルにできます。

🎉 Copied!

@startuml
hide empty description
[*] --> State1
State1 --> [*]
State1 : this is a string
State1 : this is another string

State1 -> State2
State2 --> [*]
@enduml

合成状態

状態は合成することもできます。キーワードstateと中括弧を使用して定義します。

内部サブ状態

🎉 Copied!

@startuml
scale 350 width
[*] --> NotShooting

state NotShooting {
  [*] --> Idle
  Idle --> Configuring : EvConfig
  Configuring --> Idle : EvConfig
}

state Configuring {
  [*] --> NewValueSelection
  NewValueSelection --> NewValuePreview : EvNewValue
  NewValuePreview --> NewValueSelection : EvNewValueRejected
  NewValuePreview --> NewValueSelection : EvNewValueSaved

  state NewValuePreview {
     State1 -> State2
  }

}
@enduml

サブ状態からサブ状態へ

🎉 Copied!

@startuml
state A {
  state X {
  }
  state Y {
  }
}
 
state B {
  state Z {
  }
}

X --> Z
Z --> Y
@enduml

[Ref. QA-3300]

長い名前

キーワードstateによって、状態についての長めの記述を使用することができます。

🎉 Copied!

@startuml
scale 600 width

[*] -> State1
State1 --> State2 : Succeeded
State1 --> [*] : Aborted
State2 --> State3 : Succeeded
State2 --> [*] : Aborted
state State3 {
  state "Accumulate Enough Data\nLong State Name" as long1
  long1 : Just a test
  [*] --> long1
  long1 --> long1 : New Data
  long1 --> ProcessData : Enough Data
}
State3 --> State3 : Failed
State3 --> [*] : Succeeded / Save Result
State3 --> [*] : Aborted

@enduml

履歴

[H]でサブ状態の履歴、[H*]でサブ状態の深い履歴(deep history)を定義します。

🎉 Copied!

@startuml
[*] -> State1
State1 --> State2 : Succeeded
State1 --> [*] : Aborted
State2 --> State3 : Succeeded
State2 --> [*] : Aborted
state State3 {
  state "Accumulate Enough Data" as long1
  long1 : Just a test
  [*] --> long1
  long1 --> long1 : New Data
  long1 --> ProcessData : Enough Data
  State2 --> [H]: Resume
}
State3 --> State2 : Pause
State2 --> State3[H*]: DeepResume
State3 --> State3 : Failed
State3 --> [*] : Succeeded / Save Result
State3 --> [*] : Aborted
@enduml

フォーク(非同期実行)

<<fork>><<join>> stereotypesを使うことで、フォークノードとジョインノードを表せます。

🎉 Copied!

@startuml

state fork_state <<fork>>
[*] --> fork_state
fork_state --> State2
fork_state --> State3

state join_state <<join>>
State2 --> join_state
State3 --> join_state
join_state --> State4
State4 --> [*]

@enduml

同時状態

記号--または|| で分離することで、合成状態の中に同時状態を定義することができます。

水平セパレータ--

🎉 Copied!

@startuml
[*] --> Active

state Active {
  [*] -> NumLockOff
  NumLockOff --> NumLockOn : EvNumLockPressed
  NumLockOn --> NumLockOff : EvNumLockPressed
  --
  [*] -> CapsLockOff
  CapsLockOff --> CapsLockOn : EvCapsLockPressed
  CapsLockOn --> CapsLockOff : EvCapsLockPressed
  --
  [*] -> ScrollLockOff
  ScrollLockOff --> ScrollLockOn : EvCapsLockPressed
  ScrollLockOn --> ScrollLockOff : EvCapsLockPressed
}

@enduml

垂直セパレータ||

🎉 Copied!

@startuml
[*] --> Active

state Active {
  [*] -> NumLockOff
  NumLockOff --> NumLockOn : EvNumLockPressed
  NumLockOn --> NumLockOff : EvNumLockPressed
  ||
  [*] -> CapsLockOff
  CapsLockOff --> CapsLockOn : EvCapsLockPressed
  CapsLockOn --> CapsLockOff : EvCapsLockPressed
  ||
  [*] -> ScrollLockOff
  ScrollLockOff --> ScrollLockOn : EvCapsLockPressed
  ScrollLockOn --> ScrollLockOff : EvCapsLockPressed
}

@enduml

[Ref. QA-3086]
WARNING
 This translation need to be updated. 
WARNING

条件

<<choice>>ステレオタイプで条件付きの状態を表すことができます。

🎉 Copied!

@startuml
state "Req(Id)" as ReqId <<sdlreceive>>
state "Minor(Id)" as MinorId
state "Major(Id)" as MajorId
 
state c <<choice>>
 
Idle --> ReqId
ReqId --> c
c --> MinorId : [Id <= 10]
c --> MajorId : [Id > 10]
@enduml

全ステレオタイプの例(choice, fork, join, end)

🎉 Copied!

@startuml
state choice1 <<choice>>
state fork1   <<fork>>
state join2   <<join>>
state end3    <<end>>

[*]     --> choice1 : from start\nto choice
choice1 --> fork1   : from choice\nto fork
choice1 --> join2   : from choice\nto join
choice1 --> end3    : from choice\nto end

fork1   ---> State1 : from fork\nto state
fork1   --> State2  : from fork\nto state

State2  --> join2   : from state\nto join
State1  --> [*]     : from state\nto end

join2   --> [*]     : from join\nto end
@enduml

[Ref. QA-404 and QA-1159]

[Ref. QA-404, QA-1159 and GH-887]
WARNING
 This translation need to be updated. 
WARNING

入場点と退場点

<<entryPoint>>ステレオタイプで入場点、<<exitPoint>>ステレオタイプで退場点を追加することができます。

🎉 Copied!

@startuml
state Somp {
  state entry1 <<entryPoint>>
  state entry2 <<entryPoint>>
  state sin
  entry1 --> sin
  entry2 -> sin
  sin -> sin2
  sin2 --> exitA <<exitPoint>>
}

[*] --> entry1
exitA --> Foo
Foo1 -> entry2
@enduml

WARNING
 This translation need to be updated. 
WARNING

入力ピンと出力ピン

<<inputPin>>ステレオタイプで入力ピン、<<outputPin>>ステレオタイプで出力ピンを追加することができます。

🎉 Copied!

@startuml
state Somp {
  state entry1 <<inputPin>>
  state entry2 <<inputPin>>
  state sin
  entry1 --> sin
  entry2 -> sin
  sin -> sin2
  sin2 --> exitA <<outputPin>>
}

[*] --> entry1
exitA --> Foo
Foo1 -> entry2
@enduml

[Ref. QA-4309]

WARNING
 This translation need to be updated. 
WARNING

展開

<<expansionInput>><<expansionOutput>>ステレオタイプにより、展開(expansion)を追加することができます。

🎉 Copied!

@startuml
state Somp {
  state entry1 <<expansionInput>>
  state entry2 <<expansionInput>>
  state sin
  entry1 --> sin
  entry2 -> sin
  sin -> sin2
  sin2 --> exitA <<expansionOutput>>
}

[*] --> entry1
exitA --> Foo
Foo1 -> entry2
@enduml

[Ref. QA-4309]

WARNING
 This translation need to be updated. 
WARNING

矢印の方向

記号->を水平矢印として使用でき、以下の構文を使用することで、矢印の方向を指定することができます。
  • -down-> or -->
  • -right-> or -> (デフォルトの矢印)
  • -left->
  • -up->

🎉 Copied!

@startuml

[*] -up-> First
First -right-> Second
Second --> Third
Third -left-> Last

@enduml

方向を示す単語の、最初の文字だけ(例:-down-の代わりに-d-)、または2文字(-do-)を使用することで、矢印の記述を短くすることができます。

この機能を乱用しないよう注意しなくてはいけません:通常、Graphvizは微調整なしでよい結果をもたらしてくれます。

線の色とスタイルを変更する

線のとスタイルを変更することができます。

🎉 Copied!

@startuml
State S1
State S2
S1 -[#DD00AA]-> S2
S1 -left[#yellow]-> S3
S1 -up[#red,dashed]-> S4
S1 -right[dotted,#blue]-> S5

X1 -[dashed]-> X2
Z1 -[dotted]-> Z2
Y1 -[#blue,bold]-> Y2
@enduml

[Ref. Incubation: Change line color in state diagrams]

注釈

キーワード note left of, note right of, note top of, note bottom of を使用して注釈を定義することができます。

また、複数行の注釈を定義することもできます。

🎉 Copied!

@startuml

[*] --> Active
Active --> Inactive

note left of Active : this is a short\nnote

note right of Inactive
  A note can also
  be defined on
  several lines
end note

@enduml

さらに、状態にひもづかない注釈を定義できます。

🎉 Copied!

@startuml

state foo
note "This is a floating note" as N1

@enduml

リンクへの注釈

note on linkキーワードで、状態遷移(リンク)に注釈を追加することができます。

🎉 Copied!

@startuml
[*] -> State1
State1 --> State2
note on link 
  this is a state-transition note 
end note
@enduml

その他の注釈

合成状態にも注釈をつけることができます。

🎉 Copied!

@startuml

[*] --> NotShooting

state "Not Shooting State" as NotShooting {
  state "Idle mode" as Idle
  state "Configuring mode" as Configuring
  [*] --> Idle
  Idle --> Configuring : EvConfig
  Configuring --> Idle : EvConfig
}

note right of NotShooting : This is a note on a composite state

@enduml

インライン色指定

🎉 Copied!

@startuml
state CurrentSite #pink {
    state HardwareSetup #lightblue {
       state Site #brown
        Site -[hidden]-> Controller
        Controller -[hidden]-> Devices
    }
    state PresentationSetup{
        Groups -[hidden]-> PlansAndGraphics
    }
    state Trends #FFFF77
    state Schedule #magenta
    state AlarmSupression
}
@enduml

[Ref. QA-1812]

skinparam

skinparam コマンドを使用すると、ダイアグラムの色やフォントを変更できます。

このコマンドは以下の場面で使用できます。

ステレオタイプが指定された状態に対して、特定の色とフォントを定義することができます。

🎉 Copied!

@startuml
skinparam backgroundColor LightYellow
skinparam state {
  StartColor MediumBlue
  EndColor Red
  BackgroundColor Peru
  BackgroundColor<<Warning>> Olive
  BorderColor Gray
  FontName Impact
}

[*] --> NotShooting

state "Not Shooting State" as NotShooting {
  state "Idle mode" as Idle <<Warning>>
  state "Configuring mode" as Configuring
  [*] --> Idle
  Idle --> Configuring : EvConfig
  Configuring --> Idle : EvConfig
}

NotShooting --> [*]
@enduml

すべての状態遷移図特有のskinparamのテスト

🎉 Copied!

@startuml
skinparam State {
  AttributeFontColor blue
  AttributeFontName serif
  AttributeFontSize  9
  AttributeFontStyle italic
  BackgroundColor palegreen
  BorderColor violet
  EndColor gold
  FontColor red
  FontName Sanserif
  FontSize 15
  FontStyle bold
  StartColor silver
}

state A : a a a\na
state B : b b b\nb

[*] -> A  : start
A -> B : a2b
B -> [*] : end
@enduml

スタイル変更

スタイルを変更できます。

🎉 Copied!

@startuml

<style>
stateDiagram {
  BackgroundColor Peru
  'LineColor Gray
  FontName Impact
  FontColor Red
  arrow {
    FontSize 13
    LineColor Blue
  }
}
</style>


[*] --> NotShooting

state "Not Shooting State" as NotShooting {
  state "Idle mode" as Idle <<Warning>>
  state "Configuring mode" as Configuring
  [*] --> Idle
  Idle --> Configuring : EvConfig
  Configuring --> Idle : EvConfig
}

NotShooting --> [*]
@enduml

[Ref. GH-880]
WARNING
 This translation need to be updated. 
WARNING

状態の色とスタイルを変更する(インラインスタイル)

個別の状態ごとにとスタイルを変更するには、次の記法を使用します:

  • #color ##[style]color

最初に背景色(#color)を書き、次に線のスタイルと色(##[style]color )を書きます。

🎉 Copied!

@startuml
state FooGradient #red-green ##00FFFF
state FooDashed #red|green ##[dashed]blue {
}
state FooDotted ##[dotted]blue {
}
state FooBold ##[bold] {
}
state Foo1 ##[dotted]green {
state inner1 ##[dotted]yellow
}

state out ##[dotted]gold

state Foo2 ##[bold]green {
state inner2 ##[dotted]yellow
}
inner1 -> inner2
out -> inner2
@enduml

[Ref. QA-1487]

  • #color;line:color;line.[bold|dashed|dotted];text:color

FIXME
🚩 text:color seems not to be taken into account
FIXME

🎉 Copied!

@startuml
@startuml
state FooGradient #red-green;line:00FFFF
state FooDashed #red|green;line.dashed;line:blue {
}
state FooDotted #line.dotted;line:blue {
}
state FooBold #line.bold {
}
state Foo1 #line.dotted;line:green {
state inner1 #line.dotted;line:yellow
}

state out #line.dotted;line:gold

state Foo2 #line.bold;line:green {
state inner2 #line.dotted;line:yellow
}
inner1 -> inner2
out -> inner2
@enduml
@enduml

🎉 Copied!

@startuml
state s1 : s1 description
state s2 #pink;line:red;line.bold;text:red : s2 description
state s3 #palegreen;line:green;line.dashed;text:green : s3 description
state s4 #aliceblue;line:blue;line.dotted;text:blue   : s4 description
@enduml

[Adapted from QA-3770]

別名

aliasを使用して状態に別名を付けることができます:

🎉 Copied!

@startuml
state alias1 
state "alias2"
state "long name" as alias3
state alias4 as "long name"

alias1 : ""state alias1""
alias2 : ""state "alias2"""
alias3 : ""state "long name" as alias3""
alias4 : ""state alias4 as "long name"""

alias1 -> alias2
alias2 -> alias3
alias3 -> alias4
@enduml

もしくは、

🎉 Copied!

@startuml
state alias1 : ""state alias1""
state "alias2" : ""state "alias2"""
state "long name" as alias3 : ""state "long name" as alias3""
state alias4 as "long name" : ""state alias4 as "long name"""

alias1 -> alias2
alias2 -> alias3
alias3 -> alias4
@enduml

Display JSON Data on State diagram

Simple example

🎉 Copied!

@startuml
state "A" as stateA
state "C" as stateC {
 state B
}

json jsonJ {
   "fruit":"Apple",
   "size":"Large",
   "color": ["Red", "Green"]
}
@enduml

[Ref. QA-17275]

For another example, see on JSON page.


Privacy Policy      Advertise