
idea绘制UML图
使用关键字object定义实例。
IDEA使用PlantUML插件绘制UML图
由于公司内网开发环境的内存等资源及其吃紧,多开几个软件如DBeaver、UML或其他图示绘制软件等都容易死机,无奈只能尽量只开一个IDEA完成绝大部分的设计、开发和调试工作
以下为通过IDEA的PlantUML插件,记录PlantUML的语法输出设计文档里几种常用UML图的方法,摘自PlantUML官方的用户指南,末尾已附官方用户指南(中文)下载地址
准备工作
idea插件里搜索plantUml安装
安装后右键创建文件能看到创建UML文件
时序图
可以使用->来绘制参与者之间的消息传递, 而不必显式的声明参与者。
也可以使用 “–>” 绘制一个虚线箭头表示异步消息。
也可以使用 “<-” 和 “<–”. 这虽然不影响图形绘制,但可以增加可读性。注意:仅适用于时序图,其它图形的规则是不同的
@startuml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
Alice -> Bob: Another authentication Request
Alice <-- Bob: another authentication Response
@enduml
可以使用 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
使用 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
在参与者中使用非字符
可以使用引号定义参与者。 还可以用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
消息发给自己
参与者可以发消息给自己,
消息文字可以使用\n来换行。
@startuml
Alice->Alice: This is a signal to self.\nIt also demonstrates\nmultiline \ntext
@enduml
更改箭头的样式
修改箭头样式的方式有以下几种:
末尾加 x 表示一条丢失的消息
使用 \ 或 / 替代 < 或 > 来表示 have only the bottom or top part of the arrow
使用两个箭头标记 (如 >> 或 //) 表示空心箭头。
使用 – 替代 - 以表示虚线箭头。
在箭头末尾加 “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
修改箭头颜色
可以用以下语法修改箭头标记的颜色:
@startuml
Bob -[#red]> Alice : hello
Alice -[#0000FF]->Bob : ok
@enduml
消息编号
autonumber 关键字用于自动的给消息加上编号。
@startuml
autonumber
Bob -> Alice : Authentication Request
Bob <- Alice : Authentication Response
@enduml
还可以用 autonumber ‘start’ 的语法指定编号的初始值 , 而用 autonumber ‘start’ ‘increment’ 可以同时指定编号的初始值和每次增加的值。
可以在双引号内指定编号的格式。
格式是由 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
@enduml
可以使用 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
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
分割图表(diagram)
关键字 newpage 用于把一个图表分割到多个图片中。
还可以在关键字 newpage 之后添加文字作为新图标的标题。
这便于在 Word 中打印多页的长图。
组合消息
我们可以通过以下关键词将组合消息:
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
给消息添加注释
我们可以通过在消息后面添加 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在节点(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
可以使用creole格式。
@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关键词来实现引用
@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
空间
你可以使用|||来增加空间。
还可以使用数字指定增加的像素的数量。
@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
创建参与者
你可以把关键字create放在第一次接收到消息之前,以强调本次消息实际上是在创建新的对象。
@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
进入和发出消息
如果只想关注部分图示,你可以使用进入和发出箭头。
使用方括号[和]表示图示的左、右两侧。
@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 字符来显示构造类型。 你可以使用外观参数 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
外观参数(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
用例图
用例
用例用圆括号括起来。
也可以用关键字usecase来定义用例。 还可以用关键字as定义一个别名,这个别名可以在以后定义关系的时候使用。
@startuml
(First usecase)
(Another usecase) as (UC2)
usecase UC3
usecase (Last\nusecase) as UC4
@enduml
角色
角色用两个冒号包裹起来。
也可以用actor关键字来定义角色。 还可以用关键字as来定义一个别名,这个别名可以在以后定义关系的时候使用。
后面我们会看到角色的定义是可选的
@startuml
:First Actor:
:Another\nactor: as Men2
actor Men3
actor :Last actor: as Men4
@enduml
用例描述
如果想定义跨越多行的用例描述,可以用双引号将其裹起来。
还可以使用这些分隔符:-- … == __。 并且还可以在分隔符中间放置标题。
@startuml
usecase UC1 as "You can use
several lines to define your usecase.
You can also use separators.
\--
Several separators are possible.
==
And you can add titles:
..Conclusion..
This allows large description."
@enduml
基础示例
用箭头–>连接角色和用例。
横杠"-"越多,箭头越长。 通过在箭头定义的后面加一个冒号及文字的方式来添加标签。
在这个例子中,User并没有定义,而是直接拿来当做一个角色使用。
@startuml
User -> (Start)
User --> (Use the application) : A small label
:Main Admin: ---> (Use the application) : This is\nyet another\nlabel
@enduml
继承
如果一个角色或者用例继承于另一个,那么可以用 < < <|–符号表示
注释
可以用note left of , note right of , note top of , note bottom of等关键字给一个对象添加注释。
注释还可以通过note关键字来定义,然后用…连接其他对象。
@startuml
:Main Admin: as Admin
(Use the application) as (Use)
User -> (Start)
User --> (Use)
Admin ---> (Use)
note right of Admin : This is an example.
note right of (Use)
A note can also
be on several lines
end note
note "This note is connected\nto several objects." as N2
(Start) .. N2
N2 .. (Use)
@enduml
构造类型
用" << " 和" >> "来定义角色或者用例的构造类型。
@startuml
User << Human >>
:Main Database: as MySql << Application >>
(Start) << One Shot >>
(Use the application) as (Use) << Main >>
User -> (Start)
User --> (Use)
MySql --> (Use)
@enduml
改变箭头方向
默认连接是竖直方向的,用–表示,可以用一个横杠或点来表示水平连接。
@startuml
:user: --> (Use case 1)
:user: -> (Use case 2)
@enduml
也可以通过翻转箭头来改变方向。
@startuml
(Use case 1) <.. :user:
(Use case 2) <- :user:
@enduml
还可以通过给箭头添加left, right, up或down等关键字来改变方向。
@startuml
:user: -left-> (dummyLeft)
:user: -right-> (dummyRight)
:user: -up-> (dummyUp)
:user: -down-> (dummyDown)
@enduml
分割图示
用newpage关键字将图示分解为多个页面。
@startuml
:actor1: --> (Usecase1)
newpage
:actor2: --> (Usecase2)
@enduml
改变图示方向
默认从上往下构建图示
@startuml
'default
top to bottom direction
user1 --> (Usecase 1)
user2 --> (Usecase 2)
@enduml
用left to right direction命令改变图示方向为从左到右。
@startuml
left to right direction
user1 --> (Usecase 1)
user2 --> (Usecase 2)
@enduml
显示参数
用skinparam改变字体和颜色。
可以在如下场景中使用:
在图示的定义中,
在引入的文件中,
在命令行或者ANT任务提供的配置文件中。
你也可以给构造的角色和用例指定特殊颜色和字体。
@startuml
skinparam handwritten true
skinparam usecase {
BackgroundColor DarkSeaGreen
BorderColor DarkSlateGray
BackgroundColor<< Main >> YellowGreen
BorderColor<< Main >> YellowGreen
ArrowColor Olive
ActorBorderColor black
ActorFontName Courier
ActorBackgroundColor<< Human >> Gold
}
User << Human >>
:Main Database: as MySql << Application >>
(Start) << One Shot >>
(Use the application) as (Use) << Main >>
User -> (Start)
User --> (Use)
MySql --> (Use)
@enduml
完整示例
@startuml
left to right direction
skinparam packageStyle rect
actor customer
actor clerk
rectangle checkoutDiagram {
customer -- (checkout)
(checkout) .> (payment) : include
(help) .> (checkout) : extends
(checkout) -- clerk
}
@enduml
类图
类之间的关系
类之间的关系通过下面的符号定义 :
| Extension(扩展) | <|--
|
| ----------------- | ------ |
| Composition(组合) | *--
|
| Aggregation(聚合) | o--
|
使用"…" 来代替 “–” 可以得到点 线.
在这些规则下,也可以绘制下列图形
@startuml
Class01 <|-- Class02
Class03 *-- Class04
Class05 o-- Class06
Class07 .. Class08
Class09 -- Class10
@enduml
@startuml
Class11 <|.. Class12
Class13 --> Class14
Class15 ..> Class16
Class17 ..|> Class18
Class19 <--* Class20
@enduml
@startuml
Class21 #-- Class22
Class23 x-- Class24
Class25 }-- Class26
Class27 +-- Class28
Class29 ^-- Class30
@enduml
关系上的标识
在关系之间使用标签来说明时, 使用 ":"后接 标签文字。
对元素的说明,你可以在每一边使用 “” 来说明.
@startuml
Class01 "1" *-- "many" Class02 : contains
Class03 o-- Class04 : aggregation
Class05 --> "1" Class06
@enduml
在标签的开始或结束位置添加< 或 >以表明是哪个对象作用到哪个对象上。
@startuml
class Car
Driver - Car : drives >
Car *- Wheel : have 4 >
Car -- Person : < owns
@enduml
添加方法
为了声明域或者方法,你可以使用 后接域名或方法名。
系统检查是否有括号来判断是方法还是域。
@startuml
Object <|-- ArrayList
Object : equals()
ArrayList : Object[] elementData
ArrayList : size()
@enduml
也可以使用{} 把域或者方法括起来
注意,这种语法对于类型/名字的顺序是非常灵活的。
@startuml
class Dummy {
String data
void methods()
}
class Flight {
flightNumber : Integer
departureTime : Date
}
@enduml
定义可访问性
一旦你定义了域或者方法,你可以定义 相应条目的可访问性质。
Character | Visibility |
---|---|
- | private |
# | protected |
~ | package private |
+ | public |
@startuml
class Dummy {
-field1
\#field2
~method1()
+method2()
}
@enduml
你可以采用以下命令停用这些特性 skinparam classAttributeIconSize 0 :
@startuml
skinparam classAttributeIconSize 0
class Dummy {
-field1
\#field2
~method1()
+method2()
}
@enduml
抽象与静态
通过修饰符{static}或者{abstract},可以定义静态或者抽象的方法或者属性。
这些修饰符可以写在行的开始或者结束。也可以使用{classifier}这个修饰符来代替{static}.
@startuml
class Dummy {
{static} String id
{abstract} void methods()
}
@enduml
高级类体
PlantUML默认自动将方法和属性重新分组,你可以自己定义分隔符来重排方法和属性,下面的分隔符都是可用的:-- … == __.
还可以在分隔符中添加标题:
@startuml
class Foo1 {
You can use
several lines
..
as you want
and group
==
things together.
__
You can have as many groups
as you want
\--
End of class
}
class User {
.. Simple Getter ..
\+ getName()
\+ getAddress()
.. Some setter ..
\+ setName()
__ private data __
int age
-- encrypted --
String password
}
@enduml
备注和模板
模板通过类关键字(“<<“和”>>”)来定义
你可以使用note left of , note right of , note top of , note bottom of这些关键字来添加备注。
你还可以在类的声明末尾使用note left, note right,note top, note bottom来添加备注。
此外,单独用note这个关键字也是可以的,使用 … 符号可以作出一条连接它与其它对象的虚线。
@startuml
class Object << general >>
Object <|--- ArrayList
note top of Object : In java, every class\nextends this one.
note "This is a floating note" as N1
note "This note is connected\nto several objects." as N2
Object .. N2
N2 .. ArrayList
class Foo
note left: On last defined class
@enduml
更多注释
可以在注释中使用部分html标签:
<b>
<u>
<i>
<s>, <del>, <strike>
<font color="#AAAAAA"> or <font color="colorName">
<color:#AAAAAA> or <color:colorName>
<size:nn> to change font size
<img src="file"> or <img:file> : the file must be accessible by the filesystem
你也可以在注释中展示多行。
你也可以在定义的class之后直接使用 note left, note right, note top, note bottom 来定义注释。
@startuml
class Foo
note left: On last defined class
note top of Object
In java, <size:18>every</size> <u>class</u>
<b>extends</b>
<i>this</i> one.
end note
note as N1
This note is <u>also</u>
<b><color:royalBlue>on several</color>
<s>words</s> lines
And this is hosted by <img:sourceforge.jpg>
end note
@enduml
链接的注释
在定义链接之后,你可以用 note on link 给链接添加注释
如果想要改变注释相对于标签的位置,你也可以用 note left on link, note right on link, note bottom on link。(对应位置分别在label的左边,右边,下边)
@startuml
class Dummy
Dummy --> Foo : A link
note on link #red: note that is red
Dummy --> Foo2 : Another link
note right on link #blue
this is my note on right link
and in blue
end note
@enduml
抽象类和接口
用关键字"abstract"或"abstract class"来定义抽象类。抽象类用斜体显示。 也可以使用interface, annotation和 enum关键字。
@startuml
abstract class AbstractList
abstract AbstractCollection
interface List
interface Collection
List <|-- AbstractList
Collection <|-- AbstractCollection
Collection <|- List
AbstractCollection <|- AbstractList
AbstractList <|-- ArrayList
class ArrayList {
Object[] elementData
size()
}
enum TimeUnit {
DAYS
HOURS
MINUTES
}
annotation SuppressWarnings
@enduml
使用非字母字符
如果你想在类(或者枚举)的显示中使用非字母符号,你可以:
在类的定义中使用 as 关键字
在类名旁边加上 “”
@startuml
class "This is my class" as class1
class class2 as "It works this way too"
class2 *-- "foo/dummy" : use
@enduml
活动图
简单活动
使用(*)作为活动图的开始点和结束点。
有时,你可能想用(*top)强制开始点位于图示的顶端。
使用–>绘制箭头。
@startuml
(*) --> "First Activity"
"First Activity" --> (*)
@enduml
箭头上的标签
默认情况下,箭头开始于最接近的活动。
可以用[]放在箭头定义的后面来添加标签。
@startuml
(*) --> "First Activity"
-->[You can put also labels] "Second Activity"
--> (*)
@enduml
改变箭头方向
你可以使用->定义水平方向箭头,还可以使用下列语法强制指定箭头的方向:
-down-> (default arrow)
-right-> or ->
-left->
-up->
@startuml
(*) -up-> "First Activity"
-right-> "Second Activity"
--> "Third Activity"
-left-> (*)
@enduml
分支
你可以使用关键字if/then/else创建分支。
@startuml
(*) --> "Initialization"
if "Some Test" then
-->[true] "Some Activity"
--> "Another activity"
-right-> (*)
else
->[false] "Something else"
-->[Ending process] (*)
endif
@enduml
不过,有时你可能需要重复定义同一个活动:
@startuml
(*) --> "check input"
If "input is verbose" then
--> [Yes] "turn on verbosity"
--> "run command"
else
--> "run command"
Endif
-->(*)
@enduml
更多分支
默认情况下,一个分支连接上一个最新的活动,但是也可以使用if关键字进行连接。
还可以嵌套定义分支。
@startuml
(*) --> if "Some Test" then
-->[true] "activity 1"
if "" then
-> "activity 3" as a3
else
if "Other test" then
-left-> "activity 5"
else
--> "activity 6"
endif
endif
else
->[false] "activity 2"
endif
a3 --> if "last test" then
--> "activity 7"
else
-> "activity 8"
endif
@enduml
同步
你可以使用"=== code ===" 来显示同步条。
@startuml
(*) --> ===B1===
--> "Parallel Activity 1"
--> ===B2===
===B1=== --> "Parallel Activity 2"
--> ===B2===
--> (*)
@enduml
长的活动描述
定义活动时可以用\n来定义跨越多行的描述。
还可以用as关键字给活动起一个短的别名。 这个别名可以在接下来的图示定义中使用。
@startuml
(*) -left-> "this <size:20>activity</size>
is <b>very</b> <color:red>long2</color>
and defined on several lines
that contains many <i>text</i>" as A1
-up-> "Another activity\n on several lines"
A1 --> "Short activity <img:sourceforge.jpg>"
@enduml
分区
用关键字partition定义分区,还可以设置背景色(用颜色名或者颜色值)。
定义活动的时候,它自动被放置到最新的分区中。
用}结束分区的定义。
@startuml
partition Conductor {
(*) --> "Climbs on Platform"
--> === S1 ===
--> Bows
}
partition Audience #LightSkyBlue {
=== S1 === --> Applauds
}
partition Conductor {
Bows --> === S2 ===
--> WavesArmes
Applauds --> === S2 ===
}
partition Orchestra #CCCCEE {
WavesArmes --> Introduction
--> "Play music"
}
@enduml
显示参数
用skinparam命令修改字体和颜色。
如下场景可用:
在图示定义中
在引入的文件中
在命令行或ANT任务提供的配置文件中。
还可以为构造类型指定特殊颜色和字体。
@startuml
skinparam backgroundColor #AAFFFF
skinparam activity {
StartColor red
BarColor SaddleBrown
EndColor Silver
BackgroundColor Peru
BackgroundColor<< Begin >> Olive
BorderColor Peru
FontName Impact
}
(*) --> "Climbs on Platform" << Begin >>
--> === S1 ===
--> Bows
--> === S2 ===
--> WavesArmes
--> (*)
@enduml
八边形活动
可用skinparam activityShape octagon命令将活动的外形改为八边形。
@startuml
'Default is skinparam activityShape roundBox
skinparam activityShape octagon
(*) --> "First Activity"
"First Activity" --> (*)
@enduml
完整的例子
@startuml
title Servlet Container
(*) --> "ClickServlet.handleRequest()"
--> "new Page"
if "Page.onSecurityCheck" then
->[true] "Page.onInit()"
if "isForward?" then
->[no] "Process controls"
if "continue processing?" then
-->[yes] ===RENDERING===
else
-->[no] ===REDIRECT_CHECK===
endif
else
-->[yes] ===RENDERING===
endif
if "is Post?" then
-->[yes] "Page.onPost()"
--> "Page.onRender()" as render
--> ===REDIRECT_CHECK===
else
-->[no] "Page.onGet()"
--> render
endif
else
-->[false] ===REDIRECT_CHECK===
endif
if "Do redirect?" then
->[yes] "redirect request"
--> ==BEFORE_DESTROY===
else
if "Do Forward?" then
-left->[yes] "Forward request"
--> ==BEFORE_DESTROY===
else
-right->[no] "Render page template"
--> ==BEFORE_DESTROY===
endif
endif
--> "Page.onDestroy()"
-->(*)
@enduml

当前活动图(activity diagram)的语法有诸多限制和缺点,比如代码难以维护。
所以从V7947开始提出一种全新的、更好的语法格式和软件实现供用户使用(beta版)。
就像序列图一样,新的软件实现的另一个优点是它不再依赖与Graphviz。
新的语法将会替换旧的语法。然而考虑到兼容性,旧的语法仍被能够使用以确保向前兼容。
但是我们鼓励用户使用新的语法格式。
简单活动图
活动标签(activity label)以冒号开始,以分号结束。
文本格式支持creole wiki语法。
活动默认安装它们定义的顺序就行连接。
@startuml
:Hello world;
:This is on defined on
several **lines**;
@enduml
开始/结束
你可以使用关键字start和stop表示图示的开始和结束。
@startuml
start
:Hello world;
:This is on defined on
several **lines**;
stop
@enduml
也可以使用 end 关键字
@startuml
start
:Hello world;
:This is on defined on
several **lines**;
end
@enduml
条件语句
在图示中可以使用关键字if,then和else设置分支测试。标注文字则放在括号中。
@startuml
start
if (Graphviz installed?) then (yes)
:process all\ndiagrams;
else (no)
:process only
__sequence__ and __activity__ diagrams;
endif
stop
@enduml
也可以使用关键字elseif设置多个分支测试。
@startuml
start
if (condition A) then (yes)
:Text 1;
elseif (condition B) then (yes)
:Text 2;
stop
elseif (condition C) then (yes)
:Text 3;
elseif (condition D) then (yes)
:Text 4;
else (nothing)
:Text else;
endif
stop
@enduml
重复循环
你可以使用关键字repeat和repeatwhile进行重复循环。
@startuml
start
repeat
:read data;
:generate diagrams;
repeat while (more data?)
stop
@enduml
while循环
可以使用关键字while和end while进行while循环。
@startuml
start
while (data available?)
:read data;
:generate diagrams;
endwhile
stop
@enduml
还可以在关键字endwhile后添加标注,还有一种方式是使用关键字is。
@startuml
while (check filesize ?) is (not empty)
:read file;
endwhile (empty)
:close file;
@enduml
并行处理
你可以使用关键字fork,fork again和end fork表示并行处理。
@startuml
start
if (multiprocessor?) then (yes)
fork
:Treatment 1;
fork again
:Treatment 2;
end fork
else (monoproc)
:Treatment 1;
:Treatment 2;
endif
@enduml
注释
文本格式支持creole wiki语法。
A note can be floating, using floating keyword.
@startuml
start
:foo1;
floating note left: This is a note
:foo2;
note right
This note is on several
//lines// and can
contain <b>HTML</b>
====
\* Calling the method ""foo()"" is prohibited
end note
stop
@enduml
颜色
你可以为活动(activity)指定一种颜色。
@startuml
start
:starting progress;
\#HotPink:reading configuration files
These files should edited at this point!;
\#AAAAAA:ending of the process;
@enduml
箭头
使用->标记,你可以给箭头添加文字或者修改箭头颜色。
It’s also possible to have dotted, dashed, bold or hidden arrows.
@startuml
:foo1;
-> You can put text on arrows;
if (test) then
-[#blue]->
:foo2;
-[#green,dashed]-> The text can
also be on several lines
and **very** long...;
:foo3;
else
-[#black,dotted]->
:foo4;
endif
-[#gray,bold]->
:foo5;
@enduml
组合(grouping)
通过定义分区(partition),你可以把多个活动组合(group)在一起。
@startuml
start
partition Initialization {
:read config file;
:init internal variable;
}
partition Running {
:wait for user interaction;
:print information;
}
stop
@enduml
泳道(Swimlanes)
你可以使用管道符|来定义泳道。
还可以改变泳道的颜色。
@startuml
|Swimlane1|
start
:foo1;
|#AntiqueWhite|Swimlane2|
:foo2;
:foo3;
|Swimlane1|
:foo4;
|Swimlane2|
:foo5;
stop
@enduml

可以使用关键字detach移除箭头。
@startuml
:start;
fork
:foo1;
:foo2;
fork again
:foo3;
detach
endfork
if (foo4) then
:foo5;
detach
endif
:foo6;
detach
:foo7;
stop
@enduml
特殊领域语言(SDL)
通过修改活动标签最后的分号分隔符(😉,可以为活动设置不同的形状。
|
<
>
/
]
}
@startuml
:Ready;
:next(o)|
:Receiving;
split
:nak(i)<
:ack(o)>
split again
:ack(i)<
:next(o)
on several line|
:i := i + 1]
:ack(o)>
split again
:err(i)<
:nak(o)>
split again
:foo/
split again
:i > 5}
stop
end split
:finish;
@enduml
一个完整的例子
@startuml
start
:ClickServlet.handleRequest();
:new page;
if (Page.onSecurityCheck) then (true)
:Page.onInit();
if (isForward?) then (no)
:Process controls;
if (continue processing?) then (no)
stop
endif
if (isPost?) then (yes)
:Page.onPost();
else (no)
:Page.onGet();
endif
:Page.onRender();
endif
else (false)
endif
if (do redirect?) then (yes)
:redirect process;
else
if (do forward?) then (yes)
:Forward request;
else (no)
:Render page template;
endif
endif
stop
@enduml
组件图
组件
组件必须用中括号括起来。
还可以使用关键字component定义一个组件。 并且可以用关键字as给组件定义一个别名。 这个别名可以在稍后定义关系的时候使用。
@startuml
[First component]
[Another component] as Comp2
component Comp3
component [Last\ncomponent] as Comp4
@enduml
接口
接口可以使用()来定义(因为这个看起来像个圆)。
还可以使用关键字interface关键字来定义接口。 并且还可以使用关键字as定义一个别名。 这个别名可以在稍后定义关系的时候使用。
我们稍后可以看到,接口的定义是可选的。
@startuml
() "First Interface"
() "Another interface" as Interf2
interface Interf3
interface "Last\ninterface" as Interf4
@enduml
基础示例
元素之间可以使用虚线(…)、直线(–)、箭头(–>)进行连接。
@startuml
DataAccess - [First Component]
[First Component] ..> HTTP : use
@enduml
使用注释
你可以使用 note left of , note right of , note top of , note bottom of 等关键字定义相对于对象位置的注释。
也可以使用关键字note单独定义注释,然后使用虚线(…)将其连接到其他对象。
@startuml
interface "Data Access" as DA
DA - [First Component]
[First Component] ..> HTTP : use
note left of HTTP : Web Service only
note right of [First Component]
A note can also
be on several lines
end note
@enduml
组合组件
你可以使用多个关键字将组件和接口组合在一起。
package
node
folder
frame
cloud
database
@startuml
package "Some Group" {
HTTP - [First Component]
[Another Component]
}
node "Other Groups" {
FTP - [Second Component]
[First Component] --> FTP
}
cloud {
[Example 1]
}
database "MySql" {
folder "This is my folder" {
[Folder 3]
}
frame "Foo" {
[Frame 4]
}
}
[Another Component] --> [Example 1]
[Example 1] --> [Folder 3]
[Folder 3] --> [Frame 4]
@enduml
改变箭头方向
默认情况下,对象之间用–连接,并且连接是竖直的。不过可以使用一个横线或者点设置水平方向的连接
@startuml
[Component] --> Interface1
[Component] -> Interface2
@enduml
也可以使用反向连接
@startuml
Interface1 <-- [Component]
Interface2 <- [Component]
@enduml
还可以使用关键字left, right, up or down改变箭头方向
@startuml
[Component] -left-> left
[Component] -right-> right
[Component] -up-> up
[Component] -down-> down
@enduml
使用UML2标记符
命令skinparam componentStyle uml2 可以切换到UML2标记符。
@startuml
skinparam componentStyle uml2
interface "Data Access" as DA
DA - [First Component]
[First Component] ..> HTTP : use
@enduml
不同的颜色表示
你可以在声明一个组件时加上颜色的声明。
@startuml
component [Web Server] #Yellow
@enduml
显示参数
可以使用命令skinparam改变字体和颜色。
你可以在如下场景使用这些命令:
在图示的定义中,
在包含进来的文件中,
在命令行或者ANT任务提供的配置文件中。
可以为构造类型和接口定义特殊的颜色和字体。
@startuml
skinparam interface {
backgroundColor RosyBrown
borderColor orange
}
skinparam component {
FontSize 13
BackgroundColor<<Apache>> Red
BorderColor<<Apache>> #FF6655
FontName Courier
BorderColor black
BackgroundColor gold
ArrowFontName Impact
ArrowColor #FF6655
ArrowFontColor #777777
}
() "Data Access" as DA
DA - [First Component]
[First Component] ..> () HTTP : use
HTTP - [Web Server] << Apache >>
@enduml
@startuml
[AA] <<static lib>>
[BB] <<shared lib>>
[CC] <<static lib>>
node node1
node node2 <<shared node>>
database Production
skinparam component {
backgroundColor<<static lib>> DarkKhaki
backgroundColor<<shared lib>> Green
}
skinparam node {
borderColor Green
backgroundColor Yellow
backgroundColor<<shared node>> Magenta
}
skinparam databaseBackgroundColor Aqua
@enduml
状态图
简单状态
使用([*])开始和结束状态图。
使用–>添加箭头。
@startuml
[*] --> State1
State1 --> [*]
State1 : this is a string
State1 : this is another string
State1 -> State2
State2 --> [*]
@enduml
合成状态
一个状态也可能是合成的,必须使用关键字state和花括号来定义合成状态。
@startuml
scale 350 width
[*] --> NotShooting
state NotShooting {
[*] --> Idle
Idle --> Configuring : EvConfig
Configuring --> Idle : EvConfig
}
state Configuring {
[*] --> NewValueSelection
NewValueSelection --> NewValuePreview : EvNewValue
NewValuePreview --> NewValueSelection : EvNewValueRejected
NewValuePreview --> NewValueSelection : EvNewValueSaved
state NewValuePreview {
State1 -> State2
}
}
@enduml
长名字
也可以使用关键字state定义长名字状态。
@startuml
scale 600 width
[*] -> State1
State1 --> State2 : Succeeded
State1 --> [*] : Aborted
State2 --> State3 : Succeeded
State2 --> [*] : Aborted
state State3 {
state "Accumulate Enough Data\nLong State Name" as long1
long1 : Just a test
[*] --> long1
long1 --> long1 : New Data
long1 --> ProcessData : Enough Data
}
State3 --> State3 : Failed
State3 --> [*] : Succeeded / Save Result
State3 --> [*] : Aborted
@enduml
并发状态
用–作为分隔符来合成并发状态
@startuml
[*] --> Active
state Active {
[*] -> NumLockOff
NumLockOff --> NumLockOn : EvNumLockPressed
NumLockOn --> NumLockOff : EvNumLockPressed
\--
[*] -> CapsLockOff
CapsLockOff --> CapsLockOn : EvCapsLockPressed
CapsLockOn --> CapsLockOff : EvCapsLockPressed
\--
[*] -> ScrollLockOff
ScrollLockOff --> ScrollLockOn : EvCapsLockPressed
ScrollLockOn --> ScrollLockOff : EvCapsLockPressed
}
@enduml
箭头方向
使用->定义水平箭头,也可以使用下列格式强制设置箭头方向:
-down-> (default arrow)
-right-> or ->
-left->
-up->
@startuml
[*] -up-> First
First -right-> Second
Second --> Third
Third -left-> Last
@enduml
注释
可以用 note left of, note right of, note top of, note bottom of 关键字来定义注释。
还可以定义多行注释。
@startuml
[*] --> Active
Active --> Inactive
note left of Active : this is a short\nnote
note right of Inactive
A note can also
be defined on
several lines
end note
@enduml
以及浮动注释
@startuml
state foo
note "This is a floating note" as N1
@enduml
更多注释
可以在合成状态中放置注释。
@startuml
[*] --> NotShooting
state "Not Shooting State" as NotShooting {
state "Idle mode" as Idle
state "Configuring mode" as Configuring
[*] --> Idle
Idle --> Configuring : EvConfig
Configuring --> Idle : EvConfig
}
note right of NotShooting : This is a note on a composite state
@enduml
显示参数
使用skinparam命令改变字体和颜色。
在如下场景使用:
在图示定义中,
在包含进来的文件中,
在命令行或ANT任务提供的配置文件中。
还可以为状态的构造类型指定特殊的字体和颜色。
@startuml
skinparam backgroundColor LightYellow
skinparam state {
StartColor MediumBlue
EndColor Red
BackgroundColor Peru
BackgroundColor<<Warning>> Olive
BorderColor Gray
FontName Impact
}
[*] --> NotShooting
state "Not Shooting State" as NotShooting {
state "Idle mode" as Idle <<Warning>>
state "Configuring mode" as Configuring
[*] --> Idle
Idle --> Configuring : EvConfig
Configuring --> Idle : EvConfig
}
NotShooting --> [*]
@enduml
对象图
对象的定义
使用关键字object定义实例。
@startuml
object firstObject
object "My Second Object" as o2
@enduml
对象之间的关系
对象之间的关系用如下符号定义:
| Extension(扩展) | <|--
|
| ----------------- | ------ |
| Composition(组合) | *--
|
| Aggregation(聚合) | o--
|
也可以用 “…” 来代替 “–” 以使用点线。
知道了这些规则,就可以画下面的图:
可以用冒号给关系添加标签,标签内容紧跟在冒号之后。
用双引号在关系的两边添加基数。
@startuml
object Object01
object Object02
object Object03
object Object04
object Object05
object Object06
object Object07
object Object08
Object01 <|-- Object02
Object03 *-- Object04
Object05 o-- "4" Object06
Object07 .. Object08 : some labels
@enduml
添加属性
用冒号加属性名的形式声明属性。
@startuml
object user
user : name = "Dummy"
user : id = 123
@enduml
也可以用大括号批量声明属性
@startuml
object user {
name = "Dummy"
id = 123
}
@enduml
导出
在右侧显示UML图的Tab页右键,选择Save Diagram
选择导出格式
PlantUML官网:https://plantuml.com/zh
PlantUML官方用户指南:https://plantuml.com/zh/guide
更多推荐
所有评论(0)