162306a36Sopenharmony_ciDeterministic Automata
262306a36Sopenharmony_ci======================
362306a36Sopenharmony_ci
462306a36Sopenharmony_ciFormally, a deterministic automaton, denoted by G, is defined as a quintuple:
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci        *G* = { *X*, *E*, *f*, x\ :subscript:`0`, X\ :subscript:`m` }
762306a36Sopenharmony_ci
862306a36Sopenharmony_ciwhere:
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci- *X* is the set of states;
1162306a36Sopenharmony_ci- *E* is the finite set of events;
1262306a36Sopenharmony_ci- x\ :subscript:`0` is the initial state;
1362306a36Sopenharmony_ci- X\ :subscript:`m` (subset of *X*) is the set of marked (or final) states.
1462306a36Sopenharmony_ci- *f* : *X* x *E* -> *X* $ is the transition function. It defines the state
1562306a36Sopenharmony_ci  transition in the occurrence of an event from *E* in the state *X*. In the
1662306a36Sopenharmony_ci  special case of deterministic automata, the occurrence of the event in *E*
1762306a36Sopenharmony_ci  in a state in *X* has a deterministic next state from *X*.
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ciFor example, a given automaton named 'wip' (wakeup in preemptive) can
2062306a36Sopenharmony_cibe defined as:
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci- *X* = { ``preemptive``, ``non_preemptive``}
2362306a36Sopenharmony_ci- *E* = { ``preempt_enable``, ``preempt_disable``, ``sched_waking``}
2462306a36Sopenharmony_ci- x\ :subscript:`0` = ``preemptive``
2562306a36Sopenharmony_ci- X\ :subscript:`m` = {``preemptive``}
2662306a36Sopenharmony_ci- *f* =
2762306a36Sopenharmony_ci   - *f*\ (``preemptive``, ``preempt_disable``) = ``non_preemptive``
2862306a36Sopenharmony_ci   - *f*\ (``non_preemptive``, ``sched_waking``) = ``non_preemptive``
2962306a36Sopenharmony_ci   - *f*\ (``non_preemptive``, ``preempt_enable``) = ``preemptive``
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ciOne of the benefits of this formal definition is that it can be presented
3262306a36Sopenharmony_ciin multiple formats. For example, using a *graphical representation*, using
3362306a36Sopenharmony_civertices (nodes) and edges, which is very intuitive for *operating system*
3462306a36Sopenharmony_cipractitioners, without any loss.
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciThe previous 'wip' automaton can also be represented as::
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci                       preempt_enable
3962306a36Sopenharmony_ci          +---------------------------------+
4062306a36Sopenharmony_ci          v                                 |
4162306a36Sopenharmony_ci        #============#  preempt_disable   +------------------+
4262306a36Sopenharmony_ci    --> H preemptive H -----------------> |  non_preemptive  |
4362306a36Sopenharmony_ci        #============#                    +------------------+
4462306a36Sopenharmony_ci                                            ^              |
4562306a36Sopenharmony_ci                                            | sched_waking |
4662306a36Sopenharmony_ci                                            +--------------+
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ciDeterministic Automaton in C
4962306a36Sopenharmony_ci----------------------------
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ciIn the paper "Efficient formal verification for the Linux kernel",
5262306a36Sopenharmony_cithe authors present a simple way to represent an automaton in C that can
5362306a36Sopenharmony_cibe used as regular code in the Linux kernel.
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ciFor example, the 'wip' automata can be presented as (augmented with comments)::
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci  /* enum representation of X (set of states) to be used as index */
5862306a36Sopenharmony_ci  enum states {
5962306a36Sopenharmony_ci	preemptive = 0,
6062306a36Sopenharmony_ci	non_preemptive,
6162306a36Sopenharmony_ci	state_max
6262306a36Sopenharmony_ci  };
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci  #define INVALID_STATE state_max
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci  /* enum representation of E (set of events) to be used as index */
6762306a36Sopenharmony_ci  enum events {
6862306a36Sopenharmony_ci	preempt_disable = 0,
6962306a36Sopenharmony_ci	preempt_enable,
7062306a36Sopenharmony_ci	sched_waking,
7162306a36Sopenharmony_ci	event_max
7262306a36Sopenharmony_ci  };
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci  struct automaton {
7562306a36Sopenharmony_ci	char *state_names[state_max];                   // X: the set of states
7662306a36Sopenharmony_ci	char *event_names[event_max];                   // E: the finite set of events
7762306a36Sopenharmony_ci	unsigned char function[state_max][event_max];   // f: transition function
7862306a36Sopenharmony_ci	unsigned char initial_state;                    // x_0: the initial state
7962306a36Sopenharmony_ci	bool final_states[state_max];                   // X_m: the set of marked states
8062306a36Sopenharmony_ci  };
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci  struct automaton aut = {
8362306a36Sopenharmony_ci	.state_names = {
8462306a36Sopenharmony_ci		"preemptive",
8562306a36Sopenharmony_ci		"non_preemptive"
8662306a36Sopenharmony_ci	},
8762306a36Sopenharmony_ci	.event_names = {
8862306a36Sopenharmony_ci		"preempt_disable",
8962306a36Sopenharmony_ci		"preempt_enable",
9062306a36Sopenharmony_ci		"sched_waking"
9162306a36Sopenharmony_ci	},
9262306a36Sopenharmony_ci	.function = {
9362306a36Sopenharmony_ci		{ non_preemptive,  INVALID_STATE,  INVALID_STATE },
9462306a36Sopenharmony_ci		{  INVALID_STATE,     preemptive, non_preemptive },
9562306a36Sopenharmony_ci	},
9662306a36Sopenharmony_ci	.initial_state = preemptive,
9762306a36Sopenharmony_ci	.final_states = { 1, 0 },
9862306a36Sopenharmony_ci  };
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ciThe *transition function* is represented as a matrix of states (lines) and
10162306a36Sopenharmony_cievents (columns), and so the function *f* : *X* x *E* -> *X* can be solved
10262306a36Sopenharmony_ciin O(1). For example::
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci  next_state = automaton_wip.function[curr_state][event];
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ciGraphviz .dot format
10762306a36Sopenharmony_ci--------------------
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ciThe Graphviz open-source tool can produce the graphical representation
11062306a36Sopenharmony_ciof an automaton using the (textual) DOT language as the source code.
11162306a36Sopenharmony_ciThe DOT format is widely used and can be converted to many other formats.
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ciFor example, this is the 'wip' model in DOT::
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci  digraph state_automaton {
11662306a36Sopenharmony_ci        {node [shape = circle] "non_preemptive"};
11762306a36Sopenharmony_ci        {node [shape = plaintext, style=invis, label=""] "__init_preemptive"};
11862306a36Sopenharmony_ci        {node [shape = doublecircle] "preemptive"};
11962306a36Sopenharmony_ci        {node [shape = circle] "preemptive"};
12062306a36Sopenharmony_ci        "__init_preemptive" -> "preemptive";
12162306a36Sopenharmony_ci        "non_preemptive" [label = "non_preemptive"];
12262306a36Sopenharmony_ci        "non_preemptive" -> "non_preemptive" [ label = "sched_waking" ];
12362306a36Sopenharmony_ci        "non_preemptive" -> "preemptive" [ label = "preempt_enable" ];
12462306a36Sopenharmony_ci        "preemptive" [label = "preemptive"];
12562306a36Sopenharmony_ci        "preemptive" -> "non_preemptive" [ label = "preempt_disable" ];
12662306a36Sopenharmony_ci        { rank = min ;
12762306a36Sopenharmony_ci                "__init_preemptive";
12862306a36Sopenharmony_ci                "preemptive";
12962306a36Sopenharmony_ci        }
13062306a36Sopenharmony_ci  }
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ciThis DOT format can be transformed into a bitmap or vectorial image
13362306a36Sopenharmony_ciusing the dot utility, or into an ASCII art using graph-easy. For
13462306a36Sopenharmony_ciinstance::
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci  $ dot -Tsvg -o wip.svg wip.dot
13762306a36Sopenharmony_ci  $ graph-easy wip.dot > wip.txt
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_cidot2c
14062306a36Sopenharmony_ci-----
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_cidot2c is a utility that can parse a .dot file containing an automaton as
14362306a36Sopenharmony_ciin the example above and automatically convert it to the C representation
14462306a36Sopenharmony_cipresented in [3].
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ciFor example, having the previous 'wip' model into a file named 'wip.dot',
14762306a36Sopenharmony_cithe following command will transform the .dot file into the C
14862306a36Sopenharmony_cirepresentation (previously shown) in the 'wip.h' file::
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci  $ dot2c wip.dot > wip.h
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ciThe 'wip.h' content is the code sample in section 'Deterministic Automaton
15362306a36Sopenharmony_ciin C'.
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ciRemarks
15662306a36Sopenharmony_ci-------
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ciThe automata formalism allows modeling discrete event systems (DES) in
15962306a36Sopenharmony_cimultiple formats, suitable for different applications/users.
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ciFor example, the formal description using set theory is better suitable
16262306a36Sopenharmony_cifor automata operations, while the graphical format for human interpretation;
16362306a36Sopenharmony_ciand computer languages for machine execution.
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ciReferences
16662306a36Sopenharmony_ci----------
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ciMany textbooks cover automata formalism. For a brief introduction see::
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci  O'Regan, Gerard. Concise guide to software engineering. Springer,
17162306a36Sopenharmony_ci  Cham, 2017.
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ciFor a detailed description, including operations, and application on Discrete
17462306a36Sopenharmony_ciEvent Systems (DES), see::
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci  Cassandras, Christos G., and Stephane Lafortune, eds. Introduction to discrete
17762306a36Sopenharmony_ci  event systems. Boston, MA: Springer US, 2008.
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ciFor the C representation in kernel, see::
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci  De Oliveira, Daniel Bristot; Cucinotta, Tommaso; De Oliveira, Romulo
18262306a36Sopenharmony_ci  Silva. Efficient formal verification for the Linux kernel. In:
18362306a36Sopenharmony_ci  International Conference on Software Engineering and Formal Methods.
18462306a36Sopenharmony_ci  Springer, Cham, 2019. p. 315-332.
185