Title: Composite Objects in VDMSL
1Composite Objects in VDM-SL
2Learning Outcomes
- Identify when it is appropriate to use a
composite object type - Use the composite object operators (make,
selection and mu) - Add an invariant to a composite object type
- Use the composite object type to help model
systems in VDM-SL - Use a letin clause to simplify expressions in
VDM-SL
3So far
4.. we have always associated a single type with
each item of data in our VDM specifications.
temp ? robot Status permission
Aircraft-set
5Composite Types..
.. allow us to associate more than one type
with an object.
6Defining Composite Object Types
types
TypeName
fieldname1
Type1
fieldname2
Type2
7The Time Type
8types
TypeName
fieldname1
Type1
fieldname2
Type2
9types
Time
fieldname1
Type1
fieldname2
Type2
10types
Time
hour
Type1
fieldname2
Type2
11types
Time
hour
Type1
minute
Type2
12types
Time
hour
Type1
minute
Type2
second
13types
?
Time
hour
minute
Type2
second
14types
?
Time
hour
minute
?
second
15types
?
Time
hour
minute
?
second
?
16This Time type can now be used like any other
type in your specification
someTime Time
importantTimes Time-set
17Composite object operators
18The make function
The make function that creates a new object of a
given composite type.
mk-CompositeObjectTypeName (parameter list)
Returning to the Time example
someTime mk-Time ( )
16
, 20
, 44
19someTime mk-Time (36, 20, 44)
20someTime mk-Time (36, 20, 44)
This should not be a valid time value?
21someTime mk-Time (36, 20, 44)
Add an invariant to your object definition to
make these times invalid!
22Time hour ? minute ? second ? inv
mk-Time (h, m, s) ?
h lt 24 ? m lt 60 ? s lt 60
23Composite object selectors
We can refer to a particular field of a composite
object by using a selector operator. Individual
fields are selected by the dot operator '.'
followed by the name of a field. For example
someTime.minute someTime.hour
20
16
24The mu function
The mu function returns one composite object from
another but with one or more fields changed.
newTime ? (someTime, hour ? 15)
thisTime ? (someTime, minute ? 0, second ? 0)
25The DiskScanner class
26The DiskScanner class
TRACKS
27The DiskScanner class
SECTORS
28The DiskScanner class
BLOCKS
29DiskScanner damagedBlocks Block
addBlock(Integer, Integer) removeBlock
(Integer, Integer) isDamaged(Integer, Integer)
Boolean getBadSectors(Integer) Integer
30DiskScanner damagedBlocks Block
addBlock(Integer, Integer) removeBlock
(Integer, Integer) isDamaged(Integer, Integer)
Boolean getBadSectors(Integer) Integer
31Analysing the Block type further
Block track Integer sector Integer
32Specifying the data model in VDM-SL
33types state DiskScanner
of damagedBlocks init mk-DiskScanner (dB)
? end
Block track sector
?
?
Block-set
dB
34DiskScanner damagedBlocks Block
addBlock(Integer, Integer) removeBlock
(Integer, Integer) isDamaged(Integer, Integer)
Boolean getBadSectors(Integer) Integer
35addBlock (
) ext pre post
trackIn ?, sectorIn ?
wr
damagedBlocks Block-set
36addBlock (
) ext pre post
trackIn ?, sectorIn ?
wr
damagedBlocks Block-set
37addBlock (
) ext pre post
trackIn ?, sectorIn ?
wr
damagedBlocks Block-set
mk-Block (trackIn, sectorIn) ? damagedBlocks
38DiskScanner damagedBlocks Block
addBlock(Integer, Integer) removeBlock
(Integer, Integer) isDamaged(Integer, Integer)
Boolean getBadSectors(Integer) Integer
39removeBlock (
) ext pre post
trackIn ?, sectorIn ?
wr
damagedBlocks Block-set
mk-Block (trackIn, sectorIn) ? damagedBlocks
40DiskScanner damagedBlocks Block
addBlock(Integer, Integer) removeBlock
(Integer, Integer) isDamaged(Integer, Integer)
Boolean getBadSectors(Integer) Integer
41isDamaged (
) ext pre post
trackIn ?, sectorIn ?
query ?
damagedBlocks Block-set
rd
TRUE
query
?
mk-Block (trackIn, sectorIn) ? damagedBlocks
42DiskScanner damagedBlocks Block
addBlock(Integer, Integer) removeBlock
(Integer, Integer) isDamaged(Integer, Integer)
Boolean getBadSectors(Integer) Integer
43getBadSectors ( )
ext pre post
trackIn ?
list ?-set
rd
damagedBlocks Block-set
TRUE
?
b ? damagedBlocks
b.sector
?
?
list
b.track trackIn
?
44A process management system
45timeout
admit
terminate
dispatch
block
wakeup
46ProcessManagament running ID waiting
Process admit(ID) dispatch() timeOut() block()
wakeUp(ID) terminate()
47ProcessManagament running ID waiting
Process admit(ID) dispatch() timeOut() block()
wakeUp(ID) terminate()
48ProcessManagament running ID waiting
Process admit(ID) dispatch() timeOut() block()
wakeUp(ID) terminate()
49id ID status Status
50id ID status Status
51READY BLOCKED
52Specifying the types in VDM-SL
53types
ID
TOKEN
Status
ltREADYgt ltBLOCKEDgt
ID
Process
id
Status
status
54Specifying the state in VDM-SL
55state ProcessManagement of running waiting inv
mk-ProcessManagement (run, wait) ?
init mk-ProcessManagement (run, wait) ?
end
ID
Process-set
56state ProcessManagement of running waiting inv
mk-ProcessManagement (run, wait) ?
init mk-ProcessManagement (run, wait) ?
end
ID
Process
57state ProcessManagement of running waiting inv
mk-ProcessManagement (run, wait) ? init
mk-ProcessManagement (run, wait) ?
end
ID
Process
run nil ? wait
58state ProcessManagement of running waiting inv
mk-ProcessManagement (run, wait) ? init
mk-ProcessManagement (run, wait) ?
end
ID
Process
run nil ? wait
59state ProcessManagement of running waiting inv
mk-ProcessManagement (run, wait) ? init
mk-ProcessManagement (run, wait) ?
end
ID
Process
P2
run nil ? wait
60state ProcessManagement of running waiting inv
mk-ProcessManagement (run, wait) ? (
) (
) init
mk-ProcessManagement (run, wait) ?
end
ID
Process
no waiting id should match the running id
?
the ids in the waiting queue should be unique
run nil ? wait
61no waiting id should match the running id
P2
run
wait
?
??p ? elems wait ? p.id run
run nil
62no waiting id should match the running id
P2
run
wait
?
??i ? inds wait ? wait(i).id run
run nil
63the ids in the waiting queue should be unique
P2
run
wait
card elems wait len wait
64the ids in the waiting queue should be unique
P2
run
wait
? i,j ? inds wait ? i ? j ? wait(i).id ?
wait(j).id
65state ProcessManagement of running waiting inv
mk-ProcessManagement (run, wait) ? (
) (
) init
mk-ProcessManagement (run, wait) ?
end
ID
Process
??i ? inds wait ? wait(i).id run
run nil
?
?
? i,j ? inds wait ? i ? j ? wait(i).id ?
wait(j).id
run nil ? wait
66Specifying a findPos function
P6
3
67Specifying a findPos function
findPos(qIn Process, idIn ID) pos ? pre ?p
? elems qIn ? p.id idIn post qIn(pos).id
idIn
68Specifying a findNext function
2
69Specifying a findNext function
qIn Process
findNext( ) pre post
pos ?
?p ? elems qIn ? p.status ltREADYgt
?
qIn(pos).status ltREADYgt
??i ? 1,,pos-1 ? qIn(i).status ltREADYgt
70Specifying a remove function
2
71Specifying a remove function
remove(qIn Process, posIn ?) qOut
Process pre posIn ? inds qIn post qOut
qIn(1,, posIn-1) qIn(posIn1,,len qIn)
72ProcessManagament running ID waiting
Process admit(ID) dispatch() timeOut() block()
wakeUp(ID) terminate()
73admit( idIn ID) ext pre post
waiting Process
wr
running ID
rd
(running nil ? idIn ? running ) ?
?p ? elems waiting ? p.id ? idIn
74ProcessManagament running ID waiting
Process admit(ID) dispatch() timeOut() block()
wakeUp(ID) terminate()
75dispatch() ext pre post
running ID
wr
waiting Process
wr
running nil ?
?p ? elems waiting ? p.status ltREADYgt
76ProcessManagament running ID waiting
Process admit(ID) dispatch() timeOut() block()
wakeUp(ID) terminate()
77timeOut() ext pre post
running ID
wr
waiting Process
wr
running ? nil
? running nil
78ProcessManagament running ID waiting
Process admit(ID) dispatch() timeOut() block()
wakeUp(ID) terminate()
79block() ext pre post
running ID
wr
waiting Process
wr
running ? nil
? running nil
80ProcessManagament running ID waiting
Process admit(ID) dispatch() timeOut() block()
wakeUp(ID) terminate()
81wakeUp( idIn ID) ext pre post
waiting Process
wr
waiting(findPos(waiting, idIn)).status
ltBLOCKEDgt
82ProcessManagament running ID waiting
Process admit(ID) dispatch() timeOut() block()
wakeUp(ID) terminate()
83terminate() ext pre post
running ID
wr
running ? nil
running nil
84The letin clause
A letin clause takes the following general form
let name sub-expression in expression(name)
85Re-writing postcondition of dispatch
86Nested letin clauses