Title: Sets for system modelling
1Sets for system modelling
2At 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
3A set is an unordered collection of objects in
which repetition is not significant.
When to use a set?
Collection of patients registered on the books of
a doctor's surgery?
?
?
The queue of patients waiting for a doctor?
4Declaring sets in VDM-SL
5To indicate a variable to be of the set type
-set
variableName ElementType
For example
Day ltMONgt ltTUEgt ltWEDgt ltTHUgt ltFRIgt
ltSATgt ltSUNgt aNumber ? aDay
Day someNumbers ?-set someOtherNumbers ?
-set importantDays Day-set
6Defining sets in VDM-SL
Three ways to initialise the values of a set
- by enumeration
- by number ranges
- by comprehension.
7Defining sets by enumeration
someNumbers 2, 4, 28, 19 ,10
importantDays ltFRIgt, ltSATgt, ltSUNgt
8Defining sets by number ranges
9Can 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
10Defining a set by comprehension
11Defining a set by comprehension
12Allows one set to be defined by means of another
someSet expression (x) x ? someOtherSet ?
test(x)
13Allows one set to be defined by means of another
someSet expression (x) x ? someOtherSet ?
test(x)
14Allows one set to be defined by means of another
someSet expression (x) x ? someOtherSet ?
test(x)
15Allows 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
16Allows 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
17Allows 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
18Allows 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
19Set 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 ltMONgt, ltTUEgt, ltWEDgt, ltSUNgt and
k lt MON gt, ltFRIgt, lt TUE gt then j ? k
ltMONgt, ltTUEgt, ltWEDgt, ltSUNgt , ltFRIgt
20Set operations. contd
Set intersection j ? k
Returns a set that contains all the elements that
are common to both j and k.
if j ltMONgt, ltTUEgt, ltWEDgt, ltSUNgt and
k lt MON gt, ltFRIgt, lt TUE gt then j ? k
ltMONgt, ltTUEgt
21Set 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 ltMONgt, ltTUEgt, ltWEDgt, ltSUNgt and
k lt MON gt, ltFRIgt, lt TUE gt then j \ k
ltWEDgt, ltSUNgt
22Set 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
23Set operations. contd
Cardinality card j
Returns the number of elements in a given set.
card 7, 2, 12
3
card 7, 2, 2, 12, 12
3
card 4,,10
7
card
0
24The PatientRegister class
25PatientRegister reg Patient addPatient
(Patient) removePatient (Patient) getPatients (
) Patient isRegistered (Patient)
Boolean numberRegistered ( )Integer
26PatientRegister reg Patient addPatient
(Patient) removePatient (Patient) getPatients (
) Patient isRegistered (Patient)
Boolean numberRegistered ( )Integer
27Modelling the PatientRegister class in VDM-SL
28types Patient values LIMIT state PatientRegister
of reg init mk-PatientRegister ( ) ?
end
TOKEN
There must be no more than 200 patients on the
register
? 200
Patient
-set
inv mk-PatientRegister (r) ?
card r ? LIMIT
r
r
29PatientRegister reg Patient addPatient
(Patient) removePatient (Patient) getPatients (
) Patient isRegistered (Patient)
Boolean numberRegistered ( )Integer
30addPatient (
) ext pre post
patientIn Patient
reg Patient-set
wr
patientIn ? reg
31addPatient (
) ext pre post
patientIn Patient
reg Patient-set
wr
patientIn ? reg
card reg lt LIMIT
?
32PatientRegister reg Patient addPatient
(Patient) removePatient (Patient) getPatients (
) Patient isRegistered (Patient)
Boolean numberRegistered ( )Integer
33removePatient (
) ext pre post
patientIn Patient
wr reg Patient-set
patientIn ? reg
reg \ patientIn
patientIn ? reg
34PatientRegister reg Patient addPatient
(Patient) removePatient (Patient) getPatients (
) Patient isRegistered (Patient)
Boolean numberRegistered ( )Integer
35getPatients ( ) ext pre post
output Patient-set
rd reg Patient-set
TRUE
output reg
36PatientRegister reg Patient addPatient
(Patient) removePatient (Patient) getPatients (
) Patient isRegistered (Patient)
Boolean numberRegistered ( )Integer
37isRegistered (
) ext pre post
patientIn Patient
query ?
rd reg Patient-set
TRUE
?
query
patientIn ? reg
38PatientRegister reg Patient addPatient
(Patient) removePatient (Patient) getPatients (
) Patient isRegistered (Patient)
Boolean numberRegistered ( )Integer
39numberRegistered () ext pre post
total ?
rd reg Patient-set
TRUE
card reg
total
40The Airport Class
41"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"
42A UML specification of the Airport class
Airport permission Aircraft landed Aircraft
givePermission(Aircraft) recordLanding(Aircra
ft) recordTakeOff(Aircraft) getPermission( )
Aircraft getLanded( ) Aircraft
numberWaiting() Integer
43A UML specification of the Airport class
44Modelling the Airport class in VDM-SL
45types state Airport of
init mk-Airport ( ) ? end
Aircraft TOKEN
All landed planes must have had permission to
land.
permission
Aircraft -set
Aircraft -set
landed
inv mk-Airport(p,l) ?
l ? p
p, l
p
?
l
46(No Transcript)
47craftIn Aircraft
givePermission(
) ext pre post
permission Aircraft - set
wr
craftIn ? permission
48(No Transcript)
49recordLanding (
) ext pre post
craftIn Aircraft
landed Aircraft -set
wr
permission Aircraft -set
rd
craftIn ? landed
?
craftIn ? permission
50(No Transcript)
51craftIn Aircraft
recordTakeOff (
) ext pre post
permission Aircraft -set
wr
wr
landed Aircraft -set
craftIn ? landed
?
52(No Transcript)
53getPermission( ) ext pre post
out Aircraft-set
permission Aircraft -set
rd
TRUE
out permission
54(No Transcript)
55out Aircraft -set
getLanded( ) ext pre post
landed Aircraft -set
rd
TRUE
out landed
56(No Transcript)
57numberWaiting( ) ext pre post
total ?
permission Aircraft -set
rd
landed Aircraft -set
rd
card (permission \ landed)
total
58Thats all Folks..