Software Engineering, CPSC-4360-01, CPSC-5360-01, Lecture 7

Click here to load reader

download Software Engineering,  CPSC-4360-01, CPSC-5360-01, Lecture 7

of 53

  • date post

    06-Feb-2016
  • Category

    Documents

  • view

    49
  • download

    0

Embed Size (px)

description

Software Engineering, CPSC-4360-01, CPSC-5360-01, Lecture 7. Review of Last Lecture. Class Diagram Association Class, Reification N-Ary Association Qualified Association Interface Interactions diagrams Collaborations, classifier and association roles - PowerPoint PPT Presentation

Transcript of Software Engineering, CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*Software Engineering, CPSC-4360-01, CPSC-5360-01,Lecture 7

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*Review of Last LectureClass DiagramAssociation Class, ReificationN-Ary AssociationQualified AssociationInterfaceInteractions diagramsCollaborations, classifier and association rolesInteraction diagrams, object creation and destruction Role multiplicity and iterated messagesMulti-objectsConditional messages, messages to self

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*Overview of This LectureStatechartState-dependent behaviors, events, transitionsInitial and final states, guard conditionsActions, activities, composite states, history states

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*Where are we now?Topic Covered:Detailed Class DiagramObject DiagramCollaboration DiagramSequence Diagram

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*Object BehaviorThe behavior of an object is defined by its reaction to messages at any point in execution.Some object behaviors can be studied by:Object Diagram, ORInteraction Diagram.Not sufficient:Cannot model all possible scenarios, e.g., only specific sequence of messages are studied.Only model some legal (possible) states - show how an object behaves in particular interactions.We need to know about illegal or impossible states to plan for them.

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*Specifying Behaviour in UMLA different notation is needed to summarize the overall behaviour of objects.UML defines a statechart for this purpose.Complement interaction diagrams for understanding the dynamic behaviour of the system:Interaction Diagram:Models some inter-object messages with well defined order during a short duration.Statechart:Models the entire lifetime of a single object, specifying all possible sequences of messages and responses.

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*State-dependent BehaviourObjects respond differently to the same stimulus/events at different times.Can be modelled by defining a state machine:It has a set of states:an object can be in one state at any time;the state it is in determines how it responds to events.It has a set of transitions: an event can cause the object to move from one state to another.

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*CD Player: An ExampleBehaviour of a simple CD player:A drawer to hold the CD;Control interface with 3 buttons:Load button: the drawer will open if it was shut and will shut if it was open;Stop button: the player will stop playing. If there is no CD, there is no effect;Play button: the CD will be played. If the drawer is open, the drawer shuts before playing starts.

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*UML Statechart SemanticsA statechart defines the behaviour of instances of a given class.It shows:The possible states of an object;The events it can detect;Its response to those events.An object is in one active state at a time:Events may be received at any time, which can trigger a transition to the next active state;If an Event only causes a loop on a state (i.e., no change of an active state), the transition is known as self-transition.

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*Statechart: Deciding StatesIdentifying separate states:Informal principle: States S1 and S2 are separate if an object in state S1 responds differently to at least one event from the way it responds to that event in state S2.Syntax:States: rounded rectangles with the name of the state ( ).Example: There are 3 states for the CD player: Closed, Playing, Open.StateName

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*Statechart: Identifying EventsEvents are usually external stimulus:E.g., messages that can be sent to an object;Internal stimulus will be covered later in the lecture.Events usually cause an object to change state:Moving from one state to another is known as transition;Events that causes transition are known as trigger;Events can optionally carry data (similar to message parameter).Syntax:An arrow with the trigger attached ( ).Example:There are three events for the CD Player: load, play, stop.

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*CD Player: Statechart Ver. 1State:The Open state.Transition:From Closed state to Open state triggered by load event.

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*Initial and Final StatesTo specify the first active state when an object is created/initialized:Use Initial State:A transition leading from the initial state indicates the first active state.No event should be written on a transition from an initial state.Syntax: a black disc ( ).To model the destruction of an object:Use Final State:Represent the end of flow, i.e., the object no longer exists.May correspond to actual destruction for software object.Syntax: a circled black disc ( ).

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*CD Player: Statechart Ver. 2Initial State: CD Player switched onFinal State:Switched Off the CD Player

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*Non-Deterministic SystemWhen two or more transitions leading from a state share the same trigger:No way to distinguish between them;Randomly choose one of them;The same history of events may end up in a different state;Known as non-deterministic system.Can be removed by adding information to distinguish between transitions with the same trigger.

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*CD Player: Non Deterministic ExampleStatechartThis transition should be for no CD inside.This transition should be used when there is a CD inside.In real-life, a CD player will not play if there is no CD inside.Statechart Ver. 2 does not describe this.Correct Behaviour:When the play event is detected, the CD Player should remain in the Closed state if there is no CD inside.Resulting Statechart (partial):

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*Guard ConditionAlthough the two transitions are meant for different scenarios, the statechart does not distinguish among the two, resulting in a non-deterministic behavior.The non-determinism can be removed if we can specify the condition that determine the correct transition to use.Syntax: trigger [guard_condition]Semantics:If a transition has a guard condition, it can only fire if that transition is evaluated to true;If all guard conditions are false and there is no unguarded transition, the event will be ignored.Usually, only one condition is true (determinism).

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*CD Player: Statechart Ver. 3Guard ConditionNote that guard conditions are also added to transitions leading from the Open state. What do they represent?

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*ActionsA state can have actions triggered.There are three ways an action can be triggered:By an event;By entering a state;By leaving a state.An action triggered by an event takes place in response to that event:Syntax:Trigger [Guard_Condition] /actionExample:The CD drawer will be closed when play is detected in the Open state.

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*CD Player: Statechart Ver. 4Action triggered by Event

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*Action: Entering/Exiting a StateAn action can be triggered as soon as the state is entered.Useful to capture actions that must be performed regardless the transition used to arrive at the state.Syntax:Write in a compartment in the state box: entry/action Similarly, an action can be performed just before leaving a state. Syntax:Write in a compartment in the state box: exit/action

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*CD Player: Statechart (partial)StatechartAction triggered by entering a state.Action triggered by leaving a state.Question: What happened when the play button is pressed during CD playing?

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*Action and ActivityProperties of Actions:Short, self-contained processing;Finished instantaneously;Cannot be interrupted by events.An action that does not conform to the above is termed as activity instead.Property of Activities:Performed during a state;Carry out for an extended period of time;Can be interrupted by events. Syntax:Write in a compartment of the state box: do/activity

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*CD Player: Activity ExampleQuestion: When the play track activity is being performed, what happen if the stop button is pressed?Activity performed during the Playing state.

    CPSC-4360-01, CPSC-5360-01, Lecture 7

  • *CPSC-4360-01, CPSC-5360-01, Lecture 7*Completion TransitionAs well as being interrupted by events, some activities will come to an end of their own accord.If an activity completes uninterrupted, then it can trigger a completion transition.these are transitions without event labe