1 / 43

Lecture 11 Formal Modeling: Z, OCL homework Alloy background

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.

truman
Télécharger la présentation

Lecture 11 Formal Modeling: Z, OCL homework Alloy background

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 11Formal Modeling: Z, OCL homeworkAlloy background

  2. 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

  3. Z Basic Constructs • Declarations introduce variables. • Expressions describe values that variables can assume. • Predicatesplace constraints on the values that variables do assume.

  4. 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

  5. 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:

  6. 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

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

  8. Exercise: Reading Z logic operations and predicate

  9. 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)

  10. Binary Relation and Relation Calculus

  11. Binary Relations and Functions Function: binary relations where each element in the domain appears just once phonef(doug) = 4107

  12. Lambda Expressions Define functions ( declaration | predicate expression) Use functions without writing declarations Compare to set definitions! •

  13. Sequence: Model Array and Lists Sequence: <>

  14. 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

  15. 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?

  16. 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?

  17. 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…

  18. Schema calculus: Conjunction

  19. 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

  20. Schema calculus : Disjunction

  21. 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)

  22. Z Specification Yes Yes No Yes Yes Yes Yes NL: newline Ident: variable/type names, e.g, ch?

  23. Z Specification Process

  24. Modeling Large Systems Large systems are composed of components collected into subsystems. Complex components can be built up from simpler ones using schema inclusion.

  25. 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:

  26. 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

  27. Case Study B: Birthday book Step 3 initial state Step 4 operations Add, find, reminder

  28. Case Study B: Birthday book Add Birthday

  29. Case Study B: Birthday book Find Birthday

  30. Case Study B: Birthday book Remind

  31. 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

  32. State schemas Editor left, right: TEXT # ( left  right ) maxsize left is before the cursor, right is after

  33. Initialization schemas InitEditor Editor left = right = <> conventional name begins with Init Editor is included left and right come from Editor

  34. 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

  35. Schema Inclusion InitEditor Editor left = right = <> InitEditor left, right: TEXT # ( left  right )  maxsize left = right = <> Editor left, right: TEXT # ( left  right )  maxsize

  36. 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

  37. 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)

  38. 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

  39. 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

  40. 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)

  41. 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

  42. 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!

  43. Wild & Crazy Ideas - How can we learn better in the next half quarter? Thank you for the nice words  Discuss some of your comments !

More Related