PlantUML 总结
1. 概述
PlantUML 是一个开源工具,允许用户通过简单的文本描述来生成各种UML图表。它支持多种图表类型,包括但不限于序列图、用例图、类图、活动图等。
2. 基本概念
2.1 开始和结束标记
@startuml
和@enduml
:用于标记PlantUML代码块的开始和结束。
plantuml
@startuml
...
@enduml
2.2 标题
title
:为图表添加标题。
plantuml
title My First PlantUML Activity Diagram
3. 活动图的基本元素
3.1 开始与结束
start
和stop
:表示流程的开始和结束。
plantuml
start
:Begin Process;
stop
3.2 动作(Action)
:ActionName;
:表示一个具体的操作或步骤。
plantuml
start
:Initialize System;
stop
3.3 条件分支
if (condition) then (yes)
和else
:用于条件判断。
plantuml
start
if (Is Valid Input?) then (yes)
:Process Data;
else (no)
:Log Error;
endif
stop
3.4 循环
while (condition)
和endwhile
:用于循环操作。
plantuml
start
while (More Data to Process?)
:Fetch Data;
endwhile
stop
3.5 并发处理
fork
和end fork
:用于表示并发执行的操作。
plantuml
start
fork
:Task A;
fork again
:Task B;
end fork
stop
4. 高级用法
4.1 分区(Partition)
partition "Name"
:用于将流程分组,便于组织和可视化。
plantuml
partition "Data Processing" #LightBlue {
:Fetch Data;
:Process Data;
}
4.2 注释
note left
或note right
:用于在图表中添加注释。
plantuml
start
:Initialize System;
note right: This is the initialization step.
stop
4.3 参与者(Actor)
actor ActorName
:用于表示系统外部的人或实体。
plantuml
actor User
participant "Web Browser" as WB
participant "Server" as S
User -> WB: Open Home Page
WB -> S: Request Home Page
S -> WB: Return Home Page Content
WB -> User: Show Home Page
4.4 返回箭头
- 在序列图中,可以明确显示返回值。
plantuml
actor User
participant "Web Browser" as WB
participant "Server" as S
User -> WB: Login Request
activate WB
WB -> S: Forward Login Request
activate S
S -> WB: Authentication Result
return Authenticated Successfully
deactivate S
WB -> User: Display Dashboard
deactivate WB
5. 示例
5.1 简单流程图
plantuml
@startuml
title Simple Process Example
start
partition "Process A" #LightBlue {
:Step 1;
if (Condition?) then (yes)
:Step 2;
else (no)
:Step 3;
endif
}
partition "Process B" #LightGreen {
fork
:Step 4;
fork again
:Step 5;
end fork
}
stop
@enduml
5.2 包含异常处理的流程图
plantuml
@startuml
title Exception Handling Example
start
:Start Process;
if (Is Valid Input?) then (yes)
:Process Data;
else (no)
:Log Error;
stop
endif
:Finish Process;
stop
@enduml
5.3 复杂条件分支和循环
plantuml
@startuml
title Complex Conditions and Loops
start
while (More Data to Process?)
:Fetch Data;
if (Data Valid?) then (yes)
:Process Data;
else (no)
:Log Error;
endif
endwhile
:Finalize Processing;
stop
@enduml
6. 自定义皮肤参数
你可以通过设置皮肤参数来自定义图表的外观。
plantuml
@startuml
skinparam monochrome true
skinparam backgroundColor #EEEBDC
skinparam sequence {
ArrowColor DeepSkyBlue
ActorBorderColor DeepSkyBlue
LifeLineBorderColor blue
LifeLineBackgroundColor #A9DCDF
}
actor User
participant "Web Browser" as WB
participant "Server" as S
User -> WB: Login Request
activate WB
WB -> S: Forward Login Request
activate S
S -> WB: Authentication Result
return Authenticated Successfully
deactivate S
WB -> User: Display Dashboard
deactivate WB
@enduml
7. 更多图表类型
除了活动图,PlantUML还支持其他多种图表类型,如用例图、类图、时序图等。
7.1 用例图
plantuml
@startuml
actor User
actor Admin
usecase UC1 as "Login"
usecase UC2 as "Register"
usecase UC3 as "Manage Users"
User --> UC1
Admin --> UC1
Admin --> UC2
Admin --> UC3
@enduml
7.2 类图
plantuml
@startuml
class Animal {
+name: String
+age: int
+eat()
}
class Dog {
+bark()
}
Animal <|-- Dog
@enduml
7.3 时序图
plantuml
@startuml
actor User
participant "Web Browser" as WB
participant "Server" as S
User -> WB: Login Request
activate WB
WB -> S: Forward Login Request
activate S
S -> WB: Authentication Result
return Authenticated Successfully
deactivate S
WB -> User: Display Dashboard
deactivate WB
@enduml