PlantUML权威教程-时序图
生活随笔
收集整理的這篇文章主要介紹了
PlantUML权威教程-时序图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- PlantUML
- 時序圖
- 簡單的時序圖
- participant改變先后順序
- 使用as重命名參與者
- order改變參與者的順序
- 使用非字母符號
- 修改箭頭樣式
- 修改箭頭的箭頭的顏色
- 對消息序列進行編號
- 組合消息
- 添加注釋
- 改變注釋的形狀
- 使用ref實現引用
- 延時效果
- 使用`|||`增加空間像素
- 生命線的開始和撤銷
- 生命線的嵌套
- 創建參與者
- 進入和發出消息
- 構造類型和圈點
- 多行標題
- hide footbox
- 增加外觀參數
PlantUML
時序圖
簡單的時序圖
@startuml simple ' 你可以用 -> 來繪制參與者之間傳遞的消息,而不必顯式地聲明參與者。 ' 你也可以使用 --> 繪制一個虛線箭頭。 ' 另外,你還能用 <- 和 <--,這不影響繪圖,但可以提高可讀性。注意:僅適用于時序圖,對于其它示意 ' 圖,規則是不同的。Alice -> Bob: Authentication Request Bob --> Alice: Authentication ResponseAlice -> Bob: Another authentication Request Alice <-- Bob: another authentication Response@endumlparticipant改變先后順序
關鍵字 participant 用于改變參與者的先后順序。
你也可以使用其它關鍵字來聲明參與者:
? actor
? boundary
? control
? entity
? database
使用as重命名參與者
@startuml sequence_as 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 @endumlorder改變參與者的順序
? 使用order改變參與者的順序,order的順序越小越靠前
@startuml sequence_order ' 通過order 定義參與者順序 participant Last order 30 participant Middle order 20 participant First order 10 @enduml使用非字母符號
@startuml sequence_no_alphabet ' 引號中可以使用非字母符號 ' 你可以使用引號定義參與者,還可以使用關鍵字 as 給參與者定義別名 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修改箭頭樣式
? 使用不同樣式的箭頭
@startuml arrow_num' ? 表示一條丟失的消息:末尾加 x ' ? 讓箭頭只有上半部分或者下半部分:將 < 和 > 替換成 \ 或者 / ' ? 細箭頭:將箭頭標記寫兩次 (如 >> 或 //) ' ? 虛線箭頭:用 -- 替代 - ' ? 箭頭末尾加圈:->o ' ? 雙向箭頭:<->Bob ->x Alice : 末尾加 x Bob -> Alice : 常規箭頭 Bob ->> Alice : 細箭頭 Bob -\ Alice : 粗箭頭只保留上半部分 Bob \\- Alice : 細箭頭 只保留下半部分 Bob //-- Alice : 細箭頭只保留上半部分 Bob ->o Alice : 實線箭頭 帶圓圈 Bob o\\-- Alice : 虛線細箭頭 只保留一半 帶圓圈 Bob <-> Alice : 雙向實線箭頭 Bob <->o Alice : 雙向實線箭頭 右側帶圓圈@enduml修改箭頭的箭頭的顏色
@startuml change_arrow_color Bob -[#red]> Alice : hello Alice -[#0000FF]->Bob : ok @enduml對消息序列進行編號
@startuml autonumber autonumber Bob -> Alice : Authentication Request Bob <- Alice : Authentication Response @enduml @startuml autonumber_start_increment' 語句 autonumber start 用于指定編號的初始值,而 autonumber start increment 可以同時指定編號 ' 的初始值和每次增加的值。 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的方式指定
@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 @startuml autonumber_stop 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組合消息
@startuml alt ' 我們可以通過以下關鍵詞將組合消息: ' ? alt/else ' ? opt ' ? loop ' ? par ' ? break ' ? critical ' ? group, 后面緊跟著消息內容 ' 可以在標頭 (header) 添加需要顯示的文字 (group 除外)。 ' 關鍵詞 end 用來結束分組。 ' 注意,分組可以嵌套使用 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添加注釋
我們可以使用note left或者note right的形式為時序圖添加注釋,使用end note添加多行注釋
@startuml sequence_with_notes 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 @startuml note_over ' 可以使用 note left of,note right of 或 note over 在節點 (participant) 的相對位置放置注釋。 ' 還可以通過修改背景色來高亮顯示注釋。 ' 以及使用關鍵字 end note 來添加多行注釋 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改變注釋的形狀
@startuml rnote_hnote ' 你可以使用 hnote 和 rnote 這兩個關鍵字來修改備注框的形狀 caller -> server : conReq hnote over caller : idle caller <- server : conConf rnote over server "r" as rectangle "h" as hexagon end rnote @enduml使用ref實現引用
@startuml ref participant Alice actor Bob ref over Alice, Bob : init Alice -> Bob : hello ref over Bob This can be on several lines end ref @enduml延時效果
@startuml delay ' 你可以使用... 來表示延遲,并且還可以給延遲添加注釋 Alice -> Bob: Authentication Request ... Bob --> Alice: Authentication Response ...5 minutes latter... Bob --> Alice: Bye ! @enduml使用|||增加空間像素
@startuml spqce ' 你可以使用 ||| 來增加空間。 ' 還可以使用數字指定增加的像素的數量。 Alice -> Bob: message 1 Bob --> Alice: ok ||| Alice -> Bob: message 2 Bob --> Alice: ok ||45|| Alice -> Bob: message 3 Bob --> Alice: ok @enduml生命線的開始和撤銷
@startuml life_line 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生命線的嵌套
@startuml squence_lifeline 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創建參與者
@startuml create_sequence ' 你可以把關鍵字 create 放在第一次接收到消息之前,以強調本次消息實際上是在創建新的對象 Bob -> Alice : hello create Other Alice -> Other : new create control String Alice -> String note right : You can also put notes! Alice --> Bob : ok@enduml進入和發出消息
如果只想關注部分圖示,你可以使用進入和發出箭頭。
使用方括號 [和] 表示圖示的左、右兩側。
構造類型和圈點
@startuml color_sequence ' 可以使用 << 和 >> 給參與者添加構造類型。 ' 在構造類型中,你可以使用 (X,color) 格式的語法添加一個圓圈圈起來的字符participant "Famous Bob" as Bob << Generated >> participant Alice << (C,#ADD1B2) Testable >> Bob->Alice: First message @enduml @startuml sequence_with_circle' 默認使用 guillemet 字符來顯示構造類型。你可以使用外觀參數 guillemet 來修改顯示行為。 skinparam guillemet false participant "Famous Bob" as Bob << Generated >> participant Alice << (C,#ADD1B2) Testable >>Bob->Alice: First message @enduml @startuml new_circle participant Bob << (C,#ADD1B2) >> participant Alice << (C,#ADD1B2) >> Bob->Alice: First message @enduml @startuml coreole title __Simple__ **communication** example Alice -> Bob: Authentication Request Bob -> Alice: Authentication Response @enduml
[外鏈圖片轉存失敗(img-JyPt3nSy-1564328385944)(/work/linux-sys/UML/out/時序圖/sequence_with_coreole/coreole.png)]
多行標題
@startuml title_endtitle 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 @endumlhide footbox
@startuml hide foot boot hide footbox title Footer removed Alice -> Bob: Authentication Request Bob --> Alice: Authentication Response @enduml增加外觀參數
用 skinparam 改變字體和顏色。
可以在如下場景中使用:
? 在圖示的定義中,
? 在引入的文件中,
? 在命令行或者 ANT 任務提供的配置文件中。
你也可以修改其他渲染元素,如以下示例:
總結
以上是生活随笔為你收集整理的PlantUML权威教程-时序图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 作者:洪文兴(1980-),男,厦门大学
- 下一篇: 作者:张坦(1989-),女,西安交通大