第一部分:活動圖語法測試
(1)簡單活動圖:活動標籤(activity label)以冒號開始,以分號結束。活動默認安裝它們定義的順序就行鏈接。spa
1 @startuml 2 :Hello world; 3 :This is on defined on 4 several **lines**; 5 @enduml
(2)開始/結束:你能夠使用關鍵字start
和stop
表示圖示的開始和結束。3d
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
設置分支測試。標註文字則放在括號中。code
@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
進行重複循環。orm
@startuml start repeat :read data; :generate diagrams; repeat while (more data?) stop @enduml
(5)while循環:能夠使用關鍵字while
和end while
進行while循環。還能夠在關鍵字endwhile
後添加標註,還有一種方式是使用關鍵字is
。blog
@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
表示並行處理。ip
@startuml start if (multiprocessor?) then (yes) fork :Treatment 1; fork again :Treatment 2; end fork else (monoproc) :Treatment 1; :Treatment 2; endif @enduml
(7)註釋:it
@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)箭頭:io
使用->
標記,你能夠給箭頭添加文字或者修改箭頭顏色。同時,你也能夠選擇點狀 (dotted),條狀(dashed),加粗或者是隱式箭頭form
@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