one slide per page PDF format
Transcript of one slide per page PDF format
LECTURE 17: LIBRARY CASE STUDY
Software Engineering
Mike Wooldridge
Lecture 17 Software Engineering
1 A Library Management System
• In this lecture, we specify a simple librarysystem.
• Operations:
– check out a book;– return a book;– add a book to library;– remove book from library;– get list of books by author or subject
area;– get list of books checked out by
particular borrower;
Mike Wooldridge 1
Lecture 17 Software Engineering
• All books must either be checked out oravailable for check out.
• No book may be simultaneously checkedout and available.
• There is an upper limit to number of booksthat may be checked out.
Mike Wooldridge 2
Lecture 17 Software Engineering
1.1 Types
•We need sets for:
– all possible books;– all possible copies of books;– all possible people;– all possible authors;– all possible subjects;– the various reports that may be
produced.
• So parachute in:
[BOOK,COPY,PERSON,AUTHOR,SUBJECT,REPORT]
Mike Wooldridge 3
Lecture 17 Software Engineering
1.2 State Space
• The state space is describes in severalsteps. First, a schema containinginformation relating to books in thelibrary.
ParaLibraryinstance of : COPY 7→ BOOKwritten by : BOOK 7→ IP AUTHORabout : BOOK 7→ IP SUBJECTdom written by ⊆ ran instance ofdom about ⊆ ran instance of
Mike Wooldridge 4
Lecture 17 Software Engineering
• instance of tells us what book a copy is aninstance of;
• the set
ran instance of
is the set of all books in the library;
• written by tells us who a book is writtenby; there may be more than one author,hence the powerset operation; there maybe no authors;
• about tells us the subjects a book is about;there may be no subjects;
• first invariant tells us that we only knowwho wrote books in the library;
• second invariant tells us that we onlyknow subjects of books in the library.
Mike Wooldridge 5
Lecture 17 Software Engineering
• The database part of the schema is asfollows:
LibraryDBborrower, staff : IP PERSONavailable, out : IP COPYborrowed by : COPY 7→ PERSONborrower ∩ staff = ∅available ∩ out = ∅dom borrowed by = outran borrowed by ⊆ borrower∀p : borrower • #borrowed by∼(|{p}|)≤MaxCopies
Mike Wooldridge 6
Lecture 17 Software Engineering
• borrower is the set of all borrowers knownto the system;
• staff is the set of all staff known to thesystem;
• available is the set of all available books;
• out is the set of borrowed books (i.e., onesthat have been checked out);
• borrowed by tells us who borrowed thebooks out on loan.
Mike Wooldridge 7
Lecture 17 Software Engineering
• 1st invariant tells us that a person cannotbe both a borrower and a staff;
• 2nd invariant tells us that books cannot beboth available and checked out;
• 3rd invariant tells us that the only booksappear have been borrowed by someoneare those that are out;
• 4th invariant tells us that books can onlybe borrowed by borrowers;
• 5th invariant tells us that a borrower canonly have out up to the maximum numberof books.
Mike Wooldridge 8
Lecture 17 Software Engineering
• The library state space is then as follows:
LibraryParaLibraryLibraryDBdom instance of = available ∪ out
• the only invariant in this schema tells usthat the library does not know anythingabout books which are not in stock.
Mike Wooldridge 9
Lecture 17 Software Engineering
1.3 The Operations
•We assume initialisation operations; theseare trivial.
• First we look at checking out books. . .
• Inputs: person name (n?) and copy (c?).
CheckOut∆Libraryn? : PERSONc? : COPYn? ∈ borrowerc? ∈ available#borrowed by∼(|{n?}|)< MaxCopies
available′ = available \ {c?}out′ = out ∪ {c?}borrowed by′ = borrowed by∪{c? 7→ n?}
Mike Wooldridge 10
Lecture 17 Software Engineering
• (Note that f∼ is the inverse of f .)
• 1st precondition is that the person tryingto borrow must be a known borrower;
• 2nd precondition is that the book must beavailable;
• 3rd precondition is that the person tryingto borrow must have out fewer than themaximum number of books available;
• the postconditions define the changesmade to available, out and borrowed by.
Mike Wooldridge 11
Lecture 17 Software Engineering
1.4 Returning a Book
• One input: the copy to be returned.
Return∆Libraryc? : COPYc? ∈ outavailable′ = available ∪ {c?}out′ = out \ {c?}borrowed by = {c?} −� borrowed by
Mike Wooldridge 12
Lecture 17 Software Engineering
• precondition states that the book can onlybe returned if it is out;
• 1st post-condition says that the book isavailable after the operation;
• 2nd post-condition says that the book is nolonger out;
• 3rd post-condition uses domainsubtraction to remove the correct recordfrom the borrowed by function.
• For example,
borrowed by = {b01 7→ mjw, b02 7→ en,b03 7→ mjw}{b01} −� borrowed by = {b02 7→ en,
b03 7→ mjw}
Mike Wooldridge 13
Lecture 17 Software Engineering
1.5 Adding Books to the Library
• There are two cases to consider:
– where the book is completely new tothe library;
– where the book is another copy of abook that is already in the library.
•We have two schemas to capture these twosituations:
– AddNewBook;– AddAnotherCopy.
Mike Wooldridge 14
Lecture 17 Software Engineering
AddNewBook∆Libraryc? : COPYb? : BOOKa? : IP AUTHORs? : IP SUBJECTb? 6∈ ran instance ofc? 6∈ available ∪ outavailable′ = available ∪ {c?}instance of ′ = instance of ∪ {c? 7→ b?}written by′ = written by ∪ {b? 7→ a?}about′ = about ∪ {b? 7→ s?}
Mike Wooldridge 15
Lecture 17 Software Engineering
AddAnotherCopy∆Libraryc? : COPYb? : BOOKc? 6∈ available ∪ outb? ∈ ran instance ofavailable′ = available ∪ {c?}instance of ′ = instance of ∪ {c? 7→ b?}
Mike Wooldridge 16
Lecture 17 Software Engineering
1.6 Removing Books
• Removing a books from the library issimilarly complicated; once again there are2 possibilities to consider. . .
– removing a book that is the only copy;– removing one copy of a book leaving
several other copies behind.
• Two schemas:
– RemoveOther to remove one of severalcopies;
– RemoveLast to remove the last copy.
Mike Wooldridge 17
Lecture 17 Software Engineering
RemoveOther∆Libraryc? : COPYc? ∈ available#(instance of∼(|{instance of (c?)}|)) > 1
available′ = available \ {c?}
• Note that there is no need to alter anyvariables in ParaLibrary; we only changeavailable, to indicate that the book is nolonger available.
Mike Wooldridge 18
Lecture 17 Software Engineering
RemoveLast∆Libraryc? : COPYc? ∈ available#(instance of∼(|{instance of (c?)}|)) = 1
available′ = available \ {c?}instance of ′ = {c?} −� instance ofwritten by′ = {instance of (c?)}−�
instance ofabout′ = {instance of (c?)} −� about
Mike Wooldridge 19
Lecture 17 Software Engineering
1.7 Interrogating the Database
• Two options:
– search by author;– search by subject;– find out what copies someone has
borrowed.
Mike Wooldridge 20
Lecture 17 Software Engineering
• ByAuthor takes an author name andproduces the set of all books that theauthor appeared in the ‘author’ list of.
ByAuthorΞLibrarya? : AUTHORout! : IP BOOKout! = {b : BOOK | a? ∈ written by(x)}
• BySubject takes a set of subjects andproduces a list of all the books which havethese subjects in their ‘about’ list.
BySubjectΞLibrarys? : IP SUBJECTout! : IP BOOKout! = {b : BOOK | s? ⊆ about(b)}
Mike Wooldridge 21
Lecture 17 Software Engineering
• Finally, finding out who has borrowedwhat. . .
BooksBorrowedByΞLibraryn? : PERSONout! : IP COPYn? ∈ borrowerout! = borrowed by∼(|{n?}|)
Mike Wooldridge 22