# 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.

## Presentation on theme: "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."— Presentation transcript:

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

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: -set someOtherNumbers: -set importantDays : 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 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) }

Allows one set to be defined by means of another: someSet = { expression (x) | x someOtherSet test(x) }

Allows one set to be defined by means of another: someSet = { expression (x) | x someOtherSet test(x) }

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 }

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 }

Allows one set to be defined by means of another: someSet = { expression (x) | x someOtherSet test(x) } Examples: someOtherNumbers = { x 2 | x {2,…,6}} { 2, 3, 4, 5, 6 } 4,9,16,25,36

Allows one set to be defined by means of another: someSet = { expression (x) | x someOtherSet test(x) } Examples: someOtherNumbers = { x 2 | x {2,…,6} isEven(x)} } { 2, 3, 4, 5, 6 } 4,16,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…. contd 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 > } thenj k = {<MON>, <TUE>}

Set operations…. contd 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…. contd Subset:j k Returns 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…. contd 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): Boolean numberRegistered ( ):Integer

PatientRegister reg: Patient [*] addPatient (Patient) removePatient (Patient) getPatients ( ): Patient [*] isRegistered (Patient): Boolean numberRegistered ( ):Integer

Modelling the PatientRegister class in VDM-SL

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

PatientRegister reg: Patient [*] addPatient (Patient) removePatient (Patient) getPatients ( ): Patient [*] isRegistered (Patient): Boolean numberRegistered ( ):Integer

addPatient ( ) ext pre post reg = patientIn patientIn: Patient reg: Patient-setwr patientIn reg

addPatient ( ) ext pre post reg = { patientIn} patientIn regcard reg < LIMIT patientIn: Patient reg: Patient-setwr

PatientRegister reg: Patient [*] addPatient (Patient) removePatient (Patient) getPatients ( ): Patient [*] isRegistered (Patient): Boolean numberRegistered ( ):Integer

removePatient ( ) ext pre post patientIn: Patient wrreg: Patient-set reg = \ { patientIn}patientIn reg patientIn reg

PatientRegister reg: Patient [*] addPatient (Patient) removePatient (Patient) getPatients ( ): Patient [*] isRegistered (Patient): Boolean numberRegistered ( ):Integer

getPatients ( ) ext pre post output: Patient-set rd reg: Patient-set output = reg TRUE

PatientRegister reg: Patient [*] addPatient (Patient) removePatient (Patient) getPatients ( ): Patient [*] isRegistered (Patient): Boolean numberRegistered ( ):Integer

isRegistered ( ) ext pre post patientIn: Patient query: rdreg: Patient-set query patientIn reg TRUE

PatientRegister reg: Patient [*] addPatient (Patient) removePatient (Patient) getPatients ( ): Patient [*] isRegistered (Patient): Boolean numberRegistered ( ):Integer

numberRegistered () ext pre post total: rdreg: Patient-set total = card reg TRUE

The Airport Class

" 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 Airport permission: Aircraft [*] landed: Aircraft [*] givePermission(Aircraft) recordLanding(Aircraft) recordTakeOff(Aircraft) getPermission( ): Aircraft [*] getLanded( ): Aircraft [*] numberWaiting(): Integer

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

Modelling the Airport class in VDM-SL

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 All landed planes must have had permission to land.

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

givePermission( ) ext pre post craftIn: Aircraft permission: Aircraft - set wr permission = {craftIn } craftIn permission

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

recordLanding ( ) ext pre post craftIn: Aircraft permission: Aircraft -set landed: Aircraft -set rd wr landed = {craftIn} craftIn permission craftIn landed

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

recordTakeOff ( ) ext pre post craftIn: Aircraft permission: Aircraft -set landed: Aircraft -set wr wr landed = \ {craftIn }permission = \ { craftIn } craftIn landed

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

getPermission( ) ext pre post out: Aircraft-set permission: Aircraft -setrd out = permission TRUE

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

getLanded( ) ext pre post out: Aircraft -set landed: Aircraft -set rd out = landed TRUE

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

numberWaiting( ) ext pre post total: permission: Aircraft -set landed: Aircraft -set rd rd card (permission \ landed)total =

Thats all Folks..

Similar presentations