Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sets Learning Outcomes 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.

Similar presentations


Presentation on theme: "Sets Learning Outcomes 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."— Presentation transcript:

1 Sets Learning Outcomes 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

2 So far we have looked at specifications involving simple scalar types enumerated types Most systems manipulate complex data structures like lists. To model such data structures, VDM-SL provides several data type constructors. The first abstract structured type of VDM-SL that we will look at is the familiar notion of a set. Data Type Constructors ( ,  and so on) (e.g. <BROKEN> |<WORKING> | < IDLE> )

3 Sets for system modelling 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?  

4 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:  -set someOtherNumbers:  -set importantDays : Day-set -set

5 Defining sets in VDM-SL Three ways to initialise the values of a set: by enumeration; by number ranges; by comprehension.

6 Defining sets by enumeration Involves listing the elements individually, separated by commas,,, and enclosed in braces { } For example: someNumbers = {2, 4, 28, 19,10 } importantDays = {<FRI>, <SAT>, <SUN>}

7 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} = { }

8 Defining a set by comprehension 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)} someOtherNumbers = { x 2 | x  {2,…,6}} { 2, 3, 4, 5, 6 } { 2, 3, 4, 5, 6 } 4,9,16,25,36

9 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>}

10 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 > } thenj  k = {<MON>, <TUE>}

11 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> }

12 Set operations…. cont’d 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}

13 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

14 The PatientRegister class PatientRegister reg: Patient [*] addPatient (Patient) removePatient (Patient) getPatients ( ): Patient [*] isRegistered (Patient): Boolean numberRegistered ( ):Integer

15 Modelling the PatientRegister class in VDM-SL types Patient values LIMIT state PatientRegister of reg: init mk-PatientRegister ( )  end Patient-set inv mk-PatientRegister (r)  card r  LIMIT r = { } = TOKEN :  = 200 r

16 The addPatient operation addPatient ( ) ext pre post reg =  { patientIn} patientIn  regcard reg < LIMIT  patientIn: Patient reg: Patient-setwr patientIn  reg

17 The removePatient operation removePatient ( ) ext pre post patientIn: Patient wrreg: Patient-set reg = \ { patientIn}patientIn  reg patientIn  reg

18 getPatients ( ) ext pre post The getPatients operation output: Patient-set rd reg: Patient-set output = reg TRUE

19 isRegistered ( ) ext pre post The isRegistered operation patientIn: Patient query:  rdreg: Patient-set  query patientIn  reg TRUE

20 numberRegistered () ext pre post The numberRegistered operation total:  rdreg: Patient-set total = card reg TRUE

21 The Airport Flight Control System " 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 "

22 A UML specification of the Airport class Airport permission: Aircraft [*] landed: Aircraft [*] givePermission(Aircraft) recordLanding(Aircraft) recordTakeOff(Aircraft) getPermission( ): Aircraft [*] getLanded( ): Aircraft [*] numberWaiting(): Integer

23 types state Airport of init mk-Airport ( )  end Modelling the Airport class in VDM-SL Aircraft = TOKEN permission: Aircraft-set landed: Aircraft -set inv mk-Airport(p,l)  l  p p = { } l = { }  p, l

24 The givePermission operation givePermission( ) ext pre post craftIn: Aircraft permission: Aircraft - set wr permission =  {craftIn } craftIn  permission

25 The recordLanding operation recordLanding ( ) ext pre post craftIn: Aircraft permission: Aircraft -set landed: Aircraft -set rd wr landed =  {craftIn} craftIn  permission craftIn  landed 

26 recordTakeOff ( ) ext pre post The recordTakeOff operation craftIn: Aircraft permission: Aircraft -set landed: Aircraft -set wr wr landed = \ {craftIn }permission = \ { craftIn }  craftIn  landed

27 getPermission( ) ext pre post The getPermission operation out: Aircraft-set permission: Aircraft -setrd out = permission TRUE

28 getLanded( ) ext pre post The getLanded operation out: Aircraft -set landed: Aircraft -set rd out = landed TRUE

29 numberWaiting( ) ext pre post The numberWaiting operation total:  permission: Aircraft -set landed: Aircraft -set rd rd card (permission \ landed)total =


Download ppt "Sets Learning Outcomes 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."

Similar presentations


Ads by Google