活动图(新语法)
所以从V7947开始提出一种全新的、更好的语法格式和软件实现供用户使用(beta版)。
就像序列图一样,新的软件实现的另一个优点是它不再依赖与Graphviz。
新的语法将会替换旧的语法。然而考虑到兼容性,旧的语法仍被能够使用以确保向前兼容。
但是我们鼓励用户使用新的语法格式。
简单活动图
活动标签(activity label)以冒号开始,以分号结束。文本格式支持creole wiki语法。
活动默认安装它们定义的顺序就行连接。
@startuml :Hello world; :This is on defined on several **lines**; @enduml
![basic diagram activity](https://i-blog.csdnimg.cn/blog_migrate/4a2631494af2f19feca5e3b72e633e4f.png)
开始/结束
你可以使用关键字start
和
stop
表示图示的开始和结束。
@startuml start :Hello world; :This is on defined on several **lines**; stop @enduml
![start and stop in diagram activity](https://i-blog.csdnimg.cn/blog_migrate/c1fb237339fd27a8575fb977f9193d69.png)
end
keyword.
@startuml start :Hello world; :This is on defined on several **lines**; end @enduml
![start and end in diagram activity](https://i-blog.csdnimg.cn/blog_migrate/e01cfd962a106a3af77566131e92c6c4.png)
条件语句
在图示中可以使用关键字if
,
then
和
else
设置分支测试。标注文字则放在括号中。
@startuml start if (Graphviz installed?) then (yes) :process all\ndiagrams; else (no) :process only __sequence__ and __activity__ diagrams; endif stop @enduml
![if then else example](https://i-blog.csdnimg.cn/blog_migrate/5107f3c73df057f518660524afe144fe.png)
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
![several if test](https://i-blog.csdnimg.cn/blog_migrate/d4313067e8d1c006b53f3b85ae02ed4f.png)
重复循环
你可以使用关键字repeat
和
repeatwhile
进行重复循环。
@startuml start repeat :read data; :generate diagrams; repeat while (more data?) stop @enduml
![basic repeat while loop](https://i-blog.csdnimg.cn/blog_migrate/48e437d18c497a17695b976a3e9c4b57.png)
while循环
可以使用关键字while
和
end while
进行while循环。
@startuml start while (data available?) :read data; :generate diagrams; endwhile stop @enduml
![another while loop](https://i-blog.csdnimg.cn/blog_migrate/042d244bb288d5cff5883d6722cd7fc2.png)
endwhile
后添加标注,还有一种方式是使用关键字
is
。
@startuml while (check filesize ?) is (not empty) :read file; endwhile (empty) :close file; @enduml
![while loop with labels](https://i-blog.csdnimg.cn/blog_migrate/a47a6627de1164f72c5bc56a705589bb.png)
并行处理
你可以使用关键字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
![activity diagram with parallel processing](https://i-blog.csdnimg.cn/blog_migrate/9ab7667b1666e4ea528092810c359eb4.png)
注释
文本格式支持creole wiki语法。@startuml start :foo1; 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
![add notes on activity diagram](https://i-blog.csdnimg.cn/blog_migrate/cc1ff5472fdf7e7be00d3527d9733295.png)
标题和图例
你可以给图表(diagram)添加标题、标头、脚注和图例。@startuml title this is my title if (condition?) then (yes) :yes; else (no) :no; note right this is a note end note endif stop legend this is the legend endlegend footer dummy footer header this is a long __dummy__ header end header @enduml
![title and legend on an activity diagram](https://i-blog.csdnimg.cn/blog_migrate/7d52a5c2ad2fd9ff8ac9dc792ed39601.png)
颜色
你可以为活动(activity)指定一种颜色。@startuml start :starting progress; #HotPink:reading configuration files These files should edited at this point!; #AAAAAA:ending of the process; @enduml
![changing colors](https://i-blog.csdnimg.cn/blog_migrate/0bb76771ee8da162e4b150044929de69.png)
箭头
使用->
标记,你可以给箭头添加文字或者修改箭头颜色。
@startuml :foo1; -> You can put text on arrows; if (test) then -[#blue]-> :foo2; -[#green]-> The text can also be on several lines and **very** long...; :foo3; else -[#black]-> :foo4; endif -[#gray]-> :foo5; @enduml
![colored arrows](https://i-blog.csdnimg.cn/blog_migrate/52f0e509d28501c3ad92c4ebefdf635f.png)
组合(grouping)
通过定义分区(partition),你可以把多个活动组合(group)在一起。@startuml start partition Initialization { :read config file; :init internal variable; } partition Running { :wait for user interaction; :print information; } stop @enduml
![grouping and partitionning in activity diagram](https://i-blog.csdnimg.cn/blog_migrate/d857c3b4f8a1ef7ab3da64fdd8f07449.png)
泳道(Swimlanes)
你可以使用管道符|
来定义泳道。
还可以改变泳道的颜色。
@startuml |Swimlane1| start :foo1; |#AntiqueWhite|Swimlane2| :foo2; :foo3; |Swimlane1| :foo4; |Swimlane2| :foo5; stop @enduml
![swimlanes](https://i-blog.csdnimg.cn/blog_migrate/6c8a0b5fe6f3bf93ae8a0a7ad7b90da3.png)
分离(detach)
可以使用关键字detach
移除箭头。
@startuml :start; fork :foo1; :foo2; fork again :foo3; detach endfork if (foo4) then :foo5; detach endif :foo6; detach :foo7; stop @enduml
![stop in activity diagrams](https://i-blog.csdnimg.cn/blog_migrate/39d7b04d91ce7b961e9c46b23d365949.png)
特殊领域语言(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
![SDL example](https://i-blog.csdnimg.cn/blog_migrate/06c93d45d369996c3a7f8d9d8bfdec98.png)
一个完整的例子
@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
![full servlet example](https://i-blog.csdnimg.cn/blog_migrate/93b76cf4827e0e82cf5c6c1172139349.png)
原文:http://zh.plantuml.com/activity2.html