Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modeling Dynamic Behavior: State and Activity Diagrams.

Similar presentations


Presentation on theme: "Modeling Dynamic Behavior: State and Activity Diagrams."— Presentation transcript:

1 Modeling Dynamic Behavior: State and Activity Diagrams

2 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour2 State Diagrams A state diagram describes the behaviour of a system, some part of a system, or an individual object. At any given point in time, the system or object is in a certain state. —So it can respond in a specific way to an event. Some events will cause the system to change state. —In the new state, the system will behave in a different way to events. A state diagram is a directed graph whose nodes are states and arcs are transitions between states.

3 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour3 State diagrams – an example tic-tac-toe game OTurn XTurn Tie OWin XWin

4 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour4 States At any given point in time, the system is in one state. A state is represented by a rounded rectangle containing the name of the state. Special states: —A black circle represents the start state —A circle with a ring around it represents an end state A transition is a change of state in response to an event. The label on each transition is the event that triggers it. The system remains in a state until an event occurs that causes it to change state.

5 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour5 after(30s) after(5s) after(25s) RedLight GreenLight YellowLight after(25s since exit from state RedLight) RedLight GreenLightChangeTriggered YellowLight after(5s) after(30s) vehicleWaitingToTurn GreenLightNoTrigger State diagrams and transitions

6 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour6 State diagrams – an example with conditional transitions (states of a CourseOffering) addStudent(aStudent) Closed enrollment >= capacity cancel openRegistration Offered OpenEnoughStudents OpenNotEnoughStudents enrollment >= 5 addStudent(aStudent) closeRegistration cancel Cancelled

7 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour7 Activities in state diagrams An activity is an event that occurs in a particular state. —It takes a period of time. —The system may transition out of the state when the activity completes. —Another outgoing transition may interrupt the activity before it completes. E.g., press button do: play chosen selection MusicPlaying ProposeSelection

8 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour8 Actions in state diagrams An action takes place instantaneously - either during a transition, on entry to a state, or on exit from a state. An action should consume no noticeable amount of time Enter / start motor forward Enter / start motor backward Enter / stop motor Enter / stop motor openingCompleted closingCompleted pressButton Closing Open OpeningClosed

9 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour9 Nested states and guards An entire state diagram can be nested inside a state (guards are conditions for entry). FirstSecondThird ReverseNeutral selectDrive reachThirdSpeed [driveSelected] dropBelowThirdSpeed reachSecondSpeed [driveSelected] dropBelowSecondSpeed [driveSelected] selectFirstselectSecondselectNeutral selectReverse selectFirstselectSecond

10 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour10 State diagram – an example with substates addStudent(aStudent) openRegistration Offered EnoughStudents NotEnoughStudents enrollment >= 5 Open Closed enrollment >= capacity cancel closeRegistration Cancelled do: unregister students

11 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour11 Activity Diagrams An activity diagram is an extended state diagram where: —The focus is on sequences of actions (rather than messages between objects). —The flow of events is clearly represented. —concurrent activities can be clearly represented. —most transitions are triggered by internal events, such as the completion of a computation. —interactions among different use cases can be visualized. —several classes are usually involved.

12 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour12 Representing concurrency Concurrency is shown using forks, joins and rendezvous. —A fork has one incoming transition and multiple outgoing transitions. -Execution creates two concurrent threads. —A join has multiple incoming transitions and one outgoing transition. -The incoming transitions are separate threads. -If one incoming transition completes, a wait occurs until the other transitions complete. -The outgoing transition will be taken when all incoming transitions have completed. —A rendezvous has multiple incoming and outgoing transitions. -Once all the incoming transitions complete, all the outgoing transitions are initiated.

13 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour13 Example activity diagram with concurrency Verify course not full Receive course registration request Check prerequisites Check special permission Complete registration [ok] [not ok]

14 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour14 Swimlanes Often, the division of activities among the classes in an activity diagram can be clarified using swimlanes. Verify course not full Receive course registration request Check prerequisites Check special permission Complete registration [ok] [not ok] StudentCourseOffering

15 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour15 Implementing Classes from Sequence and Activity Diagrams These diagrams are most useful for parts of the system that are most complex. —I.e. they’re not for every class Interaction, activity and state diagrams help you create a correct implementation. This is particularly true when a behaviour is distributed across several use cases. —E.g. a state diagram is useful when different conditions cause instances to respond differently to the same event.

16 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour16 E.g., The Course and CourseOffering classes Class diagram ***** ****** * CourseOffering requestToRegister CourseRecord Course getPrerequisites getOfferings Schedule addCourse meetsPrerequisites addStudent

17 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour17 E.g., Consider the Course class Three states for Course ‘id’: ‘offered’: offeringsPosted() && getOfferings(id) != null. ‘open’: offered == true && one of its CourseOfferings has enrollment < capacity ‘closed’ (all of its offerings are full, or it is not offered): offered == false || offered == true && all of its CourseOfferings have enrollment >= capacity

18 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour18 The Course class public class Course { // The 1-many association with class courseOffering private Collection courseOffering = new HashMap(); // Course has one or more offerings private boolean isOffered; // … one or more offerings are not full private boolean isOpen; // … all offerings are full private boolean isClosed; } These can change whenever an addStudent or deleteStudent occurs for one of its offerings --- only one is really needed. This can change whenever the system state changes

19 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour19 Check to see if a course needs closing public void closeCheck () { // The 1-many association with class courseOffering private Collection courseOffering = new HashMap(); isClosed = true; iterator it = new Iterator (courseOffering); while (it.hasMoreElements()) { courseOffering co = it.nextElement(); if (!co.isFull()) isClosed = false; } Note: this is rough code -- it shows only the skeleton of what’s needed. It will not compile!

20 © Lethbridge/Laganière 2001 Chapter 8: Modelling Interactions and Behaviour20 Adding a student to a CourseOffering public boolean addStudent(Student student) { if (!isFull()) // offering must not be full { // Some of the sequence diagram guides this logic Course course = this.getCourse(); if (student.getTranscript().meetsPrerequisites(course)) { // create a new course record and add this offering to // student’s schedule courseRecord.put(new CourseRecord(this, student)); student.schedule.addCourse(this); // Check for transition to ’closed' state if (courseRecord.size() >= capacity) course.closeCheck(); }


Download ppt "Modeling Dynamic Behavior: State and Activity Diagrams."

Similar presentations


Ads by Google