kurt jensen lars m. kristensen 1 coloured petri nets department of computer science coloured petri...

63
Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems Chapter 3: CPN ML Programming colset PACKETS = list PACKET; var packets : PACKETS; fun member (e,l) = let fun equal x = (e=x) in exists (equal,l) end; Kurt Jensen & Lars Michael Kristensen {kjensen,lmkristensen} @cs.au.dk

Upload: brenda-marn

Post on 11-Dec-2015

218 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

1

Coloured Petri NetsDepartment of Computer Science

Coloured Petri NetsModelling and Validation of Concurrent Systems

Chapter 3: CPN ML Programming

colset PACKETS = list PACKET;var packets : PACKETS;fun member (e,l) = let fun equal x = (e=x) in exists (equal,l) end;

Kurt Jensen &Lars Michael Kristensen

{kjensen,lmkristensen}@cs.au.dk

Page 2: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

2

Coloured Petri NetsDepartment of Computer Science

CPN ML programming language

Based on the functional programming language Standard ML.

CPN ML extends the Standard ML environment with: Constructs for defining colour sets and declaring variables. Concept of multisets and associated functions and operators.

Standard ML plays a major role in CPN modelling and CPN Tools: Provides the expressiveness required to model data and data

manipulation as found in typical industrial projects. Used to implement simulation, state space analysis, and

performance analysis in CPN Tools. Supports a flexible and open architecture that makes it

possible to develop extensions and prototypes in CPN Tools.

Page 3: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

3

Coloured Petri NetsDepartment of Computer Science

Why Standard ML?

Formal definition of CP-nets uses types, variables, and evaluation of expressions, which are basic concepts from functional programming.

Patterns in functional programming languages provide an elegant way of implementing enabling inference.

Standard ML is based on the lambda-calculus which has a formal syntax and semantics. This implies that CPN Tools get an expressive and sound formal foundation.

Standard ML is supported by mature compilers, associated documentation and textbooks.

Page 4: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

4

Coloured Petri NetsDepartment of Computer Science

Functional programming and CPN ML

Computation proceeds by evaluation of expressions not by executing statements making modifications to memory locations.

Strong typing means that all expressions have a type that can be determined at compile time. This eliminates many run-time errors.

Types of expressions are inferred by the type system rather than being declared by the user.

Functions are first-order values and is treated in the same way as basic types such as integers, Booleans, and strings.

Functions can be polymorphic and hence operate on different types of values.

Recursion is used to express iterative constructs.

Page 5: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

5

Coloured Petri NetsDepartment of Computer Science

Simple colour sets A set of basic types for defining simple colour sets:

Integers - int: {…, ~2 , ~1 , 0 , 1 , 2 ,…} Strings - string: {"a" , "abc" ,…} Booleans - bool: {true,false} Unit - unit: {()}

Two other kinds of simple colour sets: enumeration colour sets. indexed colour sets.

colset INT = int;colset STRING = string;colset BOOL = bool;colset UNIT = unit;

Standard colour set definitions:

Page 6: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

6

Coloured Petri NetsDepartment of Computer Science

Structured colour sets Structured colours sets are defined using

colour set constructors: Products Records Unions Lists Subsets

colset NOxDATA = product NO * DATA;colset DATAPACK = record seq:NO * data:DATA;colset PACKET = union Data:DATAPACK + Ack:ACKPACK;colset PACKETS = list PACKET;

Page 7: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

7

Coloured Petri NetsDepartment of Computer Science

Simple protocol

We will now develop a new version where: Data packets are modelled as a record colour set. Data packets and acknowledgement packets are modelled by

a common union colour set. We have duplication of packets – in addition to loss and

successful transmission.

data

n if successthen 1`nelse empty

n

if n=kthen k+1else k

(n,d)(n,d)

nif n=k then data^delse data

(n,d)

if successthen 1`(n,d)else empty

(n,d)

ReceiveAck

TransmitAck

ReceivePacket

TransmitPacket

SendPacket

1`1

NO

CD

NO

A

NOxDATA

NextSend

1`1

NO

DataReceived

1`""

DATA

B

NOxDATA

PacketsTo Send

AllPackets

NOxDATA

k n

NextRec

k

if n=kthen k+1else k

NO

1 1`1

11`""6

1`(1,"COL")++1`(2,"OUR")++1`(3,"ED ")++1`(4,"PET")++1`(5,"RI ")++1`(6,"NET")

11`1 The previous versions use products torepresent data packets.

Page 8: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

8

Coloured Petri NetsDepartment of Computer Science

Revised colour set definitions

colset DATA = string;colset NO = int;colset NOxDATA = product NO * DATA;

colset DATAPACK = record seq : NO * data : DATA;colset ACKPACK = NO;colset PACKET = union Data : DATAPACK + Ack : ACKPACK;

Record field names

Data constructors

Old definitions:

New definitions:

Enumeration colour set (with three explicitly specified data values)

colset RESULT = with success | failure | duplicate;

Page 9: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

9

Coloured Petri NetsDepartment of Computer Science

Example values

colset DATAPACK = record seq : NO * data : DATA;

{seq=1,data="COL"}

Data{seq=1,data="COL"}

Ack(2)

colset PACKET = union Data : DATAPACK + Ack : ACKPACK;

Data packet

Acknowledgement packet

Record colour set:

Union colour set:

{data="COL",seq=1,}

Data constructors

colset ACKPACK = NO;

Same data value

Page 10: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

10

Coloured Petri NetsDepartment of Computer Science

k n

Ack(n) if res=successthen 1`packelse if res = duplicate then 2`pack else empty

pack

if n=kthen Ack(k+1)else Ack(k)

pack

Data ({seq=n, data=d})

nif n=k then data^delse data

Data ({seq=n, data=d})

if res=successthen 1`packelse if res = duplicate then 2`pack else empty

(n,d)

ReceiveAck

TransmitAck

Receive Packet

TransmitPacket

SendPacket

1`1

NO

C

PACKET

D

PACKET

A

PACKET

NextSend

1`1

NO

DataReceived

1`""

DATA

B

PACKET

PacketsTo Send

AllPackets

NOxDATA

NextReck

if n=kthen k+1else k

data

Revised CPN modelvar n,k : NO;var d,data : DATA;

var pack : PACKET;var res : RESULT;

Page 11: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

11

Coloured Petri NetsDepartment of Computer Science

Transmit Packet transition

pack

if res=successthen 1`packelse if res = duplicate then 2`pack else emptyTransmit

PacketA

PACKET

B

PACKET

1

1`Data({seq=1,data="COL"})

pack

if res=successthen 1`packelse if res = duplicate then 2`pack else emptyTransmit

PacketA

PACKET

B

PACKET

22`Data({seq=1,data="COL"})

b+ = <pack=Data({seq=1,data="COL"}), res=success> b

– = <pack=Data({seq=1,data="COL"}), res=failure>b++ = <pack=Data({seq=1,data="COL"}), res=duplicate>

var pack : PACKET;var res : RESULT;

Page 12: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

12

Coloured Petri NetsDepartment of Computer Science

Tuples and records

Tuple components and record fields can be accessed using the family of # operators.

#seq {seq=1,data="COL"} 1

#data {seq=1,data="COL"} "COL"

#1 (3,"ED ") 3

#2 (3,"ED ") "ED "

Records

Products

Examples:

Page 13: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

13

Coloured Petri NetsDepartment of Computer Science

k n

Ack(n) if res=successthen 1`packelse if res = duplicate then 2`pack else empty

pack

if n=kthen Ack(k+1)else Ack(k)

pack

Data ({seq=n, data=d})

nif n=k then data^delse data

Data ({seq=n, data=d})

if res=successthen 1`packelse if res = duplicate then 2`pack else empty

(n,d)

ReceiveAck

TransmitAck

Receive Packet

TransmitPacket

SendPacket

1`1

NO

C

PACKET

D

PACKET

A

PACKET

NextSend

1`1

NO

DataReceived

1`""

DATA

B

PACKET

PacketsTo Send

AllPackets

NOxDATA

NextReck

if n=kthen k+1else k

data

Receiver part Binds the variables n and d

Page 14: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

14

Coloured Petri NetsDepartment of Computer Science

data

Data datapack

Receive Packet

1`1

NO

C

PACKET

1`""

DATA

B

PACKET

DataReceived

if (#seq datapack)=k then data^(#data datapack)else data

if (#seq datapack)=kthen k+1else k

k

NextRec

if (#seq datapack)=kthen Ack(k+1)else Ack(k)

First variant of receiver

var datapack : DATAPACK;Binds variable datapack

#seq datapackis used three times

Page 15: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

15

Coloured Petri NetsDepartment of Computer Science

Second variant of the receiver

var datapack : DATAPACK;var n : NO;

B

PACKET

DataReceived

1`""

DATA

C

PACKET

1`1

NO

Receive Packet

k

Data datapack

data if n=k then data^(#data datapack)else data

[n = (#seq datapack)]

if n=kthen Ack(k+1)else Ack(k)

NextRec

if n=kthen k+1else k

Guard binds variable nusing selector

Binds variable datapack

Page 16: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

16

Coloured Petri NetsDepartment of Computer Science

Sender part

k n

Ack(n) if res=successthen 1`packelse if res = duplicate then 2`pack else empty

pack

if n=kthen Ack(k+1)else Ack(k)

pack

Data ({seq=n, data=d})

nif n=k then data^delse data

Data ({seq=n, data=d})

if res=successthen 1`packelse if res = duplicate then 2`pack else empty

(n,d)

ReceiveAck

TransmitAck

Receive Packet

TransmitPacket

SendPacket

1`1

NO

C

PACKET

D

PACKET

A

PACKET

NextSend

1`1

NO

DataReceived

1`""

DATA

B

PACKET

PacketsTo Send

AllPackets

NOxDATA

NextReck

if n=kthen k+1else k

data

Binds variables n and d

Page 17: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

17

Coloured Petri NetsDepartment of Computer Science

Variant of the sender

var nextpack : NOxDATA;var n : NO;var d : DATA;

Data{seq=n,data=d}

n

nextpack

SendPacket

[n=(#1 nextpack), d=(#2 nextpack)]

A

PACKET

NextSend

1`1

NO

AllPackets

NOxDATA

PacketsTo Send

Guard binds variables n and d using selectors

Binds variable nextpack

Page 18: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

18

Coloured Petri NetsDepartment of Computer Science

Products or records?

There is a always a choice between using product or record colour sets.

Products may give shorter net inscriptions, because we avoid the selector names used in records.

Records may give more readable net inscriptions due to the mnemonic selector names. The same effect can often be achieved for products by using variables with mnemonic names, e.g. (seq,data).

As a rule of thumb we do not recommend using products with more than 4-5 components. In such cases it is better to use records.

Page 19: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

19

Coloured Petri NetsDepartment of Computer Science

Overtaking is possible

k

if n=kthen k+1else k

k

data

n

n if successthen 1`nelse empty

n

if n=kthen k+1else k

(n,d)(n,d)

nif n=k then data^delse data

(n,d)

if successthen 1`(n,d)else empty

(n,d)

ReceiveAck

TransmitAck

ReceivePacket

TransmitPacket

SendPacket

NextRec

1`1

NO

C

NO

D

NO

A

NOxDATA

NextSend

1`1

NO

DataReceived

1`""

DATA

B

NOxDATA

PacketsTo Send

AllPackets

NOxDATA

11`3

1 1`3

3

2`(2,"OUR")++1`(3,"ED ")

1 1`3

11`"COLOUR"

2

2`(2,"OUR")

6

1`(1,"COL")++1`(2,"OUR")++1`(3,"ED ")++1`(4,"PET")++1`(5,"RI ")++1`(6,"NET")

We will develop a new version where overtaking of data packets and acknowledgements is impossible.

Page 20: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

20

Coloured Petri NetsDepartment of Computer Science

List colour sets Colour set definitions:

colset DATAPACKS = list NOxDATA;colset ACKPACKS = list NO;

Example values:

[(1,"COL"),(1,"COL"),(2,"OUR")]

[2,2,3,3]

[]

Four acknowledgement packets

Three data packets

Empty list (polymorphic)

Page 21: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

21

Coloured Petri NetsDepartment of Computer Science

List concatenation (^^)

[(1,"COL"),(1,"COL")]^^[(2,"OUR"),(3,"ED ")]

[(1,"COL"),(1,"COL"),(2,"OUR"),(3,"ED ")]

List List

List

Result:

Application:

Page 22: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

22

Coloured Petri NetsDepartment of Computer Science

List construction (::)

(1,"COL")::[(1,"COL"),(2,"OUR")]

[(1,"COL"),(1,"COL"),(2,"OUR")]

Element List

List

Result:

Application:

Page 23: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

23

Coloured Petri NetsDepartment of Computer Science

datapacks

datapacks^^[(n,d)]

n

(n,d)

SendPacket

A

[]

DATAPACKS

NextSend

1`1

NO

PacketsTo Send

AllPackets

NOxDATA

Revised SendPacket

var n : NO;var d : DATA;var datapacks : DATAPACKS;

List colour set

Initial marking is the empty list

Page 24: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

24

Coloured Petri NetsDepartment of Computer Science

datapacks

datapacks^^[(n,d)]

n

(n,d)

SendPacket A

[]

DATAPACKS

NextSend

1`1

NO

PacketsTo Send

AllPackets

NOxDATA

1

1`[(1,"COL"),(1,"COL")]

1 1`2

6

1`(1,"COL")++1`(2,"OUR")++1`(3,"ED ")++1`(4,"PET")++1`(5,"RI ")++1`(6,"NET")

Enabling of SendPacket

<n=2,d="OUR",datapacks=[(1,"COL"),(1,"COL")]>

[(1,"COL"),(1,"COL"),(2,"OUR")]

Page 25: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

25

Coloured Petri NetsDepartment of Computer Science

datapacks

datapacks^^[(n,d)]

n

(n,d)

SendPacket

A

[]

DATAPACKS

NextSend

1`1

NO

PacketsTo Send

AllPackets

NOxDATA

1

1`[(1,"COL"),(1,"COL"),(2,"OUR")]

1 1`2

6

1`(1,"COL")++1`(2,"OUR")++1`(3,"ED ")++1`(4,"PET")++1`(5,"RI ")++1`(6,"NET")

Occurrence of SendPacket

A copy of packet number two has

been added to the end of the list

Page 26: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

26

Coloured Petri NetsDepartment of Computer Science

if successthen datapacks2^^[p]else datapacks2

datapacks2

TransmitPacketA

[]

DATAPACKS

B

[]

DATAPACKS

p::datapacks1

datapacks1

Revised TransmitPacket

var p : NOxDATA;var success : BOOL;var datapacks1,datapacks2 : DATAPACKS;

List colour set

Initial marking is the empty list

List colour set

Initial marking is the empty list

Page 27: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

27

Coloured Petri NetsDepartment of Computer Science

if successthen datapacks2^^[p]else datapacks2

datapacks2

TransmitPacketA

[]

DATAPACKS

B

[]

DATAPACKS

p::datapacks1

datapacks1

1

1`[(1,"COL"),(1,"COL"),(2,"OUR")]

1

1`[]

Enabling of TransmitPacket

b+ = <p=(1,"COL"),datapacks1=[(1,"COL"),(2,"OUR")], success=true,datapacks2=[ ]>

b – = <p=(1,"COL")},datapacks1=[(1,"COL"),(2,"OUR")],

success=false,datapacks2=[ ]>

Page 28: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

28

Coloured Petri NetsDepartment of Computer Science

if successthen datapacks2^^[p]else datapacks2

datapacks2

TransmitPacket

A

[]

DATAPACKS

B

[]

DATAPACKS

p::datapacks1

datapacks1

1

1`[(1,"COL"),(2,"OUR")]

1

1`[(1,"COL")]

if successthen datapacks2^^[p]else datapacks2

datapacks2

TransmitPacketA

[]

DATAPACKS

B

[]

DATAPACKS

p::datapacks1

datapacks1

1

1`[(1,"COL"),(1,"COL"),(2,"OUR")]

1

1`[]

Successful transmission

b+ = <p=(1,"COL"),datapacks1=[(1,"COL"),(2,"OUR")], success=true,datapacks2=[ ]>

[(1,"COL")]

[(1,"COL"),(2,"OUR")]

The first element from the A-list has been moved to the end of the B-list

Page 29: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

29

Coloured Petri NetsDepartment of Computer Science

n::ackpacks

ackpacks

datapacks

k n

datapacks^^[(n,d)]

(n,d)

D

[]

ACKPACKS

A

[]

DATAPACKS

1`1

NO

AllPackets

NOxDATA

ReceiveAck

n

PacketsTo Send

SendPacket

NextSend

Revised sender

var n : NO;var d : DATA;var ackpacks : ACKPACKS;var datapacks : DATAPACKS;

Page 30: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

30

Coloured Petri NetsDepartment of Computer Science

ackpacks2

datapacks2

ackpacks1

datapacks1

n::ackpacks1

p::datapacks1

if successthen datapacks2^^[p]else datapacks2

TransmitAck

TransmitPacket

C

[]

ACKPACKS

D

[]

ACKPACKS

A

[]

DATAPACKS

B

[]

DATAPACKS

if successthen ackpacks2^^[n]else ackpacks2

Revised network

var n : NO;var p : DATAPACK;var success : BOOL;var ackpacks1,ackpacks2 : ACKPACKS;var datapacks1,datapacks2 : DATAPACKS;

Page 31: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

31

Coloured Petri NetsDepartment of Computer Science

if k=nthen k+1else k

k

ackpacks

datapacksdata

ackpacks^^[if n=k then k+1 else k]

if n=k then data^delse data

(n,d)::datapacks

ReceivePacket

NextRec

1`1

NO

C

[]

ACKPACKS

DataReceived

1`""

DATA

B

[]

DATAPACKS

Revised receiver

var n,k : NO;var d,data : DATA;

var ackpacks : ACKPACKS;var datapacks: DATAPACKS;

Page 32: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

32

Coloured Petri NetsDepartment of Computer Science

Expressions and types The complete set of Standard ML expressions can be used in

net inscriptions provided that they have the proper type:

The type of an arc expression must be equal to the colour set of the place connected to the arc (or a multiset over the colour set of the place).

The type of an initial marking must be equal to the colour set of the place (or a multiset over the colour set of the place).

A guard must be a Boolean expression (or a list of Boolean expressions).

The CPN ML type system checks that all net inscriptions aretype consistent and satisfies the above type constraints.

This is done by automatically inferring the types of expressions.

Page 33: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

33

Coloured Petri NetsDepartment of Computer Science

Example of type checking

data

n if successthen 1`nelse empty

n

if n=kthen k+1else k

(n,d)(n,d)

nif n=k then data^delse data

(n,d)

if successthen 1`(n,d)else empty

(n,d)

ReceiveAck

TransmitAck

ReceivePacket

TransmitPacket

SendPacket

1`1

NO

CD

NO

A

NOxDATA

NextSend

1`1

NO

DataReceived

1`""

DATA

B

NOxDATA

PacketsTo Send

AllPackets

NOxDATA

k n

NextRec

k

if n=kthen k+1else k

NO

1 1`1

11`""6

1`(1,"COL")++1`(2,"OUR")++1`(3,"ED ")++1`(4,"PET")++1`(5,"RI ")++1`(6,"NET")

11`1

Page 34: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

34

Coloured Petri NetsDepartment of Computer Science

Type checking of (n,d)

var n : NO;var d : DATA;

(n,d)

n dNO DATA

NO * DATA

colset NOxDATA = product NO * DATA;

(n,d) is type consistent and of type NO * DATA (which is the colour set of the connected place).

Arc expression

Sub-expressions

Page 35: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

35

Coloured Petri NetsDepartment of Computer Science

Second example of type checking

data

n if successthen 1`nelse empty

n

if n=kthen k+1else k

(n,d)(n,d)

nif n=k then data^delse data

(n,d)

if successthen 1`(n,d)else empty

(n,d)

ReceiveAck

TransmitAck

ReceivePacket

TransmitPacket

SendPacket

1`1

NO

CD

NO

A

NOxDATA

NextSend

1`1

NO

DataReceived

1`""

DATA

B

NOxDATA

PacketsTo Send

AllPackets

NOxDATA

k n

NextRec

k

if n=kthen k+1else k

NO

1 1`1

11`""6

1`(1,"COL")++1`(2,"OUR")++1`(3,"ED ")++1`(4,"PET")++1`(5,"RI ")++1`(6,"NET")

11`1

Page 36: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

36

Coloured Petri NetsDepartment of Computer Science

Type checking of if expression

If expression is type consistent and of type DATA (which is the colour set of the connected place).

Arc expressioncolset DATA = string;

var n,k : NO;var d,data : DATA;

if n=kthen data^delse data

n k data dNO NO DATA DATA

bool DATA DATA

DATA

n=k data^d data

Page 37: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

37

Coloured Petri NetsDepartment of Computer Science

Third example of type checking

data

n if successthen 1`nelse empty

n

if n=kthen k+1else k

(n,d)(n,d)

nif n=k then data^delse data

(n,d)

if successthen 1`(n,d)else empty

(n,d)

ReceiveAck

TransmitAck

ReceivePacket

TransmitPacket

SendPacket

1`1

NO

CD

NO

A

NOxDATA

NextSend

1`1

NO

DataReceived

1`""

DATA

B

NOxDATA

PacketsTo Send

AllPackets

NOxDATA

k n

NextRec

k

if n=kthen k+1else k

NO

1 1`1

11`""6

1`(1,"COL")++1`(2,"OUR")++1`(3,"ED ")++1`(4,"PET")++1`(5,"RI ")++1`(6,"NET")

11`1

Page 38: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

38

Coloured Petri NetsDepartment of Computer Science

Type checking of if expression

If expression is type consistent and of type NO * DATA ms(multisets over the colour set of the connected place).

Arc expression

if successthen 1`(n,d)else empty

(n,d)1int NO * DATA

BOOL 'a mssuccess 1`(n,d) empty

var n : NO;var d : DATA;var success : BOOL;

nNO DATAd

(NO * DATA) ms

(NO * DATA) ms

Page 39: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

39

Coloured Petri NetsDepartment of Computer Science

Functions

Functions can be used in all kinds of net expressions: Guards. Arc expressions. Initial markings.

Functions are used when: Complex expressions takes up too much space in the

graphical representation. Same functionality is required in different parts of the model.

Functions make CPN models easier to read and maintain.

Page 40: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

40

Coloured Petri NetsDepartment of Computer Science

Simple protocol

data

n if successthen 1`nelse empty

n

if n=kthen k+1else k

(n,d)(n,d)

nif n=k then data^delse data

(n,d)

if successthen 1`(n,d)else empty

(n,d)

ReceiveAck

TransmitAck

ReceivePacket

TransmitPacket

SendPacket

1`1

NO

CD

NO

A

NOxDATA

NextSend

1`1

NO

DataReceived

1`""

DATA

B

NOxDATA

PacketsTo Send

AllPackets

NOxDATA

k n

NextRec

k

if n=kthen k+1else k

NO

1 1`1

11`""6

1`(1,"COL")++1`(2,"OUR")++1`(3,"ED ")++1`(4,"PET")++1`(5,"RI ")++1`(6,"NET")

11`1

UpdSeq (n,k)

AddData (data,d,n,k)

Page 41: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

41

Coloured Petri NetsDepartment of Computer Science

Definition of two functions

fun UpdSeq (n,k) = if n=k then k+1 else k;

fun AddData (data,d,n,k) = if n=k then data^d else data;

Function Parameter

All functions in Standard ML take a single parameter which may be a tuple.

Function Name

Page 42: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

42

Coloured Petri NetsDepartment of Computer Science

Inference of function type

fun UpdSeq (n,k) = if n=k then k+1 else k;

int * int -> int

k : INT

The variables n and k are local to the function definition.

They should not be confused with the variables n and k of type NO used as arguments in the function call.

n : INT

Function evaluates to an integer

Page 43: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

43

Coloured Petri NetsDepartment of Computer Science

fun AddData (data,d,n,k) = if n=k then data^d else data;

string * string * ''a * ''a -> string

Inference of function type

data : string d : string

n and k must have the same type

Function evaluates to a string

Type variable:Some type with equality operation

Polymorphic function. Can be called with different types of arguments.

Page 44: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

44

Coloured Petri NetsDepartment of Computer Science

CPN model with functions

k

UpdSeq(n,k)

data

n

n if successthen 1`nelse empty

n

UpdSeq(n,k)

(n,d)(n,d)

n

AddData(data,d,n,k)

(n,d)

if successthen 1`(n,d)else empty

(n,d)

ReceiveAck

TransmitAck

TransmitPacket

SendPacket

1`1

NO

C

NO

D

NO

A

NOxDATA

NextSend

1`1

NO

DataReceived

1`""

DATA

B

NOxDATA

PacketsTo Send

AllPackets

NOxDATA

kReceivePacketNextRec

Page 45: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

45

Coloured Petri NetsDepartment of Computer Science

Exploiting polymorphism

fun Transmit (success,pack) = if success then 1`pack else empty;

bool * 'a -> 'a ms

Polymorphic function.

Can be called with different types of arguments:

Transmit (success,(n,d)) Transmit (success,n)

success : bool

Function evaluates to amultiset over the type of pack

To transmit data packets

To transmit acknowledgments

Type variable: Some type whereequality operation not required

Multiset

Page 46: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

46

Coloured Petri NetsDepartment of Computer Science

CPN model with polymorphic function

data

n Transmit(success,n)

n

UpdSeq(n,k)

(n,d)(n,d)

n

AddData(data,d,n,k)

(n,d)Transmit(success, (n,d))

(n,d)

ReceivePacket

SendPacket

1`1

NO

C

NO

D

NO

A

NOxDATA

NextSend

1`1

NO

DataReceived

1`""

DATA

B

NOxDATA

PacketsTo Send

AllPackets

NOxDATA

TransmitPacket

TransmitAck

UpdSeq(n,k)

ReceiveAck

k n

k

NextRec

Page 47: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

47

Coloured Petri NetsDepartment of Computer Science

Revised protocol

k

acks

if n=kthen k+1else k

data

n if successthen 1`nelse empty

n

if n<=kthen 1`nelse empty

(n,d)(n,d)

if n=k then data^delse data

(n,d)

if successthen 1`(n,d)else empty

(n,d)

ReceiveAck

TransmitAck

Receive Packet

TransmitPacket

SendPacket

NextRec

1`1

NO

C

NO

D

NO

A

NOxDATA

Acked

1`[]

ACKS

DataReceived

1`""

DATA

B

NOxDATA

PacketsTo Send

AllPackets

NOxDATA

acks insert(n,acks)

[not (member (n,acks))]

colset ACKS = list NO;var acks : ACKS;

Sender can send any unacknowledged data packet.

Keepsa list of received acks

Function to insert element in list

Function to check for list membership

Page 48: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

48

Coloured Petri NetsDepartment of Computer Science

Function member

fun member (e,l) = if l = [] then false else if (e = List.hd l) then true else member (e,List.tl l);

Recursive call

Checks whether the element e is present in the list l.

Library functions

Page 49: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

49

Coloured Petri NetsDepartment of Computer Science

fun insert (e,l) = if member (e,l) then l else e::l;

Function insert

Uses themember function

Inserts the element e in the list l if it is not already present.

Page 50: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

50

Coloured Petri NetsDepartment of Computer Science

Local environments Can be introduced using a let expression:

fun member (e,l) = if l = [] then false (* if list empty, e is not a member *) else (* list is not empty *) let (* extract head and tail of the list *) val head = List.hd l val tail = List.tl l in if e = head then true (* e was equal to the head *) else member (e,tail) (* check the tail *) end;

Comments

Even short ML functions can be tricky to read and understand. Hence it is a very good idea to use comments.

Page 51: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

51

Coloured Petri NetsDepartment of Computer Science

Higher-order functions A function taking a function as parameter or returning a

function is a higher-order function.

Member is a special case of determining whether there exists an element in the list l satisfying a Boolean predicate p:

fun exists (p,l) = if l = [] then false else if p (List.hd l) then true else exists (p,List.tl l);

(''a -> bool) * ''a list -> bool

fun member (e,l) = let fun equal x = (e=x) in exists (equal,l) end;

''a * ''a list -> bool

Page 52: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

52

Coloured Petri NetsDepartment of Computer Science

Anonymous and curried functions Anonymous functions are specified without an explicit name:

fn x => (e=x);

fun member (e,l) = exists (fn x => (e=x),l);

Curried functions take their parameters one at a time:

fun equal e x = (e=x); ''a -> ''a -> bool

fun member (e,l) = exists (equal e,l);

equal e; ''a -> bool

Page 53: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

53

Coloured Petri NetsDepartment of Computer Science

Patterns in function applications Expressions are built from constants, constructors, and variables. Can be matched with arguments to bind values to the variables.

fun member (e,l) = if l = [] then false else if (e = List.hd l) then true else member (e,List.tl l);

member (2,[1,3,4])

Pattern

The argument (2,[1,3,4]) is matched with the pattern (e,l).

Function call

Page 54: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

54

Coloured Petri NetsDepartment of Computer Science

Patterns in function definitions

fun member (e,[]) = false | member (e,x::l) = if (x = e) then true else member (e,l);

Wilcard (matches everything)

Matches the empty list

Matches a non-empty list

fun member (_,[]) = false | member (e,x::l) = if (x = e) then true else member (e,l);

Not used

Page 55: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

55

Coloured Petri NetsDepartment of Computer Science

Patterns in case expressions

case res of success => 1`p | duplicate => 2`p | failure => empty;

if res = successthen 1`packelse if res = duplicate then 2`pack else empty;

(case res of success => 1 | duplicate => 2 | failure => 0)`pack

Alternative:

Case expressions can be used instead of nested if expressions.

Three patterns

Page 56: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

56

Coloured Petri NetsDepartment of Computer Science

Common patterns pitfalls Redundant match:

Non-exhaustive match:

case res of _ => empty | success => 1`p | duplicate => 2'p;

fun member (e,x::l) = if (e = x) then true else member (e,l);

Warning!

Warning! – Is it wise to ignore the warning?

Programming error: Everything will match the

first clause. The other clauses will

never be used.

NO: Recursion will always end

with a call involving the empty list.

Page 57: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

57

Coloured Petri NetsDepartment of Computer Science

Patterns in records

colset DATAPACK = record seq:NO * data:DATA;

fun ExtractData (datapack : DATAPACK) = #data datapack;

fun ExtractData ({seq=n,data=d}) = d;

fun ExtractData ({seq,data}) = data;

Pattern match:

Pattern match without explicit local variables:

Page 58: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

58

Coloured Petri NetsDepartment of Computer Science

Records with many fields

fun ExtractData ({data,...} : DATAPACK) = data;

Wildcard symbol

Extract data:

Update data:

colset DATAPACK = record seq:NO * data:DATA * ………;

DATAPACK.set_data r d

Library function

Updates the record r by changing the data field to d

Page 59: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

59

Coloured Petri NetsDepartment of Computer Science

Patterns and enabling inference Patterns are exploited when calculating the set

of enabled binding elements in a marking.

Token values are matched withpatterns on input arcs of transitions.

k n

n

(n,d)

n

(n,d)

ReceiveAck

SendPacket

D

NO

A

NOxDATA

NextSend

1`1

NO

PacketsTo Send

AllPackets

NOxDATA

1 1`2

6

1`(1,"COL")++1`(2,"OUR")++1`(3,"ED ")++1`(4,"PET")++1`(5,"RI ")++1`(6,"NET")

<n=1,d="COL"><n=2,d="OUR"><n=3,d="ED "><n=4,d="PET"><n=5,d="RI "><n=6,d="NET">

Candidate binding elements: Pattern

Check

Page 60: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

60

Coloured Petri NetsDepartment of Computer Science

Enabling inference example We may have to use patterns in different input arc expressions

to bind all variables.

k

k

(n,d)

k+1

k

data

k+1

data^d

(n,d)

Discard Packet

[n<>k]

ReceiveNext

[n=k]

NextRec

1`1

NO

C

NO

DataReceived

1`""

DATA

B

NOxDATA

1

1`2

11`"COL"

2

1`(1,"COL")++1`(2,"OUR")

<n=1,d="COL",k=?,data=?><n=2,d="OUR",k=?,data=?>

<n=1,d="COL",k=?,data="COL"><n=2,d="OUR",k=?,data="COL">

<n=1,d="COL",k=2,data="COL"><n=2,d="OUR",k=2,data="COL">OK

Page 61: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

61

Coloured Petri NetsDepartment of Computer Science

k

k

(n,d)

k+1

k

data

k+1

data^d

(n,d)

Discard Packet

[n<>k]

ReceiveNext

[n=k]

NextRec

1`1

NO

C

NO

DataReceived

1`""

DATA

B

NOxDATA

1

1`2

11`"COL"

2

1`(1,"COL")++1`(2,"OUR")

Variables can be bound in guards When the variable(s) in one side of the guard [n= k] has been

bound, we can use the guard to bind the other side.

<n=1,d="COL",k=?,data=?><n=2,d="OUR",k=?,data=?>

<n=1,d="COL",k=?,data="COL"><n=2,d="OUR",k=?,data="COL">

<n=1,d="COL",k=1,data="COL"><n=2,d="OUR",k=2,data="COL">OK

Can also be done for more complex guards: [(n,d)=pack]. When n and d have been bound, we can bind pack.

Page 62: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

62

Coloured Petri NetsDepartment of Computer Science

Binding of variables in CPN Tools CPN Tools requires that it must be possible to bind each variable

of a transition by using patterns on input arcs or in guards.

The only exception to this rule is variables of small colour sets which by default are colour sets with less than 100 values.

data

n if successthen 1`nelse empty

n

if n=kthen k+1else k

(n,d)(n,d)

nif n=k then data^delse data

(n,d)

if successthen 1`(n,d)else empty

(n,d)

ReceiveAck

TransmitAck

ReceivePacket

TransmitPacket

SendPacket

1`1

NO

CD

NO

A

NOxDATA

NextSend

1`1

NO

DataReceived

1`""

DATA

B

NOxDATA

PacketsTo Send

AllPackets

NOxDATA

k n

NextRec

k

if n=kthen k+1else k

NO

1 1`1

11`""6

1`(1,"COL")++1`(2,"OUR")++1`(3,"ED ")++1`(4,"PET")++1`(5,"RI ")++1`(6,"NET")

11`1

Variable success is of type Boolean which only has two values. Hence, it makes sense to try both of them.

Page 63: Kurt Jensen Lars M. Kristensen 1 Coloured Petri Nets Department of Computer Science Coloured Petri Nets Modelling and Validation of Concurrent Systems

Kurt Jensen Lars M. Kristensen

63

Coloured Petri NetsDepartment of Computer Science

Questions