<s>
UML	B-Application
state	I-Application
machine	I-Application
,	O
also	O
known	O
as	O
UML	B-Application
statechart	I-Application
,	O
is	O
an	O
extension	O
of	O
the	O
mathematical	O
concept	O
of	O
a	O
finite	B-Architecture
automaton	I-Architecture
in	O
computer	B-General_Concept
science	I-General_Concept
applications	O
as	O
expressed	O
in	O
the	O
Unified	B-Language
Modeling	I-Language
Language	I-Language
(	O
UML	B-Language
)	O
notation	O
.	O
</s>
<s>
UML	B-Application
state	I-Application
machine	I-Application
is	O
an	O
object-based	O
variant	O
of	O
Harel	O
statechart	B-Application
,	O
adapted	O
and	O
extended	O
by	O
UML	B-Language
.	O
</s>
<s>
The	O
goal	O
of	O
UML	B-Application
state	I-Application
machines	I-Application
is	O
to	O
overcome	O
the	O
main	O
limitations	O
of	O
traditional	O
finite-state	B-Architecture
machines	I-Architecture
while	O
retaining	O
their	O
main	O
benefits	O
.	O
</s>
<s>
UML	B-Application
statecharts	I-Application
introduce	O
the	O
new	O
concepts	O
of	O
hierarchically	O
nested	O
states	O
and	O
orthogonal	O
regions	O
,	O
while	O
extending	O
the	O
notion	O
of	O
actions	O
.	O
</s>
<s>
UML	B-Application
state	I-Application
machines	I-Application
have	O
the	O
characteristics	O
of	O
both	O
Mealy	B-General_Concept
machines	I-General_Concept
and	O
Moore	B-General_Concept
machines	I-General_Concept
.	O
</s>
<s>
They	O
support	O
actions	O
that	O
depend	O
on	O
both	O
the	O
state	O
of	O
the	O
system	O
and	O
the	O
triggering	O
event	O
,	O
as	O
in	O
Mealy	B-General_Concept
machines	I-General_Concept
,	O
as	O
well	O
as	O
entry	O
and	O
exit	O
actions	O
,	O
which	O
are	O
associated	O
with	O
states	O
rather	O
than	O
transitions	O
,	O
as	O
in	O
Moore	B-General_Concept
machines	I-General_Concept
.	O
</s>
<s>
The	O
term	O
"	O
UML	B-Application
state	I-Application
machine	I-Application
"	O
can	O
refer	O
to	O
two	O
kinds	O
of	O
state	B-Architecture
machines	I-Architecture
:	O
behavioral	O
state	B-Architecture
machines	I-Architecture
and	O
protocol	O
state	B-Architecture
machines	I-Architecture
.	O
</s>
<s>
Behavioral	O
state	B-Architecture
machines	I-Architecture
can	O
be	O
used	O
to	O
model	O
the	O
behavior	O
of	O
individual	O
entities	O
(	O
e.g.	O
,	O
class	O
instances	O
)	O
,	O
a	O
subsystem	O
,	O
a	O
package	O
,	O
or	O
even	O
an	O
entire	O
system	O
.	O
</s>
<s>
Protocol	O
state	B-Architecture
machines	I-Architecture
are	O
used	O
to	O
express	O
usage	O
protocols	O
and	O
can	O
be	O
used	O
to	O
specify	O
the	O
legal	O
usage	O
scenarios	O
of	O
classifiers	O
,	O
interfaces	O
,	O
and	O
ports	O
.	O
</s>
<s>
Many	O
software	O
systems	O
are	O
event-driven	B-Application
,	O
which	O
means	O
that	O
they	O
continuously	O
wait	O
for	O
the	O
occurrence	O
of	O
some	O
external	O
or	O
internal	O
event	O
such	O
as	O
a	O
mouse	O
click	O
,	O
a	O
button	O
press	O
,	O
a	O
time	O
tick	O
,	O
or	O
an	O
arrival	O
of	O
a	O
data	O
packet	O
.	O
</s>
<s>
(	O
That	O
's	O
why	O
event-driven	B-Application
systems	O
are	O
alternatively	O
called	O
reactive	O
systems	O
.	O
)	O
</s>
<s>
The	O
pattern	O
of	O
events	O
,	O
states	O
,	O
and	O
state	O
transitions	O
among	O
those	O
states	O
can	O
be	O
abstracted	O
and	O
represented	O
as	O
a	O
finite-state	B-Architecture
machine	I-Architecture
(	O
FSM	O
)	O
.	O
</s>
<s>
The	O
concept	O
of	O
a	O
FSM	O
is	O
important	O
in	O
event-driven	B-Application
programming	I-Application
because	O
it	O
makes	O
the	O
event	O
handling	O
explicitly	O
dependent	O
on	O
both	O
the	O
event-type	O
and	O
on	O
the	O
state	O
of	O
the	O
system	O
.	O
</s>
<s>
When	O
used	O
correctly	O
,	O
a	O
state	B-Architecture
machine	I-Architecture
can	O
drastically	O
cut	O
down	O
the	O
number	O
of	O
execution	O
paths	O
through	O
the	O
code	O
,	O
simplify	O
the	O
conditions	O
tested	O
at	O
each	O
branching	O
point	O
,	O
and	O
simplify	O
the	O
switching	O
between	O
different	O
modes	O
of	O
execution	O
.	O
</s>
<s>
Conversely	O
,	O
using	O
event-driven	B-Application
programming	I-Application
without	O
an	O
underlying	O
FSM	O
model	O
can	O
lead	O
programmers	O
to	O
produce	O
error	O
prone	O
,	O
difficult	O
to	O
extend	O
and	O
excessively	O
complex	O
application	O
code	O
.	O
</s>
<s>
UML	B-Language
preserves	O
the	O
general	O
form	O
of	O
the	O
traditional	B-Application
state	I-Application
diagrams	I-Application
.	O
</s>
<s>
The	O
UML	B-Language
state	B-Application
diagrams	I-Application
are	O
directed	O
graphs	O
in	O
which	O
nodes	O
denote	O
states	O
and	O
connectors	O
denote	O
state	O
transitions	O
.	O
</s>
<s>
For	O
example	O
,	O
Figure	O
1	O
shows	O
a	O
UML	B-Language
state	B-Application
diagram	I-Application
corresponding	O
to	O
the	O
computer	O
keyboard	O
state	B-Architecture
machine	I-Architecture
.	O
</s>
<s>
In	O
UML	B-Language
,	O
states	O
are	O
represented	O
as	O
rounded	O
rectangles	O
labeled	O
with	O
state	O
names	O
.	O
</s>
<s>
Every	O
state	B-Application
diagram	I-Application
should	O
have	O
such	O
a	O
transition	O
,	O
which	O
should	O
not	O
be	O
labeled	O
,	O
since	O
it	O
is	O
not	O
triggered	O
by	O
an	O
event	O
.	O
</s>
<s>
Strictly	O
speaking	O
,	O
in	O
the	O
UML	B-Language
specification	O
,	O
the	O
term	O
event	O
refers	O
to	O
the	O
type	O
of	O
occurrence	O
rather	O
than	O
to	O
any	O
concrete	O
instance	O
of	O
that	O
occurrence	O
.	O
</s>
<s>
An	O
event	O
instance	O
outlives	O
the	O
instantaneous	O
occurrence	O
that	O
generated	O
it	O
and	O
might	O
convey	O
this	O
occurrence	O
to	O
one	O
or	O
more	O
state	B-Architecture
machines	I-Architecture
.	O
</s>
<s>
First	O
,	O
the	O
event	O
instance	O
is	O
received	O
when	O
it	O
is	O
accepted	O
and	O
waiting	O
for	O
processing	O
(	O
e.g.	O
,	O
it	O
is	O
placed	O
on	O
the	O
event	B-Operating_System
queue	I-Operating_System
)	O
.	O
</s>
<s>
Later	O
,	O
the	O
event	O
instance	O
is	O
dispatched	O
to	O
the	O
state	B-Architecture
machine	I-Architecture
,	O
at	O
which	O
point	O
it	O
becomes	O
the	O
current	O
event	O
.	O
</s>
<s>
Finally	O
,	O
it	O
is	O
consumed	O
when	O
the	O
state	B-Architecture
machine	I-Architecture
finishes	O
processing	O
the	O
event	O
instance	O
.	O
</s>
<s>
Each	O
state	B-Architecture
machine	I-Architecture
has	O
a	O
state	O
,	O
which	O
governs	O
reaction	O
of	O
the	O
state	B-Architecture
machine	I-Architecture
to	O
events	O
.	O
</s>
<s>
In	O
the	O
context	O
of	O
software	O
state	B-Architecture
machines	I-Architecture
(	O
and	O
especially	O
classical	O
FSMs	O
)	O
,	O
the	O
term	O
state	O
is	O
often	O
understood	O
as	O
a	O
single	O
state	O
variable	O
that	O
can	O
assume	O
only	O
a	O
limited	O
number	O
of	O
a	O
priori	O
determined	O
values	O
(	O
e.g.	O
,	O
two	O
values	O
in	O
case	O
of	O
the	O
keyboard	O
,	O
or	O
more	O
generally	O
-	O
some	O
kind	O
of	O
variable	O
with	O
an	O
enum	O
type	O
in	O
many	O
programming	O
languages	O
)	O
.	O
</s>
<s>
In	O
practice	O
,	O
however	O
,	O
interpreting	O
the	O
whole	O
state	O
of	O
the	O
state	B-Architecture
machine	I-Architecture
as	O
a	O
single	O
state	O
variable	O
quickly	O
becomes	O
impractical	O
for	O
all	O
state	B-Architecture
machines	I-Architecture
beyond	O
very	O
simple	O
ones	O
.	O
</s>
<s>
This	O
interpretation	O
is	O
not	O
practical	O
,	O
so	O
in	O
UML	B-Application
state	I-Application
machines	I-Application
the	O
whole	O
state	O
of	O
the	O
state	B-Architecture
machine	I-Architecture
is	O
commonly	O
split	O
into	O
(	O
a	O
)	O
enumeratable	O
state	O
variable	O
and	O
(	O
b	O
)	O
all	O
the	O
other	O
variables	O
which	O
are	O
named	O
extended	O
state	O
.	O
</s>
<s>
State	B-Architecture
machines	I-Architecture
supplemented	O
with	O
extended	O
state	O
variables	O
are	O
called	O
extended	O
state	B-Architecture
machines	I-Architecture
and	O
UML	B-Application
state	I-Application
machines	I-Application
belong	O
to	O
this	O
category	O
.	O
</s>
<s>
Extended	O
state	B-Architecture
machines	I-Architecture
can	O
apply	O
the	O
underlying	O
formalism	O
to	O
much	O
more	O
complex	O
problems	O
than	O
is	O
practical	O
without	O
including	O
extended	O
state	O
variables	O
.	O
</s>
<s>
For	O
example	O
,	O
if	O
we	O
have	O
to	O
implement	O
some	O
kind	O
of	O
limit	O
in	O
our	O
FSM	O
(	O
say	O
,	O
limiting	O
number	O
of	O
keystrokes	O
on	O
keyboard	O
to	O
1000	O
)	O
,	O
without	O
extended	O
state	O
we	O
'd	O
need	O
to	O
create	O
and	O
process	O
1000	O
states	O
-	O
which	O
is	O
not	O
practical	O
;	O
however	O
,	O
with	O
an	O
extended	O
state	B-Architecture
machine	I-Architecture
we	O
can	O
introduce	O
a	O
key_count	O
variable	O
,	O
which	O
is	O
initialized	O
to	O
1000	O
and	O
decremented	O
by	O
every	O
keystroke	O
without	O
changing	O
state	O
variable	O
.	O
</s>
<s>
The	O
state	B-Application
diagram	I-Application
from	O
Figure	O
2	O
is	O
an	O
example	O
of	O
an	O
extended	O
state	B-Architecture
machine	I-Architecture
,	O
in	O
which	O
the	O
complete	O
condition	O
of	O
the	O
system	O
(	O
called	O
the	O
extended	O
state	O
)	O
is	O
the	O
combination	O
of	O
a	O
qualitative	O
aspect	O
—	O
the	O
state	O
variable	O
—	O
and	O
the	O
quantitative	O
aspects	O
—	O
the	O
extended	O
state	O
variables	O
.	O
</s>
<s>
The	O
obvious	O
advantage	O
of	O
extended	O
state	B-Architecture
machines	I-Architecture
is	O
flexibility	O
.	O
</s>
<s>
For	O
example	O
,	O
changing	O
the	O
limit	O
governed	O
by	O
key_count	O
from	O
1000	O
to	O
10000	O
keystrokes	O
,	O
would	O
not	O
complicate	O
the	O
extended	O
state	B-Architecture
machine	I-Architecture
at	O
all	O
.	O
</s>
<s>
This	O
flexibility	O
of	O
extended	O
state	B-Architecture
machines	I-Architecture
comes	O
with	O
a	O
price	O
,	O
however	O
,	O
because	O
of	O
the	O
complex	O
coupling	O
between	O
the	O
"	O
qualitative	O
"	O
and	O
the	O
"	O
quantitative	O
"	O
aspects	O
of	O
the	O
extended	O
state	O
.	O
</s>
<s>
Guard	O
conditions	O
affect	O
the	O
behavior	O
of	O
a	O
state	B-Architecture
machine	I-Architecture
by	O
enabling	O
actions	O
or	O
transitions	O
only	O
when	O
they	O
evaluate	O
to	O
TRUE	O
and	O
disabling	O
them	O
when	O
they	O
evaluate	O
to	O
FALSE	O
.	O
</s>
<s>
In	O
the	O
UML	B-Language
notation	O
,	O
guard	O
conditions	O
are	O
shown	O
in	O
square	O
brackets	O
(	O
e.g.	O
,	O
[	O
key_count	O
==	O
0 ]	O
in	O
Figure	O
2	O
)	O
.	O
</s>
<s>
The	O
need	O
for	O
guards	O
is	O
the	O
immediate	O
consequence	O
of	O
adding	O
memory	O
extended	O
state	O
variables	O
to	O
the	O
state	B-Architecture
machine	I-Architecture
formalism	O
.	O
</s>
<s>
When	O
an	O
event	O
instance	O
is	O
dispatched	O
,	O
the	O
state	B-Architecture
machine	I-Architecture
responds	O
by	O
performing	O
actions	O
,	O
such	O
as	O
changing	O
a	O
variable	O
,	O
performing	O
I/O	O
,	O
invoking	O
a	O
function	O
,	O
generating	O
another	O
event	O
instance	O
,	O
or	O
changing	O
to	O
another	O
state	O
.	O
</s>
<s>
In	O
extended	O
state	B-Architecture
machines	I-Architecture
,	O
a	O
transition	O
can	O
have	O
a	O
guard	O
,	O
which	O
means	O
that	O
the	O
transition	O
can	O
"	O
fire	O
"	O
only	O
if	O
the	O
guard	O
evaluates	O
to	O
TRUE	O
.	O
</s>
<s>
The	O
UML	B-Language
specification	O
intentionally	O
does	O
not	O
stipulate	O
any	O
particular	O
order	O
;	O
rather	O
,	O
UML	B-Language
puts	O
the	O
burden	O
on	O
the	O
designer	O
to	O
devise	O
guards	O
in	O
such	O
a	O
way	O
that	O
the	O
order	O
of	O
their	O
evaluation	O
does	O
not	O
matter	O
.	O
</s>
<s>
All	O
state	B-Architecture
machine	I-Architecture
formalisms	O
,	O
including	O
UML	B-Application
state	I-Application
machines	I-Application
,	O
universally	O
assume	O
that	O
a	O
state	B-Architecture
machine	I-Architecture
completes	O
processing	O
of	O
each	O
event	O
before	O
it	O
can	O
start	O
processing	O
the	O
next	O
event	O
.	O
</s>
<s>
This	O
model	O
of	O
execution	O
is	O
called	O
run	B-Algorithm
to	I-Algorithm
completion	I-Algorithm
,	O
or	O
RTC	O
.	O
</s>
<s>
New	O
incoming	O
events	O
cannot	O
interrupt	O
the	O
processing	O
of	O
the	O
current	O
event	O
and	O
must	O
be	O
stored	O
(	O
typically	O
in	O
an	O
event	B-Operating_System
queue	I-Operating_System
)	O
until	O
the	O
state	B-Architecture
machine	I-Architecture
becomes	O
idle	O
again	O
.	O
</s>
<s>
These	O
semantics	O
completely	O
avoid	O
any	O
internal	O
concurrency	O
issues	O
within	O
a	O
single	O
state	B-Architecture
machine	I-Architecture
.	O
</s>
<s>
The	O
RTC	O
model	O
also	O
gets	O
around	O
the	O
conceptual	O
problem	O
of	O
processing	O
actions	O
associated	O
with	O
transitions	O
,	O
where	O
the	O
state	B-Architecture
machine	I-Architecture
is	O
not	O
in	O
a	O
well-defined	O
state	O
(	O
is	O
between	O
two	O
states	O
)	O
for	O
the	O
duration	O
of	O
the	O
action	O
.	O
</s>
<s>
Note	O
,	O
however	O
,	O
that	O
RTC	O
does	O
not	O
mean	O
that	O
a	O
state	B-Architecture
machine	I-Architecture
has	O
to	O
monopolize	O
the	O
CPU	O
until	O
the	O
RTC	O
step	O
is	O
complete	O
.	O
</s>
<s>
The	O
preemption	B-Operating_System
restriction	O
only	O
applies	O
to	O
the	O
task	O
context	O
of	O
the	O
state	B-Architecture
machine	I-Architecture
that	O
is	O
already	O
busy	O
processing	O
events	O
.	O
</s>
<s>
In	O
a	O
multitasking	B-Operating_System
environment	I-Operating_System
,	O
other	O
tasks	O
(	O
not	O
related	O
to	O
the	O
task	O
context	O
of	O
the	O
busy	O
state	B-Architecture
machine	I-Architecture
)	O
can	O
be	O
running	O
,	O
possibly	O
preempting	O
the	O
currently	O
executing	O
state	B-Architecture
machine	I-Architecture
.	O
</s>
<s>
As	O
long	O
as	O
other	O
state	B-Architecture
machines	I-Architecture
do	O
not	O
share	O
variables	O
or	O
other	O
resources	O
with	O
each	O
other	O
,	O
there	O
are	O
no	O
concurrency	O
hazards	O
.	O
</s>
<s>
Its	O
biggest	O
disadvantage	O
is	O
that	O
the	O
responsiveness	O
of	O
a	O
state	B-Architecture
machine	I-Architecture
is	O
determined	O
by	O
its	O
longest	O
RTC	O
step	O
.	O
</s>
<s>
Though	O
the	O
traditional	B-Architecture
FSMs	I-Architecture
are	O
an	O
excellent	O
tool	O
for	O
tackling	O
smaller	O
problems	O
,	O
it	O
's	O
also	O
generally	O
known	O
that	O
they	O
tend	O
to	O
become	O
unmanageable	O
,	O
even	O
for	O
moderately	O
involved	O
systems	O
.	O
</s>
<s>
This	O
happens	O
because	O
the	O
traditional	O
state	B-Architecture
machine	I-Architecture
formalism	O
inflicts	O
repetitions	O
.	O
</s>
<s>
What	O
's	O
missing	O
in	O
the	O
traditional	O
state	B-Architecture
machines	I-Architecture
is	O
the	O
mechanism	O
for	O
factoring	O
out	O
the	O
common	O
behavior	O
in	O
order	O
to	O
share	O
it	O
across	O
many	O
states	O
.	O
</s>
<s>
UML	B-Application
state	I-Application
machines	I-Application
address	O
exactly	O
this	O
shortcoming	O
of	O
the	O
conventional	O
FSMs	O
.	O
</s>
<s>
They	O
provide	O
a	O
number	O
of	O
features	O
for	O
eliminating	O
the	O
repetitions	O
so	O
that	O
the	O
complexity	O
of	O
a	O
UML	B-Application
state	I-Application
machine	I-Application
no	O
longer	O
explodes	O
but	O
tends	O
to	O
faithfully	O
represent	O
the	O
complexity	O
of	O
the	O
reactive	O
system	O
it	O
describes	O
.	O
</s>
<s>
Obviously	O
,	O
these	O
features	O
are	O
very	O
interesting	O
to	O
software	O
developers	O
,	O
because	O
only	O
they	O
make	O
the	O
whole	O
state	B-Architecture
machine	I-Architecture
approach	O
truly	O
applicable	O
to	O
real-life	O
problems	O
.	O
</s>
<s>
The	O
most	O
important	O
innovation	O
of	O
UML	B-Application
state	I-Application
machines	I-Application
over	O
the	O
traditional	B-Architecture
FSMs	I-Architecture
is	O
the	O
introduction	O
of	O
hierarchically	O
nested	O
states	O
(	O
that	O
is	O
why	O
statecharts	B-Application
are	O
also	O
called	O
hierarchical	B-Application
state	I-Application
machines	I-Application
,	O
or	O
HSMs	O
)	O
.	O
</s>
<s>
This	O
state	B-Architecture
machine	I-Architecture
will	O
attempt	O
to	O
handle	O
any	O
event	O
in	O
the	O
context	O
of	O
the	O
substate	O
,	O
which	O
conceptually	O
is	O
at	O
the	O
lower	O
level	O
of	O
the	O
hierarchy	O
.	O
</s>
<s>
However	O
,	O
if	O
the	O
substate	O
"	O
result	O
"	O
does	O
not	O
prescribe	O
how	O
to	O
handle	O
the	O
event	O
,	O
the	O
event	O
is	O
not	O
quietly	O
discarded	O
as	O
in	O
a	O
traditional	O
"	O
flat	O
"	O
state	B-Architecture
machine	I-Architecture
;	O
rather	O
,	O
it	O
is	O
automatically	O
handled	O
at	O
the	O
higher	O
level	O
context	O
of	O
the	O
superstate	O
"	O
on	O
"	O
.	O
</s>
<s>
Because	O
the	O
internal	O
structure	O
of	O
a	O
composite	O
state	O
can	O
be	O
arbitrarily	O
complex	O
,	O
any	O
hierarchical	B-Application
state	I-Application
machine	I-Application
can	O
be	O
viewed	O
as	O
an	O
internal	O
structure	O
of	O
some	O
(	O
higher-level	O
)	O
composite	O
state	O
.	O
</s>
<s>
It	O
is	O
conceptually	O
convenient	O
to	O
define	O
one	O
composite	O
state	O
as	O
the	O
ultimate	O
root	O
of	O
state	B-Architecture
machine	I-Architecture
hierarchy	O
.	O
</s>
<s>
In	O
the	O
UML	B-Language
specification	O
,	O
every	O
state	B-Architecture
machine	I-Architecture
has	O
a	O
top	O
state	O
(	O
the	O
abstract	O
root	O
of	O
every	O
state	B-Architecture
machine	I-Architecture
hierarchy	O
)	O
,	O
which	O
contains	O
all	O
the	O
other	O
elements	O
of	O
the	O
entire	O
state	B-Architecture
machine	I-Architecture
.	O
</s>
<s>
The	O
aspect	O
of	O
state	O
hierarchy	O
emphasized	O
most	O
often	O
is	O
abstraction	B-Application
—	O
an	O
old	O
and	O
powerful	O
technique	O
for	O
coping	O
with	O
complexity	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
hierarchical	B-Application
state	I-Application
machine	I-Application
representing	O
the	O
pocket	O
calculator	O
(	O
Figure	O
3	O
)	O
avoids	O
repeating	O
the	O
transitions	O
Clear	O
and	O
Off	O
in	O
virtually	O
every	O
state	O
.	O
</s>
<s>
As	O
the	O
modeled	O
system	O
grows	O
,	O
the	O
opportunity	O
for	O
reuse	O
also	O
increases	O
and	O
thus	O
potentially	O
counteracts	O
the	O
disproportionate	O
increase	O
in	O
numbers	O
of	O
states	O
and	O
transitions	O
typical	O
of	O
traditional	B-Architecture
FSMs	I-Architecture
.	O
</s>
<s>
UML	B-Application
statecharts	I-Application
also	O
introduce	O
the	O
complementary	O
AND-decomposition	O
.	O
</s>
<s>
Therefore	O
,	O
UML	B-Application
statecharts	I-Application
provide	O
a	O
number	O
of	O
ways	O
for	O
orthogonal	O
regions	O
to	O
communicate	O
and	O
synchronize	O
their	O
behaviors	O
.	O
</s>
<s>
Even	O
though	O
orthogonal	O
regions	O
imply	O
independence	O
of	O
execution	O
(	O
allowing	O
more	O
or	O
less	O
concurrency	O
)	O
,	O
the	O
UML	B-Language
specification	O
does	O
not	O
require	O
that	O
a	O
separate	O
thread	O
of	O
execution	O
be	O
assigned	O
to	O
each	O
orthogonal	O
region	O
(	O
although	O
this	O
can	O
be	O
done	O
if	O
desired	O
)	O
.	O
</s>
<s>
The	O
UML	B-Language
specification	O
requires	O
only	O
that	O
the	O
designer	O
does	O
not	O
rely	O
on	O
any	O
particular	O
order	O
for	O
event	O
instances	O
to	O
be	O
dispatched	O
to	O
the	O
relevant	O
orthogonal	O
regions	O
.	O
</s>
<s>
Every	O
state	O
in	O
a	O
UML	B-Application
statechart	I-Application
can	O
have	O
optional	O
entry	O
actions	O
,	O
which	O
are	O
executed	O
upon	O
entry	O
to	O
a	O
state	O
,	O
as	O
well	O
as	O
optional	O
exit	O
actions	O
,	O
which	O
are	O
executed	O
upon	O
exit	O
from	O
a	O
state	O
.	O
</s>
<s>
Because	O
of	O
this	O
characteristic	O
,	O
statecharts	B-Application
behave	O
like	O
Moore	B-General_Concept
machines	I-General_Concept
.	O
</s>
<s>
The	O
UML	B-Language
notation	O
for	O
state	O
entry	O
and	O
exit	O
actions	O
is	O
to	O
place	O
the	O
reserved	O
word	O
"	O
entry	O
"	O
(	O
or	O
"	O
exit	O
"	O
)	O
in	O
the	O
state	O
right	O
below	O
the	O
name	O
compartment	O
,	O
followed	O
by	O
the	O
forward	O
slash	O
and	O
the	O
list	O
of	O
arbitrary	O
actions	O
(	O
see	O
Figure	O
5	O
)	O
.	O
</s>
<s>
The	O
value	O
of	O
entry	O
and	O
exit	O
actions	O
is	O
that	O
they	O
provide	O
means	O
for	O
guaranteed	O
initialization	O
and	O
cleanup	O
,	O
very	O
much	O
like	O
class	O
constructors	O
and	O
destructors	O
in	O
Object-oriented	B-Language
programming	I-Language
.	O
</s>
<s>
In	O
UML	B-Language
,	O
this	O
situation	O
should	O
be	O
modeled	O
with	O
internal	O
transitions	O
,	O
as	O
shown	O
in	O
Figure	O
6	O
.	O
</s>
<s>
The	O
UML	B-Language
notation	O
for	O
internal	O
transitions	O
follows	O
the	O
general	O
syntax	O
used	O
for	O
exit	O
(	O
or	O
entry	O
)	O
actions	O
,	O
except	O
instead	O
of	O
the	O
word	O
entry	O
(	O
or	O
exit	O
)	O
the	O
internal	O
transition	O
is	O
labeled	O
with	O
the	O
triggering	O
event	O
(	O
e.g.	O
,	O
see	O
the	O
internal	O
transition	O
triggered	O
by	O
the	O
ANY_KEY	O
event	O
in	O
Figure	O
6	O
)	O
.	O
</s>
<s>
In	O
fact	O
,	O
in	O
a	O
classical	O
Mealy	B-General_Concept
machine	I-General_Concept
,	O
actions	O
are	O
associated	O
exclusively	O
with	O
state	O
transitions	O
,	O
so	O
the	O
only	O
way	O
to	O
execute	O
actions	O
without	O
changing	O
state	O
is	O
through	O
a	O
self-transition	O
(	O
depicted	O
as	O
a	O
directed	O
loop	O
in	O
Figure	O
1	O
from	O
the	O
top	O
of	O
this	O
article	O
)	O
.	O
</s>
<s>
However	O
,	O
in	O
the	O
presence	O
of	O
entry	O
and	O
exit	O
actions	O
,	O
as	O
in	O
UML	B-Application
statecharts	I-Application
,	O
a	O
self-transition	O
involves	O
the	O
execution	O
of	O
exit	O
and	O
entry	O
actions	O
and	O
therefore	O
it	O
is	O
distinctively	O
different	O
from	O
an	O
internal	O
transition	O
.	O
</s>
<s>
State	O
nesting	O
combined	O
with	O
entry	O
and	O
exit	O
actions	O
significantly	O
complicates	O
the	O
state	O
transition	O
semantics	O
in	O
HSMs	O
compared	O
to	O
the	O
traditional	B-Architecture
FSMs	I-Architecture
.	O
</s>
<s>
If	O
the	O
state	B-Architecture
machine	I-Architecture
is	O
in	O
a	O
leaf	O
state	O
that	O
is	O
contained	O
in	O
a	O
composite	O
state	O
(	O
which	O
is	O
possibly	O
contained	O
in	O
a	O
higher-level	O
composite	O
state	O
,	O
and	O
so	O
on	O
)	O
,	O
all	O
the	O
composite	O
states	O
that	O
either	O
directly	O
or	O
transitively	O
contain	O
the	O
leaf	O
state	O
are	O
also	O
active	O
.	O
</s>
<s>
The	O
UML	B-Language
specification	O
refers	O
to	O
such	O
a	O
state	O
tree	O
as	O
state	O
configuration	O
.	O
</s>
<s>
In	O
UML	B-Language
,	O
a	O
state	O
transition	O
can	O
directly	O
connect	O
any	O
two	O
states	O
.	O
</s>
<s>
The	O
UML	B-Language
specification	O
prescribes	O
that	O
taking	O
a	O
state	O
transition	O
involves	O
executing	O
the	O
following	O
actions	O
in	O
the	O
following	O
sequence	O
(	O
see	O
Section	O
15.3.14	O
in	O
OMG	O
Unified	B-Language
Modeling	I-Language
Language	I-Language
(	O
OMG	O
UML	B-Language
)	O
,	O
Infrastructure	O
Version	O
2.2	O
)	O
:	O
</s>
<s>
For	O
example	O
,	O
transition	O
T1	O
shown	O
in	O
Figure	O
7	O
causes	O
the	O
evaluation	O
of	O
the	O
guard	O
g( )	O
;	O
followed	O
by	O
the	O
sequence	O
of	O
actions	O
:	O
a( )	O
;	O
b( )	O
;	O
t( )	O
;	O
c( )	O
;	O
d( )	O
;	O
and	O
e( )	O
;	O
assuming	O
that	O
the	O
guard	O
g( )	O
evaluates	O
to	O
TRUE	O
.	O
</s>
<s>
The	O
UML	B-Language
specification	O
prescribes	O
that	O
a	O
transition	O
involves	O
exiting	O
all	O
nested	O
states	O
from	O
the	O
current	O
active	O
state	O
(	O
which	O
might	O
be	O
a	O
direct	O
or	O
transitive	O
substate	O
of	O
the	O
main	O
source	O
state	O
)	O
up	O
to	O
,	O
but	O
not	O
including	O
,	O
the	O
least	O
common	O
ancestor	O
(	O
LCA	O
)	O
state	O
of	O
the	O
main	O
source	O
and	O
main	O
target	O
states	O
.	O
</s>
<s>
If	O
the	O
main	O
target	O
state	O
is	O
composite	O
,	O
the	O
UML	B-Language
semantics	O
prescribes	O
to	O
"	O
drill	O
"	O
into	O
its	O
submachine	O
recursively	O
using	O
the	O
local	O
initial	O
transitions	O
.	O
</s>
<s>
Before	O
UML	B-Language
2	I-Language
,	O
the	O
only	O
transition	O
semantics	O
in	O
use	O
was	O
the	O
external	O
transition	O
,	O
in	O
which	O
the	O
main	O
source	O
of	O
the	O
transition	O
is	O
always	O
exited	O
and	O
the	O
main	O
target	O
of	O
the	O
transition	O
is	O
always	O
entered	O
.	O
</s>
<s>
UML	B-Language
2	I-Language
preserved	O
the	O
"	O
external	O
transition	O
"	O
semantics	O
for	O
backward	O
compatibility	O
,	O
but	O
introduced	O
also	O
a	O
new	O
kind	O
of	O
transition	O
called	O
local	O
transition	O
(	O
see	O
Section	O
15.3.15	O
in	O
Unified	B-Language
Modeling	I-Language
Language	I-Language
(	O
UML	B-Language
)	O
,	O
Infrastructure	O
Version	O
2.2	O
)	O
.	O
</s>
<s>
Sometimes	O
an	O
event	O
arrives	O
at	O
a	O
particularly	O
inconvenient	O
time	O
,	O
when	O
a	O
state	B-Architecture
machine	I-Architecture
is	O
in	O
a	O
state	O
that	O
cannot	O
handle	O
the	O
event	O
.	O
</s>
<s>
UML	B-Application
state	I-Application
machines	I-Application
provide	O
a	O
special	O
mechanism	O
for	O
deferring	O
events	O
in	O
states	O
.	O
</s>
<s>
Upon	O
entry	O
to	O
such	O
a	O
state	O
,	O
the	O
UML	B-Application
state	I-Application
machine	I-Application
will	O
automatically	O
recall	O
any	O
saved	O
event(s )	O
that	O
are	O
no	O
longer	O
deferred	O
and	O
will	O
then	O
either	O
consume	O
or	O
discard	O
these	O
events	O
.	O
</s>
<s>
Consistent	O
with	O
other	O
areas	O
in	O
the	O
specification	O
of	O
UML	B-Application
state	I-Application
machines	I-Application
,	O
the	O
substate	O
takes	O
precedence	O
over	O
the	O
superstate	O
,	O
the	O
event	O
will	O
be	O
deferred	O
and	O
the	O
transition	O
for	O
the	O
superstate	O
will	O
not	O
be	O
executed	O
.	O
</s>
<s>
Harel	O
statecharts	B-Application
,	O
which	O
are	O
the	O
precursors	O
of	O
UML	B-Application
state	I-Application
machines	I-Application
,	O
have	O
been	O
invented	O
as	O
"	O
a	O
visual	O
formalism	O
for	O
complex	O
systems	O
"	O
,	O
so	O
from	O
their	O
inception	O
,	O
they	O
have	O
been	O
inseparably	O
associated	O
with	O
graphical	O
representation	O
in	O
the	O
form	O
of	O
state	B-Application
diagrams	I-Application
.	O
</s>
<s>
However	O
,	O
it	O
is	O
important	O
to	O
understand	O
that	O
the	O
concept	O
of	O
UML	B-Application
state	I-Application
machine	I-Application
transcends	O
any	O
particular	O
notation	O
,	O
graphical	O
or	O
textual	O
.	O
</s>
<s>
The	O
UML	B-Language
specification	O
makes	O
this	O
distinction	O
apparent	O
by	O
clearly	O
separating	O
state	B-Architecture
machine	I-Architecture
semantics	O
from	O
the	O
notation	O
.	O
</s>
<s>
However	O
,	O
the	O
notation	O
of	O
UML	B-Application
statecharts	I-Application
is	O
not	O
purely	O
visual	O
.	O
</s>
<s>
Any	O
nontrivial	O
state	B-Architecture
machine	I-Architecture
requires	O
a	O
large	O
amount	O
of	O
textual	O
information	O
(	O
e.g.	O
,	O
the	O
specification	O
of	O
actions	O
and	O
guards	O
)	O
.	O
</s>
<s>
The	O
exact	O
syntax	O
of	O
action	O
and	O
guard	O
expressions	O
is	O
n't	O
defined	O
in	O
the	O
UML	B-Language
specification	O
,	O
so	O
many	O
people	O
use	O
either	O
structured	O
English	O
or	O
,	O
more	O
formally	O
,	O
expressions	O
in	O
an	O
implementation	O
language	O
such	O
as	O
C	B-Language
,	O
C++	B-Language
,	O
or	O
Java	B-Language
.	O
</s>
<s>
In	O
practice	O
,	O
this	O
means	O
that	O
UML	B-Application
statechart	I-Application
notation	O
depends	O
heavily	O
on	O
the	O
specific	O
programming	O
language	O
.	O
</s>
<s>
Nevertheless	O
,	O
most	O
of	O
the	O
statecharts	B-Application
semantics	O
are	O
heavily	O
biased	O
toward	O
graphical	O
notation	O
.	O
</s>
<s>
For	O
example	O
,	O
state	B-Application
diagrams	I-Application
poorly	O
represent	O
the	O
sequence	O
of	O
processing	O
,	O
be	O
it	O
order	O
of	O
evaluation	O
of	O
guards	O
or	O
order	O
of	O
dispatching	O
events	O
to	O
orthogonal	O
regions	O
.	O
</s>
<s>
The	O
UML	B-Language
specification	O
sidesteps	O
these	O
problems	O
by	O
putting	O
the	O
burden	O
on	O
the	O
designer	O
not	O
to	O
rely	O
on	O
any	O
particular	O
sequencing	O
.	O
</s>
<s>
However	O
,	O
it	O
is	O
the	O
case	O
that	O
when	O
UML	B-Application
state	I-Application
machines	I-Application
are	O
actually	O
implemented	O
,	O
there	O
is	O
inevitably	O
full	O
control	O
over	O
order	O
of	O
execution	O
,	O
giving	O
rise	O
to	O
criticism	O
that	O
the	O
UML	B-Language
semantics	O
may	O
be	O
unnecessarily	O
restrictive	O
.	O
</s>
<s>
Similarly	O
,	O
statechart	B-Application
diagrams	O
require	O
a	O
lot	O
of	O
plumbing	O
gear	O
(	O
pseudostates	O
,	O
like	O
joins	O
,	O
forks	O
,	O
junctions	O
,	O
choicepoints	O
,	O
etc	O
.	O
)	O
</s>
<s>
In	O
other	O
words	O
,	O
these	O
elements	O
of	O
the	O
graphical	O
notation	O
do	O
not	O
add	O
much	O
value	O
in	O
representing	O
flow	O
of	O
control	O
as	O
compared	O
to	O
plain	O
structured	B-Language
code	I-Language
.	O
</s>
<s>
The	O
UML	B-Language
notation	O
and	O
semantics	O
are	O
really	O
geared	O
toward	O
computerized	O
UML	B-Protocol
tools	I-Protocol
.	O
</s>
<s>
A	O
UML	B-Application
state	I-Application
machine	I-Application
,	O
as	O
represented	O
in	O
a	O
tool	O
,	O
is	O
not	O
just	O
the	O
state	B-Application
diagram	I-Application
,	O
but	O
rather	O
a	O
mixture	O
of	O
graphical	O
and	O
textual	O
representation	O
that	O
precisely	O
captures	O
both	O
the	O
state	O
topology	O
and	O
the	O
actions	O
.	O
</s>
<s>
The	O
users	O
of	O
the	O
tool	O
can	O
get	O
several	O
complementary	O
views	O
of	O
the	O
same	O
state	B-Architecture
machine	I-Architecture
,	O
both	O
visual	O
and	O
textual	O
,	O
whereas	O
the	O
generated	O
code	O
is	O
just	O
one	O
of	O
the	O
many	O
available	O
views	O
.	O
</s>
