Chapter 9, Object Design: Object Constraint Language

Slides:



Advertisements
Similar presentations
1 Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC Survol de Object Constraint Language & IDM À partir du cours Contracts, Patterns.
Advertisements

Formal Methods of Systems Specification Logical Specification of Hard- and Software Dr. Armin Wolf Fraunhofer Institut für Rechnerarchitektur.
Semantics Static semantics Dynamic semantics attribute grammars
By Karen Richart. The Object Constraint Language (OCL)  Formal specification language that could be used for constraining the model elements that occur.
1 The Object Constraint Language: Expressing Constraints in the UML (Most slides created by Robert B. France, Professor Department of Computer Science,
OCL2 April A presentation of OCL 2 Object Constraint Language Christian Hein, Fraunhofer FOKUS April 2006.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
ISBN Chapter 3 Describing Syntax and Semantics.
CS 355 – Programming Languages
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 8, Object Design: Object Constraint Language.
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 9, Object Design: Object Constraint Language.
Using UML, Patterns, and Java Object-Oriented Software Engineering Art for Chapter 9, Specifying Interfaces.
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 9, Object Design: Specifying Interfaces.
Object Design: Specifying Interfaces Chapter 9. 2 Object Design  Object design is the process of adding details to the requirements analysis and making.
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 8, Object Design: Object Constraint Language.
CSCE 431: Interfaces and Contracts Some material from Bruegge, Dutoit.
Chapter 9: Object Design: Specifying Interfaces
1 Specifying Object Interfaces. 2 Major tasks in this stage: --are there any missing attributes or operations? --how can we reduce coupling, make interface.
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
Describing Syntax and Semantics
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 8, Object Design: Object Constraint Language.
SEG4110 – Advanced Software Engineering and Reengineering TOPIC E Object Constraint Language (OCL)
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 9, Object Design: Specifying Interfaces.
111 Writing Protocols in OCL CS 4311 Jos B. Warmer and Anneke G. Kleppe, OCL: The Constraint Language of the UML, JOOP, May Jos B. Warmer and Anneke.
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages Semantics.
TIVDM1Modelling unordered collections1 Peter Gorm Larsen.
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 9, Object Design: Specifying Interfaces.
1 OCL The Role of OCL in UML. 2 רשימת הנושאים  מבוא  מרכיבי השפה  דוגמאות  מקורות.
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 9, Object Design: Specifying Interfaces.
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 1 Few comments about midterm I graded very lightly.
Chapter 3 Part II Describing Syntax and Semantics.
Object Constraint Language
1 Kyung Hee University Constraints Spring Kyung Hee University Graphical Notations  Graphical notations are well suited for displaying structural.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Chapter 16 UML Class Diagrams.
Formal Methods in Software Engineering1 Today’s Agenda  Quiz 1 Return  Project Discussion  Quick Review  Finish Introduction to OCL.
CEN th Lecture Advance Software Engineering (CEN-5011) Instructor: Masoud Sadjadi Object Design: Specifying.
Interpreting the Object Constraint Presented by: Ed Kausmeyer.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
An association between class Flight and class Person, indicating that a certain group of persons are the passengers on a flight, will have multiplicity.
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 9, Object Design: Specifying Interfaces.
Jan Pettersen Nytun, UIA, page 1. Jan Pettersen Nytun, UIA, page 2 HISTORY COLLECTION TYPES AND QUERING IN OCL FORMAL LANGUAGE - STATEMENT EXAMPLES CONSTRAINTS.
Chapter 8, Object Design: Object Constraint Language
Chapter 9, Object Design: Specifying Interfaces
Used to help understand requirements more completely
Chapter 16 UML Class Diagrams.
Chapter 9, Object Design: Object Constraint Language
The Object Constraint Language
CSCE 606: Interfaces and Contracts
Chapter 9, Object Design: Specifying Interfaces
Syntax Questions 6. Define a left recursive grammar rule.
Chapter 9, Object Design: Specifying Interfaces
Specifying Object Interfaces
Chapter 9, Object Design: Specifying Interfaces
Defining Classes and Methods
Seminar 3 UML Class Diagram.
Slides by Steve Armstrong LeTourneau University Longview, TX
LECTURE 11: Specifying Systems – State Diag’s & OCL
Call Class Class User Realize Class Developer Class Implementor
Object Design Object design is the process of adding details to the requirements analysis and making implementation decisions The object designer must.
Defining Classes and Methods
The Object Constraint Language
Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract, Computer,
Object Oriented System Design Class Diagrams
Object Constraint Language (OCL)
Formal Methods in Software Engineering 1
Generics, Lambdas and Reflection
Presentation transcript:

Chapter 9, Object Design: Object Constraint Language

OCL: Object Constraint Language Formal language for expressing constraints over a set of objects and their attributes Part of the UML standard Used to write constraints that cannot otherwise be expressed in a diagram Declarative No side effects No control flow Based on Sets and Multi Sets

OCL Basic Concepts OCL expressions Return True or False Are evaluated in a specified context, either a class or an operation All constraints apply to all instances

OCL Simple Predicates Example: context Tournament inv: self.getMaxNumPlayers() > 0 In English: “The maximum number of players in any tournament should be a positive number.” Notes: “self” denotes all instances of “Tournament” OCL uses the same dot notation as Java.

OCL Preconditions Example: context Tournament::acceptPlayer(p) pre: not self.isPlayerAccepted(p) In English: “The acceptPlayer(p) operation can only be invoked if player p has not yet been accepted in the tournament.” Notes: The context of a precondition is an operation isPlayerAccepted(p) is an operation defined by the class Tournament

OCL Postconditions Example: context Tournament::acceptPlayer(p) post: self.getNumPlayers() = self@pre.getNumPlayers() + 1 In English: “The number of accepted player in a tournament increases by one after the completion of acceptPlayer()” Notes: self@pre denotes the state of the tournament before the invocation of the operation. Self denotes the state of the tournament after the completion of the operation.

OCL Contract for acceptPlayer() in Tournament context Tournament::acceptPlayer(p) pre: not isPlayerAccepted(p) getNumPlayers() < getMaxNumPlayers() context Tournament::acceptPlayer(p) post: isPlayerAccepted(p) getNumPlayers() = @pre.getNumPlayers() + 1

OCL Contract for removePlayer() in Tournament context Tournament::removePlayer(p) pre: isPlayerAccepted(p) context Tournament::removePlayer(p) post: not isPlayerAccepted(p) getNumPlayers() = @pre.getNumPlayers() - 1

Java Implementation of Tournament class (Contract as a set of JavaDoc comments) public class Tournament { /** The maximum number of players  * is positive at all times.  * @invariant maxNumPlayers > 0  */ private int maxNumPlayers; /** The players List contains    * references to Players who are  * are registered with the  * Tournament. */ private List players; /** Returns the current number of  * players in the tournament. */ public int getNumPlayers() {…} /** Returns the maximum number of  * players in the tournament. */ public int getMaxNumPlayers() {…} /** The acceptPlayer() operation  * assumes that the specified  * player has not been accepted * in the Tournament yet.  * @pre !isPlayerAccepted(p)  * @pre getNumPlayers()<maxNumPlayers * @post isPlayerAccepted(p) * @post getNumPlayers() = * @pre.getNumPlayers() + 1 */ public void acceptPlayer (Player p) {…} /** The removePlayer() operation * assumes that the specified player * is currently in the Tournament. * @pre isPlayerAccepted(p) * @post !isPlayerAccepted(p) * @post getNumPlayers() = * @pre.getNumPlayers() - 1 */ public void removePlayer(Player p) {…} } As we can see, the OCL contract is now written as a set of JavaDoc comments. The specific OCL Javdoc tags @ invariant, @pre and @post could be read to a verification system or a runtime debugging system That inserts specific code as a result of these tags to ensure the validity of the predicates (slows down the computation quite a bit, verification would be better, but currently not state of the art for large complex systems.

Constraints can involve more than one class How do we specify constraints on on a group of classes? The value of this expression is the set of objects on the other side of the role name association. If the multiplicity of the association-end has a maximum of one ("0..1" or "1"), then the value of this expression is an object. If the multiplicity is "0..1" then the result is an empty set if there is no object, otherwise it is the unique object. Navigation expression generate Collections of objects. By default, navigation will result in a Set. When the association on the Class Diagram is adorned with {ordered}, the navigation results in a Sequence. Collections, like Sets, Bags and Sequences, are predefined types in OCL. They have a large number of predefined operations on them. collection->operation(arguments) Starting from a specific class in the UML class diagram, we navigate the associations in the class diagram to refer to the other classes and their properties (attributes and Operations).

Example from ARENA: League, Tournament and Player players * tournaments {ordered} Tournament +start:Date +end:Date +acceptPlayer(p:Player) League +getActivePlayers() Player +name:String +email:String Constraints: A Tournament’s planned duration must be under one week. Players can be accepted in a Tournament only if they are already registered with the corresponding League. The number of active Players in a League are those that have taken part in at least one Tournament of the League. To better understand these constraints, we instantiate the class diagram for a specific group of instances 2 Leagues 5 Players 2 Tournaments

Instance Diagram: 2 Leagues , 5 Players, 2 Tournaments tttExpert:League chessNovice:League alice:Player bob:Player marc:Player joe:Player Xmas:Tournament start=Dec 23 end= Dec 25 zoe:Player winter:Tournament start=Jan 12 end= Jan 14

3 Types of Navigation through a Class Diagram 1. Local attribute 2. Directly related class 3. Indirectly related class Tournament start:Date end:Date League Player * Tournament League * Player Within the context of one class, e.g. tournamen, I can construct a predicate out of the attributes of this class. Given a certain context, say League, I can specify a constraint, that involves methods and attributes of a directly associated class, say Player. Given a certain context, say League, I can specify constraints on classes that are not directly reachable via a neighbor association, but via a couple of associations. Any constraint for an arbitrary UML class diagram can be specified using only a combination of these 3 navigation types!

Specifying the Model Constraints in OCL Local attribute navigation players * tournaments {ordered} Tournament +start:Date +end:Date +acceptPlayer(p:Player) League +getActivePlayers() Player +name:String +email:String context Tournament inv: end - start <= 7 Directly related class navigation context Tournament::acceptPlayer(p) pre: league.players->includes(p)

OCL Sets, Bags and Sequences Sets, Bags and Sequences are predefined in OCL and subtypes of Collection. OCL offers a large number of predefined operations on collections. They are all of the form: collection->operation(arguments)

OCL-Collection The OCL-Type Collection is the generic superclass of a collection of objects of Type T Subclasses of Collection are Set: Set in the mathematical sense. Every element can appear only once Bag: A collection, in which elements can appear more than once (also called multiset) Sequence: A multiset, in which the elements are ordered Example for Collections: Set(Integer): a set of integer numbers Bag(Person): a multiset of persons Sequence(Customer): a sequence of customers The element type T is the type of a primitive value (Integer, String, Boolean) or the type of an object (e.g. Person, Tournament).

OCL-Operations for OCL-Collections (1) size: Integer Number of elements in the collection includes(o:OclAny): Boolean True, if the element o is in the collection count(o:OclAny): Integer Counts how many times an element is contained in the collection isEmpty: Boolean True, if the collection is empty notEmpty: Boolean True, if the collection is not empty The OCL-Type OclAny is the most general OCL-Type

OCL-Operations for OCL-Collections(2) union(c1:Collection) Union with collection c1 intersection(c2:Collection) Intersection with Collection c2 (contains only elements, which appear in the collection as well as in collection c2 auftreten) including(o:OclAny) Collection containing all elements of the Collection and element o select(expr:OclExpression) Subset of all elements of the collection, for which the OCL-expression expr is true Operations to generate new collections (Result is again a collection):

How do we get OCL-Collections? A collection can be generated by explicitly enumerating the elements A collection can be generated by navigating along one or more 1-N associations Navigation along a single 1:n association yields a Set Navigation along a couple of 1:n associations yields a Bag (Multiset) Navigation along a single 1:n association labeled with the constraint {ordered } yields a Sequence Of course, also by calling an OCL collection operation on an OCL collection

Navigation through a 1:n-Association Example: A Customer should not have more than 4 cards context Customer inv: cards->size <= 4 Alternative writing style Customer name: String titel: age: Integer birthday: Date getage(): Customer valid: Boolean validSince: Date expires: color: enum { silver, gold} printedName : customerCard owner cards * cards denotes a set of customercards

Navigation through several 1:n-Associations Example: programPartner nrcustomer = bonusprogram.customers->size customers denotes a multiset of Customer bonusprogram denotes a set of Bonusprogram XXXX? Is this english description correct? The number of customers registered by a programPartner must be equal to the number of customers that belong to a bonusprogram offered by the programPartner Bonusprogram 1..* program Customer register(k: Customer) * name: String titel: String age: Integer .* 1..* birthday: Datum getage(): Integer programPartner nrcustomer: Integer

Navigation through a constrained Association Navigation through an association with the constraint {ordered} yields a sequence. Example: Bonusprogram level->size = 2 Bonusprogram A bonus program should offer two levels. The number of levels in a bonus program should be two. register(k: Customer) level denotes a sequence of levels {ordered} * Level name: String

Conversion between OCL-Collections OCL offers operations to convert OCL-Collections: asSet Transforms a multiset or sequence into a set asBag transforms a set or sequence into a multiset asSequence transforms a set or multiset into a sequence.

Example of a Conversion programPartner nrcustomer = bonusprogram.Customer->size This expression may contain customer multiple times, we can get the number of unique customers as follows: nrcustomer = bonusprogram.Customer->asSet->size Bonusprogram 1..* program Customer register(k: Customer) * name: String titel: String age: Integer .* 1..* birthday: Datum getage(): Integer programPartner nrcustomer: Integer

Specifying the Model Constraints: Using asSet Local attribute navigation context Tournament inv: end - start <= Calendar.WEEK Directly related class navigation context Tournament::acceptPlayer(p) pre: league.players->includes(p) players * tournaments {ordered} Tournament +start:Date +end:Date +acceptPlayer(p:Player) League +getActivePlayers() Player +name:String +email:String Indirectly related class navigation context League::getActivePlayers post: result=tournaments.players->asSet

Evaluating OCL Expressions The value of an OCL expression is an object or a collection of objects. Multiplicity of the association-end is 1 The value of the OCL expression is a single object Multiplicity is 0..1 The result is an empty set if there is no object, otherwise a single object Multiplicity of the association-end is * The result is a collection of objects By default, the navigation result is a Set When the association is {ordered}, the navigation results in a Sequence Multiple “1-Many” associations result in a Bag The value of an OCL expression is an object or a collection of objects. We obtain these objects by navigating through the associations of the class diagram If the multiplicity of the association-end is 1, the value of the OCL expression is a single object. If the multiplicity is 0..1, the result is an empty set if there is no object, otherwise a single object.

Summary Constraints are predicates (often boolean expressions) on UML model elements Contracts are constraints on a class that enable class users, implementors and extenders to share the same assumption about the class (“Design by contract”) OCL is the example of a formal language that allows us to express constraints on UML models Complicated constrains involving more than one class, attribute or operation can be expressed with 3 basic navigation types. The OCL is an interesting concept but it hasn’t been adopted by the industry. It still shows no signs of being adopted any time soon, but in my opinion it worth the effort to learn.