主页

索引

模块索引

搜索页面

活动图

  • 活动图 (新语法) [1]

  • 活动图(老语法) [2]

备注

旧语法有诸多限制和缺点,比如代码难以维护。所以从 V7947 开始提出一种全新的、更好的语法格式和软件实现供用户使用 (beta 版)。就像序列图一样,新的软件实现的另一个优点是它不再依赖于 Graphviz。新的语法将会替换旧的语法。然而考虑到兼容性,旧的语法仍被能够使用以确保 向前兼容

警告

这儿只讲新版

简单活动图

说明:

活动标签 (activity label) 以冒号开始,以分号结束
@startuml
:Hello world;
:This is on defined on
several **lines**;
@enduml

@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

@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

@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

@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 (Graphviz installed?) then (yes)
  :process all\ndiagrams;
else (no)
  :process only
  __sequence__ and __activity__ diagrams;
endif

stop

@enduml

@startuml

start

if (Graphviz installed?) then (yes)
  :process all\ndiagrams;
else (no)
  :process only
  __sequence__ and __activity__ diagrams;
endif

stop

@enduml

Conditional with error on an action:

@startuml
if (condition?) then
  #pink:error;
  kill
endif
#palegreen:action;
@enduml

@startuml
if (condition?) then
  #pink:error;
  kill
endif
#palegreen:action;
@enduml

Conditional with detach on an action:

@startuml
if (condition?) then
  #pink:error;
  detach
endif
#palegreen:action;
@enduml

@startuml
if (condition?) then
  #pink:error;
  detach
endif
#palegreen:action;
@enduml

重复循环

repeat循环

使用关键字 repeat 和 repeatwhile 进行重复循环:

@startuml

start

repeat
  :read data;
  :generate diagrams;
repeat while (more data?)

stop

@enduml

@startuml

start

repeat
  :read data;
  :generate diagrams;
repeat while (more data?)

stop

@enduml

backward keyword:

@startuml

start

repeat :foo as starting label;
  :read data;
  :generate diagrams;
backward:This is backward;
repeat while (more data?)

stop

@enduml

@startuml

start

repeat :foo as starting label;
  :read data;
  :generate diagrams;
backward:This is backward;
repeat while (more data?)

stop

@enduml

break keyword:

@startuml
start
repeat
  :Test something;
    if (Something went wrong?) then (no)
      #palegreen:OK;
      break
    endif
    ->NOK;
    :Alert "Error with long text";
repeat while (Something went wrong with long text?) is (yes)
->no;
:Alert "Sucess";
stop
@enduml

@startuml
start
repeat
  :Test something;
    if (Something went wrong?) then (no)
      #palegreen:OK;
      break
    endif
    ->NOK;
    :Alert "Error with long text";
repeat while (Something went wrong with long text?) is (yes)
->no;
:Alert "Sucess";
stop
@enduml

while循环

使用关键字 while 和 end while:

@startuml

start

while (data available?)
  :read data;
  :generate diagrams;
endwhile

stop

@enduml

@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

@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

@startuml

start

if (multiprocessor?) then (yes)
  fork
    :Treatment 1;
  fork again
    :Treatment 2;
  end fork
else (monoproc)
  :Treatment 1;
  :Treatment 2;
endif

@enduml

split, split again and end split keywords:

@startuml

start

if (multiprocessor?) then (yes)
  fork
    :Treatment 1;
  fork again
    :Treatment 2;
  end fork
else (monoproc)
  :Treatment 1;
  :Treatment 2;
endif

@enduml

@startuml

start

if (multiprocessor?) then (yes)
  fork
    :Treatment 1;
  fork again
    :Treatment 2;
  end fork
else (monoproc)
  :Treatment 1;
  :Treatment 2;
endif

@enduml

Input split (multi-start):

@startuml
split
   -[hidden]->
   :A;
split again
   -[hidden]->
   :B;
split again
   -[hidden]->
   :C;
end split
:D;
@enduml

@startuml
split
   -[hidden]->
   :A;
split again
   -[hidden]->
   :B;
split again
   -[hidden]->
   :C;
end split
:D;
@enduml

Output split (multi-end):

@startuml
start
split
   :A;
   kill
split again
   :B;
   detach
split again
   :C;
   kill
end split
@enduml

@startuml
start
split
   :A;
   kill
split again
   :B;
   detach
split again
   :C;
   kill
end split
@enduml

注释

@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

@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

特殊领域语言 (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
: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

主页

索引

模块索引

搜索页面