1*4041b9bbSDaniel Bristot de OliveiraDeterministic Automata 2*4041b9bbSDaniel Bristot de Oliveira====================== 3*4041b9bbSDaniel Bristot de Oliveira 4*4041b9bbSDaniel Bristot de OliveiraFormally, a deterministic automaton, denoted by G, is defined as a quintuple: 5*4041b9bbSDaniel Bristot de Oliveira 6*4041b9bbSDaniel Bristot de Oliveira *G* = { *X*, *E*, *f*, x\ :subscript:`0`, X\ :subscript:`m` } 7*4041b9bbSDaniel Bristot de Oliveira 8*4041b9bbSDaniel Bristot de Oliveirawhere: 9*4041b9bbSDaniel Bristot de Oliveira 10*4041b9bbSDaniel Bristot de Oliveira- *X* is the set of states; 11*4041b9bbSDaniel Bristot de Oliveira- *E* is the finite set of events; 12*4041b9bbSDaniel Bristot de Oliveira- x\ :subscript:`0` is the initial state; 13*4041b9bbSDaniel Bristot de Oliveira- X\ :subscript:`m` (subset of *X*) is the set of marked (or final) states. 14*4041b9bbSDaniel Bristot de Oliveira- *f* : *X* x *E* -> *X* $ is the transition function. It defines the state 15*4041b9bbSDaniel Bristot de Oliveira transition in the occurrence of an event from *E* in the state *X*. In the 16*4041b9bbSDaniel Bristot de Oliveira special case of deterministic automata, the occurrence of the event in *E* 17*4041b9bbSDaniel Bristot de Oliveira in a state in *X* has a deterministic next state from *X*. 18*4041b9bbSDaniel Bristot de Oliveira 19*4041b9bbSDaniel Bristot de OliveiraFor example, a given automaton named 'wip' (wakeup in preemptive) can 20*4041b9bbSDaniel Bristot de Oliveirabe defined as: 21*4041b9bbSDaniel Bristot de Oliveira 22*4041b9bbSDaniel Bristot de Oliveira- *X* = { ``preemptive``, ``non_preemptive``} 23*4041b9bbSDaniel Bristot de Oliveira- *E* = { ``preempt_enable``, ``preempt_disable``, ``sched_waking``} 24*4041b9bbSDaniel Bristot de Oliveira- x\ :subscript:`0` = ``preemptive`` 25*4041b9bbSDaniel Bristot de Oliveira- X\ :subscript:`m` = {``preemptive``} 26*4041b9bbSDaniel Bristot de Oliveira- *f* = 27*4041b9bbSDaniel Bristot de Oliveira - *f*\ (``preemptive``, ``preempt_disable``) = ``non_preemptive`` 28*4041b9bbSDaniel Bristot de Oliveira - *f*\ (``non_preemptive``, ``sched_waking``) = ``non_preemptive`` 29*4041b9bbSDaniel Bristot de Oliveira - *f*\ (``non_preemptive``, ``preempt_enable``) = ``preemptive`` 30*4041b9bbSDaniel Bristot de Oliveira 31*4041b9bbSDaniel Bristot de OliveiraOne of the benefits of this formal definition is that it can be presented 32*4041b9bbSDaniel Bristot de Oliveirain multiple formats. For example, using a *graphical representation*, using 33*4041b9bbSDaniel Bristot de Oliveiravertices (nodes) and edges, which is very intuitive for *operating system* 34*4041b9bbSDaniel Bristot de Oliveirapractitioners, without any loss. 35*4041b9bbSDaniel Bristot de Oliveira 36*4041b9bbSDaniel Bristot de OliveiraThe previous 'wip' automaton can also be represented as:: 37*4041b9bbSDaniel Bristot de Oliveira 38*4041b9bbSDaniel Bristot de Oliveira preempt_enable 39*4041b9bbSDaniel Bristot de Oliveira +---------------------------------+ 40*4041b9bbSDaniel Bristot de Oliveira v | 41*4041b9bbSDaniel Bristot de Oliveira #============# preempt_disable +------------------+ 42*4041b9bbSDaniel Bristot de Oliveira --> H preemptive H -----------------> | non_preemptive | 43*4041b9bbSDaniel Bristot de Oliveira #============# +------------------+ 44*4041b9bbSDaniel Bristot de Oliveira ^ | 45*4041b9bbSDaniel Bristot de Oliveira | sched_waking | 46*4041b9bbSDaniel Bristot de Oliveira +--------------+ 47*4041b9bbSDaniel Bristot de Oliveira 48*4041b9bbSDaniel Bristot de OliveiraDeterministic Automaton in C 49*4041b9bbSDaniel Bristot de Oliveira---------------------------- 50*4041b9bbSDaniel Bristot de Oliveira 51*4041b9bbSDaniel Bristot de OliveiraIn the paper "Efficient formal verification for the Linux kernel", 52*4041b9bbSDaniel Bristot de Oliveirathe authors present a simple way to represent an automaton in C that can 53*4041b9bbSDaniel Bristot de Oliveirabe used as regular code in the Linux kernel. 54*4041b9bbSDaniel Bristot de Oliveira 55*4041b9bbSDaniel Bristot de OliveiraFor example, the 'wip' automata can be presented as (augmented with comments):: 56*4041b9bbSDaniel Bristot de Oliveira 57*4041b9bbSDaniel Bristot de Oliveira /* enum representation of X (set of states) to be used as index */ 58*4041b9bbSDaniel Bristot de Oliveira enum states { 59*4041b9bbSDaniel Bristot de Oliveira preemptive = 0, 60*4041b9bbSDaniel Bristot de Oliveira non_preemptive, 61*4041b9bbSDaniel Bristot de Oliveira state_max 62*4041b9bbSDaniel Bristot de Oliveira }; 63*4041b9bbSDaniel Bristot de Oliveira 64*4041b9bbSDaniel Bristot de Oliveira #define INVALID_STATE state_max 65*4041b9bbSDaniel Bristot de Oliveira 66*4041b9bbSDaniel Bristot de Oliveira /* enum representation of E (set of events) to be used as index */ 67*4041b9bbSDaniel Bristot de Oliveira enum events { 68*4041b9bbSDaniel Bristot de Oliveira preempt_disable = 0, 69*4041b9bbSDaniel Bristot de Oliveira preempt_enable, 70*4041b9bbSDaniel Bristot de Oliveira sched_waking, 71*4041b9bbSDaniel Bristot de Oliveira event_max 72*4041b9bbSDaniel Bristot de Oliveira }; 73*4041b9bbSDaniel Bristot de Oliveira 74*4041b9bbSDaniel Bristot de Oliveira struct automaton { 75*4041b9bbSDaniel Bristot de Oliveira char *state_names[state_max]; // X: the set of states 76*4041b9bbSDaniel Bristot de Oliveira char *event_names[event_max]; // E: the finite set of events 77*4041b9bbSDaniel Bristot de Oliveira unsigned char function[state_max][event_max]; // f: transition function 78*4041b9bbSDaniel Bristot de Oliveira unsigned char initial_state; // x_0: the initial state 79*4041b9bbSDaniel Bristot de Oliveira bool final_states[state_max]; // X_m: the set of marked states 80*4041b9bbSDaniel Bristot de Oliveira }; 81*4041b9bbSDaniel Bristot de Oliveira 82*4041b9bbSDaniel Bristot de Oliveira struct automaton aut = { 83*4041b9bbSDaniel Bristot de Oliveira .state_names = { 84*4041b9bbSDaniel Bristot de Oliveira "preemptive", 85*4041b9bbSDaniel Bristot de Oliveira "non_preemptive" 86*4041b9bbSDaniel Bristot de Oliveira }, 87*4041b9bbSDaniel Bristot de Oliveira .event_names = { 88*4041b9bbSDaniel Bristot de Oliveira "preempt_disable", 89*4041b9bbSDaniel Bristot de Oliveira "preempt_enable", 90*4041b9bbSDaniel Bristot de Oliveira "sched_waking" 91*4041b9bbSDaniel Bristot de Oliveira }, 92*4041b9bbSDaniel Bristot de Oliveira .function = { 93*4041b9bbSDaniel Bristot de Oliveira { non_preemptive, INVALID_STATE, INVALID_STATE }, 94*4041b9bbSDaniel Bristot de Oliveira { INVALID_STATE, preemptive, non_preemptive }, 95*4041b9bbSDaniel Bristot de Oliveira }, 96*4041b9bbSDaniel Bristot de Oliveira .initial_state = preemptive, 97*4041b9bbSDaniel Bristot de Oliveira .final_states = { 1, 0 }, 98*4041b9bbSDaniel Bristot de Oliveira }; 99*4041b9bbSDaniel Bristot de Oliveira 100*4041b9bbSDaniel Bristot de OliveiraThe *transition function* is represented as a matrix of states (lines) and 101*4041b9bbSDaniel Bristot de Oliveiraevents (columns), and so the function *f* : *X* x *E* -> *X* can be solved 102*4041b9bbSDaniel Bristot de Oliveirain O(1). For example:: 103*4041b9bbSDaniel Bristot de Oliveira 104*4041b9bbSDaniel Bristot de Oliveira next_state = automaton_wip.function[curr_state][event]; 105*4041b9bbSDaniel Bristot de Oliveira 106*4041b9bbSDaniel Bristot de OliveiraGraphviz .dot format 107*4041b9bbSDaniel Bristot de Oliveira-------------------- 108*4041b9bbSDaniel Bristot de Oliveira 109*4041b9bbSDaniel Bristot de OliveiraThe Graphviz open-source tool can produce the graphical representation 110*4041b9bbSDaniel Bristot de Oliveiraof an automaton using the (textual) DOT language as the source code. 111*4041b9bbSDaniel Bristot de OliveiraThe DOT format is widely used and can be converted to many other formats. 112*4041b9bbSDaniel Bristot de Oliveira 113*4041b9bbSDaniel Bristot de OliveiraFor example, this is the 'wip' model in DOT:: 114*4041b9bbSDaniel Bristot de Oliveira 115*4041b9bbSDaniel Bristot de Oliveira digraph state_automaton { 116*4041b9bbSDaniel Bristot de Oliveira {node [shape = circle] "non_preemptive"}; 117*4041b9bbSDaniel Bristot de Oliveira {node [shape = plaintext, style=invis, label=""] "__init_preemptive"}; 118*4041b9bbSDaniel Bristot de Oliveira {node [shape = doublecircle] "preemptive"}; 119*4041b9bbSDaniel Bristot de Oliveira {node [shape = circle] "preemptive"}; 120*4041b9bbSDaniel Bristot de Oliveira "__init_preemptive" -> "preemptive"; 121*4041b9bbSDaniel Bristot de Oliveira "non_preemptive" [label = "non_preemptive"]; 122*4041b9bbSDaniel Bristot de Oliveira "non_preemptive" -> "non_preemptive" [ label = "sched_waking" ]; 123*4041b9bbSDaniel Bristot de Oliveira "non_preemptive" -> "preemptive" [ label = "preempt_enable" ]; 124*4041b9bbSDaniel Bristot de Oliveira "preemptive" [label = "preemptive"]; 125*4041b9bbSDaniel Bristot de Oliveira "preemptive" -> "non_preemptive" [ label = "preempt_disable" ]; 126*4041b9bbSDaniel Bristot de Oliveira { rank = min ; 127*4041b9bbSDaniel Bristot de Oliveira "__init_preemptive"; 128*4041b9bbSDaniel Bristot de Oliveira "preemptive"; 129*4041b9bbSDaniel Bristot de Oliveira } 130*4041b9bbSDaniel Bristot de Oliveira } 131*4041b9bbSDaniel Bristot de Oliveira 132*4041b9bbSDaniel Bristot de OliveiraThis DOT format can be transformed into a bitmap or vectorial image 133*4041b9bbSDaniel Bristot de Oliveirausing the dot utility, or into an ASCII art using graph-easy. For 134*4041b9bbSDaniel Bristot de Oliveirainstance:: 135*4041b9bbSDaniel Bristot de Oliveira 136*4041b9bbSDaniel Bristot de Oliveira $ dot -Tsvg -o wip.svg wip.dot 137*4041b9bbSDaniel Bristot de Oliveira $ graph-easy wip.dot > wip.txt 138*4041b9bbSDaniel Bristot de Oliveira 139*4041b9bbSDaniel Bristot de Oliveiradot2c 140*4041b9bbSDaniel Bristot de Oliveira----- 141*4041b9bbSDaniel Bristot de Oliveira 142*4041b9bbSDaniel Bristot de Oliveiradot2c is a utility that can parse a .dot file containing an automaton as 143*4041b9bbSDaniel Bristot de Oliveirain the example above and automatically convert it to the C representation 144*4041b9bbSDaniel Bristot de Oliveirapresented in [3]. 145*4041b9bbSDaniel Bristot de Oliveira 146*4041b9bbSDaniel Bristot de OliveiraFor example, having the previous 'wip' model into a file named 'wip.dot', 147*4041b9bbSDaniel Bristot de Oliveirathe following command will transform the .dot file into the C 148*4041b9bbSDaniel Bristot de Oliveirarepresentation (previously shown) in the 'wip.h' file:: 149*4041b9bbSDaniel Bristot de Oliveira 150*4041b9bbSDaniel Bristot de Oliveira $ dot2c wip.dot > wip.h 151*4041b9bbSDaniel Bristot de Oliveira 152*4041b9bbSDaniel Bristot de OliveiraThe 'wip.h' content is the code sample in section 'Deterministic Automaton 153*4041b9bbSDaniel Bristot de Oliveirain C'. 154*4041b9bbSDaniel Bristot de Oliveira 155*4041b9bbSDaniel Bristot de OliveiraRemarks 156*4041b9bbSDaniel Bristot de Oliveira------- 157*4041b9bbSDaniel Bristot de Oliveira 158*4041b9bbSDaniel Bristot de OliveiraThe automata formalism allows modeling discrete event systems (DES) in 159*4041b9bbSDaniel Bristot de Oliveiramultiple formats, suitable for different applications/users. 160*4041b9bbSDaniel Bristot de Oliveira 161*4041b9bbSDaniel Bristot de OliveiraFor example, the formal description using set theory is better suitable 162*4041b9bbSDaniel Bristot de Oliveirafor automata operations, while the graphical format for human interpretation; 163*4041b9bbSDaniel Bristot de Oliveiraand computer languages for machine execution. 164*4041b9bbSDaniel Bristot de Oliveira 165*4041b9bbSDaniel Bristot de OliveiraReferences 166*4041b9bbSDaniel Bristot de Oliveira---------- 167*4041b9bbSDaniel Bristot de Oliveira 168*4041b9bbSDaniel Bristot de OliveiraMany textbooks cover automata formalism. For a brief introduction see:: 169*4041b9bbSDaniel Bristot de Oliveira 170*4041b9bbSDaniel Bristot de Oliveira O'Regan, Gerard. Concise guide to software engineering. Springer, 171*4041b9bbSDaniel Bristot de Oliveira Cham, 2017. 172*4041b9bbSDaniel Bristot de Oliveira 173*4041b9bbSDaniel Bristot de OliveiraFor a detailed description, including operations, and application on Discrete 174*4041b9bbSDaniel Bristot de OliveiraEvent Systems (DES), see:: 175*4041b9bbSDaniel Bristot de Oliveira 176*4041b9bbSDaniel Bristot de Oliveira Cassandras, Christos G., and Stephane Lafortune, eds. Introduction to discrete 177*4041b9bbSDaniel Bristot de Oliveira event systems. Boston, MA: Springer US, 2008. 178*4041b9bbSDaniel Bristot de Oliveira 179*4041b9bbSDaniel Bristot de OliveiraFor the C representation in kernel, see:: 180*4041b9bbSDaniel Bristot de Oliveira 181*4041b9bbSDaniel Bristot de Oliveira De Oliveira, Daniel Bristot; Cucinotta, Tommaso; De Oliveira, Romulo 182*4041b9bbSDaniel Bristot de Oliveira Silva. Efficient formal verification for the Linux kernel. In: 183*4041b9bbSDaniel Bristot de Oliveira International Conference on Software Engineering and Formal Methods. 184*4041b9bbSDaniel Bristot de Oliveira Springer, Cham, 2019. p. 315-332. 185