活動圖(activity diagrams) by davebusters

VIEWS: 2,695 PAGES: 41

									  活動圖(activity diagrams)
• 何謂活動圖
  UML中的活動圖是一個多工的流程圖,透過繪製活動圖
不但可以讓你塑模企業的工作流程,同時可以做為將來實做
工作流程的類別或方法。
  活動圖主要是用來描述活動(activity)發生的順序,就
觀念(sense)上來說,它有點像是流程圖(flow chart),
不過它和流程圖最不同的地方就是:流程圖通常被限制只能
作順序性的處理,然而活動圖卻允許平行(parallel)處理。
而除了允許平行處理之外,它同時也支援條件式
(conditional)行為。
                                     開始

活動圖                            activity

                                          分叉
 由右圖可知,條件式的行
為可藉由分支(branches)和
                    activity                activity
合併(merges)來顯示;而
平行行為則可藉由分叉(fork)                                  分支
和會合(join)來顯示。                    [條件一]                  [條件二]

                    activity          activity     activity

                                                 合併



                                           會合

                                activity


                                           結束
使用目的
1.   描述內部行為:利用圖形描述出運算的內部行為。
2.   描述平行活動:能描述出平行的活動。
3.   描述各類型活動。
4.   呈現一般性功能:能夠將系統內部的一般性功能給呈現出來。
5.   方便規範書撰寫:當你在撰寫規範書時會使用到活動圖。
             活動圖的元素
編號    圖示     意義    編號            圖示            意義




(1)   name   活動    (2)                        分支
                         [條件b]        [條件a]




(3)          合併    (4)                        分叉



(5)          會合    (6)           *            動態並行


(7)          行動流   (8)                        物件流
(1)活動(activity)

   活動在UML中可以具有下列特質:

   •代表工作成效(performance)
   •代表某一執行的宣告
   •類似狀態
   •圖形表示
   •唯一性
(2)分支(branch)
   分支有一個輸入流和數個有成立條件的輸出流。而在這數個輸
出流中,只有一個輸出流會發生,也就是說,這些成立條件間必須
是互斥的。例如在下圖中,當學期結束,父母在家收到孩子的成績
單後,若成績單上出現滿江紅的情形,則可能對小孩子施予扣除零
用錢的處罰;若其中有一、兩科是高於90分的,則會給予適當的獎
勵。
                        小孩收到成績單的活動圖(一)


               收到成績單




       [不及格]            [及格且高於90分]


          處罰           獎勵


                 …          …
• Conditional behavior is delineated by decisions
  and merges. A decision, called branch.
• Each time you reach a decision, you can take only
  one of the outbound flows, so the guards should be
  mutually exclusive.
• Using [else] as a guard indicates that the [else]
  flow should be used if all the other guards on the
  decision are false.
另外,如果你希望讓分支和合併在圖上能清楚的顯示,則可以在這
些地方加上菱形。如下圖:

     [x>0]
                                [x>0]
             [x=0]                      [x=0]
  Activity           Activity

     [x<0]                      [x<0]
(3)合併(merge)
 合併會有數個輸入流和一個輸出流,其用來表示以分支為開端的
條件式行為的結束。
 承接上圖,當父母對小孩子處罰或獎勵後,通常會進行精神喊話,
即針對不及格科目,多用功點;針對及格的科目,望其繼續保持下
去等。
            …
                 小孩收到成績單的活動圖(二)




          精神喊話
• A merge has multiple input flows and a
  single output. A merge marks the end of
  conditional behavior started by a decision.
•
(4)分叉(fork)
 分叉有一個輸入流和數個輸出流。而這些輸出流所引發的
活動是會平行發生的。例如:晚餐時間到時,我們通常會邊
吃飯邊看電視,吃飯和看電視這兩件事情是同時發生的,因
此可用分叉來表示。如下圖所示:



           吃飯時間




      吃飯          看電視
                 Actions in Parallel
• A fork has one incoming flow and several outgoing concurrent flows
• The sequence between them is irrelevant
   – Fill order, send invoice, deliver, and then receive payment
   – Send invoice, receive payment, fill order, and then deliver
• Do actions by interleaving
   – Fill order of item #1, send invoice of item #1, fill order of item #2,
      send invoice of item #2 …
• Do actions simultaneously
   – Send invoice of item #1 and fill order of item #2
                  Synchronization
• When you have parallelism, you’ll need to synchronize
   – Don’t close an order until it is delivered and paid for
• With a join, outgoing flow is taken only when all incoming flows
  reach the join
• Principles
   – Joins and forks should balance – the number of flows leaving a
      fork should match the number of flows entering its corresponding
      join
   – Show all joins and merges explicitly
   – Use only a single incoming and outgoing flow for each action
          Join Specifications
• A join specification is a Boolean
  expression attached to a join
• Each time a token arrives, the join
  specification is evaluated and if true, an
  output token is emitted
(5) 會合(join)
•會合有數個輸入流和一個輸出流,其用來表示以分叉為開端
的平行式行為的結束。
•分叉與會合必須配對使用,即每一個分叉都有一個相對應的
會合。
•另外,你也可以在一個分叉後,又開始另一個分叉,如圖A,
即分叉A 分叉B 會合B 會合A。
•如圖A所示,如果一個執行緒(thread)離開一個分叉後,
直接進入另一個分叉,那我們可以省略第二個分叉。
•如果一個會合直接進入另一個會合,則亦可省略第一個會合。
經由上述,我們便可將圖A簡略成如圖B所示。
      圖A          圖B


           分叉A         分叉A
分叉B




會合B

            會合A        會合A
(5) 會合(join)-續
•另外,所有進入會合的狀態都必須在會合前完成。
•不過你可以為一個離開分叉的執行緒(thread)加上條件,這樣
的執行緒稱之為條件式執行緒。
•在執行期間,假如條件式執行緒的條件不成立的話,那麼這個執
行緒會在會合被考慮到的時候,會被認為已經完成了。
•如下圖所示:即使你不想喝湯,你依然可以吃到你想吃的蛋包飯。



                炒飯    煮蛋    [想喝湯]
      省略第二個分叉


                           煮湯
                 結合
活動的分解
 活動圖和狀態圖一樣,你可以將活動分解為子活動。
你也可以用父代圖的方示顯示超狀態,也可以直接顯示超
狀態而將子狀態顯示在超狀態的內部。
  在子活動中,明白標示開始和結束狀態的好處是:
這個子活動還可以被套用在其它的情境。
          Decomposing an Action
• Actions can be decomposed into subactivities
   – Take the delivery logic and define it as its own activity
   – Then we can call it as an action
Decomposing an Action
      Decomposing an Action
• Use the rake symbol for subactivities
• Methods
  – If a single method call, use syntax
             class-name :: method-name
  – Otherwise, write a code fragment into the
    action symbol
子狀態顯示在超狀態的內部




           子狀態名稱
          [條件二]    [條件一]
(7)行動流(action flow)
•UML 2 uses the terms flow and edge synonymously to describe the
connections between two actions.

•行動流是用來表示各活動之間的結合關係
其圖形符號是以一實線箭頭表示,可在其上加上條件,表示若要觸
發這個轉移,就必須滿足此條件
另外你也可以加上一斜線,其後接著一個行動表示式,用來說明當
轉移被觸發時的結果行動。如下圖所示:

                       [條件式]/行動



                     行動流的表示方法
(8)物件流
•物件流是表示各活動和物件間的結合關係。
•圖形符號是以一虛線箭頭表示。
•物件流可能是活動的輸入或輸出,當其為輸入時,表示該活動
使用此物件;反之,若為輸出,則表示該活動建立此物件或更改
此物件。如下圖所示:




            物件


         物件流的表示方法
• If you're having difficulty routing lines, you
  can use connectors. When you use
  connectors, you must use them in pairs: one
  with incoming flow, one with an outgoing
  flow, and both with the same label.
• If you are passing an object along the edge,
  you can show that by putting a class box on
  the edge, or you can use pins on the actions,
水道(swim lanes)(Partitions)
•利用水道可以讓我們獲得想要的資訊。
•使用水道時,你必須整理你的活動圖成為被線分開的垂直
區域。
•每一個區域代表一個物件類別(class)的責任。
•水道並不適合畫出太複雜的圖。
•建議在某些時候使用互動圖(interaction diagrams)會比較適
當。
類別名稱      類別名稱          類別名稱




       水道(swim lanes)
水道(swim lanes)-續
•水道是以垂直的實線來表示,整個區域由相鄰的
swim lane所分隔。
•水道內可包含活動或行動狀態。
•水道的順序並不重要。
•各水道內必須擁有一個負責行動的物件類別名稱。
                        Signals
• Actions can respond to
   – Program or routine invocations
   – Signals
• A signal indicates that the activity receives an event from
  an outside process
• A time signal occurs because of the passage of time
   – End of each month in a fiscal year
   – Each microsecond in a real-time controller
                      Signal
• Signal receipt. The receipt of a signal may be
  shown as a concave pentagon that looks like a
  rectangle with a triangular notch in its side (either
  side). The signature of the signal is shown inside
  the symbol.
• Signal sending. The sending of a signal may be
  shown as a convex pentagon that looks like a
  rectangle with a triangular point on one side
  (either side). The signature of the signal is shown
  inside the symbol.
                              Tokens
•   input/output of a state
• Tokens
   – The initial node creates a token, which then passes to the next
     action, which executes and then passes the token to the next
• Token production and consumption
   – At a fork, one token comes in, and the fork produces a token on
     each of its outbound flows
   – At a join, as each inbound token arrives, nothing happens until all
     the tokens appear at the join; then a token is produced on the
     outbound flow
     Pins and Transformations
• Pins
  – Action parameters can be shown with pins
  – Output parameters of an outbound action
    should match input parameters of another
• Transformations
  – Use transformations if the output parameters
    don’t match the input
             Expansion Regions
• Use expansion
  regions if one
  action’s output
  triggers multiple
  invocations of
  another action
• An expansion region
  marks an activity
  diagram area where
  actions occur once
  for each item in a
  collection
                   Expansion Regions




• “Choose Topics” creates a list of topics as output, each topic as a token
• A topic is used as input for one “Write Article” action
• Each “Review Article” action generates a single article that is added to the output
  list
• When all the tokens end up in the output collection, the region generates a single
  token for the list that is passed to “Publish Newsletter”
             Expansion Regions
• In the example, we have the same number of items in the
  output collection as in the input
   – If the expansion region acts as a filter, there will be
      fewer in the output
• In the example, all articles are written and reviewed in
  parallel
   – We can also have an iterative expansion region
   – Must fully process each input element one at a time
          Expansion Regions
• If there is only a single action that needs
  multiple invocation, use the shorthand
  – The shorthand assumes concurrent expansion
(6)動態並行(dynamic concurrency)
(UML1)
  利用動態並行的方式,可以讓你顯示出重複的活動,即這個活
動會被執行很多次。動態並行的表示方式為在活動內標示*號,如
下圖所示:當你在使用投幣式脫水機時,每當你投入硬幣時,機器
便會做累加金額的動作,當累加的金額滿足機器運作所需的金額時,
便開始進行脫水事宜。


              *
    投錢              進行脫水動作
           累加金額


           投幣式脫水機
               Flow Final
• With multiple tokens, you often get flows
  that stop even when the activity as a whole
  doesn’t end
• A flow final indicates the end of one
  particular flow, without terminating the
  whole activity
                        Flow Final




• If an article is rejected, the token is destroyed by the flow final
• The rest of the activity can continue
• This allows expansion regions to act as filters
   活動圖的優缺點及使用時機
優缺點

     活動圖最大的優點是它支援並且鼓勵平行行為。這使得
它非常適合建立工作流程的模型。它也很適合處理多執行緒
(multithreaded)的程式。
     最大的缺點在它沒有清楚地顯示動作(action)跟物件
間的連結。

使用時機
 •分析使用案例時。
 •瞭解工作流程時。
 •描述複雜的順序性演算法時。
 •處理多執行緒的應用程式時。
 When to Use Activity Diagrams
• We use activity diagrams to model the dynamic aspects of a system
• Model a workflow
   – Focus on activities as viewed by the actors that collaborate with
     the system
   – Modeling object flow is particularly important
• Model an operation
   – Use activity diagrams as flowcharts to model the details of a
     computation
   – Modeling of decision, fork, and join states is particularly important

								
To top