使用 PlantUML 绘制 UML
语言参考指南
(2017 年 6 月 29 日星期四上午 7:20)
PlantUML 是一个开源项目,支持快速绘制:
• 时序图
• 用例图
• 类图
• 活动图
• 组件图
• 状态图
• 对象图
通过简单直观的语言来定义这些示意图。
1 时序图(SEQUENCE DIAGRAM)
1 时序图(Sequence Diagram)
1.1 简单示例(Basic examples)
你可以用 -> 来绘制参与者之间传递的消息,而不必显式地声明参与者。
你也可以使用”-->” 绘制一个虚线箭头。
另外,你还能用”<-” 和”<--”,这不影响绘图,但可以提高可读性。注意:仅适用于时序图,对于其
它示意图,规则是不同的。
@startuml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
Alice -> Bob: Another authentication Request
Alice <-- Bob: another authentication Response
@enduml
1.2 声明参与者(Declaring participant)
关键字 participant 用于改变参与者的先后顺序。
你也可以使用其它关键字来声明参与者:
• actor
• boundary
• control
• entity
• database
@startuml
actor Foo1
boundary Foo2
control Foo3
entity Foo4
database Foo5
Foo1 -> Foo2 : To boundary
Foo1 -> Foo3 : To control
Foo1 -> Foo4 : To entity
Foo1 -> Foo5 : To database
@enduml
PlantUML : 语言参考指南 (2017 年 6 月 29 日星期四)
1 of 125
1.3 在参与者中使用非字母符号(Use non-letters in participants)1 时序图(SEQUENCE DIAGRAM)
关键字 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
1.3 在参与者中使用非字母符号(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
PlantUML : 语言参考指南 (2017 年 6 月 29 日星期四)
2 of 125
1.4 给自己发消息(Message to self)
1 时序图(SEQUENCE DIAGRAM)
1.4 给自己发消息(Message to self)
参与者可以给自己发信息,
消息文字可以用 \n 来换行。
@startuml
Alice->Alice: This is a signal to self.\nIt also demonstrates\nmultiline \ntext
@enduml
1.5 修改箭头样式(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
PlantUML : 语言参考指南 (2017 年 6 月 29 日星期四)
3 of 125
1.6 修改箭头颜色(Change arrow color)
1 时序图(SEQUENCE DIAGRAM)
1.6 修改箭头颜色(Change arrow color)
你可以用以下记号修改箭头的颜色:
@startuml
Bob -[#red]> Alice : hello
Alice -[#0000FF]->Bob : ok
@enduml
1.7 对消息序列编号(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
PlantUML : 语言参考指南 (2017 年 6 月 29 日星期四)
4 of 125
1.7 对消息序列编号(Message sequence numbering)
1 时序图(SEQUENCE DIAGRAM)
Bob <- Alice : Yet another authentication Response
@enduml
你可以在双引号内指定编号的格式。
格式是由 Java 的 DecimalFormat 类实现的:(’0’ 表示数字;’#’ 也表示数字,但默认为 0)。
你也可以用 HTML 标签来制定格式。
@startuml
autonumber "[000]"
Bob -> Alice : Authentication Request
Bob <- Alice : Authentication Response
autonumber 15 "(##)"
Bob -> Alice : Another authentication Request
Bob <- Alice : Another authentication Response
autonumber 40 10 "Message 0
"
Bob -> Alice : Yet another authentication Request
Bob <- Alice : Yet another authentication Response
@enduml
你还可以用语句 autonumber stop 和 autonumber resume 'increment' 'format' 来表示暂停或继
续使用自动编号。
@startuml
autonumber 10 10 "[000]"
Bob -> Alice : Authentication Request
Bob <- Alice : Authentication Response
autonumber stop
Bob -> Alice : dummy
autonumber resume "Message 0
"
PlantUML : 语言参考指南 (2017 年 6 月 29 日星期四)
5 of 125
1.8 分割示意图 (Splitting diagrams)
1 时序图(SEQUENCE DIAGRAM)
Bob -> Alice : Yet another authentication Request
Bob <- Alice : Yet another authentication Response
autonumber stop
Bob -> Alice : dummy
autonumber resume 1 "Message 0
Bob -> Alice : Yet another authentication Request
Bob <- Alice : Yet another authentication Response
@enduml
"
1.8 分割示意图 (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
PlantUML : 语言参考指南 (2017 年 6 月 29 日星期四)
6 of 125
1.9 组合消息
1 时序图(SEQUENCE DIAGRAM)
1.9 组合消息
我们可以通过以下关键词将组合消息:
• 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
@enduml
PlantUML : 语言参考指南 (2017 年 6 月 29 日星期四)
7 of 125