Software Engineering Design & modelling Activity Diagram Interaction Overview Diagram Introduction This chapter looks at how we can use the UML to model the details of the work that objects do, both internally and in conjunction with other objects to model the details of computational processes Chapter outline: Activity diagram Processes and threads Interaction Overview 2 Activity Diagrams Topic Outline Activity diagram Definition Properties Activity and action Transition Object flow Modelling activity diagram 4 Introduction An activity diagram is used to model the dynamic aspects of a system Activity diagrams can be attached to use cases communication Activity diagrams are used to model use cases and complex object workflows 5 Activity diagrams allow the readers to see the system’s execution, and how it changes direction based upon different conditions and stimuli. In this case, activity diagrams are used to model workflow for use cases. For the purpose of this lecture, we will see how activity diagrams can be used to take your use case to the next level.. In the context of use cases, activity diagrams are used to: elaborate identify the pre- and post-conditions discover 6 Definition An activity diagram is essentially a flowchart, showing flow of control from activity to activity It involves modelling the sequential (and possibly concurrent) steps in a computational process modelling the flow of an object as it moves from state to state at different points in the flow of control Activity diagrams emphasize the flow of control from activity to activity Whereas, interaction diagram emphasize the flow of control from object to object 7 Properties Activity diagram commonly contains: Activity states and action states Transitions Swimlanes Object flow may also contain notes and constraints 8 Activity and action states An activity is something that an object performs on an ongoing basis It is non-atomic, which means that it can be interrupted It can also be decomposed into other activities Activities ultimately result in some action An action is a set of executable computations that results in a change in the state of the system, or the return of a value to some objects, or both. It is atomic, meaning it cannot be interrupted It cannot be decomposed 9 It is assumed that an activity lasts for some duration of time, whereas an action generally takes an insignificant amount of time The UML notation is the same for an activity and an action : a lozenge Calculate shipping cost Update customer preferences shipCost = countyCode * subTotal Ship order isFulfilled = True activity action Example of some activities and actions associated with the Online Bookstore 10 Transitions When the action or activity of a state completes, flow of control passes immediately to the next action or activity states Transitions are used to show the path from one action or activity state to the next action or activity state A flow of control has to start and end someplace this can be specified by an initial state (a solid ball) and a stop state ( a solid ball inside a circle) 11 In the UML, a transition is presented as a simple directed line initial state Activity / Action transition Activity / Action stop state 12 A transition between activities or actions can be modelled: sequentially by using branching and merging by using forking and joining 13 Branching and merging A branch is a decision point at which there are two or more possible paths of flow of control Each possible path out of a branch has a guard condition, which is a boolean expression that must be evaluated to true in order for the branch to be taken. Optionally, an “else” branch can also be inserted to account for a “none of the above” situation A guard condition is shown within a square bracket near a path out of the branch Similar to conditional state 14 A merge is a point at which two or more branched paths come together. The UML represents both a branch and a merge with a diamond shape. Compute subtotal [ship outside of US] branching Look up country code [ship within of US] shipCost = regionPercent * subTotal shipCost = countryCode * subTotal merge Compute new subtotal Example of branching and merging for the scenario Process Customer’s Order of the Online Bookstore 15 Forking and Joining A fork is a splitting of a flow of control into two or more flows of control, each of which operates independently of, and concurrently with, the others. A join is a synchronization of two or more flows of control into one flow In UML, the symbol that represents both forks and joins is a long, thin, black rectangle called a synchronization bar. 16 Example of forking and joining associated with the Online Bookstore 17 Swim-lanes Group of activities and actions on an activity diagram can be grouped together with the help of vertical lines. In the UML, each group is called a swim-lane, which is simply a lane down which you can track activities and actions associated with a particular part of an organization. Each swim-lane has a name unique within its diagram The boundaries are simply conceptual and transitions may cross lanes 18 Receive order Shipping Retrieve books from inventory Marketing Update customer preferences Accounting Post General Ledger entries Calculate shipping costs swimlane Example of swimlanes Ship order swimlane 19 Object Flow Objects may be involved in the flow of control associated with an activity diagram. These objects may be specified in the activity diagram by using object flows Object flows are simply dependencies that show the details of how the object(s) involved in the various activities/actions are specifically affected 20 The UML provides two ways to show objects within object flows, involving: showing the new values for one or more attributes, using the notation (attribute = value) showing a “state” value in square brackets 21 An example of object flows 22 Modelling activity diagrams Activity diagrams can be used in two ways: To model a workflow focus on activities as viewed by the actors that collaborate with the system Workflows are used to visualize, specify, construct, and document business processes that involve the system being developed To model an operation used to model the details of a computation 23 Steps: 1. 2. Identify the activities and actions involved (to be rendered as activity and action states in the activity diagram) Identify activities that can be grouped together in order to form swimlanes 3. Render the transitions that connect these activity and action states 4. For each swimlanes identified, give an appropriate name Start with sequential flows Use branching and merging, or forking and joining, or both, if and where necessary Starting from the initial state, draw the activity diagrams until the final state If there are important objects that are involved, include them in the activity diagram 24 Receive order Shipping Marketing Retrieve books from inventory Calculate shipping cost Update customer preferences Accounting Post General Ledger entries Compute subtotal [ship outside of US] Look up country code [ship within of US] shipCost = regionPercent * subTotal shipCost = countryCode * subTotal Compute new subtotal Example of an activity diagram associated with the Process Customer’s Order scenario of the Online Bookstore Ship order 25 A simple example Develop an activity diagram based on the following narrative. The purpose of the Open Access Insurance System is to provide automotive insurance to car owners. Initially, prospective customers fill out an insurance application, which provides information about the customer and his or her vehicles. This information is sent to an agent, who sends it to various insurance companies to get quotes for insurance. When the responses return, the agent then determines the best policy for the type and level of coverage desired and gives the customer a copy of the insurance policy proposal and quote. 26 Steps: 1. 2. Identify the activities and actions involved (to be rendered as activity and action states in the activity diagram) Identify activities that can be grouped together in order to form swimlanes 3. Render the transitions that connect these activity and action states 4. For each swimlanes identified, give an appropriate name Start with sequential flows Use branching and merging, or forking and joining, or both, if and where necessary Starting from the initial state, draw the activity diagrams until the final state If there are important objects that are involved, include them in the activity diagram 27 Steps: 1) Identify the activities and actions involved The purpose of the Open Access Insurance System is to provide automotive insurance to car owners. Initially, prospective customers fill out an insurance application, which provides information about the customer and his or her vehicles. This information is sent to an agent, who sends it to various insurance companies to get quotes for insurance. When the responses return, the agent then determines the best policy for the type and level of coverage desired and gives the customer a copy of the insurance policy proposal and quote. 28 Actions and activities identified Prospective customers fill out an insurance application This information is sent to an agent… Agent receives the quotes … agent then determines the best policy Agent receives the application Agent sends it to various insurance companies to get quotes for insurance Insurance companies come up with/develop quotes When the responses return … The application is sent to an insurance agent … who sends it to various insurance companies to get quotes for insurance Prospective customers fill out form The agent decides best policy … and gives the customer a copy of the insurance policy proposal and quote. The agent sends the customer a copy of the insurance policy proposal and quote. Customer receives proposal and quotes 29 2) Identify activities that can be grouped together in order to form swimlanes. For each swimlanes identified, give an appropriate name Here, the activities and actions can be grouped together based on who performed those activities/actions Prospective customers fill out application The application is sent to an insurance agent Agent receives the application Agent sends it to various insurance companies to get quotes for insurance. Insurance companies come up with/develop quotes Agent receives the quotes, The agent decides best policy The agent sends the customer a copy of the insurance policy proposal and quote. Customer receives proposal and quotes 30 Group of activities and actions according to swimlanes: Prospective customer Insurance agent Prospective customers fill out application The application is sent to an insurance agent Customer receives proposal and quotes Agent receives the application Agent sends it to various insurance companies to get quotes for insurance. Agent receives the quotes The agent decides best policy The agent sends the customer a copy of the insurance policy proposal and quote. Insurance company Insurance companies come up with/develop quotes 31 3) Render the transitions that connect these activity and action states (sequential flows) Prospective customer 1. Fill out application 2. Send in application 9. Receives proposal and quotes Insurance agent 3. Receives application 4. Sends to insurance companies. 6. Receives quotes 7. Decides best policy 8. Sends policy to prospective customer. Insurance company 5. Develop quotes 32 33 Case Study: On-Line Bookstore On-line Bookstore is a web application that can be accessed by the store’s registered customer, whereby each customer can order books, review one or more books sold in the book store, and sell used books to other customers. Before performing any one of these transactions, the customer must first log-in into the system using their user id and password kept in their account. Problem: Draw the activity diagrams for the above system 35 On-line Bookstore System Register <<extend>> (CustID) Customer Check out Order books <<include>> Sell used books <<include>> Log-in <<include>> Use Case Context Diagram Review books 36 Register 37 Log-in 38 Order 39 Check out 40 Sell used books 41 Process, threads & active objects Introduction In the real-world, events may happen and things may take place all at the same time (concurrently). Example: In a high rise building, there exists quite a number of tenants, each following their own agenda. But all must pass through a limited set of entrances. All must share the same bank of elevators. All must share the same electrical, water and parking facilities. If they are to work together optimally, they have to communicate and synchronize their interactions properly. 43 Therefore, when we model a system of the real-world, we must take into account its process view. The process view encompasses the threads and processes that form the system’s concurrency and synchronization mechanism. 44 Flow of control There are two types of flow: - sequential and concurrent flow. In a purely sequential system, there is only one flow of control. meaning that one thing, and one thing only, can take place at a time Example: - When a sequential program starts, control is rooted at the beginning of the program and operations are dispatched one after another. No two operation can occur at the same time. 45 In a concurrent system, there is more than one flow of control meaning that more than one thing can take place at a time Here, there are multiple simultaneous flow of control, each rooted at the head of an independent process or thread Because more than one flow of control can take place at a single time, one flow may interfere with another flow of control. This may cause concurrent systems to fail. Therefore, there must exists a form of synchronization to avoid this kind of situation. In the UML, each independent flow of control is modelled as an active object. 46 Active objects, processes and threads An active object is a process or thread that can initiate control activity. A process is a heavyweight flow that can execute concurrently with other processes meaning that it is a thing known to the operating system and runs in an independent address space A thread is a lightweight flow that can execute concurrently with other threads within the same process meaning that it generally runs inside a process 47 An active object is an instance of an active class. An active class, therefore, is a class whose instances are active objects. An active class represents an independent flow of control, whereas a plain class embodies no such flow. An active class shares the same properties as all other classes Active classes can be used to model common families of processes and threads. Active objects can communicate with one another by passing messages here, message passing must be extended to include certain concurrency semantics in order to synchronize the interactions among independent flows 48 Both active objects and active classes is represented in the UML using the class box, but with a thicker border. Active class Active object 49 In a system with both active and passive objects, there are four possible combinations of interaction that must be considered: A message may be passed from one passive object to another A message may be passed (synchronously or asynchronously) from one active object to another A message may be passed from an active object to a passive object A message may be passed from a passive object to an active object 50 By modelling concurrent systems with active objects, each independent flow of control can be given a name. Furthermore, when an active object is created, the associated flow of control is started; when the active object is destroyed, the associated flow of control is terminated We can model the collaboration of active and passive objects by using interaction diagrams. When we model interactions that involve multiple flows of control, it is important to identify the process or thread that sent a particular message. In the UML, we can distinguish one flow of control from another by prefixing a message’s sequence number with the name of the process or thread that sits at the root of the sequence. 51 Example: c : CNNNewsFeed c1 : postBreaingStory( ) s1 : postValue( ) s : StockTicker a1 : Analyst s2 : postAlert( ) m1 : postValue( ) m : AlertManager t : TradingManager i2 : postAlert( ) i : IndexWatcher a2 : Analyst i1 : postValue( ) 52