Coloured Petri Nets
Lars M. Kristensen
Department of Computer Engineering
Bergen University College, NORWAY
Email: lmkr@[Link] /Web: [Link]/ansatte/lmkr
Advanced Course on Petri Nets 2010 - 1
Coloured Petri Nets (CPNs)
Petri Nets and a programming language:
Petri Nets:
concurrency
control structures
synchronisation
communication
Standard ML:
Colour sets (data types)
and markings
Arc expressions
Guards
Standard ML enables compact modelling and
convenient modelling of data manipulation.
Advanced Course on Petri Nets 2010 - 2
CPN Tools
[ [Link]/CPNTools ]
Modelling and validation of Coloured Petri
Net models are supported by CPN Tools:
Editing and syntax check.
Interactive- and
automatic simulation.
State space exploration and
verification.
Performance analysis.
Behavioural visualisation using
application domain graphics.
Currently 8000+ CPN Tools license holders in
130+ countries.
Advanced Course on Petri Nets 2010 - 3
Example: A Simple
Communication Protocol
Advanced Course on Petri Nets 2010 - 5
The Problem – in Greendale
Advanced Course on Petri Nets 2010 - 6
The Problem – in Data Networks
Data sequence numbers
01101010 1
00101010 2
11010100
3
01101101 Receiver
4 01101100
Packet
Sender
Network
Receiver must assemble original data.
Stop-and-wait protocol: transmit one data packet at a
time and wait for a matching acknowledgement.
Initially we will assume a reliable network (no loss).
Advanced Course on Petri Nets 2010 - 7
Part 1:
Basic Protocol CPN Model
Advanced Course on Petri Nets 2010 - 8
The Coloured Petri Net Model
Sender Network Receiver
Advanced Course on Petri Nets 2010 - 9
Place
Arc
Transition
Net inscriptions
Advanced Course on Petri Nets 2010 - 10
Places model the state of the system
Initial marking
(multi-set of tokens)
Definition of colour sets:
colset NO = int; (* integers *)
Name
colset DATA = string; (* text strings *)
colset NOxDATA = product NO * DATA;
Each place may contain a number
of tokens.
Colour set
(type) Each token carries a colour
(data value).
The colour set of a place specifies
the set of allowed token colours.
Advanced Course on Petri Nets 2010 - 11
Current marking
Token coefficient
Values:
Circle: 6 tokens Token colours
Square: Detailed
token values
Information about
current marking
Advanced Course on Petri Nets 2010 - 12
Initial marking of CPN model
Advanced Course on Petri Nets 2010 - 13
Intermediate marking of model
Advanced Course on Petri Nets 2010 - 14
Transitions model events of the system
The type of the arc expression must match
the colour set of the attached place
(or the multi-set type over the colour set)
Arc expression
Declaration of variables:
var n : NO; (* integers *)
var d : DATA; (* strings *)
Binding of transition variables:
<n=3,d="CPN">
Evaluation of expressions:
(n,d) → (3,"CPN") : NOxDATA
n → 3 : NO
Advanced Course on Petri Nets 2010 - 15
Enabling of transitions
Two variables:
var n : NO; (* integers *)
var d : DATA; (* strings *)
?
Binding: < n=? , d=? >
NO DATA
Green border line
indicates that the
transition is enabled
(ready to occur) ? Transition is enabled if we can find a
binding so that each input arc expression
evaluates to a multi-set of tokens that are
present on the corresponding input place.
Advanced Course on Petri Nets 2010 - 16
Enabling of SendPacket
Binding: < n=1 , d=? >
We want to find a binding for the variable n
such that the arc expression n evaluates to
a multi-set of colours which is present on
Arc expression the place NextSend.
One token with value 1
Advanced Course on Petri Nets 2010 - 17
Enabling of SendPacket
Six different tokens
Arc expression
Binding: < n=1 , d="COL"
d=? > >
We want to find a binding for the variable d
such that the arc expression (n,d) evaluates
to a multi-set which is present on the place
PacketsToSend.
Advanced Course on Petri Nets 2010 - 18
Enabling of SendPacket
We have found a binding so that
each input arc expression
evaluates to a colour that is
present on the corresponding
input place
Binding: < n=1 , d="COL" >
(1,"COL")
Transition is enabled
1
(ready to occur)
Advanced Course on Petri Nets 2010 - 19
Occurrence of SendPacket
in binding <n=1,d=“COL”>
Remove: (1,"COL")
(1,"COL") Add a new token: (1,"COL")
(1,"COL")
Remove: 1
Advanced Course on Petri Nets 2010 - 20
New marking after occurrence of
SendPacket in binding <n=1,d=“COL”>
The first packet has
been removed
Transition is no A copy of the first
longer enabled packet has been
(no green border line) put on A
No token on this place
Advanced Course on Petri Nets 2010 - 21
New marking M1
Transition
enabled
Advanced Course on Petri Nets 2010 - 22
Part 2:
Extended Protocol CPN Model
Advanced Course on Petri Nets 2010 - 23
The Simple Protocol Revisited
Data
01101010 1
00101010 2
11010100
3
01101101 Receiver
4 01101100
Packet
Sender
Network
Unreliable network (loss and overtaking).
Sender must retransmit packets and keep track of the
data packet currently being sent.
Receiver keeps track of the data packet expected next.
Advanced Course on Petri Nets 2010 - 24
Second Version
val AllPackets = 1‘(1,"COL") ++ 1‘(2,"OUR") ++
1‘(3,"ED ") ++ 1‘(4,"PET") ++
1‘(5,"RI ") ++ 1‘(6,"NET");
Advanced Course on Petri Nets 2010 - 25
Double Arcs
A double arc is a shorthand
for two oppositely directed
arcs with the same arc
expression
Double arc
We no longer remove the
Double arc tokens from the input places
Retransmission
becomes possible
Advanced Course on Petri Nets 2010 - 26
Occurrence of SendPacket <n=1,d=“COL”>
Advanced Course on Petri Nets 2010 - 27
More Complex Arc Expression
More complicated
arc expression
(if-then-else expression)
Advanced Course on Petri Nets 2010 - 28
If-then-else Expression
1`(1,"COL") 1`(1,"COL")
1 1
success = true
New variable: 1`(1,"COL")
var success : BOOL; Successful transmission
over the network
bsucc = <n=1, d="COL", success=true>
bfail = <n=1, d="COL", success=false>
Advanced Course on Petri Nets 2010 - 29
If-then-else Expression
No packet is
1`(1,"COL") added
1
success = false
empty
var success : BOOL; Packet is lost during
transmission
bsucc = <n=1, d="COL", success=true>
bfail = <n=1, d="COL", success=false>
Advanced Course on Petri Nets 2010 - 30
New Name and Type New name
Initial marking:
empty text string
New type
Advanced Course on Petri Nets 2010 - 31
New Place: NextRec
Plays a similar New place
role as NextSend
Contains the
number of the
expected packet
Advanced Course on Petri Nets 2010 - 32
Correct packet arrives
Empty text string
Packet no 1 arriving
Binding:
<n=1, d="COL“, k=1, data="">
^ is the string
Packet no 1 expected "COL" concatenation
operator
Update NextRec (from 1 to 2)
2 Send acknoweledgement
(with sequence number of next packet)
2 Add received data: "COL"
Advanced Course on Petri Nets 2010 - 33
Correct Packet Received
Advanced Course on Petri Nets 2010 - 34
Wrong Packet Arrives
Packet no 1 arriving
Binding:
<n=1, d="COL“, k=3, data="COLOUR">
Packet no 3 expected "COLOUR"
Do not change NextRec
3
Send acknowledgement
(with sequence number of next packet )
3
No data is added to DataReceived
Advanced Course on Petri Nets 2010 - 35
Wrong Packet Arrived
Advanced Course on Petri Nets 2010 - 36
Acknowledgements can be lost
Also possible to loose
acknowledgements
Advanced Course on Petri Nets 2010 - 37
NextSend is updated
NextSend is updated with sequence
number from acknowledgement
Advanced Course on Petri Nets 2010 - 38
Advanced Course on Petri Nets 2010 - 39
Transitions can have a guard
Boolean expression which must evaluate
to true for the binding to be enabled.
Additional enabling condition.
Wrong Correct
packets packets
Guard Guard
(<> is the (tests whether n and k
Inequality operator) are equal)
Advanced Course on Petri Nets 2010 - 40
Guard must evaluate to true
false RN1 = (ReceiveNext, <n=1, k=2, d="COL", data="COL">)
true RN2 = (ReceiveNext, <n=2, k=2, d="OUR", data="COL">)
Advanced Course on Petri Nets 2010 - 41
Guard must evaluate to true
true DP1 = (DiscardPacket, <n=1, k=2, d="COL" )
false DP2 = (DiscardPacket, <n=2, k=2, d="OUR")
Advanced Course on Petri Nets 2010 - 42
Formal Definition of CPNs
Net structure
Types and
variables
Net inscriptions
Advanced Course on Petri Nets 2010 - 43
Enabling and Occurrence
Advanced Course on Petri Nets 2010 - 44
Part 3: Hierarchical
Protocol CPN Model
Advanced Course on Petri Nets 2010 - 45
CPN Modules
CPN models can be hierarchically organised into a
set of modules with well-defined interfaces:
Makes it possible to split models of large systems into
manageable parts.
Makes it possible to work at different abstraction levels
and have the model reflect the structure of the system.
Makes it possible to create building blocks that are used
repeatedly in the CPN model.
CPN models of larger systems typically have up to
10 abstraction (hierarchical) levels.
CPN models with modules are also called
hierarchical Coloured Petri Nets.
Advanced Course on Petri Nets 2010 - 46
Simple Protocol
The protocol model
can be divided into
three modules:
Sender
Network
Receiver
The buffer places
are used as
interfaces between
the modules.
Sender Network Receiver
Advanced Course on Petri Nets 2010 - 47
Sender Module
Input/output port
Used to import and
export tokens
Output port
Used to export tokens
to the environment
Internal place The module interacts
Local to the with other modules
module
via three port places.
Input port
Used to import tokens
from the environment
Advanced Course on Petri Nets 2010 - 48
Receiver module
Input/output port
Used to import and
export tokens
Input port
Used to import tokens
from the environment
Internal place
Local to the module
Output port
Used to export tokens
to the environment
Advanced Course on Petri Nets 2010 - 49
Network module
Input port Output port
Interaction with Interaction with
the sender the receiver
Output port Input port
Advanced Course on Petri Nets 2010 - 50
Protocol Module
Ties the three other modules together using
substitution transitions.
Provides a more abstract view of the protocol
system.
Name of
substitution
transition
Name of
submodule
Advanced Course on Petri Nets 2010 - 51
Protocol Module
""
AllPackets
Data
Packets I/O
Received
I/O
To Send if success
DATA
NOxDATA then 1`(n,d)
(n,d) (n,d) Transmit else empty
In A B Out
Packet In B
(n,d)
Send (n,d)
A Out
Packet NOxDATA NOxDATA NOxDATA
NOxDATA data if n=k
then data^d
n else data
1`1
1`1 k
NextRec Receive
NextSend Packet
NO if n=k
Out Transmit In then k+1
NO D C else k if n=k
k n
if success Ack n then k+1
else k
NO then 1`n NO Out C
Receive
Ack n
D In else empty
NO
NO
Network Receiver
Sender
Advanced Course on Petri Nets 2010 - 52
Protocol Module
The places connected to the substitution
transitions are socket places.
They constitute the interface for the substitution
transition. Input socket for Network substitution transition
Output socket for Sender substitution transition
Input/output socket
for Sender substitution
transition
Advanced Course on Petri Nets 2010 - 53
Port-Socket Relation
Each port place of a submodule is related to a
socket place of its substitution transition:
input port ↔ input socket.
output port ↔ output socket.
input/output port ↔ input/output socket.
Ports and sockets that are related to each other
constitute a single compound place.
They have the same marking.
When a token is added/removed at one of
them it is also added/removed at the other.
Also the colour sets and initial markings are
required to be identical.
Advanced Course on Petri Nets 2010 - 54
Port-Socket Relation
""
AllPackets
Data
Packets I/O
Received
I/O
To Send if success
DATA
NOxDATA then 1`(n,d)
(n,d) (n,d) Transmit else empty
In A B Out
Packet In B
(n,d)
Send (n,d)
A Out
Packet NOxDATA NOxDATA NOxDATA
NOxDATA data if n=k
then data^d
n else data
1`1
1`1 k
NextRec Receive
NextSend Packet
NO if n=k
Out Transmit In then k+1
NO D C else k if n=k
k n
if success Ack n then k+1
else k
NO then 1`n NO Out C
Receive
Ack n
D In else empty
NO
NO
Network Receiver
Sender
For the protocol system ports and sockets have identical
names, but this is not required in general.
Advanced Course on Petri Nets 2010 - 55
References: Getting Started
K. Jensen, L.M. Kristensen, and L. Wells. Coloured
Petri Nets and CPN Tools for Modelling and
Validation of Concurrent Systems. In International
Journal on Software Tools for Technology Transfer
(STTT), Vol 9, No. 3-4, pp. 213-254. Springer-
Verlag, 2007.
CPN Tools:
[ [Link]/CPNtools ]
(see Download and Installation section)
K. Jensen and L.M. Kristensen.
Coloured Petri Nets: Modelling and Validation
of Concurrent Systems. Springer, 2009.
[book web: [Link]/CPnets/cpnbook/ ]
Advanced Course on Petri Nets 2010 - 56