UML作业第四次:分析系统,绘制活动图
一、活动图 语法小结
简单活动图
活动标签(activity label)以冒号开始,以分号结束。活动默认安装它们定义的顺序就行连接。
@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

也可以使用 end 关键字。
@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

也可以使用关键字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

重复循环
可以使用关键字repeat和repeatwhile进行重复循环。
@startuml start repeat :read data; :generate diagrams; repeat while (more data?) stop @enduml

while循环
可以使用关键字while和end while进行while循环。
@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

并行处理
可以使用关键字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 :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 :starting progress; #HotPink:reading configuration files These files should edited at this point!; #AAAAAA:ending of the process; @enduml

箭头
使用->标记,你可以给箭头添加文字或者修改箭头颜色。同时,你也可以选择点状 (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

连接器
可以使用括号定义连接器
@startuml
start
:Some activity;
(A)
detach
(A)
:Other activity;
@enduml

组合
通过定义分区(partition),你可以把多个活动组合(group)在一起。
@startuml start partition Initialization { :read config file; :init internal variable; } partition Running { :wait for user interaction; :print information; } stop @enduml

泳道
可以使用管道符|来定义泳道。还可以改变泳道的颜色。
@startuml |Swimlane1| start :foo1; |#AntiqueWhite|Swimlane2| :foo2; :foo3; |Swimlane1| :foo4; |Swimlane2| :foo5; stop @enduml

分离
可以使用关键字detach移除箭头。
@startuml :start; fork :foo1; :foo2; fork again :foo3; detach endfork if (foo4) then :foo5; detach endif :foo6; detach :foo7; stop @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

二、《超市购物》系统
系统中的活动主体:顾客、收银员、收款机;设置三个活动分区
- 顾客进入超市后,选择自己要购买的商品,并把商品拿到收银台给收银员。
- 收银员询问顾客是否是会员。如果是,向顾客索要会员卡,把会员卡扫描进系统并对会员进行认证。
- 然后逐一扫描顾客的商品条形码,收款机接受商品条形码,并累加商品金额。
- 扫描完后,收银员根据收款机上的金额收款,然后通过收款机打印售货单。
- 收银员把售货单和商品给顾客,购物过程结束。
@startuml |顾客| start :选择商品; :商品交给收银员; |#AntiqueWhite|收银员| floating note left: 是否是会员 if() then ([会员]) :扫描会员卡; |收款机| floating note left: 是否有效 if () then ([无效]) :提示会员卡无效; else ([有效]) :提示会员卡有效; :累计积分; endif |收银员| :扫描商品条码; else ([非会员]) |收银员| :扫描商品条码; endif |收款机| :接收商品条码; :统计商品金额; |收银员| while(还有商品否?) is ([有]) :扫描商品条码; endwhile ([无]) |顾客| :交付货款; |收银员| :接受货款; |收款机| :打印售货单; |收银员| :货单及货品交给顾客; |顾客| :接受货单及货品; stop @enduml


浙公网安备 33010602011771号