35
State Machine Diagram Week 14

STID3023_Week11-StateDiagram

Embed Size (px)

DESCRIPTION

note

Citation preview

  • State Machine DiagramWeek 14

  • Lecture Outline State machine Diagrams State machine syntax/Notation State Event Transition Initial/Final State

  • Learning ObjectiveTo determine states from problem statementTo understand UML notations for state machine diagramTo elaborate and identify elements in state machine diagramTo draw state machine diagram

  • State machine DiagramState machine diagram or behavioral state machine is a dynamic model that shows the different states that a single object passes through during its life in response to events, along with its responses and actions. (Dennis, 2005)A diagram that captures the behavior of an object by specifying the sequence of states it goes through during its lifetime in response to events, together with the responses to those events (Hoffer, 2004)State machine diagram is used to represent a single object and how its behavior causes it to change state.State machine diagram describes the dynamic behavior of objects over time by modeling the lifecycles of objects of each class. (The UML Ref Manual, 1999)

  • When to use State machine Diagram?State machine diagram need to be drawn only for objects that exhibit a number of states or dynamic objects.The diagram is not used for all objects, but just to further define complex objects to help simplify the design of algo for their methods. (Dennis, 2005).The more CRUD process an object has, it is possibly a candidate for state machine diagram.Fowler and Scott(1997) recommended using state machines for:Describing the behavior of an object across several use casesDescribing behavior that involves in an object, not for collaboration of objects

  • State machine Diagram (SCD) vs Interaction Diagram

    SCD should be used if you are interested in understanding the dynamic aspects of a single class and how it evolves over time.Interaction diagram should be used if you are interested in understanding how a particular use case scenario is executed over a set of classes.

  • Example: State Machine Diagram of Patient Admission

  • EmptyLoadedFullcreate stackpushl[ not full ]pop/errorpushpush / errorpoppop[ not empty ]pop[ empty ]push[full ]Example: State machine Diagram of a Stack

  • A STATE

    AN INITIAL STATE

    A FINAL STATE

    AN EVENT

    A TRANSITIONaStateanEventState machine Diagram Syntax

  • Elements in State machineStatesEventsTransitions

  • StatesState a stable condition during the lifetime of the object, where it is:Continuously performing activity,Waiting on an event, orComplying with some condition.The state of an object is defined by the value of its attributes and its relationships with other objects at a particular point in time. Eg: Patient new, current, former.However, some attributes might not affect states of an object. Eg: patient address. (Dennis, p.250)All objects in a given state execute the same action when they receive the same eventObjects in different states, may react differently to the same event, by performing different actions.Eg: ATM machine reacts to cancel button differently when it is processing a transaction compared when it is idle.

  • States> NotationState represented by rectangle with rounded cornersState name

  • States> Eg :ATMScreen object may be idle if it is inactive or no transaction in 5 seconds. The screen will be active if there is any transaction done.

  • StatesState has several parts:NameTextual string that distinguishes the state from other statesEntry/exit actionsActions when entering and leaving the stateSyntax: entry/action, exit/actionInternal transitionsWhen events are processed internally, without leaving a state.The entry/exit actions are not executed.Syntax: event/action, ActivityWhen in a state, an object is not necessarily idle it may be continuously performing some operation. Syntax: do/activity, SubstatesNested state/composite states (sequential/concurrent substates)

  • StatesState has several parts:

  • Initial/Final StatesInitial stateIndicates the initial starting state for the state machine or a substate.Final stateIndicates the state machines has completed.Initial statefinal state

  • EventsExternal stimuli, such as signal, calls to operations by external bodies or the passing of time, change in a condition and creation and deletion.Events represent the kind of changes that an object can detect.Is a triggers that cause an object move from one state to the next state.Something that happens and that may cause some actions.Anything that can affect an object can be characterized as an event.Usually indicated directly on the path of a transition from a state to another state.Events may be internal or externalExternal events : those that pass between system and its actor. Eg : pushing a buttonInternal events: those that pass among the objects that live inside the system. Eg: overflow message

  • Types of Event

    No.Description of EventsSyntax1.A change event occurs when some condition is satisfied (a Boolean condition becomes true).Change event vs Guard Condition (GC)GC is evaluated once when the trigger event on the transition occurs and the receiver handles the event. If it is false, the transition does not fire and the condition is not reevaluatedChange event is evaluated continuously until it becomes true, then the transition firesWhen (exp)2.An time event is caused by the passage of a designated period of time after a specified event (frequently the entry to the current state)after (time)

  • Example : Change & Time eventsIdleActivedo action()after(5 seconds) /displayMain()Time eventWhen(11:50pm)/selfTest()

  • Types of EventIdleActivepressButton(withdraw)

    No.Description of EventsSyntax3.A call event occurs when an object receives a call to one of its operations either from another object or from itself (synchronous).After execution, returns to the calling object.The receiver of a call event may continue its own execution in parallel with the caller.op(a:T)

  • Types of Event

    No.Description of EventsSyntax4.A signal event occurs when an object receives a signal (an asynchronous communication).The sending object does not wait for the receiver to deal with the signal but continues with its own independently.The sender can the receiver can be the same object.sname (a:T)

  • TransitionA transition is a change of an object from one state (the source state) to another (the target state).A transition is triggered when an event of interest of the given object occurs.Transition is a relationship that represent the movement of an object from one state to another state. (Dennis, 2005)To show flow from one state to another.Alternatively, a transition may be executed unconditionally when the activity associated with the source state is complete (triggerless transition) .Transition is modeled by open arrow from one state to another.It is a relationship between 2 states indicating that:an object in the first state will perform certain actions and enter second state when a specified event occurs and specified conditions are satisfied.

  • TransitionThere may be an action associated with a triggered transition. This action executes before the object enters the target state.A self-transition is a transition whose source state and target state are the same.Note that entry and exit actions are executed at a self-transition.

  • Transition has 5 parts:Source stateCurrent state, in which the transition originated.The object receives a trigger event in this state.Event triggerEligible to fire a transitionGuard ConditionBoolean expression that is evaluated when the transition triggers.TruefireNotation of event with guard:eventName[guard condition]eventName[guard condition]/action[guard condition]ActionPiece of functionality to be executed during transition (operation contained within object).Atomic computation (non decomposable process that cannot be interrupted) done by object [Activity is non-atomic]Target stateState that is active after completion of the transition

    Transition

  • Transition

  • Initial statefinal stateSelf transitionactionTransition

  • Preparing State machinesSet the contextIdentify the initial final, and stable states of the objectDetermine the order in which the object will pass through stable statesIdentify the events, actions, and guard conditions associated with the transitionsValidate the statemachine diagram

  • Example: Digital Watch Do/display hoursSet hoursmode_buttonmode_buttoninc/hours : >= hours + 1inc/minutes : >= minutes + 1mode_button

  • Example: CourseOffering (CO)Initially, a CO must be initialized with a lecturer assigned to the CO.A CO object may be open(able to add student) or closed (when achieve maximum number of students already assigned to the CO object). Assume that the max no. of students that can be assigned to a CO is 10 students.Whenever there are students register to a CO, ask CourseRegister object(external object) to create the CO into the list.The CO object must count the no. of student added to the CO.A CO may also be cancelled at any time during the semester.

  • State machine diagram for CourseOffering object.

  • Exercise 1Create a state machine diagram to show the state transitions of a class called Article as it moves through the publishing process. When an authors submits an article to be published, the article is categorized as new submission. The article is categorized as being evaluated when a referee is assigned to that article. If the article is suitable for publication, it becomes accepted article, but if the article is not suitable for publication, it becomes rejected article.

  • ClueCreate a state machine diagram to show the state transitions of a class called Article as it moves through the publishing process. When an authors submits an article to be published, the article is categorized as new submission. The article is categorized as being evaluated when a referee is assigned to that article. If the article is suitable for publication, it becomes accepted article, but if the article is not suitable for publication, it becomes rejected article.

  • Exercise 2Develop a State machine for the following specification of a CD player.The CD Player contains a CD drawer with three functional buttons: Load, Play and Stop.Load Button shall open the CD drawer if currently the CD drawer is closed and vice versa.Stop Button will stop the CD player being played. If this button is pressed without any CD being played, it shouldnt give any response.Play Button will play the CD. If this button is being pressed with the drawer is still open, the drawer shall close it first before playing the CD.Additional information:The user can press the Off button at anytime to stop the operation.It is assumed that the CD drawer is closed in the initial state.

  • Answer #1

  • Answer #2

    ****************