420 likes | 517 Views
Schema Operators. State. We can use the language of schemas to describe the state of a system, and operations upon it.
E N D
State • We can use the language of schemas to describe the state of a system, and operations upon it. • Different aspects of the state -- and different aspects of a given operation -- can be described as separate schemas; these schemas may be combined in various ways using schema operators. • In this way, we may factorise the description, identifying common aspects for re-use, and providing structure.
Same variable declared in both schemas must match the its type • Otherwise the conjunction will be undefined
Schema conjunction allows specifying different aspects of a specification separately • Combine them to form a complete description • Adv: simple, well-structured, can be easily understood
Example • From previous lecture : the schema for BoxOffice • let say the theatre has premiere and standard performances. Let declared new variable of type Status to differentiate the kind of performances
Example • let say we have a set friends who are currently registered as friends of the theatre. Only those customers who are friends may buy seats for the premieres • If the current performance is a premiere, then seats may be sold only to friends
Example • We use conjunction to describe an enhance box office • It is equivalent to
Schema Inclusion • Reuse the name of one schema in the declaration part of another • When a schema name appears in a declaration part of a schema, the result is a merging of declarations and a conjunction of constraints.
Schema decoration • Suppose the state of a system is modelled by a schema State with two components a and b, and these are introduced under a constraint P State a : A b : B P • Each object of schema type State represents a valid state : a binding of a and b in which predicate P is satisfied • P forms part of state invariant for the system
Schema decoration • to describe an operation upon the state; use two copies of State: one represents the state before the operation; the other represents the state afterwards. We use decoration to distinguish them State’ a’ : A b’ : B P[a’/a,b’/b]
Schema decoration • We may describe an operation by including both State and State’ in the declaration part
Example • The set of all valid states of box office system • Each state is a binding of variable seating and sold; the state invariant insists that only allocated seats are sold, and the relationship between seats and customers remains functional
Example • To describe the state of box office system after some operation • Introduces seating’ and sold’ to correspond to the seat seating and the sales record after the operation has been performed.
Example • Encapsulate all of the information in a single schema; schema representing the successful purchasing of seat s? by customer c?
Exercise • You were given a schema named Library during the last week lecture. The schema shows state before operation. You are required to write another schema to show the state after operation. Then write another schema named LibraryState to show the merging of the schemas before and after operation
Input and Output • An operation may involve inputs and outputs. These are declared in the normal way, although there is a convention regarding their names: - the name of an input should end in a question mark - the name of an output should end in an exclamation mark
s? is an input seat (name of seat); c? is the input customer (name of customer) buying s? • the predicate s? seating \ dom sold is to make sure that input seat is available for sale beforehand • The predicate sold’ = sold {s? ↦ c?}
We may add an output to our description of operation • We use response from a free type of responses Response = ok | sorry • And may be declared in a separate schema Success • The effect of a successful purchase is Purchase0 Success r! : Response r! = ok
exercise • Use schema inclusion for schema Add_book_to_library_ok
exercise • What should be in Add_book_to_library_ok now? • write a schema to check if a book is already in the library by using Library
Initialisation • When using an abstract data type for a system, we should include a description of the initial state. • An initialisation is a special operation for which the before state is unimportant. (does not refer to before state) • Such an operation can be modelled by an operation schema that contains only a decorated copy of the state:
example Init_library Library’ books’ = borrowers =
Schema Disjunction • If S and T are two schemas, then their disjunction S T is a schema - whose declaration is a merge of the two declarations - whose constraint is a disjunction of the two constraints
example • Schema to describe unavailable seat • Schema to indicate a seat could not be sold Failure r! : Response r! = sorry
Can combine to the schemas to describe complete operation for purchase: Purchase ≙ (Purchase0 Success) (NotAvailable Failure)
example • For a complete schema to add a book to library is as below: Add_book_to_library ≙ Add_book_to_library_ok Book_already_in_library
Exercise • Write a complete schema to add a borrower to the library system. Please refer to the handout given to the class.