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: -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
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
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 = { x2 | 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 = { x2 | 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…. 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
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
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): BooleannumberRegistered ( ):Integer
addPatient ( )
ext
pre
post regreg = patientIn
patientIn: Patient
reg: Patient-setwr
patientIn reg
addPatient ( )
ext
pre
post regreg = { patientIn}
patientIn reg card reg < LIMIT
patientIn: Patient
reg: Patient-setwr
PatientRegister
reg: Patient [*]
addPatient (Patient)removePatient (Patient)getPatients ( ): Patient [*]isRegistered (Patient): BooleannumberRegistered ( ):Integer
removePatient ( )
ext
pre
post reg
patientIn: Patient
wr reg: Patient-set
reg = \ { patientIn}patientIn reg
patientIn reg
PatientRegister
reg: Patient [*]
addPatient (Patient)removePatient (Patient)getPatients ( ): Patient [*]isRegistered (Patient): BooleannumberRegistered ( ):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): BooleannumberRegistered ( ):Integer
isRegistered ( )
ext
pre
post
patientIn: Patient query:
rd reg: Patient-set
query patientIn reg
TRUE
PatientRegister
reg: Patient [*]
addPatient (Patient)removePatient (Patient)getPatients ( ): Patient [*]isRegistered (Patient): BooleannumberRegistered ( ):Integer
numberRegistered ()
ext
pre
post
total:
rd reg: 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
Airportpermission: Aircraft [*]landed: Aircraft [*]
givePermission(Aircraft)recordLanding(Aircraft)recordTakeOff(Aircraft)getPermission( ): Aircraft [*]getLanded( ): Aircraft [*]numberWaiting(): Integer
A UML specification of the Airport class
Airportpermission: 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
Aircraft -set
All landed planes must have had permission to land.
Airportpermission: Aircraft [*]landed: Aircraft [*]
givePermission(Aircraft)recordLanding(Aircraft)recordTakeOff(Aircraft)getPermission( ): Aircraft [*]getLanded( ): Aircraft [*]numberWaiting(): Integer
givePermission( )
ext
pre
post
craftIn: Aircraft
permission: Aircraft - setwr
permissionpermission = {craftIn }
craftIn permission
Airportpermission: 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
landedlanded = {craftIn}
craftIn permission craftIn landed
Airportpermission: 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
landedlanded = \ {craftIn }
permissionpermission = \ { craftIn }
craftIn landed
Airportpermission: 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
Airportpermission: Aircraft [*]landed: Aircraft [*]
givePermission(Aircraft)recordLanding(Aircraft)recordTakeOff(Aircraft)getPermission( ): Aircraft [*]getLanded( ): Aircraft [*]numberWaiting(): Integer
getLanded( )
ext
pre
post
out: Aircraft -set
landed: Aircraft -setrd
out = landed
TRUE
Airportpermission: 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 =
That’s all Folks..