Nothing Special   »   [go: up one dir, main page]

State Diagram-S

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

State Diagram

State Chart Diagram


State Transition Diagram
Introduction
• Two interaction diagrams with objects of the same
class receiving the same messages may respond
differently

• This is because an object’s behavior is affected by


the values of its attributes

• UML State Machine Diagram records these


dependencies
State Transition Diagram
• A state transition diagram is a technique to depict:
1. The states of an entity
2. The transitions of states of the entity
3. The trigger or the event that caused the transition of
state of the entity
• The entity may be a physical device such as a light
switch or a vending machine; it may be a software
system or component such as a word processor or an
operating system; it may be a biological system such as
a cell or a human; or - - - -

This modeling technique came from a more formal area called automata theory.
State transition diagram depicted a Finite State Machine.
Software Program View

• The end product of a software is a program which


executes. In depicting the program (or an object) we can
consider:
– Variables which take on different values
– Control structure and assignment statements (events) in
the program that change the values of the variables

1. Combination of values of the data (variables & constants) at any


point of the program represent the program state at that point.

2. The change made to the values of the variables through assignment


statements represent a transition of state
A very simple example
light switch
From To
State Event State
Alt
(light) (switch) (light)
switch light
[l_st=on] on turnOff off
TurnOff ( )

[l_st=off] off turnOn on


TurnOn ( )

2. “State transition table”


1. “Sequence diagram”
for light with switch events
(alternative fragment)
turnOff
for switch and light light light
interaction on off

turnOn

3. “State transition diagram”


for light with switch events
A little “more” on the light switch

turnOff
turnOff turnOff

on off
on off

turnOn turnOn turnOn

What happens if we turn on state can “transition” to its


a light that is already on? current state
Using State Transition Diagram

• Model the entity at the “abstraction” level


where the number of states is “manageable.”

1. List (design) the states (should not be large)


2. List events that will trigger the state transition (should not be big)
3. There must be a starting state
4. There must be a terminating state or states
5. Design the transition rules (the bulk of your design work is
thinking through the transition rules)

1.The above is not necessarily performed in sequence; iterate through these.

2. Even with a modest number of states and events, the state transition
diagram, which really depicts the transition rules, can be enormous.
State Diagrams
• State diagrams are used to show possible states a
single object can get into
– shows states of an object

• How object changes state in response to events


– shows transitions between states
Elements of State Diagram
• Start marker
• Stop marker
• States – box with rounded corners
• Transitions – shown as arrows between states
• Events – that cause transition between states
• Action – an object’s reaction to an event
• Guard
Naming Conventions for a state

• Each unique state has a unique name


• State Names are verb phrases
• A noun to name a state – incorrect
State Diagrams: States
• States are represented as rounded boxes which contain:
– the state name
– and the following optional fields
• entry and exit actions: entry and exit actions are executed
whenever the state is entered or exited, respectively
• Internal transitions: A response to an event that causes the
execution of an action but does not cause a change of state or
execution of exit or entry actions.
• External transition: A response to an event that causes a change
of state or a self-transition, together with a specified action.
• Activities: Typically, once the system enters a state it sits idle
until an event triggers a transition. Activities help you to model
situations where while in a state, the object does some work that
will continue until it is interrupted by an event
State Diagrams: States

Tracking
entry action
entry / setMode(on Track)
exit action exit / setMode(off Track)
internal transition Tracking / tracker.Acquire()
newTarget/tracker.ChangeTrack()
external transition
do / followTarget
activity

Note that, “entry”, “exit”, “do” are keywords


State Diagrams: Transitions
• Transitions
– source state and target state: shown by the arrow representing the
transition
– trigger event: the event that makes the transition fire
– guard condition: a Boolean expression that is evaluated when the
trigger event occurs, the transition can fire only if the guard
condition evaluates to true
– action: an executable atomic computation that can directly act on
the object that owns the state machine or indirectly on other
objects that are visible to the object
– initial and final states: shown as filled black circle and a filled
black circle surrounded by an unfilled circle, respectively
source state target state
trigger-event[guard-condition]/action
State Diagrams
shows the initial (default) state

getFirstItem

getNextItem Checking
[not all items checked]
do / checkItem

cancelled

shows the final state


State Diagram Example: States of an Order
object
getFirstItem

getNextItem Checking [all items checked and Dispatching


[not all items checked] all items available]
do/checkItem do/initiate
Delivery
ItemReceived
and all items available
[all items checked and
some items not in stock]
cancelled

itemsReceived
[some items not in stock]
cancelled
Waiting Delivered

cancelled
Cancelled
Types of State Machine Diagram
• Protocol state machine: Protocol state machines are primarily used to
model the behavior of an external entity or component with which a
system interacts. They define the sequences of messages or events
exchanged between the system and the external entity to achieve a
specific interaction or protocol. The focus of a protocol state machine
is on defining the order and conditions under which messages or
events are sent and received during an interaction.
• Behavioral state machine: Behavioral state machines, on the other
hand, are used to model the internal behavior of a single entity or
component within a system. They describe how an object or
component transitions between different states based on events or
conditions and how it responds to those events by executing actions.
Behavioral state machines focus on modeling the behavior of an entity
or component from an internal perspective.
Protocol state machine: login authentication protocol. In this scenario, a client needs to establish a
connection with a server and perform a login process following a predefined sequence of states and
messages
States:
• Transition 3: Connected -> Authenticating
• Disconnected: The initial state where the
client is not connected to the server. • Trigger: Client sends login credentials
(username and password).
• Connecting: The client initiates a connection
to the server. • Action: Client sends an "AUTHENTICATE"
message with credentials.
• Connected: The connection is established.
• Transition 4: Authenticating -> Authenticated
• Authenticating: The client sends login
credentials to the server. • Trigger: Server verifies credentials and
confirms authentication.
• Authenticated: The server verifies the
credentials and confirms successful • Action: Server sends an "AUTH_SUCCESS"
authentication. message.
• Error: If any error occurs during the process. • Transition 5: Authenticating -> Error
Transitions and Messages: • Trigger: Server rejects the login attempt.
• Transition 1: Disconnected -> Connecting • Action: Server sends an "AUTH_FAIL"
message.
• Trigger: Client initiates a connection request.
• Transition 6: Connected -> Error
• Action: Client sends a "CONNECT" message
to the server. • Trigger: Any unexpected error occurs.
• Transition 2: Connecting -> Connected • Action: Server sends an "ERROR" message
and terminates the connection.
• Trigger: Server acknowledges the connection.
• Action: Server sends an "ACK" message.
Behavioral state machine:A traffic light can have several states and transitions based on time intervals
and user input
States:
• Red Light: The initial state where the traffic
light displays a red signal to stop vehicles.
• Red-Yellow Light: A transitional state where
both the red and yellow lights are briefly
displayed to signal that the light is about to
change.
• Green Light: The state where the traffic light
displays a green signal to allow vehicles to
proceed.
• Yellow Light: A transitional state where the
yellow light is displayed to signal that the
light is about to change.
• Flashing Yellow Light: A state where the
yellow light flashes, indicating caution before
a green light.
Transitions and Actions: • Transition 4: Yellow Light -> Red Light
• Transition 1: Red Light -> Red-Yellow Light – Trigger: A predefined time interval for
– Trigger: A predefined time interval the yellow light has passed.
(e.g., 5 seconds) for the red light has – Action: Change the light to red,
passed. signaling vehicles to stop.
– Action: Begin displaying the red-yellow • Transition 5: Green Light -> Flashing Yellow
light for a brief period. Light
• Transition 2: Red-Yellow Light -> Green – Trigger: A button is pressed by a
Light pedestrian to request a pedestrian
– Trigger: A predefined time interval for crossing.
the red-yellow light has passed. – Action: Display a flashing yellow light,
– Action: Change the light to green, indicating that vehicles should be
allowing vehicles to proceed. cautious due to the pedestrian crossing
• Transition 3: Green Light -> Yellow Light request.
– Trigger: A predefined time interval • Transition 6: Flashing Yellow Light -> Red
(e.g., 20 seconds) for the green light has Light
passed. – Trigger: The pedestrian has crossed or
– Action: Begin displaying the yellow the request is canceled.
light, indicating that the green light will – Action: Change the light to red to stop
change soon. vehicles.
Advanced State Machine Modeling

• Nested states
• Concurrent states
Nested State Machine- Telephone
Nested State Machine- Printer

• Example: When the printer is in On state, it may also be in Idle or


Working
• To show these two states, draw lower level state diagrams within On
state
• When the printer is on, it begins at Idle state, so printer is in both On
and Idle state
• When print message is received, it moves to working state and also
remains in On state
Nested State Machine- Printer
Concurrent states

• A Printer object cycles between two separate paths. The two independent
paths are;
➢ Representing states of the work cycle.
➢ Representing states of the input paper tray
Concurrent State Machine- Printer
State Diagrams Importants

• Use them to show the behavior of a single object


not many objects
– for many objects use interaction diagrams

• Do not try to draw state diagrams for every class


in the system, use them to show interesting
behavior and increase understanding
Problem # 1
• A simple digital watch has a display and two buttons to set
it, the A button and the B button. The watch has two
modes of operation, display time and set time.
• In the display time mode, the watch displays hours and
minutes, separated by a flashing colon.
• The set time mode has two submodes, set hours and set
minutes. The A button selects modes. Each time it is
pressed, the mode advances in the sequence: display, set
hours, set minutes, display, etc.
• Within the submodes, the B button advances the hours or
minutes once each time it is pressed. Buttons must be
released before they can generate another event.
• Prepare a state diagram of the watch.
Solution of Problem # 1

DigitalWatch

A
Set hours
do / show hours A
A
Display time Set minutes
B / advance hour
do / show hours and do / show minutes
minutes

B / advance minute
• Design an Appointment Management System (AMS) for a hospital to
efficiently handle patient appointments. The system should allow
patients to request appointments, doctors to accept or decline requests,
and receptionists to schedule confirmed appointments. It should also
handle appointment cancellations and rescheduling while ensuring that
overbooking is prevented.
Difference between Activity and State Chart
Diagram
State diagram shows the object undergoing a process. It gives a clear
picture of the changes in the object's state in this process.
e.g: ATM withdraw
Card object state: Checking, Approving, Rejecting

Activity diagram is a fancy flow chart which shows the flow of activity of
a process.

e.g: ATM withdraw


Withdraw activity: Insert Card, Enter PIN, Check balance, with draw
money, get card

State chart shows the dynamic behavior of an object.


Activity diagram shows the workflow behavior of an operation as set of
actions

You might also like