Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

State Machine Diagram

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

State

Machine
Diagram
CSE347 – Information System
Analysis and Design
Instructor
Dr. Shamim H Ripon
Dr. Shamim H Ripon, EWU 2
Behavioral UML diagrams
• Behavioral UML diagrams focus on illustrating the dynamic aspects of a software
system, showcasing how it behaves, responds to stimuli, and undergoes state
changes during runtime.
• During analysis, behavioral models describe what the internal logic of the
processes is without specifying how the processes are to be implemented. Later,
in the design and implementation phases, the detailed design of the operations
contained in the object is fully specified.
• During analysis, analysts use behavioral models to capture a basic understanding
of the dynamic aspects of the underlying business process. Traditionally,
behavioral models have been used primarily during design, where analysts refine
the behavioral models to include implementation details.
• The behavioral diagrams are categorized as follows:
• use case diagrams,
• interaction diagrams,
• state–chart diagrams, and
• activity diagrams.
Dr. Shamim H Ripon, EWU 3
Behavioral state machine
• A behavioral state machine is a dynamic model that shows the different
states through which a single object passes during its life in response to
events, along with its responses and actions.
• Typically, behavioral state machines are not used for all objects; rather,
behavioral state machines are used with complex objects to further defi ne
them and to help simplify the design of algorithms for their methods.
• The behavioral state machine shows the different states of the object and
what events cause the object to change from one state to another.
• Behavioral state machines should be used to help understand the dynamic
aspects of a single class and how its instances evolve over time.

Dr. Shamim H Ripon, EWU 4


What is the purpose of a state diagram?
The important purpose of a state diagram is to model the
life cycle of an object from creation to termination.
1. Model the dynamic aspects of the system and to
provide a more comprehensive representation of the
information about the system.
2. Describe the different states of an object in its life cycle,
whether it is in a triggered state, a dormant state, or a
dead state.
3. Model the life cycle of an object, which helps the
programmer to design the methods of the object, etc.
4. Help programmers to understand requirements and
improve coding speed.

Dr. Shamim H Ripon, EWU 5


UML - State Machine
Diagram

Dr. Shamim H Ripon, EWU 6


State Machine Diagrams
A state machine diagram models the behaviour of a single object, specifying the
sequence of events that an object goes through during its lifetime in response to
events.

A behavioral state machine representing the patient class in the context of a hospital environment. From
this diagram we can tell that a patient enters a hospital and is admitted after checking in. If a doctor finds
the patient to be healthy, he or she is released and is no longer considered a patient after two weeks
elapses. If a patient is found to be unhealthy, he or she remains under observation until the diagnosis
changes.
Dr. Shamim H Ripon, EWU 7
State Guard
Frame Initial state Event Transition condition Final state

Dr. Shamim H Ripon, EWU 8


• State of an object is defined by the value of its attributes and its relationships with other objects at a
particular point in time. For example, a patient might have a state of new, current, or former. The
attributes or properties of an object affect the state that it is in; however, not all attributes or attribute
changes will make a difference. For example, think about a patient’s address.

• An event is something that takes place at a certain point in time and changes a value or values that
describe an object, which, in turn, changes the object’s state. It can be a designated condition
becoming true, the receipt of the call for a method by an object, or the passage of a designated period
of time. The state of the object determines exactly what the response will be.

• A transition is a relationship that represents the movement of an object from one state to another
state. Some transitions have a guard condition.

• A guard condition is a Boolean expression that includes attribute values, which allows a transition to
occur only if the condition is true. An object typically moves from one state to another based on the
outcome of an action triggered by an event.
• An action is an atomic, non-decomposable process that cannot be interrupted. From a practical
perspective, actions take zero time, and they are associated with a transition. In contrast, an activity is
a nonatomic, decomposable process that can be interrupted. Activities take a long period of time to
complete, and they can be started and stopped by an action.

Dr. Shamim H Ripon, EWU 9


Dr. Shamim H Ripon, EWU 10
Dr. Shamim H Ripon, EWU 11
Dr. Shamim H Ripon, EWU 12
State Machine Diagrams
• States - A state is denoted by a
round-cornered rectangle with the
name of the state written inside it.

• Initial and Final States - The initial


state is denoted by a filled black circle
and may be labeled with a name. The
final state is denoted by a circle with a
dot inside and may also be labeled with
a name.
Dr. Shamim H Ripon, EWU 13
• Transitions - Transitions from one state to the
next are denoted by lines with arrowheads. A
transition may have a trigger, a guard and an
effect, as below.

• Self-Transitions - A state can have a transition


that returns to itself, as in the following diagram.
This is most useful when an effect is associated
with the transition.

• "Trigger" is the cause of the transition, which could be a signal, an event, a change in some
condition, or the passage of time. "Guard" is a condition which must be true in order for the
trigger to cause the transition. "Effect" is an action which will be invoked directly on the object
that owns the state machine as a result of the transition.
Dr. Shamim H Ripon, EWU 14
• State Actions - In the transition example above, an effect was associated with the transition. If
the target state had many transitions arriving at it, and each transition had the same effect
associated with it, it would be better to associate the effect with the target state rather than the
transitions. This can be done by defining an entry action for the state. The diagram below
shows a state with an entry action and an exit action.

• It is also possible to define actions that occur on events, or actions that always occur. It is
possible to define any number of actions of each type.

Dr. Shamim H Ripon, EWU 15


• Compound States - A state machine diagram may include sub-machine diagrams, as in the
example below.

Alternative way to
show the same
•The ∞ symbol indicates that details of the Check PIN
information
sub-machine areDr.shown
Shamim in a separate
H Ripon, EWU diagram. 16
• Entry Point - Sometimes you won’t want to enter a sub-machine at the normal initial state.
For example, in the following sub-machine it would be normal to begin in the "Initializing"
state, but if for some reason it wasn’t necessary to perform the initialization, it would be
possible to begin in the "Ready" state by transitioning to the named entry point.

• Exit Point - In a similar manner to entry points, it is possible to have named alternative exit
points. The following diagram gives an example where the state executed after the main
processing state depends on which route is used to transition out of the state.
Dr. Shamim H Ripon, EWU 17
• Choice Pseudo-State - A choice pseudo-state is shown as a diamond with one transition
arriving and two or more transitions leaving. The following diagram shows that whichever
state is arrived at, after the choice pseudo-state, is dependent on the message format
selected during execution of the previous state.

Dr. Shamim H Ripon, EWU 18


• Junction Pseudo-State - Junction
pseudo-states are used to chain together
multiple transitions. A single junction can have
one or more incoming, and one or more
outgoing, transitions; a guard can be applied to
each transition. Junctions are semantic-free. A
junction which splits an incoming transition into
multiple outgoing transitions realizes a static
conditional branch, as opposed to a choice
pseudo-state which realizes a dynamic
conditional branch.

Dr. Shamim H Ripon, EWU 19


• Terminate Pseudo-State - Entering a terminate pseudo-state indicates that the lifeline
of the state machine has ended. A terminate pseudo-state is notated as a cross.

• History States - A history state is used to remember the previous state of a state
machine when it was interrupted. The following diagram illustrates the use of history
states. The example is a state machine belonging to a washing machine.

Dr. Shamim H Ripon, EWU 20


• Concurrent Regions - A state may be divided into regions containing sub-states that
exist and execute concurrently. The example below shows that within the state "Applying
Brakes", the front and rear brakes will be operating simultaneously and independently.
Notice the use of fork and join pseudo-states, rather than choice and merge
pseudo-states. These symbols are used to synchronize the concurrent threads.

Dr. Shamim H Ripon, EWU 21


Guidelines for Creating
Behavioral State Machines
• Only create behavioral state machines for “complex” objects.

• Draw the initial state in the top left corner of the diagram.

• Draw the final state in the bottom right corner of the diagram.

• Use simple, but descriptive, names for states.

• Question black hole and miracle states.

• Make sure guard conditions are mutually exclusive.

• Make sure transitions are associated with messages and


operations

Dr. Shamim H Ripon, EWU 22


Creating a Behavioral State Machine
1. Set The context of a behavioral state machine is usually a class. Typically, the classes are very
Context dynamic and complex, requiring a good understanding of their states over time and events
triggering changes. You should examine your class diagram to identify which classes undergo
a complex series of state changes and draw a diagram for each of them.

2. Identify Object Th e second step is to identify the various states that an object will have over its lifetime. This
States includes establishing the boundaries of the existence of an object by identifying the initial
and final states of an object. We also must identify the states of an object

3. Lay Out The third step is to determine the sequence of the states that an object will pass through
Diagram during
its lifetime.
4. Add The fourth step is to identify the transitions between the states of the objects and to add the
Transitions events, actions, and guard conditions associated with the transitions. The events are the
triggers
that cause an object to move from one state to the next state.
5. The fifth step is to validate the behavioral state machine by making sure that each state is
Validate reachable and that it is possible to leave all states except for final states.
Dr. Shamim H Ripon, EWU 23

You might also like