第一部分:活動圖語法
(1)簡單活動圖:活動標簽(activity label)以冒號開始,以分號結束。活動默認安裝它們定義的順序就行連接。
1 @startuml 2 :Hello world; 3 :This is on defined on 4 several **lines**; 5 @enduml
(2)開始/結束:你可以使用關鍵字start
和stop
表示圖示的開始和結束。
1 @startuml 2 start 3 :Hello world; 4 :This is on defined on 5 several **lines**; 6 stop 7 @enduml
@startuml start :Hello world; :This is on defined on several **lines**; end @enduml
(3)條件語句:在圖示中可以使用關鍵字if
,then
和else
設置分支測試。標注文字則放在括號中。
@startuml start if (Graphviz installed?) then (yes) :process all\ndiagrams; else (no) :process only __sequence__ and __activity__ diagrams; endif stop @enduml
@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
(4)重復循環:你可以使用關鍵字repeat
和repeatwhile
進行重復循環。
@startuml start repeat :read data; :generate diagrams; repeat while (more data?) stop @enduml
(5)while循環:可以使用關鍵字while
和end while
進行while循環。還可以在關鍵字endwhile
后添加標注,還有一種方式是使用關鍵字is
。
@startuml start while (data available?) :read data; :generate diagrams; endwhile stop @enduml
@startuml while (check filesize ?) is (not empty) :read file; endwhile (empty) :close file; @enduml
(6)並行處理:你可以使用關鍵字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
(7)注釋:
@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
(8)箭頭:
使用->
標記,你可以給箭頭添加文字或者修改箭頭顏色。同時,你也可以選擇點狀 (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
(9)連接器(Connector):可以使用括號定義連接器。
@startuml
start
:Some activity;
(A)
detach
(A)
:Other activity;
@enduml
(10)組合(grouping):通過定義分區(partition),你可以把多個活動組合(group)在一起。
@startuml start partition Initialization { :read config file; :init internal variable; } partition Running { :wait for user interaction; :print information; } stop @enduml
(11)分離(detach):可以使用關鍵字detach
移除箭頭。
@startuml :start; fork :foo1; :foo2; fork again :foo3; detach endfork if (foo4) then :foo5; detach endif :foo6; detach :foo7; stop @enduml
(12)特殊領域語言(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
第二部分:分析《超市購物》系統
-
超市購物系統擁有三個部分:顧客、收銀員、收款機。
-
顧客進入商店,選擇自己所要購買的商品,並把選好的商品拿到收銀台交給收銀員。
-
收銀員詢問顧客是否是會員,如果是會員,索要顧客的會員卡,把會員卡掃描進系統並對會員卡進行驗證。
-
然后逐一掃描顧客所選商品的條形碼,收款機邊接收商品條碼,邊累加商品金額。
-
掃描完商品信息后,收銀員根據收款機上顯示的商品金額收貨款,然后通過收款機打印售貨單。
-
最后收銀員把售貨單和商品交給顧客,本次購物過程結束。
第三部分:《超市購物》活動圖
1 @startuml 2 start 3 :選擇商品; 4 :商品交給收銀員; 5 if (是否是會員) then (會員) 6 :掃描會員卡; 7 if (是否有效) then (無效) 8 :提示會員卡無效; 9 else (有效) 10 :提示會員卡有效; 11 endif 12 :掃描商品條碼; 13 14 else (非會員) 15 :掃描商品條碼; 16 endif 17 :接收商品條碼; 18 :統計商品金額; 19 while(還有商品否?) is (有) 20 :掃描商品條碼; 21 endwhile (無) 22 :交付貨款; 23 :接受貨款; 24 :打印售貨單; 25 :貨單及貨品交給顧客; 26 :接受貨單及貨品; 27 28 29 stop 30 @enduml