Download presentation
Presentation is loading. Please wait.
Published byDorothy Bishop Modified over 9 years ago
1
Software Engineering Design & Modeling Statechart Diagram
2
2 Introduction Outline: - Outline: - Events Events State Machines State Machines States States Transitions Transitions Statechart Diagrams Statechart Diagrams Advanced States and Transitions Advanced States and Transitions
3
Events
4
4 Introduction All real systems have some dynamic dimension to them, and these dynamics are triggered by things that happen externally or internally. All real systems have some dynamic dimension to them, and these dynamics are triggered by things that happen externally or internally. Ex: - At an ATM machine, action is initiated by a user pressing a button to start a transaction. Ex: - At an ATM machine, action is initiated by a user pressing a button to start a transaction. “ ” are called events, and each event has a location in time and space. “ ” are called events, and each event has a location in time and space.
5
5 Events An event is an occurrence of interest to an object. An event is an occurrence of interest to an object. Events may be synchronous or asynchronous. Events may be synchronous or asynchronous. Events may be external or internal: - Events may be external or internal: - External events are those events that pass between the system and its actors External events are those events that pass between the system and its actors Ex: - A customer clicks a Login button on the HomePage of an Online Bookstore System to initiate the login process. Ex: - A customer clicks a Login button on the HomePage of an Online Bookstore System to initiate the login process. Internal events are those events that pass among objects that live inside a system Internal events are those events that pass among objects that live inside a system Ex: - The LoginPage validates the login information against the persistent Account data to determine whether the Customer is a registered customer. Ex: - The LoginPage validates the login information against the persistent Account data to determine whether the Customer is a registered customer.
6
6 The UML supports the representation of four kinds of events: - The UML supports the representation of four kinds of events: - A eventsynchronous event A eventsynchronous event A event A event A eventasynchronous events A eventasynchronous events A signal A signal
7
7 A ______ is a synchronous communication that involves an object invoking an operation on another object or an object invoking one of its own operation on itself. A ______ is a synchronous communication that involves an object invoking an operation on another object or an object invoking one of its own operation on itself. It is equivalent to a call action. It is equivalent to a call action. ManualAutomatic startAutoPilot(normal) call event
8
8 A time event is an event that occurs after a specified period of time. A time event is an event that occurs after a specified period of time. It can be expressed by using the word “________” followed by a ________ expression. It can be expressed by using the word “________” followed by a ________ expression. A change event is an event that occurs when some condition is satisfied. A change event is an event that occurs when some condition is satisfied. It can represents. It can represents. It can be expressed by using the keyword “______” followed by a __________ expression. It can be expressed by using the keyword “______” followed by a __________ expression. change event Idle Active after (2 seconds) / dropConnection( ) when (11:49 PM) / selfTest( ) time event
9
9 A signal represents a named object that is “thrown” asynchronously by one object and then “caught” by another object. A signal represents a named object that is “thrown” asynchronously by one object and then “caught” by another object. Exceptions are the most common kind of signal Exceptions are the most common kind of signal Signal may have attributes and operations, may have instances, and may be involved in generalization relationships. Signal may have attributes and operations, may have instances, and may be involved in generalization relationships. A signal may be sent A signal may be sent as an action of a state transition in a state machine (will look into detail later on) or as an action of a state transition in a state machine (will look into detail later on) or as a message in an interaction (Chapter 6) as a message in an interaction (Chapter 6)
10
10 The execution of an operation can also send signal. The execution of an operation can also send signal. In the UML, the relationship between an operation and the signal that it can send can be modeled using a dependency relationship with the stereotype >. In the UML, the relationship between an operation and the signal that it can send can be modeled using a dependency relationship with the stereotype >. moveTo( ) moveTo( ) position position velocity velocity MovementAgent force : Float force : Float > >Collision > send dependency signal parameter
11
11 How an object responds to a particular event depends (in part), on the state that an object is in when it receives the event. How an object responds to a particular event depends (in part), on the state that an object is in when it receives the event.
12
State Machines
13
13 Introduction In the UML, the dynamic aspect of a system can also be modeled using state machine. In the UML, the dynamic aspect of a system can also be modeled using state machine. A state machine is used to model the lifetime of a single object. A state machine is used to model the lifetime of a single object. In the lifetime of an object, it may be exposed to a variety of events, such as In the lifetime of an object, it may be exposed to a variety of events, such as a signal a signal an invocation of an operation an invocation of an operation the creation or destruction of the object the creation or destruction of the object the passing of time the passing of time a change in some condition a change in some condition
14
14 In response to this events, the object reacts with some action that results in In response to this events, the object reacts with some action that results in a change in the state of the object, or a change in the state of the object, or the return of a value the return of a value Therefore, the behavior of such an object is affected by the past. An object may receive an event, respond with an action, then change its state. An object may receive an event, respond with an action, then change its state. An object may receive another event, and its respond may be different, depending on its current state in response to the previous event. An object may receive another event, and its respond may be different, depending on its current state in response to the previous event. The UML provides graphical representation for all of these situations: states, transitions, events and actions. The UML provides graphical representation for all of these situations: states, transitions, events and actions.
15
15 State machine A state machine is a behavior that specifies: - A state machine is a behavior that specifies: - the sequences of states an object goes through during its lifetime in response to events the sequences of states an object goes through during its lifetime in response to events its responses to those events its responses to those events There are two special states that may be defined for an object’s state machine: - There are two special states that may be defined for an object’s state machine: - Initial state, Initial state, indicates the default starting place for the state machine or sub-state indicates the default starting place for the state machine or sub-state Final state, Final state, indicates that the final execution of the state machine or the enclosing state has been completed indicates that the final execution of the state machine or the enclosing state has been completed If no final state, it means that the object has perpetual life. If no final state, it means that the object has perpetual life.
16
16 States A state is a condition in which an object can be at some point during its lifetime, for some finite amount of time. A state is a condition in which an object can be at some point during its lifetime, for some finite amount of time. An object can do any or all of the following while it’s in a particular state: - An object can do any or all of the following while it’s in a particular state: - perform an activity perform an activity wait for an event wait for an event satisfy one or more condition satisfy one or more condition The UML notation for a state is a rectangle The UML notation for a state is a rectangle with a rounded corner. State
17
17 A state has several parts: - A state has several parts: - Name Name Entry/exit actions (actions executed on entering and exiting the state, respectively) Entry/exit actions (actions executed on entering and exiting the state, respectively) Internal transitions (transitions that are handled without causing a change in state) Internal transitions (transitions that are handled without causing a change in state) Sub-states (the nested structure of a state, involving sequential or concurrent sub-states). Sub-states (the nested structure of a state, involving sequential or concurrent sub-states). Deferred events (events that are not handled by the state, rather postponed and queued to be handled by the object in another state) Deferred events (events that are not handled by the state, rather postponed and queued to be handled by the object in another state)
18
18 Example: - Example: - Sample states that an Order object can assume within the Online BookStore. Sample states that an Order object can assume within the Online BookStore. When an Order is in the Posting stage, it performs the activity that involves other objects, such as Accounting. When an Order is in the Posting stage, it performs the activity that involves other objects, such as Accounting. The shipped state is an example of a state in which an object has satisfied some condition. The shipped state is an example of a state in which an object has satisfied some condition. The retrieving books stage is an example of a state is which an object performs an activity that does not involve other objects. The retrieving books stage is an example of a state is which an object performs an activity that does not involve other objects.
19
19 Transitions A transition is a change of an object from one state (the source state) to another (the target state) when specified events occurred and specified conditions are satisfied. A transition is a change of an object from one state (the source state) to another (the target state) when specified events occurred and specified conditions are satisfied. On such a change in state, the transition is said to “fire”. On such a change in state, the transition is said to “fire”. A transition “fires” when an event of interest to the given object occurs. A transition “fires” when an event of interest to the given object occurs. The event “triggers” the transition. (also called trigger transition) The event “triggers” the transition. (also called trigger transition) Alternatively, a transition may fire unconditionally when the object is ready to move from one state to another, that is, when its source state has completed its activity. Alternatively, a transition may fire unconditionally when the object is ready to move from one state to another, that is, when its source state has completed its activity. This is called trigger-less transition This is called trigger-less transition
20
20 In the UML, a transition is represented as a solid directed line. In the UML, a transition is represented as a solid directed line. Source State Target State Source State Target State event Triggered transition Trigger-less transition
21
21 A transition has five parts: - A transition has five parts: - Source state (the state affected by the transition) Source state (the state affected by the transition) Event trigger (the event that triggers the transition, providing its guard condition is satisfied) Event trigger (the event that triggers the transition, providing its guard condition is satisfied) Guard condition (a Boolean expression that must be evaluated to TRUE in order for the transition to “fire”) Guard condition (a Boolean expression that must be evaluated to TRUE in order for the transition to “fire”) Action Action Target state (the state that is active after the completion of the transition) Target state (the state that is active after the completion of the transition)
22
22 There may be action associated with a triggered transition. There may be action associated with a triggered transition. Actions may include operation calls, the creation or destruction of another object, or the sending of a signal to another object. Actions may include operation calls, the creation or destruction of another object, or the sending of a signal to another object. Unless there’s a guard condition, this action executes unconditionally before the object enters the target state. Unless there’s a guard condition, this action executes unconditionally before the object enters the target state. An object doesn’t have to go from one state to a different state within a transition. An object doesn’t have to go from one state to a different state within a transition. A self-transition is a transition whose source state and target state are the same. A self-transition is a transition whose source state and target state are the same. Source State Target State event / action State Transition with actionA self-transition
23
23 Example: - Sample transitions that an Order object might make in the context of the Internet Bookstore Sample transitions that an Order object might make in the context of the Internet Bookstore The transition from Packaging to Shipping is trigger-less. The transition from Packaging to Shipping is trigger-less. The transition from Shipping to Shipped occurs when the shipConfirm event occurs; the object record the change in state by performing a call action, setFulfilledFlag( ) The transition from Shipping to Shipped occurs when the shipConfirm event occurs; the object record the change in state by performing a call action, setFulfilledFlag( ) The Retrieving Books state has self-transition. The Retrieving Books state has self-transition.
24
24 Guard conditions A guard condition is a Boolean expression that must evaluate to True before a given transition can fire. A guard condition is a Boolean expression that must evaluate to True before a given transition can fire. A guard condition is evaluated only after the trigger event for its transition occurs. A guard condition is evaluated only after the trigger event for its transition occurs. A guard condition is evaluated only once for each transition at the time the trigger event occurs. A guard condition is evaluated only once for each transition at the time the trigger event occurs. But it may be evaluated again if the transition is re- triggered. But it may be evaluated again if the transition is re- triggered.
25
25 Guard condition is shown in square brackets near the transition arrow. Guard condition is shown in square brackets near the transition arrow. If a guard condition is associated with an event, you use the form If a guard condition is associated with an event, you use the form eventName [guard condition] If the Boolean expression evaluates to False, the object ignores the event and does not change states. If a you have an event, a guard condition and an action, the form is If a you have an event, a guard condition and an action, the form is eventName [guard condition] / action If the Boolean expression evaluates to False, the object ignores the event, doesn’t execute the action and does not change states. You can also have a guard condition by itself on a transition, which takes the form You can also have a guard condition by itself on a transition, which takes the form [guard condition]
26
26 Example: - Example: - Sample of guard conditions that might be applied to transition associated with an Order object. Sample of guard conditions that might be applied to transition associated with an Order object. The transition between Packaging and Shipping occurs when the packageIsReady event comes in and it’s been established that the associated Order has been posted to the General Ledger(GL). The transition between Packaging and Shipping occurs when the packageIsReady event comes in and it’s been established that the associated Order has been posted to the General Ledger(GL). The Order object stays in the Retrieving Books state until the Order realizes that it has accumulated all of the books that the customer requested. The Order object stays in the Retrieving Books state until the Order realizes that it has accumulated all of the books that the customer requested.
27
Statechart Diagram
28
28 Introduction State machine is a combination of the following: - State machine is a combination of the following: - The state that an object can assume during its lifetime The state that an object can assume during its lifetime The events to which that object can respond The events to which that object can respond The possible responses the object can make to those events (the actions) The possible responses the object can make to those events (the actions) The transitions that occur between the object’s states. The transitions that occur between the object’s states. In UML, a statechart diagram (also known as state diagram) shows an object’s state machine. In UML, a statechart diagram (also known as state diagram) shows an object’s state machine. It shows the flow of control from state to state. It shows the flow of control from state to state. It is used to model the dynamic aspects of a system. It is used to model the dynamic aspects of a system.
29
29 Properties Statechart diagrams commonly contain: - Statechart diagrams commonly contain: - Simple states and composite states Simple states and composite states Transitions, including events, guard conditions and actions Transitions, including events, guard conditions and actions May also include notes and constraints May also include notes and constraints
30
30 Common uses Statechart diagram is used Statechart diagram is used to model the dynamic aspects of a system to model the dynamic aspects of a system may involve the event-ordered behavior of any kind of object in any view of a system’s architecture, including classes, interfaces, components and nodes. may involve the event-ordered behavior of any kind of object in any view of a system’s architecture, including classes, interfaces, components and nodes. in the context of the system as a whole, a subsystem, or a class in the context of the system as a whole, a subsystem, or a class may also attach it to use cases (to model a scenario) may also attach it to use cases (to model a scenario)
31
31 In the context of modeling the dynamic aspects of a system, statechart diagram is used to model reactive objects. In the context of modeling the dynamic aspects of a system, statechart diagram is used to model reactive objects. A reactive (or event driven) object is one whose behavior is best characterized by its response to events dispatched from outside its context. A reactive (or event driven) object is one whose behavior is best characterized by its response to events dispatched from outside its context. A reactive object is typically idle until it receives an event. A reactive object is typically idle until it receives an event. When it receives an event, its response is usually dependent on previous events. When it receives an event, its response is usually dependent on previous events. After the object responds to an event, it becomes idle again, waiting for the next event. After the object responds to an event, it becomes idle again, waiting for the next event. For these kinds of objects, you will have to focus on: - For these kinds of objects, you will have to focus on: - the idle/stable states of that object the idle/stable states of that object the events that trigger a transition from state to state the events that trigger a transition from state to state the actions that occur on each state change the actions that occur on each state change
32
32 Modeling a statechart diagram To model a reactive object: - 1. Choose the context for the state machine, whether it is a class, a use case or the system as a whole. 2. Choose the initial and final states of the object. 3. Decide on the stable/idle states of the object by considering the conditions in which the object may exist for some identifiable period of time 4. Decide on the events that may trigger a transition from state to state and/or the guard conditions that must evaluate to True for the transition to occur. 5. Attach actions to these states (if any)
33
33 A simple Example Draw a statechart diagram for the class Balloon, given the narrative below. Draw a statechart diagram for the class Balloon, given the narrative below. Imagine two objects of the class Balloon: one red and one blue balloon. To begin with, both balloons are deflated. Someone blows up both balloons so that they are fully inflated. But later on, the blue one has had some of the air let out of it (partly inflated). If someone tries to blow up both balloons again, they will respond in different ways: the blue one will become fully inflated, whereas the red one will burst as it is already fully inflated.
34
34 Steps: - 1. Choose the context for the state machine. “Imagine two objects of the class Balloon: one red and one blue balloon. To begin with…” The context for the state machine : a class 2. Choose the initial and final states of the object. “To begin with, both balloons were deflated… …whereas the red one will burst as it is already fully inflated.” The initial state: Deflated The final state: Burst
35
35 3. Decide on the stable/idle states of the object “Imagine two objects of the class Balloon: one red and one blue balloon. To begin with, both balloons were deflated. Someone blows up both balloons so that they are fully inflated. But later on, the blue one has had some of the air let out of it (partly inflated). If someone tries to blow up both balloons again, they will respond in different ways: the blue one will become fully inflated, whereas the red one will burst as it is already fully inflated.” Idle states of the object: - Deflated (initial state) Deflated (initial state) Fully inflated Fully inflated Partly inflated Partly inflated Burst (final state) Burst (final state)
36
36 4. Decide on the events that may trigger a transition from state to state. “Imagine two objects of the class Balloon: one red and one blue balloon. To begin with, both balloons were deflated. Someone blows up both balloons so that they are fully inflated. But later on, the blue one has had some of the air let out of it (partly inflated). If someone tries to blow up both balloons again, they will respond in different ways: the blue one will become fully inflated, whereas the red one will burst as it is already fully inflated.” Events that may trigger a transition from state to state: - **Deflated Fully inflated : blow up **Deflated Fully inflated : blow up Fully inflated Partly inflated : let air out “tersurat” Fully inflated Partly inflated : let air out “tersurat” Fully inflated Burst : blow up Fully inflated Burst : blow up **Deflated Partly inflated Fully inflated : blow up **Deflated Partly inflated Fully inflated : blow up Partly deflated deflated : let air out Partly deflated deflated : let air out
37
37 DeflatedPartly inflated Fully inflated blow up let air out blow up let air out Burst blow up The statechart diagram for the Balloon class.
38
Advanced States and Transitions
39
39 States On a statechart diagram, the symbol for a state may contain any of several other pieces of information. On a statechart diagram, the symbol for a state may contain any of several other pieces of information. An entry action is an action that the object always perform immediately upon entering the given state. An entry action is an action that the object always perform immediately upon entering the given state. This appear as entry / actionName within the state symbol. This appear as entry / actionName within the state symbol. If a self-transition occurs, the object performs the entry action. If a self-transition occurs, the object performs the entry action.
40
40 An exit action is an action that the object always performs immediately before leaving the given state, in response to a regular transition or a self-transition. An exit action is an action that the object always performs immediately before leaving the given state, in response to a regular transition or a self-transition. This appear in the form exit / actionName. This appear in the form exit / actionName. In many cases, an object does not do anything except wait while it is in a particular state. However, an object can perform an activity while it is in the state. In many cases, an object does not do anything except wait while it is in a particular state. However, an object can perform an activity while it is in the state. This can be shown using the notation do / activityName. This can be shown using the notation do / activityName. Since an activity is interruptible, an object is likely to stop performing the activity when an event comes in. Since an activity is interruptible, an object is likely to stop performing the activity when an event comes in.
41
41 An object may handle reception of an event by performing some action while remaining in the existing state. An object may handle reception of an event by performing some action while remaining in the existing state. This is called an internal transition and is shown as evenName / actionName. This is called an internal transition and is shown as evenName / actionName. If an object makes an internal transition, it does not execute the exit action or the entry action. If an object makes an internal transition, it does not execute the exit action or the entry action. A deferred event is an event that is of interest to the object, but which the object defers handling until the object reaches a different state. A deferred event is an event that is of interest to the object, but which the object defers handling until the object reaches a different state. This is shown as eventName / defer This is shown as eventName / defer Deferred events get put in a queue Deferred events get put in a queue When the object changes state, the first thing that the object does is check to see whether there are events in the queue to which it can now respond. When the object changes state, the first thing that the object does is check to see whether there are events in the queue to which it can now respond.
42
42 Composite States The states that have been discussed before are what UML calls simple states. The states that have been discussed before are what UML calls simple states. Composite states, on the other hand, are states that can have multiple nested states, or sub- states. Composite states, on the other hand, are states that can have multiple nested states, or sub- states.
43
43 Some properties of sub-states: - Some properties of sub-states: - A sub-state within a composite state can have any or all of the details that can be associated with a simple state, such as entry actions, activities and deferred events A sub-state within a composite state can have any or all of the details that can be associated with a simple state, such as entry actions, activities and deferred events A source state, which resides outside a given composite state, can have an associated transition whose target state is either A source state, which resides outside a given composite state, can have an associated transition whose target state is either the composite state as a whole, or the composite state as a whole, or any one of the sub-states that compose the state. any one of the sub-states that compose the state. A transition can come out of a sub-state and then leave the enclosing composite state without having to go through the other sub-states. A transition can come out of a sub-state and then leave the enclosing composite state without having to go through the other sub-states. It is assumed that when an object makes a transition to a composite state, the flow starts over again with the initial state within that state. It is assumed that when an object makes a transition to a composite state, the flow starts over again with the initial state within that state.
44
44 A composite state may either contain either: - A composite state may either contain either: - sequential sub-states sequential sub-states If an object can be in composite state and only one of that state’s sub-states at the same time, then the sub- states are referred to as sequential sub-states. If an object can be in composite state and only one of that state’s sub-states at the same time, then the sub- states are referred to as sequential sub-states. Transitions can exist between these sub-states. Transitions can exist between these sub-states. concurrent sub-states concurrent sub-states If an object can be in composite state and also be in more than one of that state’s sub-states at the same time, then the sub-states are referred to as concurrent sub- states. If an object can be in composite state and also be in more than one of that state’s sub-states at the same time, then the sub-states are referred to as concurrent sub- states. In this situation, two or more sets of sub-states can represent parallel flows of control. In this situation, two or more sets of sub-states can represent parallel flows of control.
45
45 Accumulating from Inventory Accumulating from Back Order Waiting for Back Order Packaging [back order] booksArrive Retrieving Books exit / postToGL [all books found] Example of some of the sequential sub-states that make up the Retrieving Books state of the Order object in the Online Bookstore. Example of some of the sequential sub-states that make up the Retrieving Books state of the Order object in the Online Bookstore. When an Order object enters the Retrieving Books state, it is also entering the Accumulating from Inventory sub-state. When an Order object enters the Retrieving Books state, it is also entering the Accumulating from Inventory sub-state. If the system is able to retrieve all of the books for the Order from the inventory, the object leaves the Accumulating from Inventory sub-state and the Retrieve Books state and enters the Packaging state. If the system is able to retrieve all of the books for the Order from the inventory, the object leaves the Accumulating from Inventory sub-state and the Retrieve Books state and enters the Packaging state. If there are any books on back order, the Order enters the Waiting for Back Order sub- state, where it remains until the back-ordered books arrive If there are any books on back order, the Order enters the Waiting for Back Order sub- state, where it remains until the back-ordered books arrive When the back-ordered books arrive, the object will then enter the Accumulating from Back Order state, and once it has accumulated all the ordered books, will leave the sub- state and the enclosing state and enters the Packaging state. When the back-ordered books arrive, the object will then enter the Accumulating from Back Order state, and once it has accumulated all the ordered books, will leave the sub- state and the enclosing state and enters the Packaging state. The last thing that happens is execution of the postToGL exit action. The last thing that happens is execution of the postToGL exit action.
46
46 Example of some of the concurrent sub-states that belong to the Shipping state of the Order object in the Online Bookstore. Example of some of the concurrent sub-states that belong to the Shipping state of the Order object in the Online Bookstore. When an Order object enters the Shipping state, it also enters the Filling Out Shipping Slip sub-state and the Posting Final Amount to GL sub-state at the same time. When an Order object enters the Shipping state, it also enters the Filling Out Shipping Slip sub-state and the Posting Final Amount to GL sub-state at the same time. Work proceeds along both paths in parallel. Work proceeds along both paths in parallel. Once the Order reaches the final states along both paths, the object leaves the Shipping state. Once the Order reaches the final states along both paths, the object leaves the Shipping state. Filling Out Shipping Slip Contacting Shipper Posting Final Amount to GL Billing Customer Credit Card Financial Physical Shipping
47
47 History states From before, it is assumed that when an object makes a transition to a composite state, the flow starts with the initial state within that state. From before, it is assumed that when an object makes a transition to a composite state, the flow starts with the initial state within that state. However, a history state can be used to “remember” the last sub-state that the object was in before it left the composite state. However, a history state can be used to “remember” the last sub-state that the object was in before it left the composite state. This means that an event can interrupt an activity, but then the object can pick up where it left off once it has handled that event. This means that an event can interrupt an activity, but then the object can pick up where it left off once it has handled that event. A history state is represented as a small circle with an H inside. A history state is represented as a small circle with an H inside.
48
48 Example of how a history state can be used with the Retrieving Books composite state of the Online Bookstore. Example of how a history state can be used with the Retrieving Books composite state of the Online Bookstore. When a query event comes in while an Order object is in the Retrieving Books state, the system puts the current activity on hold and puts the object in the Checking Status state. When a query event comes in while an Order object is in the Retrieving Books state, the system puts the current activity on hold and puts the object in the Checking Status state. When the activity associated with that state are finished, the system puts the Order back into the Retrieving Books state and the sub-state in which the Order resided when the activity was interrupted, and the Order resumes performing the interrupted activity. When the activity associated with that state are finished, the system puts the Order back into the Retrieving Books state and the sub-state in which the Order resided when the activity was interrupted, and the Order resumes performing the interrupted activity. Accumulating from Inventory Accumulating from Back Order Waiting for Back Order [back order] booksArrive Retrieving Books exit / postToGL Checking Status H query
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.