1 / 40

OCL Homework (due 9/24 5:05pm)

OCL Homework (due 9/24 5:05pm). Homework: Construct OCL constraints. A vehicle owner must be at least 18 years old A car owner must be at least 18 years old Nobody has more than 3 vehicles All cars of a person are black Nobody has more than 3 black vehicles

loc
Download Presentation

OCL Homework (due 9/24 5:05pm)

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. OCL Homework (due 9/24 5:05pm)

  2. Homework: Construct OCL constraints • A vehicle owner must be at least 18 years old • A car owner must be at least 18 years old • Nobody has more than 3 vehicles • All cars of a person are black • Nobody has more than 3 black vehicles • If setAge(. . . ) is called with a non-negative argument then the argument becomes the new value of the attribute age • Calling birthday() increments the age of a person by 1 • Calling getName() delivers the value of the attribute name

  3. From Previous Class • Special Symbols: • selfrefers to an object of the class being constrained self.numberOfEmployees • In most cases, self can be left out, because the context is clear, as in the above examples. • resultis used in a post-condition inside an operation specification, to indicate the object returned

  4. Context-derive Customer • Derive attributes and association context CustomerCard::printedName deriveowner.title.concat(‘ ’).concat (owner.name) e.g. Mr Johnson name: String title: String owner 1 cards 0..* Customer Card validFrom: Date goodThru: Date /printedName: String

  5. Loyalty Account Context - def • Add new attributes and operations for UML diagrams context LoyaltyAccount def: turnover: Real = transactions.amount->sum() Name of the new attributes (belong LoyaltyAcount) Derivation rule, type of return values Sum the value of the amount attribute in all transactions associated with Loyalty Account 1 account transactions 0..* Transaction amount: Real

  6. OCL and UML inv… Loyalty Account points: Integer number: Integer earn(i: Integer) burn(i: Integer) pre: i > 0 account 1 transactions 0..* init: Set { } Transaction

  7. Packaging Expressions • OCL expressions may be included in a package package Package::SubPackage context XYZ inv: …some invariant … context XYZ::operationName(..) pre: … some precondition … endpackage • It has the same meaning as the following two expressions contextPackage::SubPackage::XYZ inv: …some invariant … contextPackage::SubPackage::XYZ::operationName(..) pre: … some precondition …

  8. Comments /* the following invariant states that …*/ Context … Inv … -- hello -- world

  9. Basic Types and Operators - Boolean Type Operation Notation Result Type or a or b Boolean and a and b Boolean exclusive or a xor b Boolean negation not a Boolean equals a = b Boolean not equals a <> b Boolean implies a implies b Boolean

  10. Boolean type - examples context Service invself.pointsEarned > 0 implies not (self.pointsBurned = 0 ) age() > 21 and age() < 65 age() <= 12 xor cards->size() > 3 title = ‘Mr.’ or title = ‘Ms.’ name = ‘Foo’ if standard = ‘UML’ then ‘Using UML standard’ else ‘non UML feature used’ endif

  11. Integer and Real Types – standard operations Operation Notation Result Type equals a = b Boolean not equals a <> b Boolean less a < b Boolean more a > b Boolean less or equal a <= b Boolean more or equal a >= b Boolean plus a + b Integer or Real minus a - b Integer or Real multiplication a * b Integer or Real division a / b Real modulus a.mod(b) Integer integer division a.div(b) Integer absolute value a.abs() Integer or Real maximum of a and b a.max(b) Integer or Real minimum of a and b a.min(b) Integer or Real round a.round() Integer floor a.floor() Integer

  12. String type – standard operations Operation Notation Result Type concatenation string.concat(string) String size string.size() Integer to lower case string.toLower() String to upper case string.toUpper() String substring string.substring(int,int) String equals string1 = string2 Boolean not equals string1 <> string2 Boolean

  13. <<enumeration>> Ftype cargo passenger Enumerated Type 1 Flight * Airplane flights ftype : Ftype atype : Ftype context Flight inv ftype = Ftype::cargo implies airplane.atype = Ftype::cargo

  14. Precedence Rules Name Syntax highest Pathname :: Time expression @pre The dot, arrow, and message operations ., ,^, ^^ Unary operations -, not Multiplication and division *, / Addition and substration +, - Relational operations <,>,<=,>=,<>,= Logical operations and, or, xor Logical implies implies lowest

  15. Example: Precedence Rules context Airline inv: name.toLower = ‘klm’ context Passenger inv: age >= ((9.6 - 3.5)* 3.1).floor implies mature = true

  16. OCL Collection Types

  17. Flight 0..* 1 Airplane flights type : enum of cargo, passenger type : enum of cargo, passenger Significance of Collections in OCL • Most expressions return collections rather than single elements

  18. Types of Collection • Set: • Non-ordered, unique • Example: Set{1, 2, 5, 88} • OrderedSet: • Ordered, unique • Example: OrderedSet {‘apple’, ‘grape’, ‘orange’} • Bag: • Non-ordered, non-unique • Example: Bag {1, 2, 5, 88, 5} • Sequence: • Ordered, non-unique • Example: Sequence {2, 3, 4, 5} Sequence {2..5}

  19. Types of Collection

  20. Collection Operations • Syntax: • collection collectionOperation • operation.size() is not the same as operationsize() context Membership inv: account.isEmpty() context Membership inv: account  isEmpty() Use of the “” (arrow) operator instead of the “.” (dot) operator

  21. Dot (navigation) vs. arrow (collection) • Any OCL expression can navigate through the model by following the ‘path’ of the association • The context of the expression is the starting point. • Role names are used to identify the navigated association.

  22. Flight departing Flights origin Airport departTime: Time /arrivalTime: Time duration : Interval maxNrPassengers: Integer * name: String * arriving Flights desti- nation Example: navigations context Flight inv: origin <> destination inv: origin.name = ‘Amsterdam’ context Airport inv: arrivingFlights.maxNrPassengers < 1000

  23. Standard Operations on collection types • isEmpty(): true if collection has no elements • notEmpty(): true if collection has at least one element • size(): number of elements in collection • count(elem): number of occurrences of elem in collection • includes(elem): true if elem is in collection • excludes(elem): true if elem is not in collection • includesAll(coll): true if all elements of coll are in collection

  24. The equals and notEquals Operation • The equals operation results in true if all elements in the two collections are the same. • For sets, all elements in first set must be equal to second set, and vice versa • For ordered sets, order must also be the same • For bags, all elements and the number of times they appear must be same • For sequence, order of elements must be equal, in addition to rules applicable for bags • notEquals is the opposite

  25. The including and excluding operations • The including operation results in a new collection with one element added to the original collection. • For sets and ordered sets, element is added only if it’s not already present in the collection. • For sequence or an ordered set, the element is added after all elements in the original collection. • The excluding operation results in a new collection with an element removed from the original collection. • For sets and ordered sets, it removes only one element • For bag or sequence, all occurrences of the given element are removed.

  26. Other collection operations • asSet, asSequence, asBag, and asOrderedSet Operations • Instances of all four concrete collection types can be transformed into instances of another concrete collection type • The union operation combines two collections into a new one • The intersection operation results in another collection containing the elements in both collections • minus operation (denoted by -) results in a new set containing all the elements in the set on which the operation is called, but not in the parameter set. • Only applicable on sets and ordered sets • The symmetricDifference operation results in a set containing all elements in the set on which the operation is called or in the parameter set, but not both. • Defined on sets only

  27. Loop Operators • collect(expr) – returns the collections of objects that result from evaluating expr for each element in the source collection • select(expr) – returns a subcollection of the source collection containing all elements for which expr is true • reject(expr) – returns a subcollection of the source collection containing all elements for which expr is false • Exists(expr) – returns true if there is at least one element in the source collection for which expr is true • forAll(expr) – returns true if expr is true for all elements in the source collection • iterate(…) – iterates over all elements in the source collection • collectNested(expr) – returns a collection of collections that result from evaluating expr for each element in the source collection • one(expr) – returns true if there is exactly one element in the source collection for which expr is true • Any(expr) – returns a random element of the source collection for which the expression expr is true • sortedBy(expr) – returns a collection containing all elements of the source collection ordered by expr

  28. Operations on OrderedSets and Sequences only • first() and last() • at(index) • indexOf(object) • insertAt(index, object) • subSequence(lower, upper) • subOrderedSet(lower, upper) • append(object) and prepend(object)

  29. Classification of Operations • Note 1: In this example: • - c is an Sequence of Account objects. • x is an object instance of Account. • Note 2: Collections are immutable. Thus, c->append(), in our example, would create a new Sequence.

  30. Time $midnight: Time month : String day : Integer year : Integer hour : Integer minute : Integer difference(t:Time):Interval before(t: Time): Boolean plus(d : Interval) : Time Interval nrOfDays : Integer nrOfHours : Integer nrOfMinutes : Integer equals(i:Interval):Boolean $Interval(d,h,m : Integer) : Interval An Example Flight departing Flights origin Airport departTime: Time /arrivalTime: Time duration : Interval maxNrPassengers: Integer * 1 flights name: String 1 * airline * 1 arriving Flights desti- nation Airline 1 passengers name: String {ordered} * Passenger $minAge: Integer age: Integer needsAssistance: Boolean airline 0..1 0..1 CEO book(f : Flight)

  31. airline1 f1 airp1 f2 airline2 f3 airp2 airline3 f4 f5 departing flights arriving flights Example: collect operation All arriving flights must be associated with an airline context Airport inv: self.arrivingFlights -> collect(airLine) ->notEmpty

  32. The select operation • Syntax: collection  select(elem : T | expression) collection select(elem | expression) collection select(expression) • The select operation results in the subset of all elements for which expression is true

  33. airline1 f4 duration = 5 f1 duration = 2 f2 duration = 5 f5 duration = 2 f3 duration = 3 airp1 airline2 airp2 airline3 Example: select operation There must be at least one departing flight whose duration is less than 4 context Airport inv: self.departingFlightsselect(duration<4)notEmpty departing flights arriving flights

  34. The forAll operation • Syntax: • collection->forAll(elem : T | expr) • collection->forAll(elem | expr) • collection->forAll(expr) • The forAll operation results in true if expr is true for all elements of the collection

  35. airline1 f4 depart = 9 f1 depart = 7 f2 depart = 5 f5 depart = 8 f3 depart = 8 airp1 airline2 airp2 airline3 departing flights arriving flights Example: forAll operation All departing flights must leave after 6 context Airport inv: self.departingFlights->forAll(departTime.hour>6)

  36. The exists operation • Syntax: collection->exists(elem : T | expr) collection->exists(elem | expr) collection->exists(expr) • The exists operation results in true if there is at least one element in the collection for which the expression expr is true.

  37. airline1 f4 depart = 9 f1 depart = 7 f2 depart = 5 f5 depart = 8 f3 depart = 8 airp1 airline2 airp2 airline3 departing flights arriving flights Example: exists operation context Airport inv: self.departingFlights->exists(departTime.hour<6)

  38. Iterate • The iterate operation for collections is the most generic and complex building block. Collectioniterate(elem : Type; answer : Type = <value> | <expression-with-elem-and-answer>)

  39. Iterate example • Example iterate: context Airline inv: Flights  select(maxNrPassengers > 150) notEmpty • Is identical to: context Airline inv: Flightsiterate (f : Flight; answer : Set(Flight) = Set{ } | if f.maxNrPassengers > 150 then answer  including(f) else answer endif ) notEmpty

  40. Postcondition Example • @pre indicates the value of an attribute or association at the start of the execution of the operation • context Loyalty::enroll(c : Customer) pre : not (participants  includes (c)) post : participants = participants@pre  including(c) • Another post condition could be: membership for the new customer has loyalty account with zero points and no transactions post: Membership  select (m: Membership | m.participants = c)  forAll( account  notEmpty() and account.points = 0 and account.transactions isEmpty() )

More Related