1
SE-561
Formal Methods in Software
Petri Nets - I
2
Petri nets• The classical Petri net was invented by Carl Adam Petri in
1962.
• A lot of research has been conducted (>10,000 publications).
• Until 1985 it was mainly used by theoreticians.
• Since the 80-ties the practical use is increasing because of the introduction of high-level Petri nets and the availability of many tools.
• High-level Petri nets are Petri nets extended with
– color (for the modeling of attributes)
– time (for performance analysis)
– hierarchy (for the structuring of models)
3
The classical Petri net modelA Petri net is a network composed of places ( ) and transitions ( ).
t2
p1
p2
p3
p4t3
t1
• Connections, called arcs, are directed and between a place and a transition.
• Tokens ( ) are the dynamic objects.
• The state of a Petri net, called marking, is determined by the distribution of tokens over the places.
• Initial marking (1, 2, 0, 0)
4
Transition t1 has three input places (p1, p2 and p3) and two output places (p3 and p4).
Place p3 is both an input and an output place of t1.
p1
p2
p3
p4t1
5
Enabling condition
• Transitions are the active components, while places and tokens are passive.
• A transition is enabled if each of the input places contains tokens.
t1 t2
• Transition t1 is not enabled, transition t2 is enabled.
6
Firing
An enabled transition may fire.
Firing corresponds to consuming tokens from the input places and producing tokens for the output places.
t2t2
Firing is atomic.
7
Example
8
Petri Net Structures
• sequence of events/actions:
• concurrent execution:
t1 t2 t3
t1
t2 t3
t4 t5
9
Non-determinism
Two transitions fight for the same token: conflict.
Even if there are two tokens, there is still a conflict.
t1
t2
t1
Synchronization
10
Modeling
• States of a process or a system are modeled by tokens in places and state transitions leading from one state to another are modeled by transitions.
• Tokens represent objects (humans, goods, machines), information, conditions or states of objects.
• Places represent buffers, channels, geographical locations, conditions or states.
• Transitions represent events, transformations or transportations.
11
Example: Traffic light
rg
red
yellow
green
yr
gy
12
Two traffic lights
rg1
red1
yellow1
green1
yr1
gy1
rg2
red2
yellow2
green2
yr2
gy2
13
Two safe traffic lights
rg1
red1
yellow1
green1
yr1
gy1
rg2
red2
yellow2
green2
yr2
gy2
safe
14
Two safe and fair traffic lights
rg1
red1
yellow1
green1
yr1
gy1
rg2
red2
yellow2
green2
yr2
gy2
safe2
safe1
15
Reachability
• Reachable markingA marking is said to be reachable if it can be reached by firing a sequence of enabled transitions form the initial marking of a Petri net.
• Petri net token gamehttp://psim.tm.tue.nl/staff/wvdaalst/Downloads/pn_applet/pn_applet.html
16
Exercise: readers and writers
• How many states are reachable?
• How to model the situation with 2 writers and 3 readers?
• How to model a "bounded mailbox" (buffer size =4)?
rest
mail_box
receive_mail
type_mail
ready
rest
begin
send_mail
read_mail
17
Restaurant Scenario
WaiterfreeCustomer 1 Customer 2
Takeorder
Takeorder
Ordertaken
Tellkitchen
wait wait
Serve food Serve food
eating eating
18
A Puzzle: Crossing River
A ferry-man has to bring a goat, a cabbage, and a wolf safely from the left bank to the right bank of a river.
The ferry-man can cross the river alone or with exactly one of these three passengers.
At any time, either the ferry-man should be on the same bank as the goat, or the goat should be alone on a bank. Otherwise, the goat could go ahead and eat the cabbage or the
wolf may eat the goat
19
Modeling
• We are going to model the situation with a Petri net.
• The puzzle mentions the following objects:
– Man, wolf, goat, cabbage, boat.
• The puzzle mentions the following actions:
– Crossing the river, wolf eats goat, goat eats cabbage.
• Objects and their states are modeled by places.
• Actions are modeled by transitions.
• Actually, we can omit the boat, because it is always going to be on the same side as the man.
20
Places
21
Crossing the river (left to right)
22
Crossing the river (left to right)
23
Crossing the river (left to right)
24
Crossing the river (right to left)
25
Wolf eats goat
26
Formal Definition of a Petri Net
A Petri net N is a tuple N = {P, T, I, O, M0}, where P is a finite set of places, T is a finite set of transitions, Places P and transitions T are disjoint (P ∩ T = ), I: P × T N (N = {0, 1, 2, …})is the pre-incidence function representing input
arcs, O: T × P N (N = {0, 1, 2, …})is the post-incidence function representing
output arcs, M0 : P N is the initial marking representing the initial distribution of tokens.
27
Example
• P = {p1, p2}
• T = {t1, t2, t3}
• I(t1) = (1, 1), I(t2) = (2, 0), I(t3) = (0, 2)
• O(t1) = ?
• M0 = (3, 2)
p2p1
t3t2
t1
28
Transition firing
• A transition t is enabled at marking Mi if and only if
Mi ≥ I(t)
• Suppose that the firing of t takes the Petri net from Mi to Mi. Then
Mj = Mi - I(t) + O(t)
29
p2p1
t3t2
t1
• P = {p1, p2}• T = {t1, t2, t3}• I(t1) = (1, 1), I(t2) = (2, 0), I(t3) = (0, 2)• O(t1) = (1, 0), O(t2) = (0, 1), O(t3) = (0, 1)• M0 = (3, 2)
Mj = Mi - I(t) + O(t)