300 likes | 412 Views
Maps. Learning Outcomes. At the end of this lecture you should be able to:. provide a definition of a VDM map; utilize and interpret map notation; make appropriate use of the VDM map operators; identify situations in which a map is an appropriate data type;
E N D
Maps Learning Outcomes At the end of this lecture you should be able to: • provide a definition of a VDM map; • utilize and interpret map notation; • make appropriate use of the VDM map operators; • identify situations in which a map is an appropriate data type; • write VDM specifications using maps.
Introduction Computing systems often involve relating 2 types of value together; A map is a special sort of set, one which contains a set of maplets; Each maplet connects an element of one set to an element of another set; The first set is referred to as the domain, the second is referred to as the range. Yalmaz Coffee Aaron VDM Password Name Rajneet Apple Nkumu RANGE DOMAIN
Using the map type in VDM-SL To declare a variable to be of type Map we use a special arrow ; DomainType RangeType For example, to declare a variable m that maps characters to natural numbers we would write: m : Char To declare a variable, users, that maps names to passwords we could write users : Name Password
Notation We can express this table as a map, which we will call sensors: sensors : Sensor Condition sensors = {A <low>, B <normal>, C <normal>, D <high>, E <normal>,F <normal>} sensors = { }
Map operators m1 = { a 1, b2, c2, d 3, e 4} m2 = { a 2, f 1, c 7} The domain operator and the range operators: { a, b, c, d, e } domm1 = { 1, 2, 3, 4 } rngm1 = { a, f, c } domm2 = { 1, 2, 7 } rngm2 =
Map operators m1 = { a 1, b2, c2, d 3, e 4} m2 = { a 2, f 1, c 7} m3 = { f 2, g 6} The union operator, { a 1, b 2, c 2, d 3, e 4,f 2, g 6} m1m3 = m1m2 = undefined undefined m2m3 =
Map operators m1 = { a 1, b2, c2, d 3, e 4} m2 = { a 2, f 1, c 7} m3 = { f 2, g 6} The override operator, † { a 2, b 2, c 7, d 3, e 4, f 1} m1 † m2= m3 † m2= { f 1, g 6, a 2, c 7}
a domain restriction operator, {a, c, e} m1 = { e,f } m2 = { } m3 = Map operators m1 = { a 1, b2, c2, d 3, e 4} m2 = { a 2, f 1, c 7} m3 = { f 2, g 6} { a 1, c 2, e 4} { f 1} { }
a domain deletion operator, {a, c, e} m1 = { e,f } m2 = { } m3 = Map operators m1 = { a 1, b2, c2, d 3, e 4} m2 = { a 2, f 1, c 7} m3 = { f 2, g 6} { b 2, d 3 } { a 2, c 7} { f 2, g 6 }
a range restriction operator, m1 { 2 } = m2 {1, 4 } = m3 {} = Map operators m1 = { a 1, b2, c2, d 3, e 4} m2 = { a 2, f 1, c 7} m3 = { f 2, g 6} { b2, c2 } { f 1} { }
a range restriction operator, m1 { 2 } = m2 {1, 4 } = m3 {} = Map operators m1 = { a 1, b2, c2, d 3, e 4} m2 = { a 2, f 1, c 7} m3 = { f 2, g 6} { a 1, d 3, e 4 } { a 2, c 7} { f 2, g 6}
Map operators m1 = { a 1, b2, c2, d 3, e 4} m2 = { a 2, f 1, c 7} m3 = { f 2, g 6} Map Application m1( d ) = 3 m2( f ) = 1 m3( f ) = 2 m3( x ) = undefined
Specifying a high-security building User name + Password
Employee name: String password: String Employee name: Name password: Password SecuritySys authorized : Employee[*] inside : String[*] addEmployee(String, String) removeEmployee(String) enter(String, String) : Signal leave(String) : Signal SecuritySys authorized : Employee[*] inside : Name[*] addEmployee(Name, Password) removeEmployee(Name) enter(Name, Password) : Signal leave(Name) : Signal <<enumeration>> Signal OPEN_DOOR ACTIVATE_ALARM UML Diagrams
Name Password The VDM-SL state specification types Name = TOKEN Password = TOKEN Signal = <open_door>|< activate_alarm> Employee :: name: Name password: Password state SecuritySys of authorized : Employee-set ? : Name-set inside Only authorised employees can be inside the building inv mk-SecuritySys(a,i) i doma init mk-SecuritySys(a,i) a = { } i = { }
authorized : Name Password authorized = {nameIn passwordIn} addEmployee() ext pre post nameIn : Name, passwordIn : Password wr nameIn domauthorized
authorized : Name Password authorized = {nameIn} nameIn : Name removeEmployee() ext pre post wr rd inside : Name-set nameIn domauthorized nameIninside
authorized : Name Password authorized(nameIn) passwordIn nameIn ) (authorized(nameIn) = passwordIn nameIn ) (inside = signal = <activate_alarm>) (inside = {nameIn}signal = <open_door>) enter() ext pre post signal : Signal nameIn : Name, passwordIn : Password rd wr inside : Name-set TRUE ( nameIn dom authorised
nameIn inside = \ {nameIn} nameIn inside = leave () ext pre post nameIn : Name signal : Signal wr inside : Name-set TRUE signal = <open_door> signal = <activate_alarm>
A robot monitoring system WORKING Sector A Sector B BROKEN IDLE
RobotMonitor robots: Robot[*] addRobot (String) removeRobot (String) setToWork (String, Sector) finishWork (String) needsRepair (String) fixed(String) inSector (Sector): String [*] numberToRepair (): Integer RobotMonitor robots: Robot[*] addRobot (Name) removeRobot (Name) setToWork (Name, Sector) finishWork (Name) needsRepair (Name) fixed(Name) inSector (Sector): Name [*] numberToRepair (): Integer Robot name: String mode: Mode sector: Sector Robot name: Name mode: Mode sector: Sector <<enumeration>> Mode working idle broken <<enumeration>> Sector A B
Name Robot types Name = TOKEN Mode Sector Robot = <working>|<idle>| <broken> = <A>|< B> :: name mode sector : Name : Mode : [Sector] m = <working> snil inv mk-Robot(-, m, s) stateRobotMonitorof robots: invmk-RobotMonitorSys(r) n domr n = r(n).name initmk-RobotMonitorSys(r) r = { } end
robots: robots = Name Robot addRobot () ext pre post nameIn: Name wr nameIndom robots { nameIn mk-Robot (nameIn, <idle>, nil) }
robots: robots = {nameIn } Name Robot removeRobot () ext pre post nameIn: Name wr nameIndom robots robots(nameIn).mode <working>
robots: robots = Name Robot setToWork() ext pre post nameIn: Name, sectorIn Sector wr nameIndom robots robots(nameIn).mode = <idle> † { nameIn mk-Robot(nameIn, <working>, sectorIn )}
robots: robots = Name Robot finishWork() ext pre post nameIn: Name wr nameIndom robots robots(nameIn).mode = <working> † { nameIn mk-Robot(nameIn, <idle>,nil )}
robots: robots = Name Robot needsRepair() ext pre post nameIn: Name wr nameIndom robots † { nameIn mk-Robot(nameIn, <broken>,nil )}
robots: { nameIn ( (nameIn), mode<idle>) } robots = Name Robot fixed() ext pre post nameIn: Name wr nameIndom robots robots(nameIn).mode = <broken> †
robots: Name Robot inSector () ext pre post sectorIn: Sector result : Name-set rd TRUE result = { ? | ? ? } r.name r rng robots r.sector = sectorIn
robots: Name Robot numberToRepair () ext pre post number : rd TRUE result = r card { ? | ? ? } r rng robots r.mode = <broken>