sets learning outcomes at the end of this lecture you should be able to: identify when it is...

29
Sets Learning Outcomes At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define a set using enumeration, number ranges and comprehension Use the set operators (union, intersection, difference, subset and cardinality) Apply the set type to model systems in VDM-SL

Upload: benjamin-stanley

Post on 30-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Sets

Learning Outcomes

At the end of this lecture you should be able to:

• Identify when it is appropriate to use a set for system modelling

• Define a set using enumeration, number ranges and comprehension

• Use the set operators (union, intersection, difference, subset and cardinality)

• Apply the set type to model systems in VDM-SL

So far we have looked at specifications involving

• simple scalar types• enumerated types

Most systems manipulate complex data structures like lists.

To model such data structures, VDM-SL provides several data type constructors.

The first abstract structured type of VDM-SL that we will look at is the familiar notion of a set.

Data Type Constructors

(, and so on)(e.g. <BROKEN> |<WORKING> | < IDLE> )

Sets for system modelling

A set is an unordered collection of objects in which repetition is not significant.

Collection of patients registered on the books of a doctor's surgery?

When to use a set?

The queue of patients waiting for a doctor?

Declaring sets in VDM-SL

To indicate a variable to be of the set type:

variableName: ElementType

For example

Day = <MON> | <TUE> | <WED> | <THU> | <FRI> | <SAT> | <SUN>

aNumber: aDay : Day

someNumbers: -setsomeOtherNumbers: -setimportantDays : Day-set

-set

Defining sets in VDM-SL

Three ways to initialise the values of a set:

•by enumeration;

•by number ranges;

•by comprehension.

Defining sets by enumeration

Involves

• listing the elements individually,

• separated by commas , , ,

• and enclosed in braces { }

For example:

someNumbers = {2, 4, 28, 19 ,10 }

importantDays = {<FRI>, <SAT>, <SUN>}

Defining sets by number ranges

Can be used when a set of continuous integers required:

someRange = {5, … ,15}

equal to

someRange = {5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

When the second number in the range is smaller than the first, the empty set is returned.

{7,…,6} = { }

Defining a set by comprehension

Allows one set to be defined by means of another:

someSet = { expression (x) | x someOtherSet test(x) }

Examples:

someNumbers = { x | x {2,…,6} isEven(x)}

someOtherNumbers = { x2 | x {2,…,6}}

{ 2, 3, 4, 5, 6 }

{ 2, 3, 4, 5, 6 }4, 9, 16, 25, 36

Set operations

Set union: j k

Returns a set that contains all the elements of the set j and all the elements of the set k.

if j = { <MON>, <TUE>, <WED>, <SUN> }

and k = { < MON >, <FRI>, < TUE > }

then j k = {<MON>, <TUE>, <WED>, <SUN> , <FRI>}

Set operations…. cont’d

Set intersection: j k

Returns a set that contains all the elements that are common to both j and k.

if j = { <MON>, <TUE>, <WED>, <SUN> }

and k = { < MON >, <FRI>, < TUE > }

then j k = {<MON>, <TUE>}

Set operations…. cont’d

Set difference: j \ k

Returns the set that contains all the elements that belong to j but do not belong to k.

if j = { <MON>, <TUE>, <WED>, <SUN> }

and k = { < MON >, <FRI>, < TUE > }

then j \ k = {<WED>, <SUN> }

Set operations…. cont’d

Subset: j kReturns true if all elements that belong to j also belong to k.

{a, d, e} {a, b, c, d, e, f}

{a, d, e} {d, a, e}

Proper subset: j k

Returns true if all elements that belong to j also belong to k but false if sets j and k are equal.

{a, d, e} {a, b, c, d, e, f}

{a, d, e} {d, a, e}

Set operations…. cont’d

Cardinality: card j

Returns the number of elements in a given set.

card { 7, 2, 12}

card { 7, 2, 2, 12, 12}

card { 4,…,10}

card { }

= 3

= 3

= 7

= 0

The PatientRegister class

PatientRegister

reg: Patient [*]

addPatient (Patient)removePatient (Patient)getPatients ( ): Patient [*]isRegistered (Patient): BooleannumberRegistered ( ):Integer

Modelling the PatientRegister class in VDM-SL

types

Patient

values

LIMIT

state PatientRegister of

reg:

init mk-PatientRegister ( )

end

Patient-set

inv mk-PatientRegister (r) card r LIMIT

r = { }

= TOKEN

: = 200

r

The addPatient operation

addPatient ( )

ext

pre

post regreg = { patientIn}

patientIn reg card reg < LIMIT

patientIn: Patient

reg: Patient-setwr

patientIn reg

The removePatient operation

removePatient ( )

ext

pre

post reg

patientIn: Patient

wr reg: Patient-set

reg = \ { patientIn}patientIn reg

patientIn reg

getPatients ( )

ext

pre

post

The getPatients operation

output: Patient-set

rd reg: Patient-set

output = reg

TRUE

isRegistered ( )

ext

pre

post

The isRegistered operation

patientIn: Patient query:

rd reg: Patient-set

query patientIn reg

TRUE

numberRegistered ()

ext

pre

post

The numberRegistered operation

total:

rd reg: Patient-set

total = card reg

TRUE

The Airport Flight Control System

"A system is to be developed that keeps track of planes that are allowed to land at a particular airport.

Planes must apply for permission to land at the airport prior to landing.

When a plane arrives to land at the airport it is only allowed to do so if it has previously been given permission.

When a plane leaves the airport its permission to land is also removed"

A UML specification of the Airport class

Airportpermission: Aircraft [*]landed: Aircraft [*]

givePermission(Aircraft)recordLanding(Aircraft)recordTakeOff(Aircraft)getPermission( ): Aircraft [*]getLanded( ): Aircraft [*]numberWaiting(): Integer

types

state Airport of

init mk-Airport ( )

end

Modelling the Airport class in VDM-SL

Aircraft = TOKEN

permission: Aircraft-set

landed: Aircraft -set

inv mk-Airport(p,l) l p

p = { } l = { }p, l

The givePermission operation

givePermission( )

ext

pre

post

craftIn: Aircraft

permission: Aircraft - setwr

permissionpermission = {craftIn }

craftIn permission

The recordLanding operation

recordLanding ( )

ext

pre

post

craftIn: Aircraft

permission: Aircraft -set

landed: Aircraft -set

rd

wr

landedlanded = {craftIn}

craftIn permission craftIn landed

recordTakeOff ( )

ext

pre

post

The recordTakeOff operation

craftIn: Aircraft

permission: Aircraft -set

landed: Aircraft -set

wr

wr

landedlanded = \ {craftIn }

permissionpermission = \ { craftIn }

craftIn landed

getPermission( )

ext

pre

post

The getPermission operation

out: Aircraft-set

permission: Aircraft -setrd

out = permission

TRUE

getLanded( )

ext

pre

post

The getLanded operation

out: Aircraft -set

landed: Aircraft -setrd

out = landed

TRUE

numberWaiting( )

ext

pre

post

The numberWaiting operation

total:

permission: Aircraft -set

landed: Aircraft -set

rd

rd

card (permission \ landed)total =