使用PlantUml绘制活动图

使用PlantUml绘制活动图

1. 简单活动图

活动标签 (activity label) 以:开始,以;结束。
文本格式支持 creole wiki 语法。
活动默认安装它们定义的顺序进行连接。

@startuml

:Hello world;
:This is on defined on
several **lines**;

@enduml

2. 开始和结束

你可以使用关键字 startstop 表示图示的开始和结束。

@startuml

start
:Hello world;
:This is on defined on
several **lines**;

'也可以使用 end 关键字表示结束。
stop
@enduml

3. 条件语句

在图示中可以使用关键字 ifthenelse 设置分支测试。
标注文字则放在括号中。

@startuml

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

@enduml

4. 多个分支条件

使用关键字 elseif 设置多个分支测试

@startuml

start

if (condition A) then (yes)
    :Text 1;
elseif (condition B) then (yes)
    :Text 2;
stop
elseif (condition C) then (yes)
    :Text 3;
elseif (condition D) then (yes)
    :Text 4;
else (nothing)
    :Text else;
endif

stop
@enduml

5. repeat循环

使用关键字 repeatrepeatwhile 进行重复循环

@startuml

start

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

stop
@enduml

6. while循环1

可以使用关键字:
whileend while 进行 while 循环。

@startuml

start

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

end
@enduml

7. while循环2

还可以在关键字 endwhile 后添加标注,使用关键字 is

@startuml

while (check filesize ?) is (not empty)
:read file;
endwhile (empty)

:close file;

@enduml

8. 并行处理

你可以使用关键字 :
forkfork againend fork 表示并行处理

@startuml

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

@enduml

9. 备注

文本格式支持 creole wiki 语法。
使用 floating 关键字,备注可以单独悬浮。

@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

10. 活动颜色

@startuml

start
:starting progress;

'颜色打头
#HotPink:reading configuration files
These files should edited at this point!;

#AAAAAA:ending of the process;

@enduml

11. 箭头

使用 -> 标记,你可以给箭头添加文字或者修改箭头颜色。
同时,你也可以选择点状 (dotted),条状 (dashed),加粗或者是隐式箭头。

@startuml

 :foo1;

 -> You can put text on arrows;

 if (test) then

 -[#blue]->

 :foo2;

 -[#green,dashed]-> The text can
 also be on several lines
 and **very** long...;

 :foo3;

 else

 -[#black,dotted]->
 :foo4;

 endif

 -[#gray,bold]->
 :foo5;

@enduml

12. 连接器

使用 () 定义连接器

@startuml

start
:Some activity;

(A)

detach

(A)

:Other activity;

@enduml

13. 组合

通过定义分区 (partition),你可以把多个活动组合 (group) 在一起。

@startuml


start

partition Initialization {
:read config file;
:init internal variable;
}

partition Running {
:wait for user interaction;
:print information;
}

stop

@enduml
@startuml
'通过定义分区 (partition),
'你可以把多个活动组合 (group) 在一起。

start

partition Initialization {
:read config file;
:init internal variable;
}

partition Running {
:wait for user interaction;
:print information;
}

stop

@enduml

14. 泳道

你可以使用管道符 | 来定义泳道。
还可以改变泳道的颜色。

@startuml

|Swimlane1|
start
:foo1;

|#AntiqueWhite|Swimlane2|
:foo2;
:foo3;

|Swimlane1|
:foo4;

|Swimlane2|
:foo5;
stop

@enduml

15. 分离

可以使用关键字 detach 移除箭头。

@startuml

:start;
fork
    :foo1;
    :foo2;
fork again
    :foo3;
    detach
endfork

if (foo4) then
    :foo5;
    detach
endif

:foo6;
detach
:foo7;
stop

@enduml

16. 特殊领域语言

通过修改活动标签最后的分号分隔符 (;),可以为活动设置不同的形状。
• |
• <
• >
• /
• ]
• }

@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

17. 完整的例子

@startuml

start

:ClickServlet.handleRequest();
:new page;

if (Page.onSecurityCheck) then (true)
    :Page.onInit();

    if (isForward?) then (no)
        :Process controls;

        if (continue processing?) then (no)
            stop
        endif

        if (isPost?) then (yes)
            :Page.onPost();
        else (no)
            :Page.onGet();
        endif

        :Page.onRender();
    endif

else (false)
endif

if (do redirect?) then (yes)
    :redirect process;
else
    if (do forward?) then (yes)
        :Forward request;
    else (no)
        :Render page template;
    endif
endif

stop
@enduml

   转载规则


《使用PlantUml绘制活动图》 congwiny 采用 知识共享署名 4.0 国际许可协议 进行许可。
评论
 本篇
使用PlantUml绘制活动图 使用PlantUml绘制活动图
使用PlantUml绘制活动图1. 简单活动图活动标签 (activity label) 以:开始,以;结束。文本格式支持 creole wiki 语法。活动默认安装它们定义的顺序进行连接。 @startuml :Hello world;
2020-06-06
下一篇 
使用PlantUml绘制类图2 使用PlantUml绘制类图2
使用PlantUml绘制类图214. 隐藏字段和函数@startuml class Dummy1 { +myMethods() } class Dummy2 { +hiddenMethod() } class Dummy3 <<
2020-06-06
  目录