# Petri Net-Based Semi-Compiled Code Generation for Programmable Logic Controllers
## 1 Introduction
### PN-Based DEDSs Modeling  
Petri nets (PNs) are a powerful method to model and control such DEDSs as they have the capability to represent systems characterized as being concurrent, asynchronous, distributed, parallel, nondeterministic, and/or stochastic [3]. A PN is a graphical tool because it combines visual communication aid (block diagram) to which tokens are added to simulate the dynamics and concurrent activities of systems. It is also a mathematical tool because of algebraic models related to its behavior. Ref. [4] considers PNs as suitable formalism to model and visualize the behavior of DEDSs, noting that they have been applied successfully to several problems in different sectors, such as manufacturing. Thus, a PN represents an effective method [5] for both the design and implementation of this type of systems [6]. However, there is not a widely adopted and effective systematic way to implement a PN in a programmable logic controller (PLC), and so it is very common the implementation of such a controller outside a PLC in some external software that will communicate with the PLC. A PN is composed by its structure and interpretation [7], and its dynamics. In terms of net structure [4] describes a PN graph as a bipartite graph that contains two types of nodes: places (identified by \(p\in P\) and represented by circles) and transitions (\(t\in T\), bars). These two types of nodes are connected through directed and weighted arcs (Figure 2). Ref. [7] denotes a Place/Transition (P/T) net as \(N=<\)_P_,\(T\),Pre,Post\(>\), where \(P\) and \(T\) are the sets of places and transitions, and Pre and Post are the \(|\,P\mid\times\,|\,T\,|\) sized, natural valued incidence matrices. For instance, Post[\(p\),\(t\)] = \(w\) means that there is an arc from \(t\) to \(p\) with weight (or multiplicity) \(w\). When all weights are one the net is \(ordinary\). A marking is a \(|\,P\,|\) sized and natural valued vector. A marked net, P/T system or PN is a pair \(<\)\(N\),\(m_{0}\)\(>\), where \(m_{0}\) is the initial marking. A transition t is enabled at marking \(m\) if \(m\geq\) Pre [\(P\),\(t\)]; its firing yields a new marking \(m^{\prime}=m+\mathbb{C}[P,t]\), where C = Post \(-\) Pre is the token \(-\) flow matrix of the net. The set of all reachable markings, or reachability graph (RG), from \(m\), is denoted by RG(\(N\),\(m\)). C and \(m_{0}\) corresponding to the example are as follows:  
Figure 1: Emulated traffic lights synchronization. \[C= \begin{pmatrix}t_{0}&t_{1}&t_{2}&t_{3}&t_{4}\\ 1&-1&0&0&0\\ 0&1&-1&0&0\\ -1&0&1&0&0\\ 0&0&0&-1&1\\ 0&0&0&1&-1\\ 1&0&0&-1&0\\ 0&-1&0&0&1\end{pmatrix}\begin{matrix}p_{0}\\ p_{1}\\ p_{2}\\ p_{3}\\ p_{4}\\ p_{5}\\ p_{6}\end{matrix} \]  
\[m_{0}^{t}= \begin{pmatrix}p_{0}&p_{1}&p_{2}&p_{3}&p_{4}&p_{5}&p_{6}\\ 1&0&0&1&0&0&1\end{pmatrix} \]  
PN dynamics is characterized by the marking, i.e., tokens (dots) distribution among places, and its evolution. Marking progress depends on the controlled firing of transitions i.e., their enabling and the compliance of their associated firing conditions. In terms of structural validation, [3] defines basic properties to be fulfilled by a PN, such as its liveness, cyclicity, and boundedness. The existence of the aforementioned formal