PlantUML權威教程-時序圖

PlantUML

時序圖

簡單的時序圖

@startuml simple 
' 你可以用 -> 來繪製參與者之間傳遞的消息,而不必顯式地聲明參與者。
' 你也可以使用 --> 繪製一個虛線箭頭。
' 另外,你還能用 <- 和 <--,這不影響繪圖,但可以提高可讀性。注意:僅適用於時序圖,對於其它示意
' 圖,規則是不同的。

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

Alice -> Bob: Another authentication Request
Alice <-- Bob: another authentication Response


@enduml 

在這裏插入圖片描述

participant改變先後順序

關鍵字 participant 用於改變參與者的先後順序。
你也可以使用其它關鍵字來聲明參與者:
• actor
• boundary
• control
• entity
• database

@startuml participant

actor Foo1
boundary Foo2
control Foo3
entity Foo4
database Foo5 

collections Foo6
Foo1 -> Foo2 : To boundary
Foo1 -> Foo3 : To control
Foo1 -> Foo4 : To entity
Foo1 -> Foo5 : To database
Foo1 -> Foo6 : To collections

@enduml

在這裏插入圖片描述

使用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
@enduml

在這裏插入圖片描述

order改變參與者的順序

​ 使用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 sequene_[]
[-> A: DoWork
activate A
A -> A: Internal call
activate A
A ->] : << createRequest >>
A<--] : RequestCreated
deactivate A
[<- A: Done
deactivate A
@enduml

在這裏插入圖片描述

@startuml 
[-> Bob
[o-> Bob
[o->o Bob
[x-> Bob
[<- Bob
[x<- Bob
Bob ->]
Bob ->o]
Bob o->o]
Bob ->x]
Bob <-]
Bob x<-]
@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
@enduml

在這裏插入圖片描述

hide footbox

@startuml hide foot boot
hide footbox
title Footer removed
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
@enduml

在這裏插入圖片描述

增加外觀參數

用 skinparam 改變字體和顏色。
可以在如下場景中使用:
• 在圖示的定義中,
• 在引入的文件中,
• 在命令行或者 ANT 任務提供的配置文件中。
你也可以修改其他渲染元素,如以下示例:

@startuml skinparam
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


在這裏插入圖片描述

@startuml sikin
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 

在這裏插入圖片描述

@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

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章