活动图(新语法)

当前活动图(activity diagram)的语法有诸多限制和缺点,比如代码难以维护。

所以从V7947开始提出一种全新的、更好的语法格式和软件实现供用户使用(beta版)。

就像序列图一样,新的软件实现的另一个优点是它不再依赖于Graphviz。

新的语法将会替换旧的语法。然而考虑到兼容性,旧的语法仍被能够使用以确保向前兼容。

但是我们鼓励用户使用新的语法格式

简单活动图

活动标签(activity label)以冒号开始,以分号结束。
文本格式支持creole wiki语法。

活动默认安装它们定义的顺序就行连接。

PlantUML参考之活动图 - 图1

开始/结束

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

PlantUML参考之活动图 - 图2 也可以使用 end 关键字。

PlantUML参考之活动图 - 图3

条件语句

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

PlantUML参考之活动图 - 图4%20then%20(yes)%0A%20%20%3Aprocess%20all%5Cndiagrams%3B%0Aelse%20(no)%0A%20%20%3Aprocess%20only%0A%20%20sequence%20and%20activity%20diagrams%3B%0Aendif%0A%0Astop%0A%0A%40enduml#card=puml&code=%40startuml%0A%0Astart%0A%0Aif%20%28Graphviz%20installed%3F%29%20then%20%28yes%29%0A%20%20%3Aprocess%20all%5Cndiagrams%3B%0Aelse%20%28no%29%0A%20%20%3Aprocess%20only%0A%20%20sequence%20and%20activity%20diagrams%3B%0Aendif%0A%0Astop%0A%0A%40enduml&id=af7ca505&type=puml) 也可以使用关键字elseif设置多个分支测试。

PlantUML参考之活动图 - 图5%20then%20(yes)%0A%20%20%3AText%201%3B%0Aelseif%20(condition%20B)%20then%20(yes)%0A%20%20%3AText%202%3B%0A%20%20stop%0Aelseif%20(condition%20C)%20then%20(yes)%0A%20%20%3AText%203%3B%0Aelseif%20(condition%20D)%20then%20(yes)%0A%20%20%3AText%204%3B%0Aelse%20(nothing)%0A%20%20%3AText%20else%3B%0Aendif%0Astop%0A%40enduml#card=puml&code=%40startuml%0Astart%0Aif%20%28condition%20A%29%20then%20%28yes%29%0A%20%20%3AText%201%3B%0Aelseif%20%28condition%20B%29%20then%20%28yes%29%0A%20%20%3AText%202%3B%0A%20%20stop%0Aelseif%20%28condition%20C%29%20then%20%28yes%29%0A%20%20%3AText%203%3B%0Aelseif%20%28condition%20D%29%20then%20%28yes%29%0A%20%20%3AText%204%3B%0Aelse%20%28nothing%29%0A%20%20%3AText%20else%3B%0Aendif%0Astop%0A%40enduml&id=84b7217e&type=puml)

重复循环

你可以使用关键字repeatrepeatwhile进行重复循环。

PlantUML参考之活动图 - 图6%0A%0Astop%0A%0A%40enduml#card=puml&code=%40startuml%0A%0Astart%0A%0Arepeat%0A%20%20%3Aread%20data%3B%0A%20%20%3Agenerate%20diagrams%3B%0Arepeat%20while%20%28more%20data%3F%29%0A%0Astop%0A%0A%40enduml&id=1e7dfe4a&type=puml)

while循环

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

PlantUML参考之活动图 - 图7%0A%20%20%3Aread%20data%3B%0A%20%20%3Agenerate%20diagrams%3B%0Aendwhile%0A%0Astop%0A%0A%40enduml#card=puml&code=%40startuml%0A%0Astart%0A%0Awhile%20%28data%20available%3F%29%0A%20%20%3Aread%20data%3B%0A%20%20%3Agenerate%20diagrams%3B%0Aendwhile%0A%0Astop%0A%0A%40enduml&id=c792cbc5&type=puml) 还可以在关键字endwhile后添加标注,还有一种方式是使用关键字is。

PlantUML参考之活动图 - 图8%20is%20(not%20empty)%0A%20%20%3Aread%20file%3B%0Aendwhile%20(empty)%0A%3Aclose%20file%3B%0A%40enduml#card=puml&code=%40startuml%0Awhile%20%28check%20filesize%20%3F%29%20is%20%28not%20empty%29%0A%20%20%3Aread%20file%3B%0Aendwhile%20%28empty%29%0A%3Aclose%20file%3B%0A%40enduml&id=2c361da5&type=puml)

并行处理

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

PlantUML参考之活动图 - 图9%20then%20(yes)%0A%20%20fork%0A%09%3ATreatment%201%3B%0A%20%20fork%20again%0A%09%3ATreatment%202%3B%0A%20%20end%20fork%0Aelse%20(monoproc)%0A%20%20%3ATreatment%201%3B%0A%20%20%3ATreatment%202%3B%0Aendif%0A%0A%40enduml#card=puml&code=%40startuml%0A%0Astart%0A%0Aif%20%28multiprocessor%3F%29%20then%20%28yes%29%0A%20%20fork%0A%09%3ATreatment%201%3B%0A%20%20fork%20again%0A%09%3ATreatment%202%3B%0A%20%20end%20fork%0Aelse%20%28monoproc%29%0A%20%20%3ATreatment%201%3B%0A%20%20%3ATreatment%202%3B%0Aendif%0A%0A%40enduml&id=ed9c2694&type=puml)

注释

文本格式支持creole wiki语法。

A note can be floating, using floating keyword.

PlantUML参考之活动图 - 图10%22%22%20is%20prohibited%0Aend%20note%0Astop%0A%0A%40enduml#card=puml&code=%40startuml%0A%0Astart%0A%3Afoo1%3B%0Afloating%20note%20left%3A%20This%20is%20a%20note%0A%3Afoo2%3B%0Anote%20right%0A%20%20This%20note%20is%20on%20several%0A%20%20%2F%2Flines%2F%2F%20and%20can%0A%20%20contain%20%3Cb%3EHTML%3C%2Fb%3E%0A%20%20%3D%3D%3D%3D%0A%20%20%2A%20Calling%20the%20method%20%22%22foo%28%29%22%22%20is%20prohibited%0Aend%20note%0Astop%0A%0A%40enduml&id=63103321&type=puml)

颜色

你可以为活动(activity)指定一种颜色。

PlantUML参考之活动图 - 图11

箭头

使用->标记,你可以给箭头添加文字或者修改箭头颜色。

同时,你也可以选择点状(dotted),条状(dashed),加粗或者是隐式箭头

PlantUML参考之活动图 - 图12%20then%0A%20%20-%5B%23blue%5D-%3E%0A%20%20%3Afoo2%3B%0A%20%20-%5B%23green%2Cdashed%5D-%3E%20The%20text%20can%0A%20%20also%20be%20on%20several%20lines%0A%20%20and%20very%20long…%3B%0A%20%20%3Afoo3%3B%0Aelse%0A%20%20-%5B%23black%2Cdotted%5D-%3E%0A%20%20%3Afoo4%3B%0Aendif%0A-%5B%23gray%2Cbold%5D-%3E%0A%3Afoo5%3B%0A%40enduml#card=puml&code=%40startuml%0A%3Afoo1%3B%0A-%3E%20You%20can%20put%20text%20on%20arrows%3B%0Aif%20%28test%29%20then%0A%20%20-%5B%23blue%5D-%3E%0A%20%20%3Afoo2%3B%0A%20%20-%5B%23green%2Cdashed%5D-%3E%20The%20text%20can%0A%20%20also%20be%20on%20several%20lines%0A%20%20and%20%2A%2Avery%2A%2A%20long…%3B%0A%20%20%3Afoo3%3B%0Aelse%0A%20%20-%5B%23black%2Cdotted%5D-%3E%0A%20%20%3Afoo4%3B%0Aendif%0A-%5B%23gray%2Cbold%5D-%3E%0A%3Afoo5%3B%0A%40enduml&id=a193d7ea&type=puml)

连接器(Connector)

你可以使用括号定义连接器。

PlantUML参考之活动图 - 图13%0Adetach%0A(A)%0A%3AOther%20activity%3B%0A%40enduml#card=puml&code=%40startuml%0Astart%0A%3ASome%20activity%3B%0A%28A%29%0Adetach%0A%28A%29%0A%3AOther%20activity%3B%0A%40enduml&id=d607d57b&type=puml)

组合(grouping)

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

PlantUML参考之活动图 - 图14

泳道(Swimlanes)

你可以使用管道符|来定义泳道。

还可以改变泳道的颜色。

PlantUML参考之活动图 - 图15

分离(detach)

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

PlantUML参考之活动图 - 图16%20then%0A%20%20%20%3Afoo5%3B%0A%20%20%20detach%0A%20endif%0A%20%3Afoo6%3B%0A%20detach%0A%20%3Afoo7%3B%0A%20stop%0A%40enduml#card=puml&code=%40startuml%0A%20%3Astart%3B%0A%20fork%0A%20%20%20%3Afoo1%3B%0A%20%20%20%3Afoo2%3B%0A%20fork%20again%0A%20%20%20%3Afoo3%3B%0A%20%20%20detach%0A%20endfork%0A%20if%20%28foo4%29%20then%0A%20%20%20%3Afoo5%3B%0A%20%20%20detach%0A%20endif%0A%20%3Afoo6%3B%0A%20detach%0A%20%3Afoo7%3B%0A%20stop%0A%40enduml&id=4a93b3f8&type=puml)

特殊领域语言(SDL)

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

  • |
  • <
  • >
  • /
  • ]
  • }

PlantUML参考之活动图 - 图17%7C%0A%3AReceiving%3B%0Asplit%0A%20%3Anak(i)%3C%0A%20%3Aack(o)%3E%0Asplit%20again%0A%20%3Aack(i)%3C%0A%20%3Anext(o)%0A%20on%20several%20line%7C%0A%20%3Ai%20%3A%3D%20i%20%2B%201%5D%0A%20%3Aack(o)%3E%0Asplit%20again%0A%20%3Aerr(i)%3C%0A%20%3Anak(o)%3E%0Asplit%20again%0A%20%3Afoo%2F%0Asplit%20again%0A%20%3Ai%20%3E%205%7D%0Astop%0Aend%20split%0A%3Afinish%3B%0A%40enduml#card=puml&code=%40startuml%0A%3AReady%3B%0A%3Anext%28o%29%7C%0A%3AReceiving%3B%0Asplit%0A%20%3Anak%28i%29%3C%0A%20%3Aack%28o%29%3E%0Asplit%20again%0A%20%3Aack%28i%29%3C%0A%20%3Anext%28o%29%0A%20on%20several%20line%7C%0A%20%3Ai%20%3A%3D%20i%20%2B%201%5D%0A%20%3Aack%28o%29%3E%0Asplit%20again%0A%20%3Aerr%28i%29%3C%0A%20%3Anak%28o%29%3E%0Asplit%20again%0A%20%3Afoo%2F%0Asplit%20again%0A%20%3Ai%20%3E%205%7D%0Astop%0Aend%20split%0A%3Afinish%3B%0A%40enduml&id=5a7d747e&type=puml) 一个完整的例子

PlantUML参考之活动图 - 图18%3B%0A%3Anew%20page%3B%0Aif%20(Page.onSecurityCheck)%20then%20(true)%0A%20%20%3APage.onInit()%3B%0A%20%20if%20(isForward%3F)%20then%20(no)%0A%09%3AProcess%20controls%3B%0A%09if%20(continue%20processing%3F)%20then%20(no)%0A%09%20%20stop%0A%09endif%0A%09%0A%09if%20(isPost%3F)%20then%20(yes)%0A%09%20%20%3APage.onPost()%3B%0A%09else%20(no)%0A%09%20%20%3APage.onGet()%3B%0A%09endif%0A%09%3APage.onRender()%3B%0A%20%20endif%0Aelse%20(false)%0Aendif%0A%0Aif%20(do%20redirect%3F)%20then%20(yes)%0A%20%20%3Aredirect%20process%3B%0Aelse%0A%20%20if%20(do%20forward%3F)%20then%20(yes)%0A%09%3AForward%20request%3B%0A%20%20else%20(no)%0A%09%3ARender%20page%20template%3B%0A%20%20endif%0Aendif%0A%0Astop%0A%0A%40enduml#card=puml&code=%40startuml%0A%0Astart%0A%3AClickServlet.handleRequest%28%29%3B%0A%3Anew%20page%3B%0Aif%20%28Page.onSecurityCheck%29%20then%20%28true%29%0A%20%20%3APage.onInit%28%29%3B%0A%20%20if%20%28isForward%3F%29%20then%20%28no%29%0A%09%3AProcess%20controls%3B%0A%09if%20%28continue%20processing%3F%29%20then%20%28no%29%0A%09%20%20stop%0A%09endif%0A%09%0A%09if%20%28isPost%3F%29%20then%20%28yes%29%0A%09%20%20%3APage.onPost%28%29%3B%0A%09else%20%28no%29%0A%09%20%20%3APage.onGet%28%29%3B%0A%09endif%0A%09%3APage.onRender%28%29%3B%0A%20%20endif%0Aelse%20%28false%29%0Aendif%0A%0Aif%20%28do%20redirect%3F%29%20then%20%28yes%29%0A%20%20%3Aredirect%20process%3B%0Aelse%0A%20%20if%20%28do%20forward%3F%29%20then%20%28yes%29%0A%09%3AForward%20request%3B%0A%20%20else%20%28no%29%0A%09%3ARender%20page%20template%3B%0A%20%20endif%0Aendif%0A%0Astop%0A%0A%40enduml&id=feff8f38&type=puml)