A Concrete Language For High-Level Petri Nets: Prof - Dr.ir. Wil Van Der Aalst
A Concrete Language For High-Level Petri Nets: Prof - Dr.ir. Wil Van Der Aalst
A Concrete Language For High-Level Petri Nets: Prof - Dr.ir. Wil Van Der Aalst
PAGE 1
5 philosophers
PAGE 2
CPN (Colored Petri nets)
CPN-3
Values and types
Outline:
• Basic types: int, string, bool, (real), and unit.
• Type constructors: with, product, record, list.
• Defining constants.
CPN-4
Basic types
CPN-5
Basic operators
CPN-6
Logical operators
CPN-7
Exercise: Give type and value of each
result
a) if (4>=4) then ("Hello" ^ " " ^ "World") else "X“
CPN-8
Color set declarations
1 "color" is shown as "colset" in CPN Tools, but one can type "color"
CPN-9
Creating subtypes using the "with"
clause
CPN-10
Creating new types using the "with"
clause
CPN-11
Creating new types using product,
record, and list constructors
CPN-12
Possible values (colors)
CPN-13
Example
CPN-14
Example (2)
[{d="Jos Verstappen",
r=[(1,31000),(2,33400),(3,32800)]},
{d="Michael Schumacher",
r=[(1,32200),(2,31600),(3,30200),(4,29600)]},
{d="Rubens Barrichello",
r=[(1,34500),(2,32600),(3,37200),(4,42600)]}]
CPN-15
Operations on lists and records
CPN-16
Constants
CPN-17
Example
CPN-18
Exercise
CPN-19
So what?
CPN-20
We can now type and initialize places!
race Monaco
type of
place Race
initial
marking
CPN-21
Multi-sets
CPN-22
Initialization expressions
p1 p1 1 p1 1`2 ++ 5`4
CPN-23
Trick
INT INT
CPN-24
Arc inscriptions
t1
s
p2
STR CPN-25
Example
t1
x+2
p2
• Give final marking.
INT
CPN-26
Binding
CPN-27
Example
t1
x+2
p2
INT
CPN-28
Example
t1
x
p2
INT CPN-29
Example
No binding
(t2,<x=1,y=2>)
possible!
color INT = int;
var x:INT;
var y:INT;
p1 1 p2 2 p4 1 p5 2 p7 1 p8 2
t1 t2 t3
x x+y y
p3 p6 p9
(t2,<x=1,y=3>)
CPN-30
Exercise
p1 2`5++3`7
x x
p2 p3
INT INT
CPN-31
Exercise
p4 2`5++3`7
color INT = int;
var x:INT; INT
var y:INT; x
t2
if x <=6 if x >6
then 1`x then 1`x
else empty x else empty
p5 p6
INT INT
CPN-32
Exercise
t3
if x >6
2'x ++ 3'(x-5) then 4`x
else 5`x
p8 p9
INT INT
CPN-33
Exercise
STR S
CPN-34
Exercise
INT S
CPN-35
Example: Voting
give_vote
(x,y) (x,y+1)
votes 1`(CDA,0) ++
1`(PVDA,0) ++
PC 1`(VVD,0)
CPN-37
CPN-38
Exercise: Bank
CPN-39
Exercise: Article database
CPN-41
Guard
p3
INT
CPN-42
Example
p4 1 p5 2 p7 1 p8 2
p6 p9
INT INT
CPN-43
Exercise
mail {p="John",t="Hello"}
color Person= str;
color Text = str; Mail
color Mail = record p:Person * t:Text; x x
var x:Mail; get1 get2
x [#p(x)="John"] x [#p(x)="Sue"]
inbox_John inbox_Sue
Mail Mail
CPN-44
Exercise
p1 1`2++1`7++1`15
color INT = int;
var x:INT; INT
x x
t1 t2
[x>5] [x<10]
x x
p2 p3
INT INT
CPN-45
Exercise
color Account = int with 1..1000;
color Balance = int; deposit
color Amount = int with 1..5000;
color AB = product Account * Balance; AA
color AA = product Account * Amount; (a,x)
var a:Account;
var x:Amount;
var y:Balance;
deposit
(a,y) (a,x+y)
AA (a,y) AB
withdraw
• The CPN model assumes that an account could have
a negative balance. Change the model such that the
balance cannot become negative, i.e., do not accept
transactions which lead to a negative balance.
CPN-46
Function declarations
CPN-47
Where to find standard functions?
• These sheets.
• cpntools.org, see for example
http://cpntools.org/documentation/concepts/colors/declarations/co
lorsets/list_colour_sets and
http://cpntools.org/documentation/concepts/colors/declarations/co
lorsets/colour_set_functions
color INT:int;
color Author = string;
color Article = string;
color AL = list Article;
color AAL = product Author * AL;
var x:Author;
var y:AL;
fun count(z:AL) = if z=[] then 0 else 1+tl(z)
how_many
x
(x,y) database
Author
4x
CPN-50
Example: Multiple arcs
color I = int;
color U = unit;
color L = list I;
color R = record a:I * b:I; I I
var x:I; y
x x
var y:I;
var z:I;
var s:L;
x y y
multiple arcs
are allowed
I I
CPN-51
Example: Multi-sets and constants
multisets on
constants are
color I = int; input arcs are
allowed
color U = unit; allowed
color L = list I;
color R = record a:I * b:I;
var x:I;
var y:I;
var z:I; I I U I
var s:L;
1`x++1`y x 2`() 5+4*5
x 2`y () 2`7
I I U I
CPN-52
Example: Records
color I = int;
color U = unit;
records with
color L = list I; I
variables as R
color R = record a:I * b:I;
arc inscriptions
var x:I; {a=x,b=y} z
var y:I;
var z:I;
var s:L;
{a=y,b=z} x
R I
CPN-53
Example: Lists
color I = int;
color U = unit;
color L = list I; I L
color R = record a:I * b:I; y y::s
var x:I;
var y:I;
var z:I;
var s:L;
s s^^[y]
lists with
variables as arc
inscriptions
L L
CPN-54
Requirement
color I = int;
color U = unit; It should be possible to
color L = list I;
color R = record a:I * b:I;
var x:I;
bind variables to
var y:I;
var z:I; concrete token values!!
var s:L;
ERROR: I I
I I
z is unbound!
x y x y
[z>4]
z x x y
ERROR:
z is unbound!
I I I I
CPN-55
Trick: use lists on arcs to
produce/consume multi-sets of tokens
CPN-56
Another example
PAGE 57
Priority (no priority P_NORMAL = 1000)
PAGE 58
Priority (same)
PAGE 59
Priority (P_HIGH wins)
PAGE 60
Priority: Guess final state
PAGE 61
Result
PAGE 62
Global property (t2 never fires)
PAGE 63
Time in CPN
CPN-64
Example
p1 "Hi" p2 4
STR INT
color STR = string timed; x y
var x:STR;
color INT = int; t1
var y:INT;
x@+1 x@+y
p3 p4
STR STR
CPN-65
Exercise
p5 2`"Hi"++2`"Ho"
y y+1 x@+y
p6 0 p7
INT STR
CPN-66
Time (t1 is enabled at time 2)
PAGE 67
t1 fired at time 2; t2 is enabled at time 4
PAGE 68
“Real” time
PAGE 69
Note the types and the @++
PAGE 70
Determine final state
PAGE 71
Final state (time = 10000)
PAGE 72
Overview of CPN (with color and time)
timed color
declarations initialization
set delay
expression
transition
function variable name
declaration
CPN-74
Coffee and tea example (1)
coffee 12 4
CPN-75
Coffee and tea example (2)
priorities/preferences? coffee 12 4
CPN-76
Ready @ +/- 620
Eve just makes tea and Adam just makes coffee.
Ready @ 400
Adam makes coffee and Eve can make both
Ready @ 356
Adam: 87 coffee
Eve: 100 tea and 13 coffee Optimal
Makespan = 356 Adam: 88 coffee
Eve: 100 tea and 12 coffee
Makespan = 352
M/G/1 queue
CPN'Replications.nreplications 10
Results with confidence intervals
Will be explained in detail …
Coffee and tea example (3)
CPN-96
Flow
PAGE 101
FIFO
PAGE 102
Coffee and tea example (4)
CPN-103
LIFO
PAGE 104
LIFO
PAGE 105
SPT
PAGE 106
PAGE 107
SPT
PAGE 108
SMART
PAGE 109
PAGE 110
SMART
PAGE 111
FIFO
LIFO
Compare (1/2) SPT
SMART
PAGE 112
FIFO
LIFO
Compare (2/2) SPT
SMART
free 1`Ann
CPN-114
Improved color sets
color Name = string;
color Street = string;
color Number = int;
color Town = string;
color Address = record s:Street * n:Number *
t:Town;
color Day = int with 1..31;
color Month = with Jan | Feb | Mar | Apr | May | Jun |
Jul | Aug | Sep | Oct | Nov | Dec;
color Year = int with 0..2100;
color Date = record d:Day * m:Month * y:Year;
color Gender = with male | female;
color Pat = record name:Name * address:Address *
birthdate:Date * gender:Gender timed; CPN-115
Improved color sets (2)
1`{prod="bell",
in number=3} s stock 1`initstock s [check(descrs(x,s))] out
x
x
StockItem incrs(x,s) descrs(x,s)
increase Stock decrease StockItem
CPN-117
Store
CPN-118
Function "totalstock"
fun totalstock(s:Stock) =
if s=[ ]
then 0
else (#number(hd(s)))+totalstock(tl(s));
color Product = string;
color Number = int;
color StockItem = record prod:Product * number:Number;
color Stock = list StockItem;
var x:StockItem;
var s:Stock;
fun incrs(x:StockItem,s:Stock) = if s=[] then [x] else (if (#prod(hd(s)))=(#prod(x))
then {prod=(#prod(hd(s))),number=((#number(hd(s)))+(#number(x)))}::tl(s)
else hd(s):: incrs(x,tl(s)));
fun decrs(x:StockItem,s:Stock)= incrs({prod=(#prod(x)),number=(~(#number(x)))},s);
fun check(s:Stock)= if s=[] then true else if (#number(hd(s)))<0 then false
else check(tl(s));
val initstock = [{prod="bike", number=4},{prod="wheel", number=2},
{prod="bell", number=3}, {prod="steering wheel", number=3},
{prod="frame", number=2}];
1`{prod="bell",
in number=3} s stock 1`initstock s [check(descrs(x,s))] out
x
x
StockItem incrs(x,s) descrs(x,s)
increase Stock decrease StockItem
CPN-119
Alternative model
1`("bike",4)++
1`("wheel",2)++
1`("bell",3)++
1`("steering wheel",3)++
1`("bell",2) 1`("frame",2) [y>=x]
in (p,y) stock (p,y) out
(p,x)
(p,x)
StockItem (p,x+y) (p,y-x)
increase StockItem decrease StockItem
CPN-120
Example: Signing documents
CPN-121
Signing documents: Declarations
CPN-122
Signing documents: Network structure
BlackToken
CPN-123
Exercise
BlackToken
CPN-124
Example: Thermostat system
CPN-125
CPN model of thermostat system
B B
a a a a
[t>22] [t<18]
a a
switch_off switch_on
t t
t t
heater 1`()
b t temp 1`15 t b door 1`()
CPN-126
Exercise
CPN-127
WARNING
It is not sufficient to understand the
(process) models. You have to be able to
design them yourself !
CPN-128
sector 0 sector 1
Exercise:
Train system Train A
sector 2
sector 6
• 7 sectors (tracks)
Train B
• 2 trains: A and B sector 3
PAGE 130
Partially folded
PAGE 131
Trains and tracks folded
PAGE 132
Exercise: Philosophers
ph1
cs1 cs2
• 5 philosophers
• 5 chopsticks ph5 Rice ph2
Dish
• Each philosopher is
cs5
either thinking or eating. cs3
• For eating two chopsticks ph4 ph3
cs4
are needed.
• Chopsticks need to be
shared among neighbors. • Model as a classical Petri
• Both chopsticks are net.
taken and released at the • Model in terms of CPN
same time. using only three places
and two transitions.
CPN-133
Classical Petri net
PAGE 134
Folded
PAGE 135
Exercise: Philosophers (2)
ph1
• 5 philosophers cs1 cs2
• 5 chopsticks
• Each philosopher is either ph5 Rice ph2
thinking or eating. Dish
CPN-136
Initial state
PAGE 137
4 is eating, 3 took his right chopstick
PAGE 138
Deadlock
PAGE 139
From state space report
• State Space
− Nodes: 392
− Arcs: 1415
• One home marking
• One dead marking
PAGE 140
Adding philosophers (n=8)
Nodes: 14158
Arcs: 81848
PAGE 141
Exercise: Philosophers (3)
ph1
• 5 philosophers cs1 cs2
• 5 chopsticks
• Each philosopher is either ph5 Rice ph2
thinking or eating. Dish
CPN-142
Model
PAGE 143
State space analysis
• 1473 states
• 6270 transitions
• two dead markings
PAGE 144
Tradeoff
PAGE 145
More on functions: Recursion
CPN-146
Recursion (1)
fun maxstockname(s:Stock) =
if s = [ ]
then "no product found"
else if (#number(hd(s)))=maxstock(tl(s)) then #prod(hd(s))
else maxstockname(tl(s));
Prod:Product Number:number
"apple" 301
"orange" 504
"pear" 423 "orange"
“banana" 134
… …
CPN-149
Function
Recursion (4) has two
arguments
fun enoughstock(s:Stock,n:Number) =
if s = [ ]
then [ ]
else if (#number(hd(s)))>= n then hd(s)::enoughstock(tl(s),n)
else enoughstock(tl(s),n);
CPN-151
More on functions: Pattern matching
fun lenlist1(s:Stock) =
if s = [ ]
then 0
else 1+lenlist(tl(s));
base case
fun lenlist2([ ]) = 0 |
induction step
lenlist2(si::s) = 1+lenlist2(s);
No explicit typing!!!
CPN-152
Pattern matching (1)
fun totalstock(s:Stock) =
if s = [ ]
then 0
else (#number(hd(s)))+totalstock(tl(s));
CPN-153
Pattern matching (2)
fun maxstock(s:Stock) =
if s=[ ]
then 0
else if (#number(hd(s))) >= maxstock(tl(s)) then #number(hd(s))
else maxstock(tl(s));
CPN-154
Pattern matching (3)
fun incrs(x:StockItem,[ ]:Stock) = [x] |
incrs (x,(si::s)) =
if (#prod(si))=(#prod(x))
then {prod=(#prod(si)),
number=((#number(si))+(#number(x)))}
::incrs(x,s)
else (si::incrs(x,s));
CPN-157
PAGE 158
Example: Sudoku for 9 rows and
columns
CPN-159
Input 0 values are not inserted
val v4 = [
[6,0,0, 0,8,0, 0,0,9],
fun readcell(x,i,j) = if x=[ ] then [ ] else if
[0,7,0, 4,0,6, 0,8,0],
hd(x) = 0 then readcell(tl(x),i,j+1) else
[0,0,0, 5,0,1, 0,0,0],
((i,j),hd(x))::readcell(tl(x),i,j+1);
fun readrow(x,i) = if x=[ ] then [ ] else
[0,1,7, 2,0,9, 8,5,0], readcell(hd(x),i,0)^^readrow(tl(x),i+1);
[2,0,0, 0,0,0, 0,0,1], fun read(x) = readrow(x,0):Sudoku;
[0,8,4, 1,0,3, 6,7,0],
difference of two
sets
CPN-161
Basic functions
same block values in the block
containing (i,j)
remaining options
CPN-162
Possible moves
given an s of type
all possible
Sudoku, all undefined
values of type
positions are returned
Pos, i.e., list of all
cells
CPN-163
add error entry
Solve (no options left)
add entry with just
one possible move c
fun new([ ]) = [ ] |
new(((i,j),[ ])::s) = ((i,j),~1)::new(s) |
new(((i,j),[c])::s) = ((i,j),c)::new(s) | skip if multiple
moves possible
new(((i,j),c::cs)::s) = new(s);
fun solve(s) =
if new(analyze(s)) = [ ]
then s
else solve(new(analyze(s))^^s);
CPN-164
Sort results
CPN-165
Generate string (just for presentation)
CPN-166
CPN-167
note the
two 1’s in
middle
PAGE 168
block
More information
• About Standard ML:
• Robin Milner, Mads Tofte, Robert Harper, and David MacQueen.The Definition of
Standard ML: Revised 1997. The MIT Press, 1997.
• J. D. Ullman. Elements of ML Programming (ML 97 edition). Prentice-Hall, 1998.
• http://www.standardml.org/Basis/ (for functions)
• About CPN:
• K. Jensen and L.M. Kristensen. Coloured Petri Nets: Modelling and Validation of
Concurrent Systems, Springer-Verlag, 2009.
• W. van der Aalst and C. Stahl. Modeling Business Processes: A Petri Net-Oriented
Approach. MIT Press, 2011.
• K. Jensen: Coloured Petri Nets. Basic Concepts, Analysis Methods and Practical
Use. Volume 1, Basic Concepts. Monographs in Theoretical Computer Science,
Springer-Verlag, 1997.
• K. Jensen: Coloured Petri Nets. Basic Concepts, Analysis Methods and Practical
Use. Volume 2, Analysis Methods. Monographs in Theoretical Computer Science,
Springer-Verlag, 1997.
• K. Jensen: Coloured Petri Nets. Basic Concepts, Analysis Methods and Practical
Use. Volume 3, Practical Use. Monographs in Theoretical Computer Science,
Springer-Verlag, 1997.
• K. Jensen and G. Rozenberg (eds.): High-level Petri Nets. Theory and Application.
Springer-Verlag, 1991.
CPN-169