PlantUML语法学习

4 分钟阅读

PlantUML语法学习

类图

类之间的关系

类之间的关系通过下面的符号定义:

类之间的关系

@startuml
Class01 <|-- Class02
Class03 *-- Class04
Class05 o-- Class06
Class07 .. Class08
Class09 -- Class10
@enduml

class1

@startuml
Class11 <|.. Class12
Class13 --> Class14
Class15 ..> Class16
Class17 ..|> Class18
Class19 <--* Class20
@enduml

class2

@startuml
Class21 #-- Class22
Class23 x-- Class24
Class25 }-- Class26
Class27 +-- Class28
Class29 ^-- Class30
@enduml

class3

关系上的标识

在关系之间使用标签来说明时, 使用: 后接标签文字。 对元素的说明,你可以在每一边使用”” 来说明.

@startuml
Class01 "1" *-- "many" Class02 : contains
Class03 o-- Class04 : aggregation
Class05 --> "1" Class06
@enduml

class4

在标签的开始或结束位置添加< 或> 以表明是哪个对象作用到哪个对象上。

@startuml
class Car
Driver - Car : drives >
Car *- Wheel : have 4 >
Car -- Person : < owns
@enduml

class5

添加方法

为了声明字段(对象属性)或者方法,你可以使用后接字段名或方法名。 系统检查是否有括号来判断是方法还是字段。

@startuml
Object <|-- ArrayList
Object : equals()
ArrayList : Object[] elementData
ArrayList : size()
@enduml

class6

也可以使用{} 把字段或者方法括起来 注意,这种语法对于类型/名字的顺序是非常灵活的。

@startuml
class Dummy {
String data
void methods()
}
class Flight {
flightNumber : Integer
departureTime : Date
}
@enduml

class7

你可以(显式地)使用{field} 和{method} 修饰符来覆盖解析器的对于字段和方法的默认行为

@startuml
class Dummy {
{field} A field (despite parentheses)
{method} Some method
}
@enduml

class8

定义可访问性

一旦你定义了域或者方法,你可以定义相应条目的可访问性质。 定义可访问性

@startuml
class Dummy {
-field1
#field2
~method1()
+method2()
}
@enduml

class9

你可以采用命令(skinparam classAttributeIconSize 0 :)停用该特性

@startuml
skinparam classAttributeIconSize 0
class Dummy {
-field1
#field2
~method1()
+method2()
}
@enduml

class10

抽象与静态

通过修饰符{static} 或者{abstract},可以定义静态或者抽象的方法或者属性。 这些修饰符可以写在行的开始或者结束。也可以使用{classifier} 这个修饰符来代替{static}.

@startuml
class Dummy {
{static} String id
{abstract} void methods()
}
@enduml

class11

高级类体

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

class12

更多注释

可以在注释中使用部分html 标签:

@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

class13

对象图

对象的定义

使用关键字object 定义实例。

@startuml
object firstObject
object "My Second Object" as o2
@enduml

如下图生成:

object1

对象之间的关系

对象之间的关系可以用如下符号定义:

对象之间的关系

也可以用.. 来代替– 以使用点线。 知道了这些规则,就可以画下面的图: 可以用冒号给关系添加标签,标签内容紧跟在冒号之后。 用双引号在关系的两边添加基数。

@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

如下图生成:

object2

添加属性

用冒号加属性名的形式声明属性。

@startuml
object user
user : name = "Dummy"
user : id = 123
@enduml

如下图生成:

object3

活动图

简单活动

使用() 作为活动图的开始点和结束点。 有时,你可能想用(top) 强制开始点位于图示的顶端。 使用–> 绘制箭头。

@startuml
(*) --> "First Activity"
"First Activity" --> (*)
@enduml

activity1

箭头上的标签

默认情况下,箭头开始于最接近的活动。 可以用[ 和 ] 放在箭头定义的后面来添加标签。

@startuml
(*) --> "First Activity"
-->[You can put also labels] "Second Activity"
--> (*)
@enduml

activity2

改变箭头方向

你可以使用-> 定义水平方向箭头,还可以使用下列语法强制指定箭头的方向:

  • -down-> (default arrow)
  • -right-> or ->
  • -left->
  • -up->

activity3

分支

你可以使用关键字if/then/else 创建分支。

@startuml
(*) --> "Initialization"
if "Some Test" then
-->[true] "Some Activity"
--> "Another activity"
-right-> (*)
else
->[false] "Something else"
-->[Ending process] (*)
endif
@enduml

activity4

不过,有时你可能需要重复定义同一个活动:

@startuml
(*) --> "check input"
If "input is verbose" then
--> [Yes] "turn on verbosity"
--> "run command"
else
--> "run command"
Endif
-->(*)
@enduml

activity5

更多分支

默认情况下,一个分支连接上一个最新的活动,但是也可以使用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

activity6

注释

你可以在活动定义之后用note left, note right, note top or note bottom, 命令给活动添加注释。 如果想给开始点添加注释,只需把注释的定义放在活动图最开始的地方即可。 也可以用关键字endnote 定义多行注释。

@startuml
(*) --> "Some Activity"
note right: This activity has to be defined
"Some Activity" --> (*)
note left
This note is on
several lines
end note
@enduml

activity7

分区

用关键字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

activity8

一个完整的例子

@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

activity9

界面格式相关

@startuml
skinparam SequenceGroupBodyBackgroundColor #FFFFFF90

box "Internal Service" #LightBlue
    participant Bob
    participant Alice
end box

box "Other" #LightGreen
    participant Other
end box

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

skinparam_uml_0

颜色示例

@startuml

!unquoted procedure $DrawColor($colour)

    skinparam rectangle {
            backgroundColor<<$colour>> $colour
            borderColor<<$colour>> $colour
            shadowing<<$colour>> true
            BorderThickness<<$colour>> 1
        }

    rectangle $colour <<$colour>> as "<color:$colour></color>"

!endprocedure

package HexCodes {
$DrawColor("00ff00")
$DrawColor("ff0000")
$DrawColor("0000ff")
$DrawColor("123456")
$DrawColor("654321")
$DrawColor("165432")
$DrawColor("ff22ff")
}

package Colours {
$DrawColor("APPLICATION")
$DrawColor("AliceBlue")
$DrawColor("AntiqueWhite")
$DrawColor("Aqua")
$DrawColor("Aquamarine")
$DrawColor("Azure")
$DrawColor("BUSINESS")
$DrawColor("Beige")
$DrawColor("Bisque")
$DrawColor("Black")
$DrawColor("BlanchedAlmond")
$DrawColor("Blue")
$DrawColor("BlueViolet")
$DrawColor("Brown")
$DrawColor("BurlyWood")
$DrawColor("CadetBlue")
$DrawColor("Chartreuse")
$DrawColor("Chocolate")
$DrawColor("Coral")
$DrawColor("CornflowerBlue")
$DrawColor("Cornsilk")
$DrawColor("Crimson")
$DrawColor("Cyan")
$DrawColor("DarkBlue")
$DrawColor("DarkCyan")
$DrawColor("DarkGoldenRod")
$DrawColor("DarkGray")
$DrawColor("DarkGreen")
$DrawColor("DarkGrey")
$DrawColor("DarkKhaki")
$DrawColor("DarkMagenta")
$DrawColor("DarkOliveGreen")
$DrawColor("DarkOrchid")
$DrawColor("DarkRed")
$DrawColor("DarkSalmon")
$DrawColor("DarkSeaGreen")
$DrawColor("DarkSlateBlue")
$DrawColor("DarkSlateGray")
$DrawColor("DarkSlateGrey")
$DrawColor("DarkTurquoise")
$DrawColor("DarkViolet")
$DrawColor("Darkorange")
$DrawColor("DeepPink")
$DrawColor("DeepSkyBlue")
$DrawColor("DimGray")
$DrawColor("DimGrey")
$DrawColor("DodgerBlue")
$DrawColor("FireBrick")
$DrawColor("FloralWhite")
$DrawColor("ForestGreen")
$DrawColor("Fuchsia")
$DrawColor("Gainsboro")
$DrawColor("GhostWhite")
$DrawColor("Gold")
$DrawColor("GoldenRod")
$DrawColor("Gray")
$DrawColor("Green")
$DrawColor("GreenYellow")
$DrawColor("Grey")
$DrawColor("HoneyDew")
$DrawColor("HotPink")
$DrawColor("IMPLEMENTATION")
$DrawColor("IndianRed")
$DrawColor("Indigo")
$DrawColor("Ivory")
$DrawColor("Khaki")
$DrawColor("Lavender")
$DrawColor("LavenderBlush")
$DrawColor("LawnGreen")
$DrawColor("LemonChiffon")
$DrawColor("LightBlue")
$DrawColor("LightCoral")
$DrawColor("LightCyan")
$DrawColor("LightGoldenRodYellow")
$DrawColor("LightGray")
$DrawColor("LightGreen")
$DrawColor("LightGrey")
$DrawColor("LightPink")
$DrawColor("LightSalmon")
$DrawColor("LightSeaGreen")
$DrawColor("LightSkyBlue")
$DrawColor("LightSlateGray")
$DrawColor("LightSlateGrey")
$DrawColor("LightSteelBlue")
$DrawColor("LightYellow")
$DrawColor("Lime")
$DrawColor("LimeGreen")
$DrawColor("Linen")
$DrawColor("MOTIVATION")
$DrawColor("Magenta")
$DrawColor("Maroon")
$DrawColor("MediumAquaMarine")
$DrawColor("MediumBlue")
$DrawColor("MediumOrchid")
$DrawColor("MediumPurple")
$DrawColor("MediumSeaGreen")
$DrawColor("MediumSlateBlue")
$DrawColor("MediumSpringGreen")
$DrawColor("MediumTurquoise")
$DrawColor("MediumVioletRed")
$DrawColor("MidnightBlue")
$DrawColor("MintCream")
$DrawColor("MistyRose")
$DrawColor("Moccasin")
$DrawColor("NavajoWhite")
$DrawColor("Navy")
$DrawColor("OldLace")
$DrawColor("Olive")
$DrawColor("OliveDrab")
$DrawColor("Orange")
$DrawColor("OrangeRed")
$DrawColor("Orchid")
$DrawColor("PHYSICAL")
$DrawColor("PaleGoldenRod")
$DrawColor("PaleGreen")
$DrawColor("PaleTurquoise")
$DrawColor("PaleVioletRed")
$DrawColor("PapayaWhip")
$DrawColor("PeachPuff")
$DrawColor("Peru")
$DrawColor("Pink")
$DrawColor("Plum")
$DrawColor("PowderBlue")
$DrawColor("Purple")
$DrawColor("Red")
$DrawColor("RosyBrown")
$DrawColor("RoyalBlue")
$DrawColor("STRATEGY")
$DrawColor("SaddleBrown")
$DrawColor("Salmon")
$DrawColor("SandyBrown")
$DrawColor("SeaGreen")
$DrawColor("SeaShell")
$DrawColor("Sienna")
$DrawColor("Silver")
$DrawColor("SkyBlue")
$DrawColor("SlateBlue")
$DrawColor("SlateGray")
$DrawColor("SlateGrey")
$DrawColor("Snow")
$DrawColor("SpringGreen")
$DrawColor("SteelBlue")
$DrawColor("TECHNOLOGY")
$DrawColor("Tan")
$DrawColor("Teal")
$DrawColor("Thistle")
$DrawColor("Tomato")
$DrawColor("Turquoise")
$DrawColor("Violet")
$DrawColor("Wheat")
$DrawColor("White")
$DrawColor("WhiteSmoke")
$DrawColor("Yellow")
$DrawColor("YellowGreen")

}
@enduml

color_uml

附注

PlantUML资料

标签:

分类:

更新时间: