Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
OCL
Object Constraint Language
UML Constraint
Charles André – University of Nice2
OCL is a formal language that provides a way to specify UML constraints
Charles André - University of Nice3
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
Charles André - University of Nice4
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 Nice5
Charles André - University of Nice6
Example 1
“A vehicle owner must be at least 18 years old”:
Charles André - University of Nice7
Class Invariants (1)
“A vehicle owner must be at least 18 years old”:context Vehicle
inv: self.owner.age >= 18
Charles André - University of Nice8
Class Invariants (1)
“A vehicle owner must be at least 18 years old”:context Vehicle
inv: self.owner.age >= 18
Charles André - University of Nice9
Class Invariants (1)
“A vehicle owner must be at least 18 years old”:context Vehicle
inv: self.owner.age >= 18
Charles André - University of Nice10
Class Invariants (1)
“A vehicle owner must be at least 18 years old”:context Vehicle
inv: self.owner.age >= 18
Charles André - University of Nice11
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
Charles André - University of Nice12
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 Nice13
Class Invariants (2)
“Nobody has more than 3 vehicles”:context Person
inv: self.fleet->size() <= 3
Charles André - University of Nice14
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 Nice15
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)
Charles André - University of Nice16
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 Nice17
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) )
Charles André - University of Nice18
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 Nice19
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
Charles André - University of Nice20
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
“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 Nice21
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
“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)
Charles André - University of Nice22
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 Nice23
Pre-/Postconditions (2)
“Calling birthday() increments the age of a person by 1.”context Person::birthDay( )
post: self.age = self.age@pre + 1
Charles André - University of Nice24
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 Nice25
Query
“Calling getName() delivers the value of the attribute name.”
context Personbody: self.getName( ) = name
Charles André - University of Nice26
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 Nice27
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.