| 1 |
% Copyright 2019 by Till Tantau |
| 2 |
% |
| 3 |
% This file may be distributed and/or modified |
| 4 |
% |
| 5 |
% 1. under the LaTeX Project Public License and/or |
| 6 |
% 2. under the GNU Free Documentation License. |
| 7 |
% |
| 8 |
% See the file doc/generic/pgf/licenses/LICENSE for more details. |
| 9 |
|
| 10 |
|
| 11 |
\section{Petri-Net Drawing Library} |
| 12 |
|
| 13 |
\begin{tikzlibrary}{petri} |
| 14 |
This packages provides shapes and styles for drawing Petri nets. |
| 15 |
\end{tikzlibrary} |
| 16 |
|
| 17 |
|
| 18 |
\subsection{Places} |
| 19 |
|
| 20 |
The package defines a style for drawing places of Petri nets. |
| 21 |
|
| 22 |
\begin{stylekey}{/tikz/place} |
| 23 |
This style indicates that a node is a place of a Petri net. Usually, the |
| 24 |
text of the node should be empty since places do not contain any text. You |
| 25 |
should use the |label| option to add text outside the node like its name or |
| 26 |
its capacity. You should use the |tokens| options, explained in |
| 27 |
Section~\ref{section-tokens}, to add tokens inside the place. |
| 28 |
% |
| 29 |
\begin{codeexample}[preamble={\usetikzlibrary{petri,positioning}}] |
| 30 |
\begin{tikzpicture} |
| 31 |
\node[place,label=above:$p_1$,tokens=2] (p1) {}; |
| 32 |
\node[place,label=below:$p_2\ge1$,right=of p1] (p2) {}; |
| 33 |
\end{tikzpicture} |
| 34 |
\end{codeexample} |
| 35 |
|
| 36 |
\begin{stylekey}{/tikz/every place} |
| 37 |
This style is evoked by the style |place|. To change the appearance of |
| 38 |
places, you can change this style. |
| 39 |
% |
| 40 |
\begin{codeexample}[preamble={\usetikzlibrary{petri,positioning}}] |
| 41 |
\begin{tikzpicture} |
| 42 |
[every place/.style={draw=blue,fill=blue!20,thick,minimum size=9mm}] |
| 43 |
\node[place,tokens=7,label=above:$p_1$] (p1) {}; |
| 44 |
\node[place,structured tokens={3,2,9}, |
| 45 |
label=below:$p_2\ge1$,right=of p1] (p2) {}; |
| 46 |
\end{tikzpicture} |
| 47 |
\end{codeexample} |
| 48 |
\end{stylekey} |
| 49 |
\end{stylekey} |
| 50 |
|
| 51 |
|
| 52 |
\subsection{Transitions} |
| 53 |
|
| 54 |
Transitions are also nodes. They should be drawn using the following style: |
| 55 |
|
| 56 |
\begin{stylekey}{/tikz/transition} |
| 57 |
This style indicates that a node is a transition. As for places, the text |
| 58 |
of a transition should be empty and the |label| option should be used for |
| 59 |
adding labels. |
| 60 |
|
| 61 |
To connect a transition to places, you can use the |edge| command as in the |
| 62 |
following example: |
| 63 |
% |
| 64 |
\begin{codeexample}[preamble={\usetikzlibrary{petri,positioning}}] |
| 65 |
\begin{tikzpicture} |
| 66 |
\node[place,tokens=2,label=above:$p_1$] (p1) {}; |
| 67 |
\node[place,label=above:$p_2\ge1$,right=of p1] (p2) {}; |
| 68 |
|
| 69 |
\node[transition,below right=of p1,label=below:$t_1$] {} |
| 70 |
edge[pre] (p1) |
| 71 |
edge[post] node[auto] {2} (p2); |
| 72 |
\end{tikzpicture} |
| 73 |
\end{codeexample} |
| 74 |
|
| 75 |
\begin{stylekey}{/tikz/every transition} |
| 76 |
This style is evoked by the style |transition|. |
| 77 |
\end{stylekey} |
| 78 |
|
| 79 |
\begin{stylekey}{/tikz/pre} |
| 80 |
This style can be used with paths leading \emph{from} a transition |
| 81 |
\emph{to} a place to indicate that the place is in the pre-set of the |
| 82 |
transition. By default, this style is |<-,shorten <=1pt|, but feel free |
| 83 |
to redefine it. |
| 84 |
\end{stylekey} |
| 85 |
|
| 86 |
\begin{stylekey}{/tikz/post} |
| 87 |
This style is also used with paths leading \emph{from} a transition |
| 88 |
\emph{to} a place, but this time the place is in the post-set of the |
| 89 |
transition. Again, feel free to redefine it. |
| 90 |
\end{stylekey} |
| 91 |
|
| 92 |
\begin{stylekey}{/tikz/pre and post} |
| 93 |
This style is to be used to indicate that a place is both in the pre- |
| 94 |
and post-set of a transition. |
| 95 |
\end{stylekey} |
| 96 |
\end{stylekey} |
| 97 |
|
| 98 |
|
| 99 |
\subsection{Tokens} |
| 100 |
\label{section-tokens} |
| 101 |
|
| 102 |
Interestingly, the most complicated aspect of drawing Petri nets in \tikzname\ |
| 103 |
turns out to be the placement of tokens. |
| 104 |
|
| 105 |
Let us start with a single token. They are also nodes and there is a simple |
| 106 |
style for typesetting them. |
| 107 |
|
| 108 |
\begin{stylekey}{/tikz/token} |
| 109 |
This style indicates that a node is a token. By default, this causes the |
| 110 |
node to be a small black circle. Unlike places and transitions, it |
| 111 |
\emph{does} make sense to provide text for the token node. Such text will |
| 112 |
be typeset in a tiny font and in white on black (naturally, you can easily |
| 113 |
change this by setting the style |every token|). |
| 114 |
% |
| 115 |
\begin{codeexample}[preamble={\usetikzlibrary{petri,positioning}}] |
| 116 |
\begin{tikzpicture} |
| 117 |
\node[place,label=above:$p_1$] (p1) {}; |
| 118 |
\node[token] at (p1) {}; |
| 119 |
|
| 120 |
\node[place,label=above:$p_2$,right=of p1] (p2) {}; |
| 121 |
\node[token] at (p2) {$y$}; |
| 122 |
\end{tikzpicture} |
| 123 |
\end{codeexample} |
| 124 |
% |
| 125 |
\begin{stylekey}{/tikz/every token} |
| 126 |
Change this style to change the appearance of tokens. |
| 127 |
\end{stylekey} |
| 128 |
\end{stylekey} |
| 129 |
|
| 130 |
In the above example, it is bothersome that we need an extra command for the |
| 131 |
token node. Worse, when we have \emph{two} tokens on a node, it is difficult to |
| 132 |
place both nodes inside the node without overlap. |
| 133 |
|
| 134 |
The Petri library offers a solution to this problem: The |children are tokens| |
| 135 |
style. |
| 136 |
|
| 137 |
\begin{stylekey}{/tikz/children are tokens} |
| 138 |
The idea behind this style is to use trees mechanism for placing tokens. |
| 139 |
Every token lying on a place is treated as a child of the node. Normally |
| 140 |
this would have the effect that the tokens are placed below the place and |
| 141 |
they would be connected to the place by an edge. The |children are tokens| |
| 142 |
style, however, redefines the growth function of trees such that it places |
| 143 |
the children next to each other inside (or, rather, on top) of the place |
| 144 |
node. Additionally, the edge from the parent node is not drawn. |
| 145 |
% |
| 146 |
\begin{codeexample}[preamble={\usetikzlibrary{petri}}] |
| 147 |
\begin{tikzpicture} |
| 148 |
\node[place,label=above:$p_1$] {} |
| 149 |
[children are tokens] |
| 150 |
child {node [token] {1}} |
| 151 |
child {node [token] {2}} |
| 152 |
child {node [token] {3}}; |
| 153 |
\end{tikzpicture} |
| 154 |
\end{codeexample} |
| 155 |
|
| 156 |
In detail, what happens is the following: Tree growth functions tell |
| 157 |
\tikzname\ where it should place the children of nodes. These functions get |
| 158 |
passed the number of children that a node has an the number of the child |
| 159 |
that should be placed. The special tree growth function for tokens has a |
| 160 |
special mapping for each possible number of children up to nine children. |
| 161 |
This mapping decides for each child where it should be placed on top of the |
| 162 |
place. For example, a single child is placed directly on top of the place. |
| 163 |
Two children are placed next to each other, separated by the |
| 164 |
|token distance|. Three children are placed in a triangle whose side |
| 165 |
lengths are |token distance|; and so on up to nine tokens. If you wish to |
| 166 |
place more than nice tokens on a place, you will have to write your own |
| 167 |
placement code. |
| 168 |
% |
| 169 |
\begin{codeexample}[preamble={\usetikzlibrary{petri}}] |
| 170 |
\begin{tikzpicture} |
| 171 |
\node[place,label=above:$p_2$] {} |
| 172 |
[children are tokens] |
| 173 |
child {node [token] {1}} |
| 174 |
child {node [token,fill=red] {2}} |
| 175 |
child {node [token,fill=red] {2}} |
| 176 |
child {node [token] {1}}; |
| 177 |
\end{tikzpicture} |
| 178 |
\end{codeexample} |
| 179 |
|
| 180 |
\begin{key}{/tikz/token distance=\meta{distance}} |
| 181 |
This specifies the distance between the centers of the tokens in the |
| 182 |
arrangements of the option |children are tokens|. |
| 183 |
% |
| 184 |
\begin{codeexample}[preamble={\usetikzlibrary{petri}}] |
| 185 |
\begin{tikzpicture} |
| 186 |
\node[place,label=above:$p_3$] {} |
| 187 |
[children are tokens,token distance=1.1ex] |
| 188 |
child {node [token] {}} |
| 189 |
child {node [token,red] {}} |
| 190 |
child {node [token,blue] {}} |
| 191 |
child {node [token] {}}; |
| 192 |
\end{tikzpicture} |
| 193 |
\end{codeexample} |
| 194 |
\end{key} |
| 195 |
\end{stylekey} |
| 196 |
|
| 197 |
The |children are tokens| option gives you a lot of flexibility, but it is a |
| 198 |
bit cumbersome to use. For this reason there are some options that help in |
| 199 |
standard situations. They all use |children are tokens| internally, so any |
| 200 |
change to, say, the |every token| style will affect how these options depict |
| 201 |
tokens. |
| 202 |
|
| 203 |
\begin{key}{/tikz/tokens=\meta{number}} |
| 204 |
This option is given to a |place| node, not to a |token| node. The effect |
| 205 |
of this option is to add \meta{number} many child nodes to the place, each |
| 206 |
having the style |token|. Thus, the following two pieces of codes have the |
| 207 |
same effect: |
| 208 |
% |
| 209 |
\begin{codeexample}[preamble={\usetikzlibrary{petri}}] |
| 210 |
\tikz |
| 211 |
\node[place] {} |
| 212 |
[children are tokens] |
| 213 |
child {node [token] {}} |
| 214 |
child {node [token] {}} |
| 215 |
child {node [token] {}}; |
| 216 |
\tikz |
| 217 |
\node[place,tokens=3] {}; |
| 218 |
\end{codeexample} |
| 219 |
% |
| 220 |
It is legal to say |tokens=0|, no tokens are drawn in this case. This |
| 221 |
option does not handle ten or more tokens correctly. If you need this many |
| 222 |
tokens, you will have to program your own code. |
| 223 |
% |
| 224 |
\begin{codeexample}[preamble={\usetikzlibrary{petri}}] |
| 225 |
\begin{tikzpicture}[every place/.style={minimum size=9mm}] |
| 226 |
|
| 227 |
\foreach \x/\y/\tokennumber in {0/2/1,1/2/2,2/2/3, |
| 228 |
0/1/4,1/1/5,2/1/6, |
| 229 |
0/0/7,1/0/8,2/0/9} |
| 230 |
\node [place,tokens=\tokennumber] at (\x,\y) {}; |
| 231 |
\end{tikzpicture} |
| 232 |
\end{codeexample} |
| 233 |
% |
| 234 |
\end{key} |
| 235 |
|
| 236 |
\begin{key}{/tikz/colored tokens=\meta{color list}} |
| 237 |
This option, which must also be given when a place node is being created, |
| 238 |
gets a list of colors as parameter. It will then add as many tokens to the |
| 239 |
place as there are colors in this list, each filled correspondingly. |
| 240 |
% |
| 241 |
\begin{codeexample}[preamble={\usetikzlibrary{petri}}] |
| 242 |
\tikz \node[place,colored tokens={black,black,red,blue}] {}; |
| 243 |
\end{codeexample} |
| 244 |
% |
| 245 |
\end{key} |
| 246 |
|
| 247 |
\begin{key}{/tikz/structured tokens=\meta{token texts}} |
| 248 |
This option, which must again be passed to a place, gets a list of texts |
| 249 |
for tokens. For each text, a new token will be added to the place. |
| 250 |
% |
| 251 |
\begin{codeexample}[preamble={\usetikzlibrary{petri}}] |
| 252 |
\tikz \node[place,structured tokens={$x$,$y$,$z$}] {}; |
| 253 |
\end{codeexample} |
| 254 |
% |
| 255 |
\begin{codeexample}[preamble={\usetikzlibrary{petri}}] |
| 256 |
\begin{tikzpicture}[every place/.style={minimum size=9mm}] |
| 257 |
|
| 258 |
\foreach \x/\y/\tokennumber in {0/2/1,1/2/2,2/2/3, |
| 259 |
0/1/4,1/1/5,2/1/6, |
| 260 |
0/0/7,1/0/8,2/0/9} |
| 261 |
\node [place,structured tokens={1,...,\tokennumber}] at (\x,\y) {}; |
| 262 |
\end{tikzpicture} |
| 263 |
\end{codeexample} |
| 264 |
% |
| 265 |
If you use lots of structured tokens, consider redefining the |every token| |
| 266 |
style so that the tokens are larger. |
| 267 |
\end{key} |
| 268 |
|
| 269 |
|
| 270 |
\subsection{Examples} |
| 271 |
|
| 272 |
\begin{codeexample}[preamble={\usetikzlibrary{petri}}] |
| 273 |
\begin{tikzpicture}[yscale=-1.6,xscale=1.5,thick, |
| 274 |
every transition/.style={draw=red,fill=red!20,minimum size=3mm}, |
| 275 |
every place/.style={draw=blue,fill=blue!20,minimum size=6mm}] |
| 276 |
|
| 277 |
\foreach \i in {1,...,6} { |
| 278 |
\node[place,label=left:$p_\i$] (p\i) at (0,\i) {}; |
| 279 |
\node[place,label=right:$q_\i$] (q\i) at (8,\i) {}; |
| 280 |
} |
| 281 |
\foreach \name/\var/\vala/\valb/\height/\x in |
| 282 |
{m1/m_1/f/t/2.25/3,m2/m_2/f/t/2.25/5,h/\mathit{hold}/1/2/4.5/4} { |
| 283 |
\node[place,label=above:{$\var = \vala$}] (\name\vala) at (\x,\height) {}; |
| 284 |
\node[place,yshift=-8mm,label=below:{$\var = \valb$}] (\name\valb) at (\x,\height) {}; |
| 285 |
} |
| 286 |
\node[token] at (p1) {}; \node[token] at (q1) {}; |
| 287 |
\node[token] at (m1f) {}; \node[token] at (m2f) {}; |
| 288 |
\node[token] at (h1) {}; |
| 289 |
|
| 290 |
\node[transition] at (1.5,1.5) {} edge [pre] (p1) edge [post] (p2); |
| 291 |
\node[transition] at (1.5,2.5) {} edge [pre] (p2) edge[pre] (m1f) |
| 292 |
edge [post](p3) edge[post] (m1t); |
| 293 |
\node[transition] at (1.5,3.3) {} edge [pre] (p3) edge [post] (p4) |
| 294 |
edge [pre and post] (h1); |
| 295 |
\node[transition] at (1.5,3.7) {} edge [pre] (p3) edge [pre] (h2) |
| 296 |
edge [post] (p4) edge [post] (h1.west); |
| 297 |
\node[transition] at (1.5,4.3) {} edge [pre] (p4) edge [post] (p5) |
| 298 |
edge [pre and post] (m2f); |
| 299 |
\node[transition] at (1.5,4.7) {} edge [pre] (p4) edge [post] (p5) |
| 300 |
edge [pre and post] (h2); |
| 301 |
\node[transition] at (1.5,5.5) {} edge [pre] (p5) edge [pre] (m1t) |
| 302 |
edge [post] (p6) edge [post] (m1f); |
| 303 |
\node[transition] at (1.5,6.5) {} edge [pre] (p6) edge [post] (p1.south east); |
| 304 |
\node[transition] at (6.5,1.5) {} edge [pre] (q1) edge [post] (q2); |
| 305 |
\node[transition] at (6.5,2.5) {} edge [pre] (q2) edge [pre] (m2f) |
| 306 |
edge [post] (q3) edge [post] (m2t); |
| 307 |
\node[transition] at (6.5,3.3) {} edge [pre] (q3) edge [post] (q4) |
| 308 |
edge [pre and post] (h2); |
| 309 |
\node[transition] at (6.5,3.7) {} edge [pre] (q3) edge [pre] (h1) |
| 310 |
edge [post] (q4) edge [post] (h2.east); |
| 311 |
\node[transition] at (6.5,4.3) {} edge [pre] (q4) edge [post] (q5) |
| 312 |
edge [pre and post] (m1f); |
| 313 |
\node[transition] at (6.5,4.7) {} edge [pre] (q4) edge [post] (q5) |
| 314 |
edge [pre and post] (h1); |
| 315 |
\node[transition] at (6.5,5.5) {} edge [pre] (q5) edge [pre] (m2t) |
| 316 |
edge [post] (q6) edge [post] (m2f); |
| 317 |
\node[transition] at (6.5,6.5) {} edge [pre] (q6) edge [post] (q1.south west); |
| 318 |
\end{tikzpicture} |
| 319 |
\end{codeexample} |
| 320 |
|
| 321 |
Here is the same net once more, but with these styles changes: |
| 322 |
% |
| 323 |
\begin{codeexample}[code only] |
| 324 |
\begin{tikzpicture}[yscale=-1.1,thin,>=stealth, |
| 325 |
every transition/.style={fill,minimum width=1mm,minimum height=3.5mm}, |
| 326 |
every place/.style={draw,thick,minimum size=6mm}] |
| 327 |
\end{codeexample} |
| 328 |
|
| 329 |
\begin{tikzpicture}[yscale=-1.1,thin,>=stealth, |
| 330 |
every transition/.style={fill,minimum width=1mm,minimum height=3.5mm}, |
| 331 |
every place/.style={draw,thick,minimum size=6mm}] |
| 332 |
|
| 333 |
\foreach \i in {1,...,6} { |
| 334 |
\node[place,label=left:$p_\i$] (p\i) at (0,\i) {}; |
| 335 |
\node[place,label=right:$q_\i$] (q\i) at (8,\i) {}; |
| 336 |
} |
| 337 |
\foreach \name/\var/\vala/\valb/\height/\x in |
| 338 |
{m1/m_1/f/t/2.25/3,m2/m_2/f/t/2.25/5,h/\mathit{hold}/1/2/4.5/4} { |
| 339 |
\node[place,label=above:{$\var = \vala$}] (\name\vala) at (\x,\height) {}; |
| 340 |
\node[place,yshift=-8mm,label=below:{$\var = \valb$}] (\name\valb) at (\x,\height) {}; |
| 341 |
} |
| 342 |
\node[token] at (p1) {}; \node[token] at (q1) {}; |
| 343 |
\node[token] at (m1f) {}; \node[token] at (m2f) {}; |
| 344 |
\node[token] at (h1) {}; |
| 345 |
|
| 346 |
\node[transition] at (1.5,1.5) {} edge [pre] (p1) edge [post] (p2); |
| 347 |
\node[transition] at (1.5,2.5) {} edge [pre] (p2) edge [pre] (m1f) |
| 348 |
edge [post] (p3) edge [post] (m1t); |
| 349 |
\node[transition] at (1.5,3.3) {} edge [pre] (p3) edge [post] (p4) |
| 350 |
edge [pre and post] (h1); |
| 351 |
\node[transition] at (1.5,3.7) {} edge [pre] (p3) edge [pre] (h2) |
| 352 |
edge [post] (p4) edge [post] (h1.west); |
| 353 |
\node[transition] at (1.5,4.3) {} edge [pre] (p4) edge [post] (p5) |
| 354 |
edge [pre and post] (m2f); |
| 355 |
\node[transition] at (1.5,4.7) {} edge [pre] (p4) edge [post] (p5) |
| 356 |
edge [pre and post] (h2); |
| 357 |
\node[transition] at (1.5,5.5) {} edge [pre] (p5) edge [pre] (m1t) |
| 358 |
edge [post] (p6) edge [post] (m1f); |
| 359 |
\node[transition] at (1.5,6.5) {} edge [pre] (p6) edge [post] (p1.south east); |
| 360 |
\node[transition] at (6.5,1.5) {} edge [pre] (q1) edge [post] (q2); |
| 361 |
\node[transition] at (6.5,2.5) {} edge [pre] (q2) edge [pre] (m2f) |
| 362 |
edge [post] (q3) edge [post] (m2t); |
| 363 |
\node[transition] at (6.5,3.3) {} edge [pre] (q3) edge [post] (q4) |
| 364 |
edge [pre and post] (h2); |
| 365 |
\node[transition] at (6.5,3.7) {} edge [pre] (q3) edge [pre] (h1) |
| 366 |
edge [post] (q4) edge [post] (h2.east); |
| 367 |
\node[transition] at (6.5,4.3) {} edge [pre] (q4) edge [post] (q5) |
| 368 |
edge [pre and post] (m1f); |
| 369 |
\node[transition] at (6.5,4.7) {} edge [pre] (q4) edge [post] (q5) |
| 370 |
edge [pre and post] (h1); |
| 371 |
\node[transition] at (6.5,5.5) {} edge [pre] (q5) edge [pre] (m2t) |
| 372 |
edge [post] (q6) edge [post] (m2f); |
| 373 |
\node[transition] at (6.5,6.5) {} edge [pre] (q6) edge [post] (q1.south west); |
| 374 |
\end{tikzpicture} |
| 375 |
|
| 376 |
|
| 377 |
%%% Local Variables: |
| 378 |
%%% mode: latex |
| 379 |
%%% TeX-master: "pgfmanual-pdftex-version" |
| 380 |
%%% End: |