State Machine representation in UCDF
What is a State Machine?
Main function of a state machine is "changing of states". What does
it mean "change of a state"? It means the underlying system is
changed. It becomes new in some way, but not completely new.
Control and data flows may take new paths, new code blocks are
executed, and new data objects may be involved.
Therefore, a state is an equivalent to a module. A module spanning
across possibly many parts of the code.
state X
This "module" is not explicitly defined in programming languages
like C++. It includes all control flows which are executed,
if the system is in that state, but it does not include control paths
which are not executed. This can be shown by control lines
drawn to various control blocks already existing in the diagram.
What is a state transition?
Change of state is the result of a process. It can be shown in
the same way as for a manufacturing process when the process is
an action block (code in software) and the state is a product
(dataflow content in software) and the flow is shown as an object flow
(dataflow in software).
Process
of transition
State A
State B
How a state transition
is shown?
State A
State B
The transition may be shown as the special "Transition" line. State
blocks do not have to be shown with dashed lines to distinguish the
block lines from the transition line.
The source
of a state transition
State A
State B
The cause of a state change is shown by the "valve" block [X]
and a control line from a module which initiates the change.
X
source of
state transition
main code parsing
state machine switcher
stMainCode
The first step in explicit definition of a state machine
is introduction of a state variable
X
Condition Switcher
"\n\r \t"
(c>='!' and c<='/') ||
(c>=':' and c<='@') ||
(c>='[' and c<='^') ||
(c>='{' and c<='~') ||
(c=='`')
(c>='a' && c<='z') ||
(c=='_') ||
(c>='0' && c<='9' ) ||
c>='A' && c<='Z'
X
X
X
X
X
X
character flow
X
_OperatorOrPunctuator
_LetterOrDigit
_Space
Conditions
of state transitions
On the top level, switching
can be shown with an "if" block.
As life shows, actual if-statements
switching the states
can be discovered anywhere in code.
Informational content of the control flow.
It is suitable for a switcher description.
Indeed, what a particular control flow line means?
It means that a certain condition was met.
enum { _Space, _OperatorOrPunctuator, _LetterOrDigit}
action on a
first letter or digit
1
Actions on transitions from a particular state
to another state are on the same control line
which switches states.
In practice, these actions may control
other state machines, especially those which work
on "rising or falling edge".
Action on transition
Example
Actions on enter or exit from a particular
state can be shown here as well.
On leaving _Space
On transition
from _Space to _LetterOrDigit
On entering _LetterOrDigit
1
2
3
Action in a state
In practice, actions in a state may happen
in any and many pieces of code when a condition
recognizing the state is satisfied. The example
shows it when the state variable is used.
Condition Switcher
stMainCode
stMaincode=_LetterOrDigit;
stMainCode==_LetterOrDigit
stMainCode!=_LetterOrDigit
A control line from the state box shows that
this control flow belongs to the state.
Thus all relevant code can be traced.
The word _Space here is not
an enum value which stMainCode can take.
It is the state name and can be any.
That is how in general the cause-effect diagram can be made:
what is the cause, what was before, what is after - three things.
Action on entry or exit
Actions on entry or exit can be shown in
a similar manner through submodules
of the state module. Note, that just as the state machine
itself, the entry module may not be explicitly
defined, and entry actions may be scattered around the code,
but UCDF allows you to trace it.
on entry to
_LetterOrDigit
on entry to
_LetterOrDigit
1
2
States are modules. A complete diagram of every state
may be shown!
The concepts of a state and its change are very valuable. We have
to find a way to show it on the real-code software diagram.
Programmers may define state-machines and states explicity.
But still the code may span across many classes, functions, and files.
Besides, there is much code where it is not defined explicity,
still, state machines are there, "invisible".
X
Next
has
has
THE SAME
has
THE SAME