Title: Peter Gorm Larsen
1Modelling ordered collections
2Agenda
- Sequence Characteristics and Primitives
- The Congestion Warning System
3Sequence Characteristics
- Sequences are ordered collections of elements
- There can be many copies of each element
- The elements themselves can be arbitrary complex,
e.g. they can be sequences as well - Sequences in VDM are finite
- Sequence types in VDM are written as
- seq of Type
- seq1 of Type (for non-empty sequences)
4Sequence Enumeration
- A sequence enumeration consists of a
comma-separated list enclosed between square
brackets, - For example
- 1,5,8,1,3
- true, false
- , 4,3,2,4
- g,o,d
- 3.567, 0.33455,7,7,7,7
- Are all sequences
- The empty sequence can be written as
5Sequence Length
- The length of a sequence is the number of
elements in the sequence i.e. its size - Multiple occurrences of the same value counts
- The length of a sequence L is written as len L
- Quick examples
- len 1,2,3
- len
- len 3,2,3,2
6Sequence Equality
- Two sequences are equal if both have the same
length and for all indices in the sequences the
respective index values are equal - Quick examples
- 2,4,1,2 4,1,2
- true, true, false false, true
- 1,1,1,1,1,1,1,1,1,1,1,1 1
- 3,4,5,4 3,5,4,4,4,4
7Sequence Head and Tail
- A non-empty sequence can be divided into its head
(hd) and its tail (tl). - The head of a sequence is the first element
- The tail of a sequence is the rest of the
sequence - Quick examples
- hd 1,2,3,4,5
- tl 1,2,3,4,5
- hd 5,6,1,4,4,4
- tl 5,6,1,4,4,4
8Sequence Elements
- It is possible to extract the elements of a
sequence using an elems operator - elems takes a sequence an yield a set of its
elements (i.e. destroying the ordering
information) - Quick examples
- elems 1,2,2
- elems
- elems 3,2,3,1
9Sequence Indices
- It is possible to get hold of the indices for a
sequence using the inds operator - In VDM indexing starts with 1
- Quick examples
- inds 1,2,21,6,5
- inds , true
- inds
- inds 3,2,3,1
10Sequence Application
- Given a non-empty sequence it is possible to hold
of its contents at the ith index - Sequence application is written as function
application, i.e. sequence(index expression) - Quick examples
- 1,2,21,6,5(3)
- ,false(2)
- 3,2,3,1,4(1)
11Sequence Modification
- Given a non-empty sequence it is possible to
obtain a new sequence where the contents of
certain indices are changed - A sequence modification expression looks as
sequence modified mapping - The modified mapping goes from index to new value
at that index - Quick examples
- 2,4,3,1,2,2,3,4,3 1 -gt
- 2,4,3,1,1, 2 -gt 7,5,1 -gt 8
- true,false, 3 -gt true,false
12Sequence Concatenation
- Two sequences A and B can be concatenated
together to form a new sequence where As
elements are followed by Bs elements - Sequence concatenation is written as A B
- Quick examples
- 1,2,2 1,6,5
- true
- 3,2,3,1 4
13Distributed Sequence Concatenation
- If we have a sequence of sequences then the
elements can be concatenated together in a
distributed fashion - Distributed sequence concatenation is written as
conc SS where SS is a sequence of sequences - Quick examples
- conc 1,2,2, 1,6,5, , 8,3
- conc ,true,false
- conc 3,2,3,1, ,9,5,4
14Sequence Operators
hd l Head seq1 of A -gt A tl l Tail seq1 of A -gt
seq of A len l Length seq of A -gt nat elems
l Elements seq of A -gt set of A inds
l Indexes seq of A -gt set of nat1 l1
l2 Concatenation seq of A seq of A -gt seq of
A conc ll Distr. conc. seq of seq of A -gt seq of
A l(i) Seq. application seq1 of A nat1 -gt A l
m Seq. modification seq1 of A map nat1 to A
-gt seq1 of A l1 l2 Equality seq of A seq
of A -gt bool l1 ltgt l2 Inequality seq of A seq
of A -gt bool
15Sequence Comprehensions
- Using predicates to define sequences implicitly
- In VDM formulated like
- element numeric set binding predicate
- The predicate part is optional
- The numeric order of the binding is used to
determine the order in the sequence - The smallest number is taken to be the first
index - Quick examples
- 3 x x in set 0,,2
- x x in set 0,,4 x gt 2
16Questions
- What are the sequence enumerations for
- xx in set 8,,1 x lt 3
- xx in set 1,,10 x gt 3 and x lt 6
- y y in set 3,1,7,3
- x6 x in set 1,2
- mk_(x,8) x in set 1,2,7 x gt 4
- yy in set 0,1,2 exists x in set 0,,3 x
2 y - x 7 x in set 1,,10 x lt 6
17Sub-sequence Expressions
- A subsequence of a sequence L is a sequence
formed from consecutive elements of L from index
n1 up to and including index n2. It has the form - L(n1, ..., n2)
- where n1 and n2 are integer expressions.
- Quick Examples
- 5,4,3,7,8,2(2,,4)
- 5,4,3,7,8,2(-6,,4)
- 5,4,3,7,8,2(2,,8)
- 5,4,3,7,8,2(6,,4)
18Agenda
- Sequence Characteristics and Primitives
- The Congestion Warning System
19The Congestion Warning System
- A system for warning drivers of upcoming
congestion on highways with lower speed limits to
reduce the likelihood of collisions.
20The Main CWS Components
- Sensors These are used to derive status
information about the traffic. Sensors include
video cameras, radar and human observers. - Traffic Controls This interpret the data coming
from sensors and take appropriate action. - Actuators These are used to signal to the
drivers about potential congestions. Here traffic
signs will be used but different technologies
could be envisaged as well.
21Overview of the CWS System
22UML Class Diagram for CWS
23Example Journey Plan
- class CWS
-
- instance variables
- roadNetwork seq of CongestionMonitor
- sensors seq of PassageSensor
- inv len roadNetwork len sensors
- am ActuatorManager new ActuatorManager()
- op OperatorControl new OperatorControl()
- types
- Location nat1
- end CWS
24Multiple Assignment Statements
- We somehow need to update the roadNetwork and the
sensors instance variables synchronously to
ensure the invariant - VDM Construct
- atomic (assignment statement 1
- assignment statement 2
- ...
- assignment statement n
- )
25The AddCongestionMonitor Operation
- public AddCongestionMonitor Location gt ()
- AddCongestionMonitor(loc)
- (def sensor new PassageSensor(loc)
- cm new CongestionMonitor(loc, sensor, am,
op) - in
- let numberOfWarners len roadNetwork
- in
- atomic(roadNetwork roadNetwork(1,...,loc)
- cm
- roadNetwork(loc1,...,
-
numberOfWarners) - sensors sensors(1,...,loc)
sensor - sensors(loc1,...,numberOfW
arners) - )
- am.AddActuator(loc)
- )
26Different kinds of Sensors
27Sensors and PassageSensors
- class Sensor
- instance variables
- protected location CWSLocation
- end Sensor
- class PassageSensor is subclass of Sensor
- instance variables
- passages seq of CWSSpeed
-
- operations
- public PassageSensor CWSLocation gt
PassageSensor - PassageSensor(loc)
- location loc
- end PassageSensor
28Finding the Average Speed
- class PassageSensor is subclass of Sensor
-
- public AverageSpeed nat1 gt CWSSpeed
- AverageSpeed(numberOfPassages)
- ( dcl accSpeed CWSSpeed 0
- let passInAccount passages(1,...,numberOfPassa
ges) - in
- ( for speed in passInAccount do
- accSpeed accSpeed speed
- return (accSpeed/numberOfPassages)
- )
- )
- pre len passages gt numberOfPassages
- end PassageSensor
29The Congestion Sensor
- class CongestionSensor is subclass of Sensor
- types
- public CongestionStatus ltCongestiongtltNoCongesti
ongt - ltDoubtgt
- operations
- public CongestionSensor PassageSensor gt
CongestionSensor - CongestionSensor(sensor)
- passageSensor sensor
- public IssueCongestionStatus () gt
CongestionStatus - IssueCongestionStatus()
- def averageSpeed passageSensor.AverageSpeed(no
Passages) - in
- if averageSpeed lt congestionThreshold
- then return ltCongestiongt
- elseif averageSpeed gt noCongestionThreshold
- then return ltNoCongestiongt
- else return ltDoubtgt
- end CongestionSensor
30Actuator Structure
public Signal ltNoWarninggt
ltPreAnnouncementgt
ltCongestionWarninggt
as seq of Actuator
31Show Signal in Actuation Manager
- class ActuationManager
-
- public ShowSignal CWSLocation
- CongestionMonitorSignal gt
() - ShowSignal(location, signal)
- (let downstream as(location 1),
- actuator as(location),
- upstream as(location - 1)
- in
- -- Set the right signal at the location itself
- (ShowSignalAtLoc(signal,downstream,actuator)
- -- Set the right signal upstream
- ShowSignalUpstream(signal,upstream)
- )
- )
- pre location in set 2,..., len as -1 and
- (signal ltNoWarninggt or
- signal ltCongestionWarninggt)
- end ActuationManager
32Show Signal at a given Location
- class ActuationManager
-
- ShowSignalAtLoc CongestionMonitorSignal
Actuator - Actuator gt ()
- ShowSignalAtLoc(signal,downstream,actuator)
- if signal ltNoWarninggt
- then def downstreamsignal downstream.GetSignal
() - in
- if downstreamsignal ltCongestionWarninggt
- then actuator.SetSignal(ltPreAnnouncementgt
) - else actuator.SetSignal(ltNoWarninggt)
- else def currentsignal actuator.GetSignal()
- in
- let safest MostRestrictive(currentsigna
l, - signal)
- in
- actuator.SetSignal(safest)
- end ActuationManager
33Most Restrictive Signal
- class ActuationManager
-
- functions
- MostRestrictive CongestionMonitorSignal
- CongestionMonitorSignal -gt
- CongestionMonitorSignal
- MostRestrictive(s1, s2)
- if s1 ltCongestionWarninggt or s2
ltCongestionWarninggt - then ltCongestionWarninggt
- elseif s1 ltPreAnnouncementgt or s2
ltPreAnnouncementgt - then ltPreAnnouncementgt
- else ltNoWarninggt
- end ActuationManager
34Adding and Replacing Actuators
- class ActuationManager
-
- public AddActuator CWSLocation gt ()
- AddActuator(loc)
- def act new Actuator()
- in
- as as(1,...,loc) act as(loc1,...,
len as) - pre loc in set inds as
- public ReplaceActuator CWSLocation gt ()
- ReplaceActuator(loc)
- def act new Actuator()
- in
- as as loc -gt act
- pre loc in set inds as
- end ActuationManager
35Operator Control
- class OperatorControl
-
- instance variables
- messageLog seq of seq1 of char
- locations seq of CWSLocation
- inv len messageLog len locations
- end OperatorControl
36Manipulating Log Messages
- class OperatorControl
-
- operations
- public ResetLog () gt ()
- ResetLog()
- atomic
- (messageLog
- locations
- )
- public WriteLog seq1 of char CWSLocation gt
() - WriteLog(message, location)
- atomic
- (messageLog messageLog
- message ConvertNum2String(locati
on) - locations locations location
- )
- end OperatorControl
Notice that WriteLog has an error in the book.
This is the right version.
37Operator Utilities
- class OperatorControl
-
- operations
- public CongestionSpots () gt set of
CWSLocation - CongestionSpots()
- return elems locations
- ConvertLog2File () gt seq of char
- ConvertLog2File()
- return conc messageLog
- end OperatorControl
38Summary
- What have I presented today?
- The notion of sequences as ordered collections
- The basic operations in VDM for manipulating
sequences - The congestion warning system example
- What do you need to do now?
- Continue with your project
- Present your status to all of us
- Read chapter 8 before next lecture
39Quote of the day
In science the credit goes to the man who
convinces the world, not the man to whom the idea
first occurs.
By Sir Francis Darwin (1848 - 1925)