430 likes | 708 Views
Lecture 11 Formal Modeling: Z, OCL homework Alloy background. Review Lecture 10 Z. Roadmap for Today Z language : elements of Z - review with small examples Z modeling : walk through 3 case studies – put it together break Alloy background. Z Basic Constructs.
E N D
Review Lecture 10 Z Roadmap for Today • Z language: elements of Z - review with small examples • Z modeling: walk through 3 case studies – put it together • break • Alloy background
Z Basic Constructs • Declarations introduce variables. • Expressions describe values that variables can assume. • Predicatesplace constraints on the values that variables do assume.
Set and Type Set Name: Z, DICE Set Expression: {i: Z|1 i 6} Every object belongs to a set called its type. Free Type Basic Type Z, [NAME] - include indefinite number of elements
Exercise: Set Expression in Z SetName == {x: T | pred(x)• expr(x)} Natural numbers: N == {i: Z|i 1} Odd numbers: ODD == {i: Z• 2*i+1} // the first part introduces local variables; their scope includes the expression after • Prime numbers:
Introduce Variables in Z Declaration: x: Z Axiomatic definitions: define global, optional constraints size: N size > 640 Normalized Declaration: s, b: Z s SMALL b BIG
Expressions and Predicates • Expressions: computing values • Constants: 1, 2, red • Operators on constants and variables: arithmetic, set • Predicate: constraining values – return true/false • Equality • Arithmetic relations • Set membership • Set relations • Logic: building complex predicate
Structures • Tuples: instances of Cartesian product types [NAME] ID==N DEPT::= admin|manufacturing|research EMPLOYEE == ID× NAME ×DEPT • Relations: a set of tuples (table/database)
Binary Relations and Functions Function: binary relations where each element in the domain appears just once phonef(doug) = 4107
Lambda Expressions Define functions ( declaration | predicate expression) Use functions without writing declarations Compare to set definitions! •
Sequence: Model Array and Lists Sequence: <>
let/if-then-else let In the let-expression (let x1 == E1; … ; xn == En •E), the variables x1 … xnare local; their scope includes the expression E (let r == iroot(a) • r*r < a < (r+1)*(r+1)) if then else - conditional expression: if P then E1 else E2 – E1 and E2 type should be the same | x | = if x > 0 then x else -x
Exercise: Formal Reasoning Philip works on the adhesives team in the materials group, which is part of the research division Is Philip in the research division?
Exercise: Formal Reasoning A train moves at a constant velocity of sixty miles per hour for four hours Z spec: Query the spec: How far does the train travel?
Z Schemas • Schema: math in a box, with a name attached • Schema calculus builds large schemas from smaller ones. • Model states and operations. • Can be used as declarations, predicates, expressions…
Example - Conjunction Quotient n, d, q, r: N d 0 n = q * d + r Remainder r, d: N r < d Division = Quotient Remainder Division n, d, q, r: N d 0 n = q * d + r r < d
Example - Disjunction Division n, d, q, r: N d 0 n = q * d + r r < d DivideByZero d, q, r: N report!: TEXT d = 0 q = 0 r = 0 report! = Error T_Division = Division DivideByZero T_Division n, d, q, r: N report!: TEXT (d 0 r < d n = q * d + r ) (d = 0 q = 0 r = 0 report! = Error)
Z Specification Yes Yes No Yes Yes Yes Yes NL: newline Ident: variable/type names, e.g, ch?
Modeling Large Systems Large systems are composed of components collected into subsystems. Complex components can be built up from simpler ones using schema inclusion.
Case Study A: Using Z to define an abstract data type state space (a set of variables and their constrains), initial state, operations State space: Initial State: Operation:
Case Study B: Birthday book Model a system which records people's birthdays, and is able to issue a reminder when the day comes round. Step 1 Define given sets and types [NAME] [DATE] Step 2 Define state space • knownis the set of names with birthdays recorded • birthdayis a function which, when applied to certain names, gives the birthdays associated with them
Case Study B: Birthday book Step 3 initial state Step 4 operations Add, find, reminder
Case Study B: Birthday book Add Birthday
Case Study B: Birthday book Find Birthday
Case Study B: Birthday book Remind
Case Study C: Editor Example Natural language (English) description: • User can type in text • User can move cursor forward and backward through the text • User can delete the character in front of the cursor
State schemas Editor left, right: TEXT # ( left right ) maxsize left is before the cursor, right is after
Initialization schemas InitEditor Editor left = right = <> conventional name begins with Init Editor is included left and right come from Editor
Operation schemas printing: P Char Insert Editor ch?: CHAR ch? printing left’ = left <ch?> right’ = right global declaration of printing characters “” means that the state will change “?” means input (“!” means output) precondition left’ is the value after the operation
Schema Inclusion InitEditor Editor left = right = <> InitEditor left, right: TEXT # ( left right ) maxsize left = right = <> Editor left, right: TEXT # ( left right ) maxsize
Homework 3: OCL If an order item is linked to a StockItemDescobject then the associated order is linked to the warehouse linked to the StockItemDescobject. contextOrderItem inv: self.stock->notEmpty() implies self.itemord.itemSources.Stocks→ includesAll(self.stock) includesAll: collection->includesAll(collection) Includes: collection->include(object) self.itemord.itemSources→includesAll(self.stock.Stocks) collection? the same type
Reading OCL Context Booking inv: self.bookedseats.hasSeats→forAll(f |f = self.bookingFor) forAll: collection→ forall (condition must hold for all elements) Step 1: direct translation: the flights where the booked seats belong to should be the flights we aim to book Step 2: meaning: All the booked seats should belong to the booked flights Context FlightPlane inv: self.numberOfSeats = self.hasSeats→size() All the seats on the FightPlane are available for booking (ambiguity on the UML)
Implies Context FlightSection inv: (seatClass = Section::first or seatClass = Section::business or seatClass= Section::premier) impliesisLocatedIn collect (f.booking.passenger.status) forAll(s | s = PassengerClass:: executive or s = PassengerClass:: premier) reading implies: if a is true then b is true Truth table for implies
implies and if-then-else DVD orders: Give an OCL invariant that specifies that the sum attribute will be zero if no DVDs are ordered context Order inv: if self.OrderedVideos→size()=0 thenself.sum = 0 incorrect! self.OrderedVideos→size()=0 impliesself.sum= 0 if <boolexpr> then < bool expr1> // the result of the if-then-else expression is either bool expr1 else <bool expr2> // or boolexpr2, thus else cannot be omitted endif
Operations on Collections Specifies that the sum attribute really describes the price of the DVDs ordered context order inv: self.sum = self.OrderedVideos→ collect(price)→ sum() Specifies that different instances of DVD have different Id context DVD inv: DVD.allInstances() isUnique(Id) context DVD inv: DVD.allInstances() forAll(b1, b2 | b1 <> b2 implies b1.Id <> b2.Id) context DVD inv: DVD.allInstances() forAll(b | b <> self implies b.Id <> self.Id)
forAll, exists Syntax: forAll, exists collection->forAll(elem : T | expr) collection->forAll(elem | expr) collection->forAll(expr) • The result is a Boolean value • forAll(expr) --- expr should be about attributes or operations • forAll(elem| expr) --- elem is a local variable you specifies, if no type is specified for elem, elem has the same type as the one specified under context
To Do • Homework 4: Z - due 10/18 (Tues) 11:00am • Reading assignment - in class • Alloy (Wed 10/13) – Corey • An Approach to Combine Model-based and Scenario based Software Architecture Analysis (Mon 10/17) – Fernado We are moving to Software Architecture next time!
Wild & Crazy Ideas - How can we learn better in the next half quarter? Thank you for the nice words Discuss some of your comments !