A state diagram is a type of diagram used in
computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
and related fields to describe the behavior of systems. State diagrams require that the system described is composed of a finite number of
state
State may refer to:
Arts, entertainment, and media Literature
* ''State Magazine'', a monthly magazine published by the U.S. Department of State
* ''The State'' (newspaper), a daily newspaper in Columbia, South Carolina, United States
* ''Our S ...
s; sometimes, this is indeed the case, while at other times this is a reasonable
abstraction. Many forms of state diagrams exist, which differ slightly and have different
semantics
Semantics (from grc, σημαντικός ''sēmantikós'', "significant") is the study of reference, meaning, or truth. The term can be used to refer to subfields of several distinct disciplines, including philosophy
Philosophy (f ...
.
Overview
State diagrams are used to give an abstract description of the
behavior
Behavior (American English) or behaviour (British English) is the range of actions and mannerisms made by individuals, organisms, systems or artificial entities in some environment. These systems can include other systems or organisms as wel ...
of a
system
A system is a group of Interaction, interacting or interrelated elements that act according to a set of rules to form a unified whole. A system, surrounded and influenced by its environment (systems), environment, is described by its boundaries, ...
. This behavior is analyzed and represented by a series of events that can occur in one or more possible states. Hereby "each diagram usually represents objects of a single class and track the different states of its objects through the system".
State diagrams can be used to graphically represent
finite-state machine
A finite-state machine (FSM) or finite-state automaton (FSA, plural: ''automata''), finite automaton, or simply a state machine, is a mathematical model of computation. It is an abstract machine that can be in exactly one of a finite number o ...
s (also called finite automata). This was introduced by
Claude Shannon
Claude Elwood Shannon (April 30, 1916 – February 24, 2001) was an American people, American mathematician, electrical engineering, electrical engineer, and cryptography, cryptographer known as a "father of information theory".
As a 21-year-o ...
and
Warren Weaver
Warren Weaver (July 17, 1894 – November 24, 1978) was an American scientist, mathematician, and science administrator. He is widely recognized as one of the pioneers of machine translation and as an important figure in creating support for scien ...
in their 1949 book
''The Mathematical Theory of Communication''. Another source is
Taylor Booth in his 1967 book ''Sequential Machines and Automata Theory''. Another possible representation is the
state-transition table.
Directed graph
A classic form of state diagram for a
finite automaton
A finite-state machine (FSM) or finite-state automaton (FSA, plural: ''automata''), finite automaton, or simply a state machine, is a mathematical model of computation. It is an abstract machine that can be in exactly one of a finite number o ...
(FA) is a
directed graph
In mathematics, and more specifically in graph theory, a directed graph (or digraph) is a graph that is made up of a set of vertices connected by directed edges, often called arcs.
Definition
In formal terms, a directed graph is an ordered pa ...
with the following elements (Q, Σ, Z, δ, q
0, F):
[ Taylor Booth (1967) ''Sequential Machines and Automata Theory'', John Wiley and Sons, New York.]John Hopcroft
John Edward Hopcroft (born October 7, 1939) is an American theoretical computer scientist. His textbooks on theory of computation (also known as the Cinderella book) and data structures are regarded as standards in their fields. He is the IBM Pro ...
and Jeffrey Ullman
Jeffrey David Ullman (born November 22, 1942) is an American computer scientist and the Stanford W. Ascherman Professor of Engineering, Emeritus, at Stanford University. His textbooks on compilers (various editions are popularly known as the dr ...
(1979) ''Introduction to Automata Theory, Languages, and Computation
''Introduction to Automata Theory, Languages, and Computation'' is an influential computer science textbook by John Hopcroft and Jeffrey Ullman on formal languages and the theory of computation. Rajeev Motwani contributed to later editions begi ...
'', Addison-Wesley Publishing Company, Reading Mass,
*Vertices Q: a finite set of states, normally represented by circles and labeled with unique designator symbols or words written inside them
*Input symbols Σ: a finite collection of input symbols or designators
*Output symbols Z: a finite collection of output symbols or designators
The output function ω represents the mapping of ordered pairs of input symbols and states onto output symbols, denoted mathematically as ω : Σ × Q→ Z.
*Edges δ: represent transitions from one state to another as caused by the input (identified by their symbols drawn on the edges). An edge is usually drawn as an arrow directed from the present state to the next state. This mapping describes the state transition that is to occur on input of a particular symbol. This is written mathematically as δ : Q × Σ → Q, so δ (the transition function) in the definition of the FA is given by both the pair of vertices connected by an edge and the symbol on an edge in a diagram representing this FA. Item δ(q, a) = p in the definition of the FA means that from the state named q under input symbol a, the transition to the state p occurs in this machine. In the diagram representing this FA, this is represented by an edge labeled by a pointing from the vertex labeled by q to the vertex labeled by p.
*Start state q
0: (not shown in the examples below). The start state q
0 ∈ Q is usually represented by an arrow with no origin pointing to the state. In older texts,
[ Edward J. McClusky, Introduction to the Theory of Switching Circuits, McGraw-Hill, 1965] the start state is not shown and must be inferred from the text.
*Accepting state(s) F: If used, for example for accepting automata, F ∈ Q is the accepting state
A finite-state machine (FSM) or finite-state automaton (FSA, plural: ''automata''), finite automaton, or simply a state machine, is a mathematical model of computation. It is an abstract machine that can be in exactly one of a finite number o ...
. It is usually drawn as a double circle. Sometimes the accept state(s) function as "Final" (halt, trapped) states.[
For a ]deterministic finite automaton
In the theory of computation, a branch of theoretical computer science, a deterministic finite automaton (DFA)—also known as deterministic finite acceptor (DFA), deterministic finite-state machine (DFSM), or deterministic finite-state automa ...
(DFA), nondeterministic finite automaton
In automata theory, a finite-state machine is called a deterministic finite automaton (DFA), if
* each of its transitions is ''uniquely'' determined by its source state and input symbol, and
* reading an input symbol is required for each state tr ...
(NFA), generalized nondeterministic finite automaton In the theory of computation, a generalized nondeterministic finite automaton (GNFA), also known as an expression automaton or a generalized nondeterministic finite state machine, is a variation of a
nondeterministic finite automaton (NFA) where ea ...
(GNFA), or Moore machine
In the theory of computation, a Moore machine is a finite-state machine whose current output values are determined only by its current state. This is in contrast to a Mealy machine, whose output values are determined both by its current state and ...
, the input is denoted on each edge. For a Mealy machine
In the theory of computation, a Mealy machine is a finite-state machine whose output values are determined both by its current state and the current inputs. This is in contrast to a Moore machine, whose output values are determined solely by its cu ...
, input and output are signified on each edge, separated with a slash "/": "1/0" denotes the state change upon encountering the symbol "1" causing the symbol "0" to be output. For a Moore machine
In the theory of computation, a Moore machine is a finite-state machine whose current output values are determined only by its current state. This is in contrast to a Mealy machine, whose output values are determined both by its current state and ...
the state's output is usually written inside the state's circle, also separated from the state's designator with a slash "/". There are also variants that combine these two notations.
For example, if a state has a number of outputs (e.g. "a= motor counter-clockwise=1, b= caution light inactive=0") the diagram should reflect this : e.g. "q5/1,0" designates state q5 with outputs a=1, b=0. This designator will be written inside the state's circle.
Example: DFA, NFA, GNFA, or
Moore machine
In the theory of computation, a Moore machine is a finite-state machine whose current output values are determined only by its current state. This is in contrast to a Mealy machine, whose output values are determined both by its current state and ...
''S''1 and ''S''2 are states and ''S''1 is an accepting state or a final state. Each edge is labeled with the input. This example shows an acceptor for binary numbers that contain an even number of zeros.
:
Example:
Mealy machine
In the theory of computation, a Mealy machine is a finite-state machine whose output values are determined both by its current state and the current inputs. This is in contrast to a Moore machine, whose output values are determined solely by its cu ...
''S''0, ''S''1, and ''S''2 are states. Each edge is labeled with "''j'' / ''k''" where ''j'' is the input and ''k'' is the output.
:
Harel statechart
Harel statecharts,David Harel
David Harel ( he, דוד הראל; born 12 April 1950) is a computer scientist, currently serving as President of the Israel Academy of Sciences and Humanities. He has been on the faculty of the Weizmann Institute of Science in Israel since 1980, ...
Statecharts: A visual formalism for complex systems. Science of Computer Programming
8(3):231–274, June 1987. invented by computer scientist David Harel
David Harel ( he, דוד הראל; born 12 April 1950) is a computer scientist, currently serving as President of the Israel Academy of Sciences and Humanities. He has been on the faculty of the Weizmann Institute of Science in Israel since 1980, ...
, are gaining widespread usage since a variant has become part of the Unified Modeling Language
The Unified Modeling Language (UML) is a general-purpose, developmental modeling language in the field of software engineering that is intended to provide a standard way to visualize the design of a system.
The creation of UML was originally m ...
(UML). The diagram type allows the modeling of superstates, orthogonal regions, and activities as part of a state.
Classic state diagrams require the creation of distinct nodes for every valid combination of parameters that define the state. This can lead to a very large number of nodes and transitions between nodes for all but the simplest of systems ( state and transition explosion). This complexity reduces the readability of the state diagram. With Harel statecharts it is possible to model multiple cross-functional state diagrams within the statechart. Each of these cross-functional state machines can transition internally without affecting the other state machines in the statechart. The current state of each cross-functional state machine in the statechart defines the state of the system. The Harel statechart is equivalent to a state diagram but it improves the readability of the resulting diagram.
Alternative semantics
There are other sets of semantics available to represent state diagrams. For example, there are tools for modeling and designing logic for embedded controllers. These diagrams, like Harel's original state machines, support hierarchically nested states, orthogonal regions, state actions, and transition actions.Alur, R., Kanade, A., Ramesh, S., & Shashidhar, K. C. (2008). Symbolic analysis for improving simulation coverage of Simulink/Stateflow models. International Conference on Embedded Software (pp. 89–98). Atlanta, GA: ACM.
/ref>
State diagrams versus flowcharts
Newcomers to the state machine formalism often confuse state diagrams with flowchart
A flowchart is a type of diagram that represents a workflow or process. A flowchart can also be defined as a diagrammatic representation of an algorithm, a step-by-step approach to solving a task.
The flowchart shows the steps as boxes of va ...
s. The figure below shows a comparison of a state diagram
A state diagram is a type of diagram used in computer science and related fields to describe the behavior of systems. State diagrams require that the system described is composed of a finite number of states; sometimes, this is indeed the case, ...
with a flowchart. A state machine (panel (a)) performs actions in response to explicit events. In contrast, the flowchart (panel (b)) does not need explicit events but rather transitions from node to node in its graph automatically upon completion of activities.
:
Nodes of flowcharts are edges in the induced graph of states.
The reason is that each node in a flowchart represents a program command.
A program command is an action to be executed.
So it is not a state, but when applied to the program's state, it results in a transition to another state.
In more detail, the source code listing represents a program graph.
Executing the program graph (parsing and interpreting) results in a state graph.
So each program graph induces a state graph.
Conversion of the program graph to its associated state graph is called "unfolding" of the program graph.
The program graph is a sequence of commands.
If no variables exist, then the state consists only of the program counter, which keeps track of where in the program we are during execution (what is the next command to be applied).
In this case before executing a command the program counter is at some position (state before the command is executed).
Executing the command moves the program counter to the next command.
Since the program counter is the whole state, it follows that executing the command changed the state.
So the command itself corresponds to a transition between the two states.
Now consider the full case, when variables exist and are affected by the program commands being executed.
Then between different program counter locations, not only does the program counter change, but variables might also change values, due to the commands executed.
Consequently, even if we revisit some program command (e.g. in a loop), this doesn't imply the program is in the same state.
In the previous case, the program would be in the same state, because the whole state is just the program counter, so if the program counterpoints to the same position (next command) it suffices to specify that we are in the same state.
However, if the state includes variables, then if those change value, we can be at the same program location with different variable values, meaning in a different state in the program's state space.
The term "unfolding" originates from this multiplication of locations when producing the state graph from the program graph.
A self transition is a transition where the initial and the final state are the same.
A representative example is a do loop incrementing some counter until it overflows and becomes 0 again.
Although the do loop executes the same increment command iteratively, so the program graph executes a cycle, in its state space is not a cycle, but a line.
This results from the state being the program location (here cycling) combined with the counter value, which is strictly increasing (until the overflow), so different states are visited in sequence, until the overflow.
After the overflow the counter becomes 0 again, so the initial state is revisited in the state space, closing a cycle in the state space (assuming the counter was initialized to 0).
The figure above attempts to show that reversal of roles by aligning the arcs of the state diagrams with the processing stages of the flowchart.
You can compare a flowchart to an assembly line in manufacturing because the flowchart describes the progression of some task from beginning to end (e.g., transforming source code input into object code output by a compiler). A state machine generally has no notion of such a progression. The door state machine shown at the top of this article, for example, is not in a more advanced stage when it is in the "closed" state, compared to being in the "opened" state; it simply reacts differently to the open/close events. A state in a state machine is an efficient way of specifying a particular behavior, rather than a stage of processing.
Other extensions
An interesting extension is to allow arcs to flow from any number of states to any number of states. This only makes sense if the system is allowed to be in multiple states at once, which implies that an individual state only describes a condition or other partial aspect of the overall, global state. The resulting formalism is known as a Petri net
A Petri net, also known as a place/transition (PT) net, is one of several mathematical modeling languages for the description of distributed systems. It is a class of discrete event dynamic system. A Petri net is a directed bipartite graph that ...
.
Another extension allows the integration of flowcharts within Harel statecharts. This extension supports the development of software that is both event driven and workflow driven.
See also
* David Harel
David Harel ( he, דוד הראל; born 12 April 1950) is a computer scientist, currently serving as President of the Israel Academy of Sciences and Humanities. He has been on the faculty of the Weizmann Institute of Science in Israel since 1980, ...
* DRAKON
DRAKON is a free and open source algorithmic visual programming and modeling language developed within the Buran space project following ergonomic design principles. The language provides a uniform way to represent flowcharts of any com ...
* SCXML
SCXML stands for State Chart XML: State Machine Notation for Control Abstraction. It is an XML-based markup language that provides a generic state-machine-based execution environment based on Harel statecharts.
SCXML is able to describe comple ...
an XML language that provides a generic state-machine based execution environment based on Harel statecharts.
* UML state machine
UML state machine, also known as UML statechart, is an extension of the mathematical concept of a finite automaton in computer science applications as expressed in the Unified Modeling Language (UML) notation.
The concepts behind it are about ...
* YAKINDU Statechart Tools
YAKINDU Statechart Tools (YAKINDU SCT) is a tool for the specification and development of reactive, event-driven systems with the help of finite-state machines. It comprises a tool for the graphical editing of statecharts and provides validation, ...
is a software for modeling state diagrams (Harel statecharts, Mealy machines, Moore machines), simulation, and source code generation.
References
External links
Introduction to UML 2 State Machine Diagrams
by Scott W. Ambler
Scott W. Ambler (born 1966) is a Canadian software engineer, consultant and author. He is an author of books about the Disciplined Agile Delivery toolkit, the Unified process, Agile software development, the Unified Modeling Language, and Capabili ...
UML 2 State Machine Diagram Guidelines
by Scott W. Ambler
Intelliwizard - UML StateWizard
- A discontinued round-trip UML dynamic modeling/development framework and tool that ran in popular IDEs under an open-source license.
YAKINDU Statechart Tools
- an Open-Source-Tool for the specification and development of reactive, event-driven systems with the help of state machines
A finite-state machine (FSM) or finite-state automaton (FSA, plural: ''automata''), finite automaton, or simply a state machine, is a mathematical model of computation. It is an abstract machine that can be in exactly one of a finite number o ...
.
Understanding and Using State Machines
MATLAB Tech Talks on State Machines
* FSM: Open Source Finite State Machine Generation in Java by Alexander Sakharo
scxmlcc
An efficient scxml state machine to C++ compiler.
* SMC: An Open Source State Machine Compiler that generates FSM for many languages as C, Python, Lua, Scala, PHP, Java, VB, etc
SMC
{{DEFAULTSORT:State Diagram
Models of computation
Unified Modeling Language diagrams
Diagrams
Infographics
Application-specific graphs
Graph drawing
Modeling languages