58
Sets for system modelling

Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

Embed Size (px)

Citation preview

Page 1: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

Sets for system modelling

Page 2: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

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

Page 3: Sets for system modelling. 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 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?

Page 4: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

Declaring sets in VDM-SL

Page 5: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

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

Page 6: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

Defining sets in VDM-SL

Three ways to initialise the values of a set:

•by enumeration;

•by number ranges;

•by comprehension.

Page 7: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

Defining sets by enumeration

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

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

Page 8: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

Defining sets by number ranges

Page 9: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

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} = { }

Page 10: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

Defining a set by comprehension

Page 11: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

Defining a set by comprehension

Page 12: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

Allows one set to be defined by means of another:

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

Page 13: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

Allows one set to be defined by means of another:

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

Page 14: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

Allows one set to be defined by means of another:

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

Page 15: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

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)}

{ 2, 3, 4, 5, 6 }

Page 16: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

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)}

{ 2, 3, 4, 5, 6 }

Page 17: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

Allows one set to be defined by means of another:

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

Examples:

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

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

Page 18: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

Allows one set to be defined by means of another:

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

Examples:

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

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

Page 19: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

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>}

Page 20: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

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>}

Page 21: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

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> }

Page 22: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

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}

Page 23: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

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

Page 24: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

The PatientRegister class

Page 25: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

PatientRegister

reg: Patient [*]

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

Page 26: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

PatientRegister

reg: Patient [*]

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

Page 27: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

Modelling the PatientRegister class in VDM-SL

Page 28: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

types

Patient

values

LIMIT

state PatientRegister of

reg:

init mk-PatientRegister ( )

end

Patient

inv mk-PatientRegister (r) card r LIMIT

r = { }

= TOKEN

: = 200

r

-set

There must be no more than 200 patients on the register

Page 29: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

PatientRegister

reg: Patient [*]

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

Page 30: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

addPatient ( )

ext

pre

post regreg = patientIn

patientIn: Patient

reg: Patient-setwr

patientIn reg

Page 31: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

addPatient ( )

ext

pre

post regreg = { patientIn}

patientIn reg card reg < LIMIT

patientIn: Patient

reg: Patient-setwr

Page 32: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

PatientRegister

reg: Patient [*]

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

Page 33: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

removePatient ( )

ext

pre

post reg

patientIn: Patient

wr reg: Patient-set

reg = \ { patientIn}patientIn reg

patientIn reg

Page 34: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

PatientRegister

reg: Patient [*]

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

Page 35: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

getPatients ( )

ext

pre

post

output: Patient-set

rd reg: Patient-set

output = reg

TRUE

Page 36: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

PatientRegister

reg: Patient [*]

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

Page 37: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

isRegistered ( )

ext

pre

post

patientIn: Patient query:

rd reg: Patient-set

query patientIn reg

TRUE

Page 38: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

PatientRegister

reg: Patient [*]

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

Page 39: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

numberRegistered ()

ext

pre

post

total:

rd reg: Patient-set

total = card reg

TRUE

Page 40: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

The Airport Class

Page 41: Sets for system modelling. 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 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"

Page 42: Sets for system modelling. 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 UML specification of the Airport class

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

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

Page 43: Sets for system modelling. 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 UML specification of the Airport class

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

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

Page 44: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

Modelling the Airport class in VDM-SL

Page 45: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

types

state Airport of

init mk-Airport ( )

end

Aircraft = TOKEN

permission:

landed:

inv mk-Airport(p,l) l p

p = { } l = { }p, l

Aircraft -set

Aircraft -set

All landed planes must have had permission to land.

Page 46: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

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

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

Page 47: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

givePermission( )

ext

pre

post

craftIn: Aircraft

permission: Aircraft - setwr

permissionpermission = {craftIn }

craftIn permission

Page 48: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

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

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

Page 49: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

recordLanding ( )

ext

pre

post

craftIn: Aircraft

permission: Aircraft -set

landed: Aircraft -set

rd

wr

landedlanded = {craftIn}

craftIn permission craftIn landed

Page 50: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

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

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

Page 51: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

recordTakeOff ( )

ext

pre

post

craftIn: Aircraft

permission: Aircraft -set

landed: Aircraft -set

wr

wr

landedlanded = \ {craftIn }

permissionpermission = \ { craftIn }

craftIn landed

Page 52: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

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

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

Page 53: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

getPermission( )

ext

pre

post

out: Aircraft-set

permission: Aircraft -setrd

out = permission

TRUE

Page 54: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

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

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

Page 55: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

getLanded( )

ext

pre

post

out: Aircraft -set

landed: Aircraft -setrd

out = landed

TRUE

Page 56: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

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

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

Page 57: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

numberWaiting( )

ext

pre

post

total:

permission: Aircraft -set

landed: Aircraft -set

rd

rd

card (permission \ landed)total =

Page 58: Sets for system modelling. At the end of this lecture you should be able to: Identify when it is appropriate to use a set for system modelling Define

That’s all Folks..