簡單示例(Basic examples)
你可以用 ->
來繪制參與者之間傳遞的消息刘陶, 而不必顯式地聲明參與者动壤。
你也可以使用 "-->
" 繪制一個虛線箭頭锅风。
另外酥诽,你還能用 "<-
" 和 "<--
",這不影響繪圖皱埠,但可以提高可讀性肮帐。 注意:僅適用于時序圖,對于其它示意圖边器,規(guī)則是不同的训枢。
@startuml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
Alice -> Bob: Another authentication Request
Alice <-- Bob: another authentication Response
@enduml
聲明參與者(Declaring participant)
關鍵字 participant
用于改變參與者的先后順序。
你也可以使用其它關鍵字來聲明參與者:
actor
boundary
control
entity
database
@startuml
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
用于重命名參與者
你可以使用RGB值或者顏色名修改 actor 或參與者的背景顏色饰抒。
@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
在參與者中使用非字母符號(Use non-letters in participants)
你可以使用引號定義參與者肮砾,還可以用關鍵字 as
給參與者定義別名。
@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
給自己發(fā)消息(Message to self)
參與者可以給自己發(fā)信息袋坑,
消息文字可以用\n
來換行仗处。
@startuml
Alice->Alice: This is a signal to self.\nIt also demonstrates\nmultiline \ntext
@enduml
修改箭頭樣式(Change arrow style)
修改箭頭樣式的方式有以下幾種:
- 表示一條丟失的消息:末尾加
x
- 讓箭頭只有上半部分或者下半部分:將
<
和>
替換成\
或者/
- 細箭頭:將箭頭標記寫兩次 (如
>>
或//
) - 虛線箭頭:用
--
替代-
- 箭頭末尾加圈:
->o
- 雙向箭頭:
<->
@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
修改箭頭顏色(Change arrow color)
你可以用以下記號修改箭頭的顏色:
@startuml
Bob -[#red]> Alice : hello
Alice -[#0000FF]->Bob : ok
@enduml
對消息序列編號(Message sequence numbering)
關鍵字 autonumber
用于自動對消息編號眯勾。
@startuml
autonumber
Bob -> Alice : Authentication Request
Bob <- Alice : Authentication Response
@enduml
語句 autonumber 'start'
用于指定編號的初始值,而 autonumber 'start' 'increment'
可以同時指定編號的初始值和每次增加的值婆誓。
@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
你可以在雙引號內(nèi)指定編號的格式吃环。
格式是由 Java 的DecimalFormat
類實現(xiàn)的: ('0
' 表示數(shù)字;'\#
' 也表示數(shù)字洋幻,但默認為0)郁轻。
你也可以用 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
你還可以用語句 autonumber stop
和 autonumber resume 'increment' 'format'
來表示暫臀牧簦或繼續(xù)使用自動編號好唯。
@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
分割示意圖(Splitting diagrams)
關鍵字 newpage
用于把一張圖分割成多張。
在 newpage
之后添加文字燥翅,作為新的示意圖的標題骑篙。
這樣就能很方便地在 Word 中將長圖分幾頁打印。
@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
組合消息
我們可以通過以下關鍵詞將組合消息:
alt/else
opt
loop
par
break
critical
-
group
, 后面緊跟著消息內(nèi)容
可以在標頭(header)添加需要顯示的文字(group
除外)森书。
關鍵詞end
用來結束分組靶端。
注意,分組可以嵌套使用凛膏。
@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
給消息添加注釋
我們可以通過在消息后面添加 note left
或者 note right
關鍵詞來給消息添加注釋杨名。
你也可以通過使用 end note
來添加多行注釋。
@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
其他的注釋
可以使用 note left of
猖毫, note right of
或 note over
在節(jié)點(participant)的相對位置放置注釋台谍。
還可以通過修改背景色來高亮顯示注釋。
以及使用關鍵字end note
來添加多行注釋鄙麦。
@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
這兩個關鍵字來修改備注框的形狀典唇。
@startuml
caller -> server : conReq
hnote over caller : idle
caller <- server : conConf
rnote over server
"r" as rectangle
"h" as hexagon
endrnote
@enduml
Creole和HTML
@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
分隔符
你可以通過使用 ==
關鍵詞來將你的圖表分割多個步驟胯府。
@startuml
== Initialization ==
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
== Repetition ==
Alice -> Bob: Another authentication Request
Alice <-- Bob: another authentication Response
@enduml
引用
你可以在圖中通過使用 ref over
關鍵詞來實現(xiàn)引用
@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
延遲
你可以使用 ...
來表示延遲,并且還可以給延遲添加注釋恨胚。
@startuml
Alice -> Bob: Authentication Request
...
Bob --> Alice: Authentication Response
...5 minutes latter...
Bob --> Alice: Bye !
@enduml
空間
你可以使用 |||
來增加空間骂因。
還可以使用數(shù)字指定增加的像素的數(shù)量。
@startuml
Alice -> Bob: message 1
Bob --> Alice: ok
|||
Alice -> Bob: message 2
Bob --> Alice: ok
||45||
Alice -> Bob: message 3
Bob --> Alice: ok
@enduml
生命線的激活與撤銷
關鍵字 activate
和 deactivate
用來表示參與者的生命活動赃泡。
一旦參與者被激活寒波,它的生命線就會顯示出來。
activate
和deactivate
適用于以上情形升熊。
destroy
表示一個參與者的生命線的終結俄烁。
@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
還可以使用嵌套的生命線,并且運行給生命線添加顏色级野。
@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
創(chuàng)建參與者
你可以把關鍵字 create
放在第一次接收到消息之前页屠,以強調(diào)本次消息實際上是在 創(chuàng)建新的對象。
@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
進入和發(fā)出消息
如果只想關注部分圖示,你可以使用進入和發(fā)出箭頭辰企。
使用方括號[
和]
表示圖示的左风纠、右兩側。
@startuml
[-> 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
構造類型和圈點
可以使用 <<
和 >>
給參與者添加構造類型牢贸。
在構造類型中竹观,你可以使用(X,color)
格式的語法添加一個圓圈圈起來的字符。
@startuml
participant "Famous Bob" as Bob << Generated >>
participant Alice << (C,#ADD1B2) Testable >>
Bob->Alice: First message
@enduml
默認使用 guillemet 字符來顯示構造類型潜索。 你可以使用外觀參數(shù) guillemet
來修改顯示行為臭增。
@startuml
skinparam guillemet false
participant "Famous Bob" as Bob << Generated >>
participant Alice << (C,#ADD1B2) Testable >>
Bob->Alice: First message
@enduml
@startuml
participant Bob << (C,#ADD1B2) >>
participant Alice << (C,#ADD1B2) >>
Bob->Alice: First message
@enduml
更多標題信息
你可以在標題中使用creole格式。
@startuml
title __Simple__ **communication** example
Alice -> Bob: Authentication Request
Bob -> Alice: Authentication Response
@enduml
在標題描述中使用 \n
表示換行竹习。
@startuml
title __Simple__ communication example\non several lines
Alice -> Bob: Authentication Request
Bob -> Alice: Authentication Response
@enduml
還可以使用關鍵字 title
和 end title
定義多行標題速址。
@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
包裹參與者
可以使用 box
和 end box
畫一個盒子將參與者包裹起來。
還可以在box
關鍵字之后添加標題或者背景顏色。
@startuml
box "Internal Service" #LightBlue
participant Bob
participant Alice
end box
participant Other
Bob -> Alice : hello
Alice -> Other : hello
@enduml
移除腳注
使用 hide footbox
關鍵字移除腳注篷店。
@startuml
hide footbox
title Footer removed
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
@enduml
外觀參數(shù)(skinparam)
使用 skinparam
命令改變顏色和字體洽糟。
如下場景可以使用這一命令:
- 在圖示定義中,
- 在一個包含文件中,
- 在由命令行或者ANT任務提供的配置文件中并炮。
你也可以修改其他渲染元素,如以下示例:
@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
@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
填充區(qū)設置
可以設定填充區(qū)的參數(shù)配置甥郑。
@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