UML作業第五次:分析系統,繪制狀態圖


UML作業第五次:分析系統,繪制狀態圖

作業題目:分析電梯控制系統和銀行賬戶系統,分別畫出狀態圖

一、要求及步驟

step1:學習PlantUML狀態圖繪制方法

step2:以《電梯控制》系統、《銀行賬戶》系統為例,分析系統中的狀態。

step3: 分析電梯運行、銀行賬戶的狀態轉換;

step4:用PlantUML 編寫 《電梯控制》系統、《銀行賬戶》系統 狀態轉換的腳本;

step5:在線繪制UML圖;

一、活動圖語法

活動標簽(activity label)以冒號開始,以分號結束,文本格式支持creole wiki語法。,活動默認安裝它們定義的順序就行連接。

@startuml
:Hello world;
:This is on defined on
several **lines**;
@enduml

開始/結束

可以使用關鍵字startstop表示圖示的開始和結束。

@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

條件語句

在圖示中可以使用關鍵字ifthenelse設置分支測試。標注文字則放在括號中。

@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

 

重復循環

可以使用關鍵字repeatrepeatwhile進行重復循環。

@startuml

start

repeat
  :read data;
  :generate diagrams;
repeat while (more data?)

stop

@enduml
while循環

可以使用關鍵字whileend 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

並行處理

可以使用關鍵字forkfork againend fork表示並行處理。

@startuml

start

if (multiprocessor?) then (yes)
  fork
	:Treatment 1;
  fork again
	:Treatment 2;
  end fork
else (monoproc)
  :Treatment 1;
  :Treatment 2;
endif

@enduml

注釋

文本格式支持creole wiki語法。

A note can be floating, using floating keyword.

@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

 顏色

可以為活動(activity)指定一種顏色。

@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

連接器(Connector)

可以使用括號定義連接器。

@startuml
start
:Some activity;
(A)
detach
(A)
:Other activity;
@enduml

組合(grouping)

通過定義分區(partition),你可以把多個活動組合(group)在一起。

@startuml
start
partition Initialization {
	:read config file;
	:init internal variable;
}
partition Running {
	:wait for user interaction;
	:print information;
}

stop
@enduml

泳道(Swimlanes)

你可以使用管道符|來定義泳道。

還可以改變泳道的顏色。

@startuml
|Swimlane1|
start
:foo1;
|#AntiqueWhite|Swimlane2|
:foo2;
:foo3;
|Swimlane1|
:foo4;
|Swimlane2|
:foo5;
stop
@enduml

分離(detach)

可以使用關鍵字detach移除箭頭。

@startuml
 :start;
 fork
   :foo1;
   :foo2;
 fork again
   :foo3;
   detach
 endfork
 if (foo4) then
   :foo5;
   detach
 endif
 :foo6;
 detach
 :foo7;
 stop
@enduml

特殊領域語言(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)電梯控制

1.電梯存在四種基本狀態,即:待載、上升、下降、樓間停;

2.電梯無人承載時停在某一樓層,處於待載狀態;

3.當有人進入電梯,並且當前樓層比目標樓層低,電梯上升;

4.當有人進入電梯,並且當前樓層比目標樓層高,電梯下降;

5.當在某一層時電梯內所有人都離開電梯,電梯回到待載狀態;

6.如果電梯中存在人,則執行2或3步驟

(2)銀行賬戶

1.銀行賬戶存在三種狀態,即:空額、有余額、負債;

2.初建立的賬戶沒有存款,處於空額狀態;

3.若在空額狀態時向賬戶存款,則進入有余額狀態;若在空額狀態時從賬戶取款,進入負債狀態;

4.在有余額狀態時向賬戶存款,余額增加,但還是處於有余額狀態;

5.從賬戶中取款,若取款額小於余額,處於有余額狀態;等於余額,回到空額狀態;大於余額,進入負債狀態;

6.負債時,取款則余額減少,仍處於負債狀態;存款,當存款額小於負債額,還是負債;存款額等於負債額,回到空額狀態;若存款額大於負債額,進入有余額狀態。

三、腳本

電梯控制:

@startuml [*] --> 待載

  待載 -up-> 上升 : 進入[目標樓層>當前樓層]/關門上行  

 上升 -down-> 樓間停 : [進人/出人]/停機開門
 待載 -down-> 下降 : 進入[目標樓層<當前樓層]/關門下行  

 下降 -up-> 樓間停 : [進人/出人]/停機開門

  樓間停 -up-> 上升:[目標樓層>當前樓層]/關門上行  

  樓間停 -down-> 下降:[目標樓層<當前樓層]/關門下行   

  樓間停 -left-> 待載:[無人]/關門

@enduml

銀行賬戶:

@startuml [*] --> 空額
空額 -down-> 有余額 : 存款/余額=余額+存款額

空額 -down-> 負債 : 取款[取款額<最大限額]/余額=余額-存款額

有余額 --> 有余額 : 存款/余額=余額+存款額

有余額 --> 有余額 : 取款[取款額<余額]/余額=余額-取款額

有余額 -right-> 負債 : 取款[取款額>余額]/余額=余額-取款額

有余額 -up-> 空額 : 取款[取款額=余額]/余額=余額-取款額

負債 --> 負債 : 取款[取款額<最大限額]/余額=余額-存款額

負債 --> 負債 : 存款[存款額<負債額]/余額=余額+存款額

負債 -left-> 有余額 : 存款[存款額>負債額]/余額=余額+存款額

負債 -up-> 空額 : 存款[存款額=負債額]/余額=余額+存款額

空額 -up-> [*]

@enduml

四、UML圖

電梯控制:

銀行賬戶:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM