6.005 elements of software construction fall 2008 http ... · why is this useful? conceptual...

37
MIT OpenCourseWare http://ocw.mit.edu 6.005 Elements of Software Construction Fall 2008 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms .

Upload: others

Post on 27-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

MIT OpenCourseWarehttp://ocw.mit.edu

6.005 Elements of Software Construction Fall 2008

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

Page 2: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

6.005elements ofsoftwareconstruction

how to design a photo catalog Daniel Jackson

Page 3: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

topics for today a problem � conceptual design of a photo organizer

a new paradigm � computation over relational structures � today, the abstract design level: object modelling � determines, in particular, model part of MVC (see last lecture)

object modelling � snapshot semantics � basic notation: domain/range, multiplicity, classification � some classic patterns

© Daniel Jackson 2008 2

Page 4: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

the problem

Page 5: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

problem

Screenshot of Adobe Photoshop Lightroom removed due to copyright restrictions. In the Library view, you can select images to add or remove. The left-hand sidebar includes Collections that you can define.

design a photo cataloguing application � Lightroom, iView MediaPro, iPhoto, Aperture, Picasa, etc © Daniel Jackson 2008 4

Page 6: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

what kind of problem is this? mostly about conceptual design � what are the key concepts? � how are they related to one another? � what kinds of structures?

good conceptual design leads to � straightforward path to implementation � simplicity and flexibility in final product

© Daniel Jackson 2008 5

Page 7: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

why a new model? why not use datatype productions? � tree-like structures only: no sharing � immutable types only

why not state machines? � our catalog is a state machine � but the problem lies in the structure of the state � our state machine notation assumed simple states

a new approach: object models � structure is a labelled graph � put another way: sets of objects + relations

© Daniel Jackson 2008 6

Page 8: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

the relational paradigm computation is about � actions, states, transitions � functions, expressions, values � and now: updates and queries on relations

why is this useful? � conceptual modeling � relational databases � object-oriented programming* � semantic web, document object models, etc

*for proposals to make relations explicit in object-oriented programming, see this survey: James Noble, Roles and Relationships, ECOOP 2007 Workshop on Roles and Relationships in Object-Oriented Programming, Multiagent Systems, and Ontologies; http://iv.tu-berlin.de/TechnBerichte/2007/2007-09.pdf

Page 9: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

basic OM notation

Page 10: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

snapshots a snapshot or object diagram � shows a single instance of a structure

example for photo organizer � in this case, two sets

Photo (shown in beige) Collection (in grey)

� and two relations photos: Collection -> Photo

subs: Collection -> Collection

9

a collection

a relationship: C0 is subcollection of C1

a photo

a relationship: P0 in collection C2

© Daniel Jackson 2008

Page 11: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

more snapshots how can we summarize this infinite set?

© Daniel Jackson 2008 10

Page 12: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

an object model each box � denotes a (maybe empty) set of objects

each arc � denotes a relation, ie. set of links between objects

note � objects have no internal structure! � all structure is in the relations

exercise � draw a snapshot that the OM rules out

© Daniel Jackson 2008 11

Collection

Photo

subs

photos

Page 13: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

enriching the notation what’s wrong with these snapshots? � how would we rule them out?

key idea: multiplicity � measure the in-degree and out-degree of each relation

© Daniel Jackson 2008 12

Page 14: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

multiplicity

A BR m n

multiplicity markings � on ends of relation arc � show relative counts

interpretation � R maps m A’s to each B � R maps each A to n B’s

marking/meaning

+ one or more * zero or more ! exactly one ? at most one omitted marking equivalent to *

© Daniel Jackson 2008 13

Page 15: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

R

kinds of function standard kinds of function � easily expressed with multiplicities

? A B

A

R

BR

R is a function

! R is a total function

? ! A B

A

R

BR

R is an injection

+ ! R is a surjection

! ! A B R is a bijection

© Daniel Jackson 2008 14

Page 16: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

multiplicity example we’ve added naming � always an important and subtle issue � is the multiplicity constraint desirable? necessary?

Collection

Photo

subs

photos

Collection Name

name ?!

*

*

?

© Daniel Jackson 2008 15

Page 17: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

classifying objects suppose we to classify photos � by file location: online, offline, missing � by selection: selected, focus Photo

Photo

Offline Missing Online

Selected

Focus

oval means singleton set

© Daniel Jackson 2008 16

Page 18: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

A

classification syntax can build a taxonomy of objects � introduce subsets � indicate which are disjoint � and which exhaust the superset

A

B

A

B C

abstract A

B C

B 㱪 B 㱯 C = 㱵 B 㱮 C = A © Daniel Jackson 2008 17

Page 19: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

relations on subsets when placing a relation � can place on subset � loose multiplicity is a hint

Photo Filepath path !

Offline Missing Online

Volume Date

vol since ! !

© Daniel Jackson 2008 18

Page 20: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

composite a classic pattern � hierarchical containment � file systems, org charts, network domains, etc

you’ve seen this with datatypes � technical differences though � OM allows cycles (but often rule out) � OM can say just one root

Collection

FolderFolio

subs

Photo

photos

© Daniel Jackson 2008 19

Page 21: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

hotel locking

Page 22: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

example: hotel locking modelling physical, distributed state

state in OM need not represent � a centralized store � data stored in a computer

© Daniel Jackson 2008 21

Page 23: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

hotel locking recodable locks (since 1980) � new guest gets a different key � lock is ‘recoded’ to new key � last guest can no longer enter

how does it work? � locks are standalone, not wired

Page 24: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

a recodable locking scheme

card has two keysif first matches lock,recode with second

if second matches,just open

k1

k0

k1 k1

k0

k1 k0

k1

k1

k0

k1 k1

k0

k1 k0

k1

Page 25: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

exercise draw an object model � showing the essential state of hotel locking � state includes front desk, locks, keys held by guests

review � did you exploit multiplicities? keys are all about uniqueness � did you include only the sets and relations that are needed? � are your sets really sets, or are some of them ‘singleton placeholders’? � do all your sets and relations have a clear interpretation? � where are the various parts of the state stored physically? � which relations are modifiable?

Page 26: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

a solution Guest

Card

holds

Room

Key

key

occupies

fst, snd

?

!

!

Issued

g->r in occupies: guest g has checked in for room r but has not yet checked out

k in Issued: key k has already been issued by front desk on some card: used to ensure that locks are always recoded with fresh keys

some subtleties � guest may occupy more than one room � family members may have identical cards

Page 27: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

common errors Desk

Key

issues

Card

Key

has

Hotel

Guest

guests

Key

Snd Fst

be wary of top-level singleton � Desk and Hotel not needed

relations represent state, not actions � so issues is suspect

need enough information in state to support application � has is not enough: need to know which key is first, second

scope of classification � classification of keys into first and second, is by card, not global � so need relation, not subsets to indicate the distinction

Page 28: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

colour palettes

Page 29: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

example: colour palettes modelling the state of an application � how colours are organized

Screenshots of color schemes in the Keynote and PowerPoint presentation programs removed due to copyright restrictions.

essential idea � elements are coloured � can assign colour from palette � gives consistent appearance

© Daniel Jackson 2008 28

Page 30: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

colour

colour

colour

palette object models three subtly different approaches � think what happens when palette is modified � hard vs. soft links: as in Unix

palette

Doc

Element

elements

Palette

Swatch

swatches

palette

Doc

Element

elements

Palette

Swatch

swatches

value key

lcolour

palette

Colour

value

Name

key

Colour

value

lcocolour

Doc

elements

Palette

Swatch

swatches

Element

Name

keylcolour

Name Colour

“Every problem in computer science can be solved by introducing another level of indirection” -- David Wheeler

© Daniel Jackson 2008 29

Page 31: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

completing the organizer

Page 32: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

issues to resolve can collections hold photos and subcollections? � decision: yes, so not Composite pattern

how are “all photos” in catalog represented? � decision: introduce non-visible root collection

unique collection names? � decision: file system style, so siblings have distinct names

do parents hold children’s photos? � in logic: all c: Collection | c.subs.photos in c.photos ? � decision: use two relations instead

c.inserted: the photos explicitly inserted into collection c

c.photos: the photos in collection c implicitly and explicitly

invariant relates these: c.photos = c.inserted + c.subs.photos © Daniel Jackson 2008 31

Page 33: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

final object model

Collection

Visible

subs

Photo

inserted, photos

Root Selected

Image image

!

Collection Name

name !

!

additional constraints � all collections reachable from root (implies acyclic)

Collection in Root.*subs � implicit photos are inserted photos plus photos in subcollections

all c: Collection | c.photos = c.inserted + c.subs.photos � names unique within parent

all c: Collection | no c1, c2: c.subs | c1 != c2 and c1.name = c2.name © Daniel Jackson 2008 32

Page 34: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

modeling hints

Page 35: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

hints how to pick sets � be as abstract as possible (thus Name, not String; SSN, not Number) � but values to be compared must have same type (so Date, not Birthday) � beware of singletons -- often a sign of code thinking

how to pick relations � represent state, not actions (so atFloor: Elevator->Floor, not arrives) � direction is semantic; doesn’t constrain ‘navigation’

choosing names � choose names that make interpretation clear � include a glossary explaining what relations and sets mean

© Daniel Jackson 2008 34

Page 36: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

summary

Page 37: 6.005 Elements of Software Construction Fall 2008 http ... · why is this useful? conceptual modeling relational databases object-oriented programming* semantic web, document object

principles data before function � before thinking about system function, think about data

an object model is an invariant � meaning is set of structured states � declared sets + subset relationships + relations between sets + multiplicities � augment diagram with textual constraints (in Alloy, as above, or just English)

model objects are immutable � all state kept in subsets and relations � model objects have no ‘contents’ � important to keep coding options open

© Daniel Jackson 2008 36