# A Program Model of Fuzzy Interpreted Petri Net to Control Discrete Event Systems
## 3 The Formal Basis and the Conception of FIPN
### Algorithm of the Program Generation Based on FIPN  
The method of program generation based on FIPN is divided into two algorithms. The first creates the declaration of variables. At the beginning, an empty buffer is created. Next, for each of the place declarations of the current marking, the new marking and the capacity along with their initial values are added to the text buffer. Then, for each transition, declarations of the following variables are added to the text buffer: degree to which the condition corresponding to the transition is satisfied with an initial value of zero; the increment of this degree has an initial value of zero if the transition is synchronised by a sensor, otherwise it has an initial value of one; the activation of the transition has an initial value of _false_. Finally, the declarations of output variables related to places and input variables related to transitions are inserted into the text buffer. However, before the addition, it is checked to determine whether a declaration of an input or output variable has already been added (by another transition or place) to the text buffer to avoid duplication. Output variables should be initialized with _true_ if the initial marking of the place is equal to one and this output variable is set to _true_ in the statement assigned to the place. Otherwise it should be initialized with _false_. The first algorithm (Algorithm 1) is as follows:  
```
1: Create empty text buffers \(ht_{1}\), \(ht_{2}\), \(ht_{3}\), \(ht_{4}\) and empty lists \(l_{1}\), \(l_{2}\). 2:for each place \(p_{i}\in P\)do
3: Add the declaration of the variable \(m_{i}\) with the initial value \(M_{0}(p_{i})\) to \(ht_{2}\)
4: Add the declaration of the variable \(mn_{i}\) with the initial value \(M_{0}(p_{i})\) to \(bt_{3}\). 5: Add the declaration of the variable \(k_{i}\) with the value \(K(p_{i})\) to \(bt_{4}\). 6:endfor
7: Add \(ht_{2}\), \(ht_{3}\), \(ht_{4}\) to \(bt_{1}\), and then clear \(ht_{2}\), \(ht_{3}\), \(bt_{4}\). 8:for each transition \(t_{i}\in T\)do
9: Add the declaration of the variable \(tet_{i}\) with the initial value zero to \(bt_{2}\). 10:if the condition \(\psi_{i}=\Gamma(t_{i})\) assigned to \(t_{i}\) is related to a sensor variable \(in_{i}\)then
11: Add the declaration of the variable \(tet_{i}\) to \(bt_{3}\) with the initial value zero. 12:else
13: Add the declaration of the variable \(tet_{i}\) to \(bt_{3}\) with the initial value one. 14:endif
15: Add the declaration of the variable \(tet_{i}\) to \(bt_{3}\) with the initial value one. The second algorithm generates the part of the program that is executed repeatedly during the cycles. At first, an empty buffer is created. Next, for each transition synchronized by a sensor a line of code is added to this buffer. This line calculates the degree to which the condition corresponding to the transition is satisfied. Then, for each transition the next line of code is added that checks whether the transition loses activity or is fired. If the firing of the transition is related to a logic condition, an additional code is inserted to this line. After that, for each transition two lines are added to the text buffer. They compute the new marking of input and output places of the transition. Finally, for each place the line that updates the current marking (based on the new marking) of the place is inserted. Additionally, output variables are set if the current marking of the \(p^{\prime}\)-type place is changed to one. The second algorithm (Algorithm 2) is as follows:
```
1: Create an empty text buffer \(bt_{1}\) to store the result of the algorithm. 2:for each transition \(t_{i}\in T\)do//compute the change of a sensor value
3:if the condition \(\psi_{i}=\Gamma(t_{i})\) assigned to \(t_{i}\) is related to a sensor \(in_{i}\)then
4: Add to \(bt_{1}\) the line of code that calculates the change of the value from the sensor \(\Delta\theta_{i}\) based on (10): \(det_{i}:=CalcDet(tet_{i},in_{i})\)
5:endif
6:endfor
7: Create an empty text buffer \(bt_{2}\) to store the single line of code.