Relations
Relations � By the end of this part of the course the student should understand and be able to use the concepts of relations and functions in a Z specification. � The concepts introduced are: � Power set � Cartesian product � Relation � Function
A “Registry” Specification Declarations registered is a set of STUDENTs options is a set of MODULES is_enrolled_on is a relation between STUDENTs and MODULEs Invariant More later, but essentially this is saying that Only STUDENTs who are registered may be enrolled_on MODULEs that are options.
Power Set � The power set of a set is the set of all subsets of that set. � The power set of a set A is written � e.g. STUDENT = { Bill, John, Sue} � �
The power set of STUDENT Sue Bill Bill John Sue Bill Sue John John Sue John Bill STUDENT= {Bill, John,Sue} Sue John Bill Three Elements
If A has n elements, how many does its powerset have? 1. 2. 3. 4. 5. 0% 0% 0% 0% 0%
Which of the following are in ? 1. 2. 3. 4. 5. 0% 0% 0% 0% 0%
Cartesian Product of two sets � The cartesian product of two sets A and B is the set of all ordered pairs (x,y), where � x is an element of A and � y is an element of B � The cartesian product of A and B is written A x B � Example:
How many elements does A x B have? 1. 2. 3. 4. 0% 0% 0% 0%
Relations A Relation, ρ , from a set A to a set B , � is a subset of A X B . It is a set of pairs . is_on = { (john,CS189), (sue,CS189), (sue,CS154) } knows_about ={ (john,CS189), (john,CS154), (bill,CS189), (bill,CS154), (sue,CS189), (sue,CS154) } likes ={ } attends ={ (john,CS189), (john,CS154), (sue,CS189), (sue,CS154) } �
Relations in UML diagrams � The existence of a relationship between objects of type A and objects of type B is denoted: B A
Relations: sets of ordered pairs (mary is_enrolled_on CS266) (john is_enrolled_on CS189) (mary, CS266) (john is_enrolled_on CS153) (john, CS189) (mark is_enrolled_on CS189) (mark, CS189) (john,CS153) (william is_enrolled_on CS266) (william, CS266) (sue is_enrolled_on CS153) (sue, CS153) (mark is_enrolled_on CS153) (mark, CS153)
is_enrolled_on as a picture John CS153 CS189 Mary Sue CS266 Bill CS183 Mark CS288 William
is_enrolled_on as a database table John CS153 John CS189 Mary CS266 Sue CS153 Mark CS153 Mark CS189 William CS266
Infix notation � If then we can write “ ’’ � This is called infix notation � e.g. (Mary,CS266) is_enrolled_on written as Mary is_enrolled_on CS266 � e.g. 2 < 3
Domain � The domain of a relation is the set of all elements in A that are related by to something in B � This is written � Thus if then � It is always true that
dom(is_enrolled_on) CS153 John CS189 Mary Sue CS266 Bill CS183 Mark William CS288 The set of students enrolled on some course
What is dom(R)? { a,b,c} 0% 1. { (a,1),(a,2),(b,4)} 0% 2. 0% { a,b} 3. 0% { 1,2,4} 4. 1 a 2 b 3 c 4 R
Range � The range of a relation is the set of all elements in B that are related by to something in A � This is written � Thus if then � It is always true that
ran(is_enrolled_on) John CS153 CS189 Mary Sue CS266 Bill CS183 Mark CS288 William The set of courses with some students enrolled
What is ran(R)? { 1,2,3,4} 0% 1. { 1,2} 0% 2. 0% { a,b} 3. 0% None of the above 4. 1 a 2 b 3 c 4 R
Library example again � is_on_loan_to is a Library a set stock relation from a set members BOOKS to PEOPLE A rule is_on_loan_to � is_on_loan_to : BOOKS PEOPLE � We will use relational notation to specify the library system
The Library state in Z � We now have an INVARIANT on the Library state: that only elements of stock can be on loan, and only to members.
Borrowing Books Borrow( book b, person p) b is not already on loan to anyone Add b is on loan to p ` Delta’ means change of state } Declare input parameters } Preconditions Postcondition
Exercise: returning books Return( book b, person p) b is currently on loan to p Remove b is on loan to p
Summary � Power set � Cartesian Product � Relations � Infix notation 2< 3 � Domain of a relation/function � Range of a relation/function � Z schemas � Input and output parameters in Z event schemas [Currie chapters 6-7 , Haggarty chapters 4-5 ]
More relational notation
Running example: registered Abigail CS189 CS153 Barney CS154 Charlotte CS173 Dora Emily CS183 Fran
Domain restriction
registered without domain restriction Abigail CS189 CS153 Barney CS154 Charlotte CS173 Dora Emily CS183 Fran
registered with domain restriction Abigail CS189 CS153 Barney CS154 Charlotte CS173 Dora Emily CS183 Fran
Domain anti-restriction
Example: Registered with domain anti-restriction Abigail CS189 CS153 Barney CS154 Charlotte CS173 Dora Emily CS183 Fran
Range restriction
Example: registered with a range restriction Abigail CS189 CS153 Barney CS154 Charlotte CS173 Dora Emily CS183 Fran
Range anti-restriction
Example: registered with a range anti-restriction Abigail CS189 CS153 Barney CS154 Charlotte CS173 Dora Emily CS183 Fran
What is denoted by ? 0% The course CS154 1. 0% The people registered for CS154 2. Something else 0% 3.
What is denoted by ? Those of ian, james, kate who have 1. 0% registered for some courses The courses ian, james, and kate have 0% 2. registered for 0% The course registrations that ian, 3. james, and kate are involved in
What is denoted by ? The people not 1. registered for CS154 The people 2. registered for 0% 0% 0% courses other than CS154 The registrations for 3. courses other than CS154 The people not re... The people regist... The registrations...
Relational inverse
Example: inverse of registered CS189 Abigail CS153 Barney Charlotte CS154 Dora CS173 Emily CS183 Fran
Relational image
Example: Registered CS189 Abigail Barney CS153 Charlotte CS154 Dora CS173 Emily CS183 Fran
Example: extracting information from a relation
Exercise: extracting information
What denotes the courses that Ian is registered for? 1. 2. 3. 4. 0% 0% 0% 0%
Summary
Please ensure you return your handset before leaving. The handset is useless outside of this class and non-returns will decease the likelihood of future voting system use on this course.
Recommend
More recommend