cours2 compositestructure ocl - unice.frmap/cours/master_tsm/cours2_compositestru… · though the...
TRANSCRIPT
![Page 1: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/1.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Composite Structures
Marie-Agnès Peraldi-Frati UNSA/I3S/INRIA
Charles André - University of Nice-Sophia Antipolis2
UML 2 Composition Model• Purpose: improve the “black diamond” composition• Supports connections between parts at the same level of
decomposition, in addition to the usual part-whole associations.
• Complex networks of entities can be represented within a single class, inherited to subclasses, with links maintained between objects playing parts at runtime.
• StructuredClassifiers• See:
– UML 2.0 Superstructure– C. Bock, “UML 2 Composition model”, JOT, vol 3, n° 10, 2004,
pp 47-73
![Page 2: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/2.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice-Sophia Antipolis3
UML 1.x Composition
• UML 1.x composition is familiar to users as the black diamond notation on associations (called composite aggregation, or informally “strong composition”).
• The black diamonds mean the same instance of ENGINE cannot be used simultaneously in an instance of CAR and an instance of BOAT, even though the class ENGINE is shared between the classes CAR and BOAT.
• The black diamond also means that an instance of ENGINE is destroyedwhen its containing instance of CAR or BOAT is destroyed.
• Note: Classes in the physical examples only describe information records of real or imagined physical objects.
Charles André - University of Nice-Sophia Antipolis4
Associations at the same level of decomposition
• While the UML 1.x model is fine for hierarchical decomposition, as in the figure in the previous slide, it has significant limitations when connecting elements at the same level of decomposition.
• The associations in the figure below are defined globally for all engines and boats, not in the context of individual cars and boats. This leads to erroneous interpretations.
![Page 3: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/3.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice-Sophia Antipolis5
An incorrect instance model
The E1 instance of ENGINE is contained by MYCAR, butpowers wheels contained by YOURCAR.
The engine E2 is powering the correct propeller P1, but also powers wheels RIGHTFRONT1 and RIGHTBACK1.
The right twowheels of YOURCAR receive power instead of the front two.
Charles André - University of Nice-Sophia Antipolis6
Using generalized classesThe REDEFINES propertyon association ends indicates that the association is restricted to the class at that end, andrenamed for that purpose.
Generalized class
Generalized association This diagram is
borrowed from C. Bock’s paper.
But it is not fully correct.
See a better solution later.
![Page 4: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/4.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice-Sophia Antipolis7
And yet, still misinterpretationsHowever, even the above cumbersome class model do not prevent anengine in one car from powering wheels in another. This requires each individual car to have classes for its particular wheels and engine, so the association can be further specialized in each car. This is obviouslyimpractical.
Charles André - University of Nice-Sophia Antipolis8
UML 2 Composition (1)
Car
e: Engine 1 front: Wheel 2
back: Wheel2
: powers
Class=context
Usage of other classes
Nb of instancesthat will be used
Class being usedHow it is used
Usage of association
Association being used
How it is used (optional)
Name of the usage
Part
Boat
e: Engine 1 P: Propellor 1..4: powers
connector
New diagram specifically for composite structure
![Page 5: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/5.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Connector• Connectors represent communication links
between instances of classes participating in an internal structure.
• They can be runtime instances of associations, or they can represent dynamic communication set up at runtime.
• Note that while associations between classes represent a link between any instances of those classes, a connector represents a link between only the two instances specified at each end of the connector (Definition vs. Usage)
Charles André - UNSA9
Connector end types
• UML specifies several rules for determinating the types of the elements at each end of a connector:– If a connector represents an instance of an association, the
types of the instances at either end of the connector must be the same types at either end of the association.
– If an instance at one end of a connector has required interfaces, the instance at the other end of the connector must provide one of those interfaces.
– If an instance at one end of a connector has required interfaces and a port is connected to the other end of the connector, the port must provide a required interface.
Charles André - UNSA10
![Page 6: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/6.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Ports (1)• A port is a way to offer functionality from a composite structure
without exposing the internal details of how the functionality is realized.
• Ports are associated with required/provided interfaces. Required interfaces show what the owning classifier may ask of its environment through a given port. Provided interfaces show what functionality a classifier exposes top the environment.
Charles André - UNSA11
Provided interface(type of the port)
Required interface
p is a port on the Engine class
Ports (2)
Charles André - UNSA12
• Behavioral port: the classifier owning the port provides the implementation of the functionality.
• Service port: the functionality is realized by internal elements.
![Page 7: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/7.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice-Sophia Antipolis13
Simple Example of Ports
B.P. Douglass “UML 2.0: Incremental Improvements for Scalability and Architecture”,I-Logix 2003.
Charles André - University of Nice-Sophia Antipolis14
More elaborate example of Ports and Interfaces
![Page 8: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/8.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice-Sophia Antipolis15
UML 2 Composition (2)An alternate representation:
The SUBSETS property on association ends isanother association specialization capability in UML 2. It indicates the association is specialized and renamed at that end, but does not restrict the inherited association to the class at that end (compare to REDEFINES).
There is still a problem with redefines
Charles André - University of Nice-Sophia Antipolis16
Redefines/Subsets revisited (1)
Propeller
CarEngine
BoatEngine
FrontWheel
transmittersource1 1..*
powers
transmittersource
1 2
powers
Two analogous concepts.Try to factorize!
Generalization
![Page 9: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/9.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice-Sophia Antipolis17
Redefines/Subsets revisited (2)
Propeller
CarEngine
BoatEngine
FrontWheel
transmittersource1 1..*
powers
transmittersource
1 2
powers
PowerSource PowerTransmitter1..*1
source transmitter
powers
Incorrect
Possible instantiation
Charles André - University of Nice-Sophia Antipolis18
Redefines/Subsets revisited (3)
Break the relation
![Page 10: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/10.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice-Sophia Antipolis19
Redefines/Subsets revisited (4)
Propeller
CarEngine
BoatEngine
FrontWheel
btransmitter{subsets transmitter}
bsource{subsets source}
1 1..*
powers
ctransmitter{subsets transmitter}
csource{subsets source}
1 2
powers
PowerSource PowerTransmitter1..*1
/source{union}
/transmitter{union}
/powers
Derived union
Charles André - University of Nice-Sophia Antipolis20
Redefines/Subsets revisited (5)Vehicle
PowerSource PowerTransmitter
Engine Propeller Wheel
1
1
/inVehicle{union}
/powerSource{union}
/powerTransmitter{union}
/inVehicle{union}
1
1..*
1..*1
/source{union}
/transmitter{union}
CarEngine
BoatEngine BackWheel
FrontWheel
btransmitter{subsets transmitter}
1
bsource{subsets source}
1..*
powers
21
csource{subsets source}
ctransmitter{subsets transmitter}
powers
![Page 11: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/11.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice-Sophia Antipolis21
UML 2 Composition (3)Instance model:
Charles André - University of Nice-Sophia Antipolis22
UML 2 Composition (4)An alternate representation of an instance model:
Class being reused
Instance name
Association end name
![Page 12: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/12.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice-Sophia Antipolis23
UML 2 Composition (5)
• Connectors can have multiplicities different from the association ends (parts) they relate. For example, a more general model of cars that covers both front-wheel drive and rear-wheel drive is shown in the Figure below. It uses a new association end w that navigates to all the wheels in a car. The new end has a multiplicity of 4, but the POWERS connector to it has a multiplicity of 2 on that end. Thismeans for each car, two of the four wheels in the car will be powered.
UML 2 Composition (6)
Charles André - UNSA24
Application
Window Button2
Application does not own the Window
![Page 13: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/13.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice-Sophia Antipolis25
Inheritance of composite structure
Connectors and association ends, including parts, inherit from class to subclass as all features do. Subclasses can specialize inherited parts and connectors, and introduce new ones.
Collaborations (1)• One of the primary purpose for composite structures is to document
how a particular piece of functionality is implemented within a system.
• This organization of elements to realize behavior is called a Collaboration.
• A Collaboration = collection of instances wired together using connectors to show the communication flow.
• Within a Collaboration, it is helpful to name the instances involved. Typically named based on its role in the collaboration.
• Representation: a dashed ellipsis with the name of the collaboration written inside.
Charles André - UNSA26
![Page 14: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/14.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Collaborations (2)• Details of a collaboration:
• Collaboration UsesA collaboration use represents one particular use of a collaboration to
explain the relationships between the properties of a classifier. A collaboration use shows how the pattern described by a collaboration is applied in a given context, by binding specific entities from that context to the roles of the collaboration.
Charles André - UNSA27
Charles André - University of Nice-Sophia Antipolis28
Component• [UML-Glossary] Component = A modular part of a system that
encapsulates its contents and whose manifestation is replaceablewithin its environment. A component defines its behavior in terms of provided and required interfaces. As such, a component serves as a type, whose conformance is defined by these provided and required interfaces (encompassing both their static as well as dynamic semantics).
• A Component is a kind of Classifier; it can have methods that realize interfaces, have statecharts and use cases.
• Components are coarse-grained elements that are usually replaced as a whole in the application.
• Component-based Development approaches use the metaphor of construction through assembly rather than construction via invention.
![Page 15: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/15.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice-Sophia Antipolis29
Subsystems
• [UML-Glossary] Subsystem = A unit of hierarchical decomposition for large systems. A subsystem is commonly instantiated indirectly. Definitions of subsystems vary widely among domains and methods, and it is expected that domain and method profiles will specialize this construct. A subsystem may be defined to have specification and realization elements.
• A subsystem is a stereotype of both Package and Classifier.• A subsystem is instantiable.• A subsystem is used to organize the run-time system consisting of
instances.• The real work of a subsystem is implemented by the run-time instances
contains within the subsystem. It offers up the collaborative behavior of the contained elements.
• Subsystems have three aspects: operation, specification, and implementation. The operations are the set of services directly offered by the Subsystem.
OCL
Object Constraint Language
![Page 16: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/16.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
UML Constraint
Charles André – University of Nice31
OCL is a formal language that provides a way to specify UML constraints
Charles André - University of Nice32
OCL
• Part of the UML standard• Formal specification language• Object-oriented language• Query-only language
• Why?because UML is not enough
• Where?http://www.omg.org/docs/ptc/03-10-14.pdf
![Page 17: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/17.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice33
UML is not enough…
Sources: Andreas Roth – Introduction to OCL
Praktikum WS0304: Formale Entwicklung objektorientierter Software
•How many persons can own a car?•How old must a car owner be?•How can we require that a person must at most own one black car?
OCL Basic Types• Built-in types that can be used in expressions:
– Boolean: true/false. Supports logical operators (and, or, xor, not, implies, if-then-else).
– Integer: 10, -56, etc. Supports the operators *, +, -, /, abs().
– Real: 2.23, -273.15, etc. Supports the operators *, +, -, /, floor()
– String: "a string". Supports concat(), size(), substring().
• CastingCasting objects from one type to another (related to
generalization)oclAsType()
Charles André - University of Nice34
![Page 18: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/18.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice35
Example 1
“A vehicle owner must be at least 18 years old”:
Charles André - University of Nice36
Class Invariants (1)
“A vehicle owner must be at least 18 years old”:context Vehicle
inv: self.owner.age >= 18
![Page 19: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/19.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice37
Class Invariants (1)
“A vehicle owner must be at least 18 years old”:context Vehicle
inv: self.owner.age >= 18
Charles André - University of Nice38
Class Invariants (1)
“A vehicle owner must be at least 18 years old”:context Vehicle
inv: self.owner.age >= 18
![Page 20: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/20.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice39
Class Invariants (1)
“A vehicle owner must be at least 18 years old”:context Vehicle
inv: self.owner.age >= 18
Charles André - University of Nice40
Class Invariants (1)
“A vehicle owner must be at least 18 years old”:context Vehicle
inv: self.owner.age >= 18
What does this mean, instead?context Person
inv: self.age >= 18
![Page 21: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/21.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice41
Class Invariants (1)
“A vehicle owner must be at least 18 years old”:context Vehicle
inv: self.owner.age >= 18
“A car owner must be at least 18 years old”:context Car
inv: self.owner.age >= 18
Charles André - University of Nice42
Class Invariants (2)
“Nobody has more than 3 vehicles”:context Person
inv: self.fleet->size() <= 3
![Page 22: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/22.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice43
Class Invariants (2)
“All vehicles of a person are black”:context Person
inv: self.fleet->forAll(v|v.color=Color::black)
Charles André - University of Nice44
Class Invariants (2)
“All vehicles of a person are black”:context Person
inv: self.fleet->forAll(v|v.color=Color::black)
“All cars of a person are black”:context Person
inv: self.fleet->forAll(v|v.oclIsTypeOf(Car) implies v.color=Color::black)
![Page 23: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/23.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice45
Collections (1)• Collection =Set Set{1,6,2} *OrderedSet OrderedSet{1,6,2} *{ordered}Bag Bag{1,6,6,2} *{nonunique}Sequence Sequence{1,6,6,2} *{ordered,nonunique}
OCL 2.0 supports collections of collections.
Explicit flattening if needed
Charles André - University of Nice46
Collections (1)
• select(), reject(): specify a subset of the collection
• collect():Specified a derived collection, which contains
objects of a different type• iterate():
Builds one value by iterating over a collection• forAll
: ( )o c P o∀ ∈
c->forAll( o| P(o) )
![Page 24: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/24.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice47
Class Invariants (3)“Nobody has more than 3 black vehicules”:
context Personinv: self.fleet->select(v|v.color=Color::black)
->size() <=3
Charles André - University of Nice48
Class Invariants (3)“Nobody has more than 3 black vehicules”:
context Personinv: self.fleet->select(v|v.color=Color::black)
->size() <=3
What does it mean?context Person
inv: self.fleet->iterate(v; acc: Integer=0|if v.color=Color::black then
acc+1 else accendif ) <= 3
![Page 25: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/25.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice49
Class Invariants (3)“Nobody has more than 3 black vehicules”:context Person
inv: self.fleet->select(v|v.color=Color::black)->size() <=3
What does it mean?context Person
inv: self.fleet->iterate(v; acc: Integer=0|if v.color=Color::black then
acc+1 else accendif ) <= 3
“A person younger than 18 owns no cars”:context Person
inv: age < 18 implies self.fleet->forAll(v|not v.oclIsKindOf(Car))
Charles André - University of Nice50
Class Invariants (3)“Nobody has more than 3 black vehicules”:context Person
inv: self.fleet->select(v|v.color=Color::black)->size() <=3
What does it mean?context Person
inv: self.fleet->iterate(v; acc: Integer=0|if v.color=Color::black then
acc+1 else accendif ) <= 3
“A person younger than 18 owns no cars”:context Person
inv: age < 18 implies self.fleet->forAll(v|not v.oclIsKindOf(Car))
“There is a red car”:context Car
•inv: Car.AllInstances()->exists(c | c.color=Color::red)
![Page 26: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/26.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice51
Pre-/Postconditions (1)
“If setAge(. . . ) is called with a not negative argument then the argument becomes the new value of the attribute age.”
context Person::setAge(newAge:Integer)pre: newAge >= 0post: self.age = newAge
Charles André - University of Nice52
Pre-/Postconditions (2)
“Calling birthday() increments the age of a person by 1.”context Person::birthDay( )
post: self.age = self.age@pre + 1
![Page 27: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/27.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice53
Pre-/Postconditions (2)
“Calling birthday() increments the age of a person by 1.”context Person::birthDay( )
post: self.age = self.age@pre + 1
“Calling getName() delivers the value of the attribute name.”context Person::getName( ): String
body: self.name -- or post: result = self.name
Charles André - University of Nice54
Query
“Calling getName() delivers the value of the attribute name.”
context Personbody: self.getName( ) = name
![Page 28: Cours2 CompositeStructure OCL - unice.frmap/Cours/MASTER_TSM/Cours2_CompositeStru… · though the class ENGINE is shared between the classes CAR and BOAT. • The black diamond also](https://reader030.vdocuments.net/reader030/viewer/2022040404/5e9372f726fa2815b0700854/html5/thumbnails/28.jpg)
Master2 TSM Janvier2009
Marie-agnès Peraldi-Frati
Charles André - University of Nice55
OCL Basics
• OCL is used to specify invariants of objects and pre- and post conditions of operations. Makes UML (class) diagrams more precise.
• OCL expressions use vocabulary of UML class diagram.
• OCL attribute accesses “navigate” through UML class diagram.
• “context” specifies about which elements we are talking.
• “self” indicates the current object. “result”the return value.
Charles André - University of Nice56
OCL Basics (cont’d)
• OCL can talk about collections (here: sets).Operations on collections: –>Example operations: select, forAll, iterate
• “iterate” can simulate all other operations on collections.
• Queries (= side effect free operations) can be used in OCL expressions.