statechart diagrams. creating a statechart diagram a statechart diagram shows the life cycle of a...

37
Statechart Diagrams

Upload: elfrieda-lee

Post on 19-Jan-2016

222 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Statechart Diagrams

Page 2: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Creating a Statechart Diagram

• A Statechart diagram shows the life cycle of a single object, from the time that it is created until it is destroyed. These diagrams are a good way to model the dynamic behavior of a class.– 1. Right−click the desired class in the browser.– 2. Select New / Statechart Diagram from the pop−up

menu.

Page 3: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Adding States

• A state is one of the possible conditions in which an object may exist.– 1. Select State from the toolbox toolbar.– 2. Click on the Statechart diagram where the state should

appear.

OR– Select Tools/ Create / State.– Click on the Statechart diagram where the state

should appear.

Page 4: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

To add documentation to a state:

1. Double−click the desired state to open the state specification window.

2. Select the General tab.

3. Enter documentation in the Documentation field.

Page 5: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Adding State Details

• There are five types of information you can include for a state: an activity, an entry action, an exit action, an event, or a state history.

Page 6: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Activity

Page 7: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Entry Action

• An entry action is a behavior that occurs while the object is transitioning into the state.

Page 8: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Exit Action

• An exit action is similar to an entry action. However, an exit action occurs as part of the transition out of a state.

Page 9: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Activity

• The behavior in an activity, entry action, or exit action can include sending an event to some other object.

• For example, if the flight is delayed for more than four hours, the flight object may need to send an event to a flight scheduler object, which will automatically reschedule the flight for another day.

• In this case, the activity, entry action, or exit action is preceded by a caret (^). The diagram would then read:

Page 10: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Activity

Page 11: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

To add an activity:

1. Open the specification window for the desired state.

2. Select the Action tab.

3. Right−click the Actions box.

4. Select Insert from the pop−up menu.

5. Double−click the new action.

6. Enter the action in the Actions field.

7. In the When box, select Do to make the new action an activity.

Page 12: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

To add an entry action:

1. Open the specification window for the desired state.

2. Select the Action tab.3. Right−click the Actions box.4. Select Insert from the pop−up menu.5. Double−click the new action.6. Enter the action in the Actions field.7. In the When box, select On Entry.

Page 13: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

To add an exit action:

1. Open the specification window for the desired state.

2. Select the Action tab.3. Right−click on the Actions box.4. Select Insert from the pop−up menu.5. Double−click the new action.6. Enter the action in the Actions field.7. In the When box, select On Exit.

Page 14: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

To add an action that occurs on a specific event:

1. Open the specification window for the desired state.2. Select the Action tab.3. Right−click the Actions box.4. Select Insert from the pop−up menu.5. Double−click the new action.6. Enter the action in the Actions field.7. In the When box, select On Event.8. Enter the event that triggers the action, along with any

arguments of the event and any guard conditions that control whether or not the action should occur. If the guard condition is true, the action will occur on the event. If not, the action will not occur, even if the event happens.

Page 15: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

To send an event:

1. Open the specification window for the desired state.

2. Select the Detail tab.

3. Right−click the Actions box.

4. Select Insert from the pop−up menu.

5. Double−click the new action.

6. Select Send Event as the type.

7. Enter the event, arguments, and target in their respective fields

Page 16: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Adding Transitions

1. Select Transition from the toolbox toolbar.

2. Click on the state where the transition begins.

3. Drag the transition line to the state where the transition ends.

Page 17: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

To add a reflexive transition

1. Select Transition to Self from the toolbox toolbar.

2. Click on the state where the reflexive transition occurs.

Page 18: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

To add documentation to a transition:1. Double−click the desired transition to open the

specification window.

2. Select the General tab.

3. Enter documentation in the Documentation field.

Page 19: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Adding Transition Details

• There are various specifications you can include for each transition.

• These include events, arguments, guard conditions, actions, and send events.

Page 20: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Event

• An event is something that causes a transition from one state to another to occur.

• On the diagram, an event can be drawn using an operation name or simply using an English phrase

• Events can have arguments. For example, when removing a passenger, we will need the name of the passenger to be removed.

Page 21: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Guard Condition

• A guard condition controls when a transition can or cannot occur.

• In the airline example, adding a passenger will move the flight from the Open to the Full state, but only if the last seat was sold.

• The guard condition in this example is "Last seat was sold.“

• A guard condition is drawn along the transition line, after the event name, and enclosed in square brackets.

Page 22: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Action• An action, as we mentioned above, is a noninterruptible behavior

that occurs as part of a transition. • Entry and exit actions are shown inside states, because they define

what happens every time an object enters or leaves a state.• For example, when transitioning from the Scheduled state to the

Open state, we may want to initialize the number of passengers at 0.

• An action is shown along the transition line, after the event name, and preceded by a slash.

Page 23: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Adding Transition Details

• An event or action may be a behavior that occurs inside the object or a message that is sent to another object.

• If an event or action is sent to another object, it is preceded by a caret (^) on the diagram.

Page 24: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

To add an event:

• 1.Double−click the desired transition to open the specification window.

• 2.Select the General tab.

• 3. Enter the event in the Event field.

Page 25: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

To add arguments to an event:

1. Double−click the desired transition to open the specification window.

2. Select the General tab.

3. Enter the arguments in the Arguments field.

Page 26: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

To add a guard condition:

• 1.Double−click the desired transition to open the specification window.

• 2. Select the Detail tab.

• 3. Enter the guard condition in the Condition field.

Page 27: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

To add an action:

• 1.Double−click the desired transition to open the specification window.

• 2. Select the Detail tab.

• 3. Enter the action in the Action field.

Page 28: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

To send an event:

• 1.Double−click the desired transition to open the specification window.

• 2. Select the Detail tab.• 3.Enter the event in the Send Event field.• 4. Enter any arguments in the Send Arguments field.• 5. Enter the target in the Send Target field.

Page 29: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Create a Statechart Diagram

• Create the Statechart diagram shown in

Page 30: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Exercise Steps:

• Create the Diagram– 1.Locate the Product class in the browser.– 2. Right−click the class and select New

Statechart Diagram.

Page 31: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Exercise Steps

• Add the Start and Stop States• 1.Select Start State from the toolbox.• 2.Place the state on the diagram.• 3.Select End State from the toolbox.• 4.Place the state on the diagram.

Page 32: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Add the States

• 1. Select State from the toolbox.

• 2. Place the state on the diagram.

• 3. Name the state Ordered.

• 4. Select State from the toolbox.

• 5. Place the state on the diagram.

• 6.Name the state Inventoried.

Page 33: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Add the States

• 7.Select State from the toolbox.

• 8.Name the state Out of Stock.

• 9.Select State from the toolbox.

• 10.Name the state Selected for Purchase.

• 11. Select State from the toolbox.

• 12.Place the state on the diagram.

• 13.Name the state Purchased.

Page 34: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Add Transitions

• 1. Select Transition from the toolbox.

• 2. Click the Start State.

• 3. Drag the transition line to the Ordered state.

Page 35: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

• 4. Repeat steps 1–3 to add the following transitions:– Ordered to Out of Stock– Ordered to Inventoried– Inventoried to Selected for Purchase– Selected for Purchase to Inventoried– Selected for Purchase to Purchased– Purchased to End State– Out of Stock to End State– Inventoried to End State

Page 36: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Add Transition Details

• 1.Double−click the Ordered to Out of Stock transition to open the specification.

• 2.Select the Detail tab.

• 3.In the Guard Condition field, enter backordered.

• 4. Click OK to close the specification.

Page 37: Statechart Diagrams. Creating a Statechart Diagram A Statechart diagram shows the life cycle of a single object, from the time that it is created until

Add Transition Details

• 5. Repeat steps 1–4 to add the guard conditions to the following transitions: