--- lang: ja-jp breaks: true --- # HackMD Markdown 内で PlantUML の シーケンス図 を書く 2021-03-17 ```plantuml @startuml Foo -> Bar : メッセージ Foo <-- Bar @enduml ``` ## シーケンス図 > https://plantuml.com/ja/sequence-diagram ### 基本的な例 ```plantuml @startuml Alice -> Bob: Authentication Request Bob --> Alice: Authentication Response Alice -> Bob: Another authentication Request Alice <-- Bob: another authentication Response @enduml ``` ### 分類子の宣言 ```plantuml @startuml participant Participant as Foo actor Actor as Foo1 boundary Boundary as Foo2 control Control as Foo3 entity Entity as Foo4 database Database as Foo5 collections Collections as Foo6 queue Queue as Foo7 Foo -> Foo1 : To actor Foo -> Foo2 : To boundary Foo -> Foo3 : To control Foo -> Foo4 : To entity Foo -> Foo5 : To database Foo -> Foo6 : To collections Foo -> Foo7: To queue @enduml ``` ### キーワード as を使って分類子の名前を変更 ```plantuml @startuml actor Bob #red ' The only difference between actor 'and participant is the drawing participant Alice participant "I have a really\nlong name" as L #99FF99 /' You can also declare: participant L as "I have a really\nlong name" #99FF99 '/ Alice->Bob: Authentication Request Bob->Alice: Authentication Response Bob->L: Log transaction @enduml ``` ### orderキーワードを使って、分類子が表示される順序を変更 ```plantuml @startuml participant Last order 30 participant Middle order 20 participant First order 10 @enduml ``` ### 分類子名にアルファベット以外を使う ```plantuml @startuml Alice -> "Bob()" : Hello "Bob()" -> "This is very\nlong" as Long ' You can also declare: ' "Bob()" -> Long as "This is very\nlong" Long --> "Bob()" : ok @enduml ``` ### 自分自身へのメッセージ ```plantuml @startuml Alice -> Alice: This is a signal to self.\nIt also demonstrates\nmultiline \ntext @enduml ``` ```plantuml @startuml Alice <- Alice: This is a signal to self.\nIt also demonstrates\nmultiline \ntext @enduml ``` ### テキストの位置 ```plantuml @startuml skinparam sequenceMessageAlign right Bob -> Alice : Request Alice -> Bob : Response @enduml ``` ### 応答メッセージのテキストを矢印の下に表示する ```plantuml @startuml skinparam responseMessageBelowArrow true Bob -> Alice : hello Alice -> Bob : ok @enduml ``` ### 矢印の見た目を変える ```plantuml @startuml Bob ->x Alice Bob -> Alice Bob ->> Alice Bob -\ Alice Bob \\- Alice Bob //-- Alice Bob ->o Alice Bob o\\-- Alice Bob <-> Alice Bob <->o Alice @enduml ``` ### 矢印の色を替える ```plantuml @startuml Bob -[#red]> Alice : hello Alice -[#0000FF]->Bob : ok @enduml ``` ### メッセージシーケンスの番号付け ```plantuml @startuml autonumber Bob -> Alice : Authentication Request Bob <- Alice : Authentication Response @enduml ``` ### autonumber <開始> で開始番号を、また、autonumber <開始> <増分> で増分も指定 ```plantuml @startuml autonumber Bob -> Alice : Authentication Request Bob <- Alice : Authentication Response autonumber 15 Bob -> Alice : Another authentication Request Bob <- Alice : Another authentication Response autonumber 40 10 Bob -> Alice : Yet another authentication Request Bob <- Alice : Yet another authentication Response @enduml ``` ### 一部のHTMLタグを書式に使う ```plantuml @startuml autonumber "<b>[000]" Bob -> Alice : Authentication Request Bob <- Alice : Authentication Response autonumber 15 "<b>(<u>##</u>)" Bob -> Alice : Another authentication Request Bob <- Alice : Another authentication Response autonumber 40 10 "<font color=red><b>Message 0 " Bob -> Alice : Yet another authentication Request Bob <- Alice : Yet another authentication Response @enduml ``` ### autonumber stop と autonumber resume <増分> <書式> を自動採番の一時停止と再開にそれぞれ使用する ```plantuml @startuml autonumber 10 10 "<b>[000]" Bob -> Alice : Authentication Request Bob <- Alice : Authentication Response autonumber stop Bob -> Alice : dummy autonumber resume "<font color=red><b>Message 0 " Bob -> Alice : Yet another authentication Request Bob <- Alice : Yet another authentication Response autonumber stop Bob -> Alice : dummy autonumber resume 1 "<font color=blue><b>Message 0 " Bob -> Alice : Yet another authentication Request Bob <- Alice : Yet another authentication Response @enduml ``` #### 最初の数字を増加させるにはautonumber inc Aを、2番目の数字を増加させるにはautonumber inc Bを使用する ```plantuml @startuml autonumber 1.1.1 Alice -> Bob: Authentication request Bob --> Alice: Response autonumber inc A 'Now we have 2.1.1 Alice -> Bob: Another authentication request Bob --> Alice: Response autonumber inc B 'Now we have 2.2.1 Alice -> Bob: Another authentication request Bob --> Alice: Response autonumber inc A 'Now we have 3.1.1 Alice -> Bob: Another authentication request autonumber inc B 'Now we have 3.2.1 Bob --> Alice: Response @enduml ``` ### 現在のautonumberの値は%autonumber%変数で参照する ```plantuml @startuml autonumber 10 Alice -> Bob note right the <U+0025>autonumber<U+0025> works everywhere. Here, its value is ** %autonumber% ** end note Bob --> Alice: //This is the response %autonumber%// @enduml ``` ### タイトル、ヘッダー、フッター ```plantuml @startuml autonumber 10 Alice -> Bob note right the <U+0025>autonumber<U+0025> works everywhere. Here, its value is ** %autonumber% ** end note Bob --> Alice: //This is the response %autonumber%// @enduml ``` ### 図の分割 ```plantuml @startuml Alice -> Bob : message 1 Alice -> Bob : message 2 newpage Alice -> Bob : message 3 Alice -> Bob : message 4 newpage A title for the\nlast page Alice -> Bob : message 5 Alice -> Bob : message 6 @enduml ``` ### メッセージのグループ化 ```plantuml @startuml Alice -> Bob: Authentication Request alt successful case Bob -> Alice: Authentication Accepted else some kind of failure Bob -> Alice: Authentication Failure group My own label Alice -> Log : Log attack start loop 1000 times Alice -> Bob: DNS Attack end Alice -> Log : Log attack end end else Another type of failure Bob -> Alice: Please repeat end @enduml ``` ### groupの2つ目のラベル ```plantuml @startuml Alice -> Bob: Authentication Request Bob -> Alice: Authentication Failure group My own label [My own label 2] Alice -> Log : Log attack start loop 1000 times Alice -> Bob: DNS Attack end Alice -> Log : Log attack end end @enduml ``` ### メッセージに付けるノート ```plantuml @startuml Alice->Bob : hello note left: this is a first note Bob->Alice : ok note right: this is another note Bob->Bob : I am thinking note left a note can also be defined on several lines end note @enduml ``` ### その他のノート ```plantuml @startuml participant Alice participant Bob note left of Alice #aqua This is displayed left of Alice. end note note right of Alice: This is displayed right of Alice. note over Alice: This is displayed over Alice. note over Alice, Bob #FFAAAA: This is displayed\n over Bob and Alice. note over Bob, Alice This is yet another example of a long note. end note @enduml ``` ### ノートの形を変える [hnote, rnote] ```plantuml @startuml caller -> server : conReq hnote over caller : idle caller <- server : conConf rnote over server "r" as rectangle "h" as hexagon endrnote rnote over server this is on several lines endrnote hnote over caller this is on several lines endhnote @enduml ``` ### すべての分類子にまたがるノート [across] ```plantuml @startuml Alice->Bob:m1 Bob->Charlie:m2 note over Alice, Charlie: Old method for note over all part. with:\n ""note over //FirstPart, LastPart//"". note across: New method with:\n""note across"" Bob->Alice hnote across:Note across all part. @enduml ``` ### 複数のノートを同じレベルに並べる [/] ```plantuml @startuml note over Alice : initial state of Alice note over Bob : initial state of Bob Bob -> Alice : hello @enduml ``` ### /を使った場合(ノートが整列されます) ```plantuml @startuml note over Alice : initial state of Alice / note over Bob : initial state of Bob Bob -> Alice : hello @enduml ``` ### Creole と HTML ```plantuml @startuml participant Alice participant "The **Famous** Bob" as Bob Alice -> Bob : hello --there-- ... Some ~~long delay~~ ... Bob -> Alice : ok note left This is **bold** This is //italics// This is ""monospaced"" This is --stroked-- This is __underlined__ This is ~~waved~~ end note Alice -> Bob : A //well formatted// message note right of Alice This is <back:cadetblue><size:18>displayed</size></back> __left of__ Alice. end note note left of Bob <u:red>This</u> is <color #118888>displayed</color> **<color purple>left of</color> <s:red>Alice</strike> Bob**. end note note over Alice, Bob <w:#FF33FF>This is hosted</w> by <img sourceforge.jpg> end note @enduml ``` ### 境界線(区切り線) ```plantuml @startuml == Initialization == Alice -> Bob: Authentication Request Bob --> Alice: Authentication Response == Repetition == Alice -> Bob: Another authentication Request Alice <-- Bob: another authentication Response @enduml ``` ### リファレンス ```plantuml @startuml participant Alice actor Bob ref over Alice, Bob : init Alice -> Bob : hello ref over Bob This can be on several lines end ref @enduml ``` ### 遅延 ```plantuml @startuml Alice -> Bob: Authentication Request ... Bob --> Alice: Authentication Response ...5 minutes later... Bob --> Alice: Good Bye ! @enduml ``` ### テキストの折り返し ```plantuml @startuml skinparam maxMessageSize 50 participant a participant b a -> b :this\nis\nmanually\ndone a -> b :this is a very long message on several words @enduml ``` ## 間隔 ```plantuml @startuml Alice -> Bob: message 1 Bob --> Alice: ok ||| Alice -> Bob: message 2 Bob --> Alice: ok ||45|| Alice -> Bob: message 3 Bob --> Alice: ok @enduml ``` ### ライフラインの活性化と破棄 ```plantuml @startuml participant User User -> A: DoWork activate A A -> B: << createRequest >> activate B B -> C: DoWork activate C C --> B: WorkDone destroy C B --> A: RequestCreated deactivate B A -> User: Done deactivate A @enduml ``` ### ライフラインはネスト(入れ子に)することができ、色をつけることもできます ```plantuml @startuml participant User User -> A: DoWork activate A #FFBBBB A -> A: Internal call activate A #DarkSalmon A -> B: << createRequest >> activate B B --> A: RequestCreated deactivate B deactivate A A -> User: Done deactivate A @enduml ``` ### 自動的に活性化(autoactivate)することもできます。この場合はreturnキーワードを使用します ```plantuml @startuml autoactivate on alice -> bob : hello bob -> bob : self call bill -> bob #005500 : hello from thread 2 bob -> george ** : create return done in thread 2 return rc bob -> george !! : delete return success @enduml ``` ### Return ```plantuml @startuml Bob -> Alice : hello activate Alice Alice -> Alice : some action return bye @enduml ``` ### 分類子の生成 ```plantuml @startuml Bob -> Alice : hello create Other Alice -> Other : new create control String Alice -> String note right : You can also put notes! Alice --> Bob : ok @enduml ``` ### 活性化、非活性化、生成のショートカット記法 ```plantuml @startuml alice -> bob ++ : hello bob -> bob ++ : self call bob -> bib ++ #005500 : hello bob -> george ** : create return done return rc bob -> george !! : delete return success @enduml ``` ### 同じ行に活性化と非活性化を混ぜることもできます ```plantuml @startuml alice -> bob ++ : hello1 bob -> charlie --++ : hello2 charlie --> alice -- : ok @enduml ``` ```plantuml @startuml alice -> bob --++ #gold: hello bob -> alice --++ #gold: you too alice -> bob --: step1 alice -> bob : step2 @enduml ``` ### インとアウトのメッセージ ```plantuml @startuml [-> A: DoWork activate A A -> A: Internal call activate A A ->] : << createRequest >> A<--] : RequestCreated deactivate A [<- A: Done deactivate A @enduml ``` ### 次の書き方も使えます ```plantuml @startuml participant Alice participant Bob #lightblue Alice -> Bob Bob -> Carol ... [-> Bob [o-> Bob [o->o Bob [x-> Bob ... [<- Bob [x<- Bob ... Bob ->] Bob ->o] Bob o->o] Bob ->x] ... Bob <-] Bob x<-] @enduml ``` ### インとアウトのメッセージに短い矢印を使う ```plantuml @startuml ?-> Alice : ""?->""\n**short** to actor1 [-> Alice : ""[->""\n**from start** to actor1 [-> Bob : ""[->""\n**from start** to actor2 ?-> Bob : ""?->""\n**short** to actor2 Alice ->] : ""->]""\nfrom actor1 **to end** Alice ->? : ""->?""\n**short** from actor1 Alice -> Bob : ""->"" \nfrom actor1 to actor2 @enduml ``` ### アンカーと持続時間 ```plantuml @startuml !pragma teoz true {start} Alice -> Bob : start doing things during duration Bob -> Max : something Max -> Bob : something else {end} Bob -> Alice : finish {start} <-> {end} : some time @enduml ``` ### ステレオタイプとスポット ```plantuml @startuml participant "Famous Bob" as Bob << Generated >> participant Alice << (C,#ADD1B2) Testable >> Bob->Alice: First message @enduml ``` ### スキンパラメータ guillemet を使用してこの動作を変更する ```plantuml @startuml skinparam guillemet false participant "Famous Bob" as Bob << Generated >> participant Alice << (C,#ADD1B2) Testable >> Bob->Alice: First message @enduml ``` ```plantuml @startuml participant Bob << (C,#ADD1B2) >> participant Alice << (C,#ADD1B2) >> Bob->Alice: First message @enduml ``` ### タイトルについての詳細 ```plantuml @startuml title __Simple__ **communication** example Alice -> Bob: Authentication Request Bob -> Alice: Authentication Response @enduml ``` ### タイトルの記述では \n を使用して新しい行を追加する ```plantuml @startuml title __Simple__ communication example\non several lines Alice -> Bob: Authentication Request Bob -> Alice: Authentication Response @enduml ``` ### キーワード title と end title を使うことにより、タイトルを複数行にわたって記述 ```plantuml @startuml title <u>Simple</u> communication example on <i>several</i> lines and using <font color=red>html</font> This is hosted by <img:sourceforge.jpg> end title Alice -> Bob: Authentication Request Bob -> Alice: Authentication Response @enduml ``` ### 分類子の囲み ```plantuml @startuml box "Internal Service" #LightBlue participant Bob participant Alice end box participant Other Bob -> Alice : hello Alice -> Other : hello @enduml ``` ### フッターの除去 ```plantuml @startuml hide footbox title Foot Box removed Alice -> Bob: Authentication Request Bob --> Alice: Authentication Response @enduml ``` ### スキンパラメータ ```plantuml @startuml skinparam sequenceArrowThickness 2 skinparam roundcorner 20 skinparam maxmessagesize 60 skinparam sequenceParticipant underline actor User participant "First Class" as A participant "Second Class" as B participant "Last Class" as C User -> A: DoWork activate A A -> B: Create Request activate B B -> C: DoWork activate C C --> B: WorkDone destroy C B --> A: Request Created deactivate B A --> User: Done deactivate A @enduml ``` ```plantuml @startuml skinparam backgroundColor #EEEBDC skinparam handwritten true skinparam sequence { ArrowColor DeepSkyBlue ActorBorderColor DeepSkyBlue LifeLineBorderColor blue LifeLineBackgroundColor #A9DCDF ParticipantBorderColor DeepSkyBlue ParticipantBackgroundColor DodgerBlue ParticipantFontName Impact ParticipantFontSize 17 ParticipantFontColor #A9DCDF ActorBackgroundColor aqua ActorFontColor DeepSkyBlue ActorFontSize 17 ActorFontName Aapex } actor User participant "First Class" as A participant "Second Class" as B participant "Last Class" as C User -> A: DoWork activate A A -> B: Create Request activate B B -> C: DoWork activate C C --> B: WorkDone destroy C B --> A: Request Created deactivate B A --> User: Done deactivate A @enduml ``` ### パディングの変更 ```plantuml @startuml skinparam ParticipantPadding 20 skinparam BoxPadding 10 box "Foo1" participant Alice1 participant Alice2 end box box "Foo2" participant Bob1 participant Bob2 end box Alice1 -> Bob1 : hello Alice1 -> Out : out @enduml ``` ### 付録:全種類の矢印の例 ```plantuml @startuml participant Alice as a participant Bob as b a -> b : ""-> "" a ->> b : ""->> "" a -\ b : ""-\ "" a -\\ b : ""-\\\\"" a -/ b : ""-/ "" a -// b : ""-// "" a ->x b : ""->x "" a x-> b : ""x-> "" a o-> b : ""o-> "" a ->o b : ""->o "" a o->o b : ""o->o "" a <-> b : ""<-> "" a o<->o b : ""o<->o"" a x<->x b : ""x<->x"" a ->>o b : ""->>o "" a -\o b : ""-\o "" a -\\o b : ""-\\\\o"" a -/o b : ""-/o "" a -//o b : ""-//o "" a x->o b : ""x->o "" @enduml ``` ### 自分自身への矢印 ```plantuml @startuml participant Alice as a participant Bob as b a -> a : ""-> "" a ->> a : ""->> "" a -\ a : ""-\ "" a -\\ a : ""-\\\\"" a -/ a : ""-/ "" a -// a : ""-// "" a ->x a : ""->x "" a x-> a : ""x-> "" a o-> a : ""o-> "" a ->o a : ""->o "" a o->o a : ""o->o "" a <-> a : ""<-> "" a o<->o a : ""o<->o"" a x<->x a : ""x<->x"" a ->>o a : ""->>o "" a -\o a : ""-\o "" a -\\o a : ""-\\\\o"" a -/o a : ""-/o "" a -//o a : ""-//o "" a x->o a : ""x->o "" @enduml ``` ### インのメッセージ('[') ```plantuml @startuml participant Alice as a participant Bob as b [-> b : ""[-> "" [->> b : ""[->> "" [-\ b : ""[-\ "" [-\\ b : ""[-\\\\"" [-/ b : ""[-/ "" [-// b : ""[-// "" [->x b : ""[->x "" [x-> b : ""[x-> "" [o-> b : ""[o-> "" [->o b : ""[->o "" [o->o b : ""[o->o "" [<-> b : ""[<-> "" [o<->o b : ""[o<->o"" [x<->x b : ""[x<->x"" [->>o b : ""[->>o "" [-\o b : ""[-\o "" [-\\o b : ""[-\\\\o"" [-/o b : ""[-/o "" [-//o b : ""[-//o "" [x->o b : ""[x->o "" @enduml ``` ### アウトのメッセージ(']') ```plantuml @startuml participant Alice as a participant Bob as b a ->] : ""->] "" a ->>] : ""->>] "" a -\] : ""-\] "" a -\\] : ""-\\\\]"" a -/] : ""-/] "" a -//] : ""-//] "" a ->x] : ""->x] "" a x->] : ""x->] "" a o->] : ""o->] "" a ->o] : ""->o] "" a o->o] : ""o->o] "" a <->] : ""<->] "" a o<->o] : ""o<->o]"" a x<->x] : ""x<->x]"" a ->>o] : ""->>o] "" a -\o] : ""-\o] "" a -\\o] : ""-\\\\o]"" a -/o] : ""-/o] "" a -//o] : ""-//o] "" a x->o] : ""x->o] "" @enduml ``` ### 短いインのメッセージ('?') ```plantuml @startuml participant Alice as a participant Bob as b a -> b : //Long long label// ?-> b : ""?-> "" ?->> b : ""?->> "" ?-\ b : ""?-\ "" ?-\\ b : ""?-\\\\"" ?-/ b : ""?-/ "" ?-// b : ""?-// "" ?->x b : ""?->x "" ?x-> b : ""?x-> "" ?o-> b : ""?o-> "" ?->o b : ""?->o "" ?o->o b : ""?o->o "" ?<-> b : ""?<-> "" ?o<->o b : ""?o<->o"" ?x<->x b : ""?x<->x"" ?->>o b : ""?->>o "" ?-\o b : ""?-\o "" ?-\\o b : ""?-\\\\o "" ?-/o b : ""?-/o "" ?-//o b : ""?-//o "" ?x->o b : ""?x->o "" @enduml ``` ### 短いアウトのメッセージ('?') ```plantuml @startuml participant Alice as a participant Bob as b a -> b : //Long long label// a ->? : ""->? "" a ->>? : ""->>? "" a -\? : ""-\? "" a -\\? : ""-\\\\?"" a -/? : ""-/? "" a -//? : ""-//? "" a ->x? : ""->x? "" a x->? : ""x->? "" a o->? : ""o->? "" a ->o? : ""->o? "" a o->o? : ""o->o? "" a <->? : ""<->? "" a o<->o? : ""o<->o?"" a x<->x? : ""x<->x?"" a ->>o? : ""->>o? "" a -\o? : ""-\o? "" a -\\o? : ""-\\\\o?"" a -/o? : ""-/o? "" a -//o? : ""-//o? "" a x->o? : ""x->o? "" @enduml ``` ### 特有のskinparam ```plantuml @startuml Bob -> Alice : hello Alice -> Bob : ok @enduml ``` ### ライフラインの設定(lifelineStrategy) ```plantuml @startuml skinparam lifelineStrategy nosolid Bob -> Alice : hello Alice -> Bob : ok @enduml ``` ### シーケンス図のライフラインを実線で表示するには、skinparam lifelineStrategy solidを設定します ```plantuml @startuml skinparam lifelineStrategy solid Bob -> Alice : hello Alice -> Bob : ok @enduml ``` ### 厳密なUMLスタイル(style strictuml) ```plantuml @startuml skinparam style strictuml Bob -> Alice : hello Alice -> Bob : ok @enduml ``` ### 未接続の分類子を表示しない ```plantuml @startuml participant Alice participant Bob participant Carol Alice -> Bob : hello @enduml ``` ### hide unlinkedを指定すると、接続されていない分類子を非表示にできます ```plantuml @startuml hide unlinked participant Alice participant Bob participant Carol Alice -> Bob : hello @enduml ``` ### グループメッセージに色を付ける ```plantuml @startuml Alice -> Bob: Authentication Request alt#Gold #LightBlue Successful case Bob -> Alice: Authentication Accepted else #Pink Failure Bob -> Alice: Authentication Rejected end @enduml ``` ### メインフレーム ```plantuml @startuml mainframe This is a **mainframe** Alice->Bob : Hello @enduml ``` ###### tags: `HackMD` `MarkDown` `PlantUML` `シーケンス図`