Relations Relations By the end of this part of the course the - - PDF document

relations relations
SMART_READER_LITE
LIVE PREVIEW

Relations Relations By the end of this part of the course the - - PDF document

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


slide-1
SLIDE 1

Relations

slide-2
SLIDE 2

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

slide-3
SLIDE 3

A “Registry” Specification

Declarations registered is a set of STUDENTs

  • ptions 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.

slide-4
SLIDE 4

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}

slide-5
SLIDE 5

Three Elements

The power set of STUDENT

John Bill Sue John Bill Sue John Bill Sue John Bill Bill Sue Sue John

STUDENT= {Bill, John,Sue}

slide-6
SLIDE 6

If A has n elements, how many does its powerset have?

0% 0% 0% 0% 0%

1. 2. 3. 4. 5.

slide-7
SLIDE 7

Which of the following are in ?

0% 0% 0% 0% 0%

1. 2. 3. 4. 5.

slide-8
SLIDE 8

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:

slide-9
SLIDE 9

How many elements does A x B have?

0% 0% 0% 0%

1. 2. 3. 4.

slide-10
SLIDE 10

A Relation, ρ , from a set A to a set B, is a subset of AXB. 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
slide-11
SLIDE 11

Relations in UML diagrams

The existence of a relationship between

  • bjects of type A and objects of type B

is denoted:

A B

slide-12
SLIDE 12

(mark is_enrolled_on CS153) (mary is_enrolled_on CS266) (sue is_enrolled_on CS153) (william is_enrolled_on CS266) (mark is_enrolled_on CS189) (john is_enrolled_on CS153) (john is_enrolled_on CS189) (mark, CS153) (mary, CS266) (sue, CS153) (william, CS266) (mark, CS189) (john,CS153) (john, CS189)

Relations: sets of ordered pairs

slide-13
SLIDE 13

is_enrolled_on as a picture

John Mary Sue Mark William Bill CS189 CS153 CS266 CS183 CS288

slide-14
SLIDE 14

is_enrolled_on as a database table

John CS153 John CS189 Mary CS266 Sue CS153 Mark CS153 Mark CS189 William CS266

slide-15
SLIDE 15

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

slide-16
SLIDE 16

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

slide-17
SLIDE 17

dom(is_enrolled_on)

John Mary Sue Mark William Bill CS189 CS153 CS266 CS183 CS288 The set of students enrolled on some course

slide-18
SLIDE 18

What is dom(R)?

0% 0% 0% 0%

a b c 1 2 3 4

1.

{ a,b,c}

2.

{ (a,1),(a,2),(b,4)}

3.

{ a,b}

4.

{ 1,2,4}

R

slide-19
SLIDE 19

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

slide-20
SLIDE 20

ran(is_enrolled_on)

John Mary Sue Mark William Bill CS189 CS153 CS266 CS183 CS288 The set of courses with some students enrolled

slide-21
SLIDE 21

What is ran(R)?

0% 0% 0% 0%

a b c 1 2 3 4 R

1.

{ 1,2,3,4}

2.

{ 1,2}

3.

{ a,b}

4.

None of the above

slide-22
SLIDE 22

Library example again

is_on_loan_to is a

relation from BOOKS to PEOPLE

is_on_loan_to : BOOKS PEOPLE We will use relational notation to specify the

library system

Library a set stock a set members A rule is_on_loan_to

slide-23
SLIDE 23

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.

slide-24
SLIDE 24

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

}

}

slide-25
SLIDE 25

Exercise: returning books

Return(book b, person p) b is currently on loan to p Remove b is on loan to p

slide-26
SLIDE 26

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 ]

slide-27
SLIDE 27

More relational notation

slide-28
SLIDE 28

Running example: registered

Abigail Barney Charlotte Dora Emily CS189 CS153 CS154 CS173 CS183 Fran

slide-29
SLIDE 29

Domain restriction

slide-30
SLIDE 30

registered without domain restriction

Abigail Barney Charlotte Dora Emily CS189 CS153 CS154 CS173 CS183 Fran

slide-31
SLIDE 31

registered with domain restriction

Abigail Barney Charlotte Dora Emily CS189 CS153 CS154 CS173 CS183 Fran

slide-32
SLIDE 32

Domain anti-restriction

slide-33
SLIDE 33

Abigail Barney Charlotte Dora Emily CS189 CS153 CS154 CS173 CS183 Fran

Example: Registered with domain anti-restriction

slide-34
SLIDE 34

Range restriction

slide-35
SLIDE 35

Example: registered with a range restriction

Abigail Barney Charlotte Dora Emily CS189 CS153 CS154 CS173 CS183 Fran

slide-36
SLIDE 36

Range anti-restriction

slide-37
SLIDE 37

Example: registered with a range anti-restriction

Abigail Barney Charlotte Dora Emily CS189 CS153 CS154 CS173 CS183 Fran

slide-38
SLIDE 38

What is denoted by ?

0% 0% 0%

1.

The course CS154

2.

The people registered for CS154

3.

Something else

slide-39
SLIDE 39

What is denoted by ?

0% 0% 0%

1.

Those of ian, james, kate who have registered for some courses

2.

The courses ian, james, and kate have registered for

3.

The course registrations that ian, james, and kate are involved in

slide-40
SLIDE 40

What is denoted by ?

0% 0% 0%

The people not re... The people regist... The registrations...

1.

The people not registered for CS154

2.

The people registered for courses other than CS154

3.

The registrations for courses other than CS154

slide-41
SLIDE 41

Relational inverse

slide-42
SLIDE 42

Example: inverse of registered

Abigail Barney Charlotte Dora Emily CS189 CS153 CS154 CS173 CS183 Fran

slide-43
SLIDE 43

Relational image

slide-44
SLIDE 44

Example: Registered

Abigail Barney Charlotte Dora Emily CS189 CS153 CS154 CS173 CS183 Fran

slide-45
SLIDE 45

Example: extracting information from a relation

slide-46
SLIDE 46

Exercise: extracting information

slide-47
SLIDE 47

What denotes the courses that Ian is registered for?

0% 0% 0% 0%

1. 2. 3. 4.

slide-48
SLIDE 48

Summary

slide-49
SLIDE 49

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.