Todays Agenda q Quiz 2 next class q










































- Slides: 42
Today’s Agenda q Quiz 2 next class q Presentation Schedule q Quick Review q Build Models Using OCL Formal Methods in Software Engineering 1
Quick Review q What are the common types of context? For each type of context, what kind of OCL expressions can be written? Formal Methods in Software Engineering 2
Building Models with OCL q Introduction q Completing UML Diagrams q Modeling Tips and Hints q Summary Formal Methods in Software Engineering 3
What Is a Model? Simply put, a model is a high level system description. It consists of a consistent, coherent set of model elements, i. e. , artifacts written in a well-defined modeling language such as UML and OCL. Question: Why do we want to have a model? Formal Methods in Software Engineering 4
View Consistency An UML model consists of several diagrams, each of which presents one view of the model. Multiple views must be consistent. For example, objects in the interaction diagram must be instances of classes present in the class diagram. Formal Methods in Software Engineering 5
UML and OCL (1) Both UML diagrams and OCL expressions are part of a model. They are connected by the context definition. Note that OCL expressions can be written in a textual format or attached to the UML diagrams directly. Formal Methods in Software Engineering 6
Example inv: transactions. card. owner -> size() = 1 Loyalty. Account points: Integer number: Integer pre: i > 0 init: 0 earn (i: Integer) burn (i: Integer) is. Empty(): Boolean 1 transactions account body: points = 0 0. . * Transaction init: Set {} points: Integer date: Date amount: Real program(): Loyalty. Program Formal Methods in Software Engineering 7
Adding Extra Info OCL expressions can add extra info. to UML diagrams in the following situations: q Elements might be under-specified. q Business rules need to be incorporated. q Interfaces need to be precisely defined. q Ambiguity must be removed. Formal Methods in Software Engineering 8
Building Models with OCL q Introduction q Completing UML Diagrams q Modeling Tips and Hints q Summary Formal Methods in Software Engineering 9
Discovering Constraints OCL expressions can be used to specify constraints on a class diagram, but where do they come from? q Uniqueness Constraints q Optional & Dynamic Multiplicities q Inheritance q Association Cycles Formal Methods in Software Engineering 10
Uniqueness Constraints (1) Recall the following uniqueness constraint: context Person inv: Person: : all. Instances() -> is. Unique (soc. Sec. Nr) Question: How to determine if the value of a attribute needs to be unique? Formal Methods in Software Engineering 11
Uniqueness Constraints (2) q In general, we should ask the question: “Should equality imply identity? ” § § If two people have the same name, does it mean they are the same person? If two people have the same social security number, does it mean they are the same person? q One way to identify uniqueness constraints is looking for “sharing” § If an instance can be shared, then it is not unique. Formal Methods in Software Engineering 12
Optional Multiplicity (1) An optional multiplicity indicates that an associated object may or may not exist. The existence of an associated object often depends on the state of the objects involved. In this case, it is necessary to use OCL constraints to describe precisely when the optional association may be empty. Formal Methods in Software Engineering 13
Optional Multiplicity (2) inv: mortgages -> not. Empty() implies houses -> not. Empty () House value : Money Person 1 0. . * houses owner soc. Sec. Nr : Integer salary : Money get. Mortage (sum : Money security : House) security 1 1 borrower Mortgage principal : Money 0. . * monthly. Payment : Money start. Date : Date mortgages end. Date : Date Formal Methods in Software Engineering 0. . * mortgages 14
Dynamic Multiplicity (1) A dynamic multiplicity is one that needs to be determined based on another value in the system. In this case, OCL expressions can be use to precisely specify how to determine such a multiplicity. Formal Methods in Software Engineering 15
Dynamic Multiplicity (2) Airplane Flightnr: Integer available. Seats() : Integer flights 0. . * flights 1 num. Of. Seats: Integer plane 0. . * passengers 0. . * Person name: String Formal Methods in Software Engineering context Flight inv: passengers -> size () <= plane. num. Of. Seats 16
Inheritance (1) Guitar Electric. Guitar 0. . 1 0. . * strings Classic. Guitar Formal Methods in Software Engineering 0. . 1 Guitar. String Metal. String 0. . * strings Plastic. String 17
Inheritance (2) Guitar 0. . 1 Electric. Guitar 0. . * strings Guitar. String Metal. String Classic. Guitar Plastic. String context Classic. Guitar inv: strings -> for. All (s | s. ocl. Is. Type. Of(Plastic. String)) context Electric. Guitar inv: strings -> for. All (s | s. ocl. Is. Type. Of(Metal. String)) Formal Methods in Software Engineering 18
Association Cycles (1) An association cycle consists of a set of classes and associations such that one can start from one class, navigate through the associations, and return to the same class. Cycles are often the source of ambiguity. The general rule is that cycles should be checked carefully, especially when the multiplicities are higher than one. Formal Methods in Software Engineering 19
Association Cycles (2) House value : Money Person 0. . * 1 soc. Sec. Nr : Integer owner salary : Money houses get. Mortage (sum : Money security : House) security 1 1 borrower Mortgage principal : Money 0. . * monthly. Payment : Money mortgages start. Date : Date end. Date : Date 0. . * mortgages The model allows a person to have a mortgage that takes as security a house owned by another person! Formal Methods in Software Engineering 20
Association Cycles (3) From Drs. Dwyer and Hatcliff Formal Methods in Software Engineering 21
Association Cycles (4) The cycle allows a graduate student to teach and enroll a class at the same time. Therefore, it is desirable to impose the following invariants: context Instructor inv No. Teaching. While. Enrolled: self. person. ocl. Is. Kind. Of(Graduate) implies teaching->intersection( self. person. taking)-> is. Empty () context Course inv No. Teaching. While. Enrolled: enrolled->intersection(taught. By. person -> select (is. Ocl. Kind. Of (Graduate))) -> is. Empty () Formal Methods in Software Engineering 22
ocl. Is. Type. Of vs ocl. Is. Kind. Of The ocl. Is. Type. Of operation returns true only if the type of the object is identical to the argument. The ocl. Is. Kind. Of operation returns true if the type of object is identical to the argument, or identical to any of the subtypes of the argument. context Person inv: self. ocl. Is. Kind. Of (Person) = ? inv: self. ocl. Is. Type. Of(Student) = ? inv: self. ocl. Is. Kind. Of(Student) = ? Formal Methods in Software Engineering context Student inv: self. ocl. Is. Kind. Of (Person) = ? inv: self. ocl. Is. Type. Of(Student) = ? inv: self. ocl. Is. Kind. Of(Student) = ? inv: self. ocl. Is. Type. Of(Faculty) = ? inv: self. ocl. Is. Kind. Of(Faculty) = ? 23
Association Cycles (5) From Drs. Dwyer and Hatcliff Formal Methods in Software Engineering 24
Association Cycles (6) The waiting list of a course should be empty unless the enrollment is full. context Course inv No. Waiting. Unless. Full: wait. List->not. Empty() implies enrolled->size = 25 Formal Methods in Software Engineering 25
Association Cycles (7) -dept 1 -prereq -required Department -Name -Building -Head -College * * 1. . * -courses * -requiredfor -teaching Course -Title -Number -Time -Room 0. . * Instructor 1 -transcript 0. . 1 1. . * -major -taught by * 0. . * -waiting for 0. . * -waitlist -taking -enrolled 1 3. . 5 ID -id -as instructor -person 1. . * Person -Name 5. . 25 1 1 * Faculty Student -majors -faculty -GPA -Probation 1. . * -advisor Undergraduate 1 3. . 5 -advisee Graduate 0. . * * -grad commitees From Drs. Dwyer and Hatcliff Formal Methods in Software Engineering 26 -commitee
Association Cycles (8) Each student should take at least one major course: context Student inv One. Course. From. Major: taking -> intersection (major. courses) -> not. Empty () Formal Methods in Software Engineering 27
Association Cycles (9) From Drs. Dwyer and Hatcliff Formal Methods in Software Engineering 28
Association Cycles (10) An advisor must be on the committees of his advisees: context Faculty inv Advisor. On. Committee: grad. Committees -> includes. All (advisees) Formal Methods in Software Engineering 29
Design by Contract A contract is a precise specification of mutual obligations and benefits between the client and the supplier of a service. Design by contract is a method that emphasizes the precise description of interface semantics. An object is responsible for executing a service if and only if certain conditions are fulfilled. Formal Methods in Software Engineering 30
An Example Contract Client (Traveler) • Obligation • check in 10 minutes before boarding • < 3 small carry-ons • buy ticket • Benefit • reach Boston Formal Methods in Software Engineering Supplier (Airline) • Obligation • take traveler to Boston • Benefit • don’t need to wait for late travelers • don’t need to store arbitrary amounts of luggage 31
Why Contract? q Facilitates design § establishes the responsibilities of each object clearly and precisely. q Simplifies implementation § allows certain assumptions to be made. q Helps debugging § clearly identifies the responsible party in case of a failure q Enables automatic reasoning § tools can be developed to automatic checking the conformance of a contract Formal Methods in Software Engineering 32
Contract in OCL In OCL, a contract is specified using pre-/postconditions. Flight takeoff: Time ticketed : Set (Person) destination: Destination take. Flight (Person p, Luggage l) pre: System: : current. Time () < takeoff - 10 pre: l. number < 3 pre: ticketed -> includes (p) post: result = destination Formal Methods in Software Engineering 33
Building Models with OCL q Introduction q Completing UML Diagrams q Modeling Tips and Hints q Summary Formal Methods in Software Engineering 34
Navigation q Avoid complex navigation expressions § makes them easier to read and understand § is consistent with the encapsulation principle context Membership inv: programs. partners. delivered. Services -> for. All (points. Earned = 0) implies account -> is. Empty () context Loyalty. Program def: is. Saving : Boolean = partners. delivered. Services -> for. All (points. Earned = 0) context Membership inv: programs. is. Saving implies account -> is. Empty () Formal Methods in Software Engineering 35
Choose Context Wisely (1) q If the invariant restricts the value of an attribute of one class, the class containing the attribute is a clear candidate. q If the invariant restricts the values of attributes of more than one class, the class containing any of the attributes are candidates. q If a class can be appointed the responsibility for maintaining the constraint, the class should be the context. q Any invariant should navigate through the smallest possible number of associations. Formal Methods in Software Engineering 36
Choose Context Wisely (2) 0. . 1 wife Person 0. . * employees 0. . * employers Company 0. . 1 husband Consider how to write the constraint that two persons who are married to each other are not allowed to work at the same company. context Company inv: employees. wife -> intersection(employees) -> is. Empty() Formal Methods in Software Engineering 37
Split and Constraints q A large invariant can often be split into smaller ones at the and operations § § § easier to read and write localizes the problem in case of a broken invariant easier to maintain context Loyalty. Program inv: participants -> for. All (age() > 0) and partners. delivered. Services -> for. All (points. Earned = 0) context Loyalty. Program inv: participants -> for. All (age() > 0) inv: partners. delivered. Services -> for. All (points. Earned = 0) Formal Methods in Software Engineering 38
Use the collect shorthand q Use the collect shorthand whenever possible § the shorthand is easy to read and understand context Person inv: parents -> collect (brothers) -> collect (children) -> not. Empty () context Person inv: parents. brothers. children -> not. Empty () Formal Methods in Software Engineering 39
Always Name Association Ends q Naming association ends is a good practice § indicates the purpose of the associated object § suggests the attribute name to maintain the association Formal Methods in Software Engineering 40
Building Models with OCL q Introduction q Completing UML Diagrams q Modeling Tips and Hints q Summary Formal Methods in Software Engineering 41
Summary q A model allows one to focus on high level design decisions, rather than low level details. q One way to identify constraints is to look for optional/dynamic multiplicities and cycles. q OCL expressions can also be used to simplify a class diagram. q Design by contract allows the interface of an object to be precisely specified. q The rule of thumb for writing a good OCL spec is to keep the expressions as simple as possible. Formal Methods in Software Engineering 42










































