inv

OCL
Object Constraint Language
UML Constraint
OCL is a formal language that provides a way to specify UML constraints
2
Charles André – University of Nice
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
3
Charles André - University of Nice
UML is not enough…
•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?
Sources: Andreas Roth – Introduction to OCL
Praktikum WS0304: Formale Entwicklung objektorientierter Software
4
Charles André - University of Nice
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().
• Casting
Casting objects from one type to another (related to
generalization)
oclAsType()
5
Charles André - University of Nice
Example 1
“A vehicle owner must be at least 18 years old”:
6
Charles André - University of Nice
Class Invariants (1)
“A vehicle owner must be at least 18 years old”:
context Vehicle
inv: self.owner.age >= 18
7
Charles André - University of Nice
Class Invariants (1)
“A vehicle owner must be at least 18 years old”:
context Vehicle
inv: self.owner.age >= 18
8
Charles André - University of Nice
Class Invariants (1)
“A vehicle owner must be at least 18 years old”:
context Vehicle
inv: self.owner.age >= 18
9
Charles André - University of Nice
Class Invariants (1)
“A vehicle owner must be at least 18 years old”:
context Vehicle
inv: self.owner.age >= 18
10
Charles André - University of Nice
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
11
Charles André - University of Nice
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
12
Charles André - University of Nice
Class Invariants (2)
“Nobody has more than 3 vehicles”:
context Person
inv: self.fleet->size() <= 3
13
Charles André - University of Nice
Class Invariants (2)
“All vehicles of a person are black”:
context Person
inv: self.fleet->forAll(v|v.color=Color::black)
14
Charles André - University of Nice
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)
15
Charles André - University of Nice
Collections (1)
• Collection =
Set
OrderedSet
Bag
Sequence
Set{1,6,2}
OrderedSet{1,6,2}
Bag{1,6,6,2}
Sequence{1,6,6,2}
*
*{ordered}
*{nonunique}
*{ordered,nonunique}
OCL 2.0 supports
collections of collections.
Explicit flattening if needed
16
Charles André - University of Nice
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) )
17
Charles André - University of Nice
Class Invariants (3)
“Nobody has more than 3 black vehicules”:
context Person
inv: self.fleet->select(v|v.color=Color::black)
->size() <=3
18
Charles André - University of Nice
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 acc
endif ) <= 3
19
Charles André - University of Nice
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 acc
endif ) <= 3
“A person younger than 18 owns no cars”:
context Person
inv: age < 18 implies self.fleet->forAll(v|
not v.oclIsKindOf(Car))
20
Charles André - University of Nice
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 acc
endif ) <= 3
“A person younger than 18 owns no cars”:
context Person
inv: age < 18 implies self.fleet->forAll(v|
not v.oclIsKindOf(Car))
21
“There is a red car”:
context Car
•inv: Car.AllInstances()->exists(c |
c.color=Color::red)
Charles André - University of Nice
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 >= 0
post: self.age = newAge
22
Charles André - University of Nice
Pre-/Postconditions (2)
“Calling birthday() increments the age of a person by 1.”
context Person::birthDay( )
post: self.age = self.age@pre + 1
23
Charles André - University of Nice
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
24
Charles André - University of Nice
Query
“Calling getName() delivers the value of the attribute name.”
context Person
body: self.getName( ) = name
25
Charles André - University of Nice
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.
26
Charles André - University of Nice
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.
27
Charles André - University of Nice