活动图
一、概览
1.PlantUML活动图语法学习小结。图例及用法
2.语言描述《超市购物》购物活动
3.绘制活动图的脚本程序
4.绘制的活动图
二、语法小结
1.开始和结束
使用关键字start
和stop
表示图示的开始和结束
示例:
@startuml start :Hello wtl; :This is on defined on several **lines**; stop @enduml
2.条件语句
图示中可以使用关键字if
,then
和else
设置分支测试。标注文字则放在括号中
示例:
@startuml start if (Graphviz installed?) then (yes) :process all\ndiagrams; else (no) :process only __sequence__ and __activity__ diagrams; endif stop @enduml
3.重复循环
使用关键字repeat
和repeatwhile
进行重复循环
示例:
@startuml start repeat :read data; :generate diagrams; repeat while (more data?) stop @enduml
4.while循环
使用关键字while
和end while
进行while循环。
示例:
@startuml start while (data available?) :read data; :generate diagrams; endwhile stop @enduml
5.并行处理
使用关键字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
6.添加颜色
@startuml start :starting progress; #HotPink:reading configuration files These files should edited at this point!; #AAAAAA:ending of the process; @enduml
7.箭头属性
使用->
标记,你可以给箭头添加文字或者修改箭头颜色,同时,你也可以选择点状 (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
8.连接器
使用括号定义连接器
示例:
@startuml
start
:Some activity;
(A)
detach
(A)
:Other activity;
@enduml
9.组合
定义分区,可以把多个活动组合在一起
示例:
@startuml start partition Initialization { :read config file; :init internal variable; } partition Running { :wait for user interaction; :print information; } stop @enduml
10.泳道
可以使用管道符|
来定义泳道,还可以改变泳道的颜色
示例:
@startuml |Swimlane1| start :foo1; |#AntiqueWhite|Swimlane2| :foo2; :foo3; |Swimlane1| :foo4; |Swimlane2| :foo5; stop @enduml
11.分离
使用关键字detach
移除箭头
示例:
@startuml :start; fork :foo1; :foo2; fork again :foo3; detach endfork if (foo4) then :foo5; detach endif :foo6; detach :foo7; stop @enduml
二、《超市购物》购物活动
活动主体:顾客、收银员、收款机
活动分区:顾客去、收银区、
超市购物流程:
1.顾客进入超市选择商品,将商品提交给收银员;
2.收银员询问顾客是否是会员。是则向顾客索要会员卡,将会员卡扫描进系统并对会员进行认证;
3 .收银员逐一扫描顾客所选商品的商品条形码,收款机接收商品条形码,累加商品金额;
4.扫描完全部商品后收银员根据收款机上的金额收款,并通过收款机打印售货单;收银员将售货单和商品提交给顾客,购物流程结束
5.收银员将售货单和商品提交给顾客,购物流程结束
三、脚本程序
@startuml |顾客| start :选择商品; :商品递交收银员; |#AAAAAA|收银员| floating note left: 会员卡 if() then ([会员]) :扫描会员卡; |收款机| floating note left: 是否有效 if () then ([无效]) :提示会员卡无效; else ([有效]) :提示会员卡有效; :累计积分; endif |收银员| :扫描商品条码; else ([非会员]) |收银员| :扫描商品条码; endif |收款机| :接收商品条码; :累加商品金额; |收银员| while(还有商品否?) is ([有]) :扫描商品条码; endwhile ([无]) |顾客| :交付货款; |收银员| :接受货款; |收款机| :打印售货单; |收银员| :货单及货品交给顾客; |顾客| :接受货单及货品; stop @enduml