27
OCL Object Constraint Language

Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

OCL

Object Constraint Language

Page 2: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

UML Constraint

Charles André – University of Nice2

OCL is a formal language that provides a way to specify UML constraints

Page 3: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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

Page 4: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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?

Page 5: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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

Page 6: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

Charles André - University of Nice6

Example 1

“A vehicle owner must be at least 18 years old”:

Page 7: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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

Page 8: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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

Page 9: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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

Page 10: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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

Page 11: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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

Page 12: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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

Page 13: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

Charles André - University of Nice13

Class Invariants (2)

“Nobody has more than 3 vehicles”:context Person

inv: self.fleet->size() <= 3

Page 14: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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)

Page 15: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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)

Page 16: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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

Page 17: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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) )

Page 18: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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

Page 19: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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

Page 20: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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))

Page 21: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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)

Page 22: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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

Page 23: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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

Page 24: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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

Page 25: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

Charles André - University of Nice25

Query

“Calling getName() delivers the value of the attribute name.”

context Personbody: self.getName( ) = name

Page 26: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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.

Page 27: Object Constraint Language - Inria4 Charles André - University of Nice UML is not enough… Sources: Andreas Roth – Introduction to OCL Praktikum WS0304: Formale Entwicklung objektorientierter

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.