[plantuml实践]时序图(sequence diagram)

本文是博主学习PlantUML时序图(Sequence Diagram)的实践与总结。希望对你有帮助。

1 简单示例(Basic examples)

1.1 要点

  • -> 表示实线箭头
  • --> 表示虚线箭头

1.2 实践

@startuml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Resonse

Alice -> Bob: Another authntication Request
Alice <-- Bob: Another authntication Resonse
@enduml

1.3 总结

这里需要注意的是 @startuml 之前和 @enduml 之后不可以有空行, 否则会出错。

2 声明参与者(Declaring participant)

2.1 要点

  • 关键字 participant 用于改变参与者的顺序。此外还有其他的关键字可以声明参与者:

    名称 用途
    actor 参与者
    boundary 边界
    control 控制
    entity 实体
    database 数据库
  • 关键字 as 用于重命名参与者
  • 可以自定义各种参与者的背景颜色
@startuml
actor Foo1
boundary Foo2
control Foo3
entity Foo4
database Foo5
collections Foo6
Foo1 -> Foo2 : To boun dary
Foo1 -> Foo3 : To control
Foo1 -> Foo4 : To entity
Foo1 -> Foo5 : To database
Foo1 -> Foo6 : To collections
@enduml

@startuml
actor Bob #red
' The only difference between actor
' and participant is the drawing
participant Alice
participant "I have a reallynlong name" as L #99FF99
/'
  You can also declare:
  participant L as "I have a reallynlong name" #99FF99
'/

Alice -> Bob : Authentication Request
Bob -> Alice : Authentication Response
Bob -> L : Log transaction
@enduml

2.2 总结

  • 自定义参与者,并更改显示顺序
    participant, actor, boundary, control, entity, database 和 =collections=用于声明不同的参与者
  • 使用 as 重命名参与者
  • 自定义颜色
    可以使用 RGB 和颜色英文名来指定颜色。
  • 注释
    • 使用 ' 定义单行注释
    • 使用 /'...'/ 定义多行注释

3 在参与者中使用非字母符号(Use non-letters in participants)

3.1 要点

PlantUML允许使用非字母符号,比较常用的,例如换行符 n.

3.2 实践

@startuml
Alice -> "Bob()" : Hello
"Bob()" -> "This is verynlong" as Long
' You can also declare:
' "Bob()" -> Long as "This is verynlong"
Long --> "Bob()" : ok
@enduml

3.3 总结

注意这两种写法是等效的:

"Bob()" -> "This is verynlong" as Long
' is same with
"Bob()" -> Long As "This is verynlong"

4 给自己发消息(Message to self)

4.1 要点

  • 参与者可以给自己发消息
  • 消息文字可以用 n 来换行,前面提到了在参与者中使用非字母字符,这里与前者类似。

4.2 实践

@startuml
Alice -> : This is a signal to self.nIt also demonstratesnmultilinentext
@enduml

4.3 总结

最好在操作符两边加上空格。

5 修改箭头样式(Change arrow style)

5.1 要点

修改箭头的方式有以下几种:

  • 表示一条的消息: 末尾加x
  • 让箭头只有上半部分或者下半部分:将 <> 替换成 /
  • 细箭头: 将箭头标记写两次(如 >> 或者 //)
  • 虚线箭头: 用 -- 替代 -
  • 箭头末尾加圈: ->o
  • 双向箭头: <->

5.2 实践

@startuml
participant Bob as B
participant Alice as A

B ->x A
B -> A
B ->> A
B - A
B \- A
B //-- A

B ->o A
B o\-- A

B <-> A
B <->o A
@enduml

5.3 总结

不同的箭头样式可以表示不同的消息类型。

6 修改箭头颜色(Change arrow color)

6.1 要点

可以使用颜色名字或者RGB值表示颜色

6.2 实践

@startuml
participant Bob as B
participant Alice as A

B -[#red]> A : hello
A -[#0000FF]->B : ok
@enduml

6.3 总结

有时候在想,这些花哨的东西有没有必要。。。

7 对消息序列编号(Message sequence numbering)

7.1 要点

  • 关键字 autonumber 用于自动对消息编号。
  • autonumber [<start>] [<increment>] ["<format>"] 可以同时指定编号的初始值,增量和格式。

    参数 类型 含义 备注
    start 整型 编号起始值  
    increment 整型 编号增量  
    format 字符串 编号格式 1.引号必须要有;2.格式语法参考 Java 的 =DecimalFormat=类实现的
  • autonumber stop 暂停自动编号
  • autonumber resume [<increment>] ["<format>"] 恢复编号
    格式参考上述表格.

7.2 实践

@startuml
autonumber
participant Bob as B
participant Alice as A

B -> A : Authenticaiton Request
B <- A : Authenticaiton Response
@enduml

@startuml
participant Bob as B
participant Alice as A

autonumber 10 10 "<b>[000]"
B -> A : Authentication Request
B <- A : Authentication Request

autonumber stop
B -> A : dummy

autonumber resume "<font color=red><b>Message 0"
B -> A : Yet another authenticaiton Request
B <- A : Yet another authenticaiton Response

autonumber stop
B -> A : dummy

autonumber resume 1 "<font color=blue><b>Message 0"
B -> A : Yet another authenticaiton Request
B <- A : Yet another authenticaiton Response
@enduml

7.3 总结

编号可以让消息的时序更清楚简明。

8 分割示意图(Splitting diagrams)

8.1 要点

关键字 newpage 用于把一张图分割成多张。

8.2 实践

@startuml
participant Alice as A
participant Bob as B

A -> B : message 1
A -> B : message 2

newpage
A -> B : message 3
A -> B : message 4

newpage  A title for thenlast page
A -> B : message 5
A -> B : message 6

@enduml

8.3 总结

当时序图太过复杂时,可以分页。

9 组合消息

9.1 要点

  • 可以通过以下关键字组合消息
    • alt/else
    • opt
    • loop
    • par
    • break
    • critical
    • group, 后面紧跟着消息内容
  • 这些关键字,可以在header后添加需要显示的文字(group除外)
  • 关键词 end 用来结束分组
  • 分组可以嵌套使用

9.2 实践

@startuml
participant Alice as A
participant Bob as B
participant Log as L

A -> B : Authentication Reuest

alt successful case
    B -> A : Authentication Accepted
else some kind of failure
    B -> A : Authentication Failure
    group My own label
      A -> L : Log attack start
      loop 1000 times
           A -> B : DNS Attack
      end
      A -> L : Log attack end
    end
else Another type of failure
     B -> A : Please repeat
end
@enduml

9.3 总结

组合很强大, 以前用的比较少。使用plantuml还挺方便的。

10 给消息添加注释

10.1 要点

  • 给消息添加注释
    这种类型的注释需要紧跟消息即可: note <left|right>
  • 多行注释
    当需要多行注释时,需要使用 end note 语法即可。

    A -> B
    note <left|right> [<color>]
    ...
    end note
  • participant 添加注释

    A -> B
    note <left|right> of <participant> [<color>]
    ...
    end note
  • 可以自定义注释的形状

10.2 实践

@startuml
participant Alice as A
participant Bob as B

A -> B : hello
note left : this is a first note

B -> A : ok
note right : this is another note

B -> B : I am thinking
note left
     a note
      can also be defined
      on overall lines
end note

note left of A #aqua
     This is displayed
     left of Alice.
end note

note right of A : This is displayed right of A.

note over A : This is displayed over Alice.

note over A, B #FFAAAA : This is displayedn over Bob and Alice.

note over A, B
     This is yet another
     example of
     a long note.
end note
@enduml

@startuml
participant caller as C
participant server as S

c -> s : conReq
hnote over c : idle
c <- s : conConf
rnote over s
      "r" as rectangle
      "h" as hexagon
end note
@enduml

10.3 总结

注释很有必要,这也是一种反馈。

11 分隔符

11.1 要点

== 关键词可以用来分割图表。

11.2 实践

@startuml
participant Alice as A
participant Bob as B
== Initialization ==
A -> B : Authentication Request
B -> A : Authentication Response
== Repetition ==
A -> B : Another authentication Request
A <-- B : Another authentication Response
@enduml

11.3 总结

12 引用

12.1 要点

ref over 用来实现引用

12.2 实践

@startuml
participant Alice as A
actor Bob as B

ref over A, B : int
A -> B : hello

ref over B
    This can be on serveral lines
end ref
@enduml

12.3 总结

13 延迟

... 用来表示延迟。此外我们也可以给延迟添加注释。

@startuml
participant Alice as A
participant Bob as B

A -> B : Authentication Request
...
B --> A : Authentication Response
...5 minutes latter...
B --> A : Bye!
@enduml

14 空间

||| 用来增加空间,另外可以使用数字指定增加的像素的数量。

@startuml
participant Alice as A
participant Bob as B

A -> B : message 1
B --> A : ok
|||
A -> B : message 2
B --> A : ok
||45||
A -> B : message 3
B --> A : ok
@enduml

15 生命线的集火与撤销

15.1 要点

  • active <participant> 激活活动块
  • deactive <participant> 终止活动块
  • destroy <participant> 表示一个参与者的生命线的终结。
  • 此外,活动块也可以嵌套,并且进行样式的自定义。

15.2 实践

@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

16 创建参与者

可以把 create 放在 participant 第一次收到消息之前,以强调本次消息实际上是在创建新的对象。

@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

17 进入和发出消息

如果只想关注部分图示,你可以使用进入和发出箭头。使用方括号 [] 分表表示图示的左右两侧。

@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

18 构造型和圈点

18.1 要点

  • << <type> >> 用于定义构造类型
  • 在构造类型中,=(<x>,<color>)=语法添加一个圆圈圈起来的字符。

18.2 实践

@startuml
participant "Famous Bob" as Bob << Generated >>
participant Alice << (C,#ADD1B2) Testable>>

Bob -> Alice : First message
@enduml

18.3 总结

19 包裹参与者

可以使用 box ... end box 将多个参与者包裹起来。

@startuml
box "Internal Service " #LightBlue
    participant Bob
    participant Alice
end box
participant Other

Bob -> Alice : hello
Alice -> Other : hello
@enduml

20 移除 foot box

使用 hide footbox 关键字移除脚注

@startuml
hide footbox
title Footer removed

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

去掉 foot box 的时序图比较多见吧。

21 自定义外观

还有其他的一些方式来自定义外观。

@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