Skip to content

流程图

开始和结束

开始一般使用 start 关键字;结束一般使用 stop/end 关键字。基础用法包括:

start ... stop
start ... end
@startuml
start
:执行操作A;
stop
@enduml
uml diagram

操作步骤

语法为 :operation,其中 operation 是你为该步骤定义的名称。例如:

@startuml
:开始;
:执行操作A;
:执行操作B;
:结束;
@enduml
uml diagram

条件判断

使用 PlantUML 语法通常使用 if-else 关键字。通常由三种语法:

if (...) then (...) ... [else (...) ...] endif
if (...) is (...) then ... [else (...) ...] endif
if (...) equals (...) then ... [else (...) ...] endif
@startuml
:开始;
:执行操作A;
:判断是否满足条件;
if (满足条件) then (是)
  :执行操作B;
else (否)
  :执行操作C;
endif
 
if (条件) is (是) then
   :执行操作D;
else
   :执行操作E;
endif
 
if (条件) equals (是) then
   :执行操作F;
else
   :执行操作G;
endif
:结束;
@enduml
uml diagram

多分支判断使用 elseif 关键字,其中包含水平模式、垂直模式;

  • 水平模式:
@startuml
start
if (condition A) then (yes)
  :Text 1;
elseif (condition B) then (yes)
  :Text 2;
  stop
(no) elseif (condition C) then (yes)
  :Text 3;
(no) elseif (condition D) then (yes)
  :Text 4;
else (nothing)
  :Text else;
endif
stop
@enduml
uml diagram
  • 垂直模式:使用 !pragma useVerticalIf on 进入垂直模式。
@startuml
start
!pragma useVerticalIf on
if (condition A) then (yes)
  :Text 1;
elseif (condition B) then (yes)
  :Text 2;
  stop
(no) elseif (condition C) then (yes)
  :Text 3;
(no) elseif (condition D) then (yes)
  :Text 4;
else (nothing)
  :Text else;
endif
stop
@enduml
uml diagram

并行处理

并行处理可以用 fork 和 join 来表示,fork 表示分支开始,merge 表示分支合并。

fork ... fork again ... end fork
fork ... fork again ... end merge
@startuml
start
fork
  :action 1;
fork again
  :action 2;
end fork
 
fork
  :action 3;
fork again
  :action 4;
end merge
stop
@enduml
uml diagram

循环

循环包括无条件循环(Repeat loop)、有条件循环(While loop),典型的写法包括:

repeat ... repeat while (...) is (...) not (...)
repeat ... backward ... repeat while (...) is (...) -> (...)
while (...) end while
@startuml
start
 
repeat
  :read data;
  :generate diagrams;
repeat while (more data?) is (yes) not (no)
 
repeat
  :read data;
  :generate diagrams;
backward :write data;
repeat while (more data?) is (yes)
-> no;
  :next action;
 
while (check filesize ?) is (not empty)
  :read file;
  backward:log;
endwhile (empty)
:close file;
 
stop
@enduml
uml diagram

分区

在 PlantUML 中,Partition 是用来定义分区的关键字,它允许你将多个活动组合在一起。

@startuml
start
partition Initialization {
    :read config file;
    :init internal variable;
}
partition Running {
    :wait for user interaction;
    :print information;
}
 
stop
@enduml
uml diagram

泳道

在 PlantUML 中,使用 |Swimlane1| 来表示泳道,例如:

@startuml
|Swimlane1|
start
:foo1;
|#AntiqueWhite|Swimlane2|
:foo2;
:foo3;
|Swimlane1|
:foo4;
|Swimlane2|
:foo5;
stop
@enduml
uml diagram

附录