使用PlantUml绘制时序图1
1. 简单示例
你可以用 -> 来绘制参与者之间传递的消息,而不必显式地声明参与者。
你也可以使用 --> 绘制一个虚线箭头。
另外,你还能用 <- 和 <--,这不影响绘图,但可以提高可读性。
注意:仅适用于时序图,对于其它示意图,规则是不同的。
@startuml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
Alice -> Bob: Another authentication Request
Alice <-- Bob: another authentication Response
@enduml2. 声明参与者1
关键字 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
@enduml3. 声明参与者2
关键字 as 用于重命名参与者
你可以使用 RGB 值或者颜色名修改 actor 或参与者的背景颜色。
actor 和 participant 的唯一区别就是绘制
@startuml
actor Bob #red
participant Alice
participant "I have a really\nlong name" as L #99FF99
/'你也可以这样声明:
participant L as "I have a really\nlong name" #99FF99
'/
Alice->Bob: Authentication Request
Bob->Alice: Authentication Response
Bob->L: Log transaction
@enduml4. 参与者的顺序
您可以使用关键字 order 自定义顺序来打印参与者。
@startuml
participant Last order 30
participant Middle order 20
participant First order 10
@enduml5. 在参与者中使用字符串
你可以使用双引号定义参与者,还可以用关键字 as 给参与者定义别名。
@startuml
Alice -> "Bob()" : Hello
"Bob()" -> "This is very\nlong" as Long
' 你还可以这样声明:
' "Bob()" -> Long as "This is very\nlong"
Long --> "Bob()" : ok
@enduml6. 给自己发消息
参与者可以给自己发信息,消息文字可以用 \n 来换行
@startuml
Alice->Alice: This is a signal to self.\nIt also demonstrates\nmultiline \ntext
@enduml7. 修改箭头样式
修改箭头样式的方式有以下几种:
• 表示一条丢失的消息:末尾加 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
@enduml8. 修改箭头颜色
@startuml
'你可以用以下记号修改箭头的颜色:
Bob -[#red]> Alice : hello
Alice -[#0000FF]->Bob : ok
@enduml9. 对消息序列编号
关键字 autonumber 用于自动对消息编号。
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
@enduml10. 对消息序列编号和格式化1
你可以在双引号内指定编号的格式。
格式是由 Java 的 DecimalFormat 类实现的:(0 表示数字; # 也表示数字,但默认为 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
@enduml11. 对消息序列编号和格式化2
你还可以用语句:autonumber stop 和 autonumber resume [increment] [format]来表示暂停或继续使用自动编号。
@startuml
autonumber 10 10 "<b>[000]"
Bob -> Alice : Authentication Request
Bob <- Alice : Authentication Response
autonumber stop
Bob -> Alice : dummy
' resume时增加10
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
' resume时增加1
autonumber resume 1 "<font color=blue><b>Message 0 "
Bob -> Alice : Yet another authentication Request
Bob <- Alice : Yet another authentication Response
@enduml12. 标题页眉页脚
使用 title 关键字为页面添加标题。
使用 header 和 footer 为页面添加页眉和页脚。
@startuml
header Page Header
footer Page %page% of %lastpage%
title Example Title
Alice -> Bob : message 1
Alice -> Bob : message 2
@enduml13. 分割示意图
关键字 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
header Page Header
footer Page %page% of %lastpage%
@enduml14. 组合消息
我们可以通过以下关键词将组合消息:
• alt/else
• opt
• loop
• par
• break
• critical
• group 后面紧跟着消息内容
可以在标头 (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
@enduml15. 给消息添加备注
我们可以通过在消息后面添加 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
@enduml16. 其他的备注
可以使用 note left of, note right of 或 note over 在节点 (participant) 的相对位置放置注释。
还可以通过修改背景色来高亮显示注释。以及使用关键字 end note 来添加多行注释。
@startuml
participant Alice
participant Bob
note left of Alice #aqua
这是Alice左边的备注
end note
note right of Alice: 这是Alice右边的备注
note over Alice: 这是Alice上的备注
note over Alice, Bob #FFAAAA: 这是显示在\n Bob 和 Alice之间的备注.
note over Bob, Alice
This is yet another
example of
a long note.
end note
@enduml17. 改变备注框的形状
你可以使用 hnote 和 rnote 这两个关键字来修改备注框的形状。
@startuml
caller -> server : conReq
hnote over caller : idle
caller <- server : conConf
rnote over server
"r" 表示矩形
"h" 表示六边形
endrnote
@enduml18. Creole和HTML格式化
可以使用 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:/medias/logo.png>
end note
@enduml19. 分隔符
你可以通过使用 == 关键词来将你的图表分割多个步骤
@startuml
== Initialization ==
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
== Repetition ==
Alice -> Bob: Another authentication Request
Alice <-- Bob: another authentication Response
@enduml