Professional Documents
Culture Documents
Part 1: Model Management Part 2: Extension Mechanisms and Profiles Part 3: Object Constraint Language (OCL)
Jos Warmer, Klasse Objecten j.warmer@klasse.nl
82
Overview
What are constraints Core OCL Concepts Advanced OCL Concepts Wrap up
83
84
Thats why !!
flights 0..*
1
Flight
Airplane
PassengerFlight 0..*
CargoFlight
0..* 1
PasssengerPlane 1
CargoPlane
85
0..* flights
context Flight inv: type = #cargo implies airplane.type = #cargo inv: type = #passenger implies airplane.type = #passenger
86
Definition of constraint
A constraint is a restriction on one or more
87
a constraint that must always be met by all instances of the class a constraint that must always be true BEFORE the execution of the operation a constraint that must always be true AFTER the execution of the operation
Precondition of an operation
T
Postcondition of an operation
T
88
Constraint stereotypes
UML defines three standard stereotypes for
constraints:
T T T
89
What is OCL?
OCL is
90
model
91
Overview
What are constraints Core OCL Concepts Advanced OCL Concepts Wrap up
92
Example model
Flight
Airport
name: String
origin
departing Flights departTime: Time * /arrivalTime: Time duration : Interval * maxNrPassengers: Integer arriving Flights passengers
* {ordered}
flights
*
destination
airline
Airline
name: String
Passenger
$minAge: Integer age: Integer needsAssistance: Boolean book(f : Flight)
0..1
airline
0..1
CEO
93
context. The context may be denoted within the expression using the keyword self. Who? Me?
94
Notation
Constraints may be denoted within the UML
the expression:
context Flight inv: self.duration < 4
is identical to:
context Flight inv: duration < 4
is identical to:
Flight <<invariant>> duration < 4 duration: Integer
95
used:
basic types: String, Boolean, Integer, Real. T classifiers from the UML model and their features
T
attributes, and class attributes T query operations, and class query operations
T
96
context Airline inv: name.toLower = klm context Passenger inv: age >= ((9.6 - 3.5)* 3.1).floor implies mature = true
97
98
99
starting point.
Role names are used to identify the
navigated association.
100
Example: navigations
Navigations
context Flight inv: origin <> destination inv: origin.name = Amsterdam context Flight inv: airline.name = KLM
101
Association classes
context Person inv: if employer.name = Klasse Objecten then job.type = #trainer else job.type = #programmer endif
Job type : {trainer, programmer} Person
* employee
1 employer
Collections
103
arrivingFlights(from the context Airport) arrivingFlights.duration (from the context Airport) passengers (from the context Flight)
Bag:
T
Sequence:
T
104
Collection operations
OCL has a great number of predefined
Syntax:
collection->operation
105
the values resulting evaluating expr for all elements in the collection
Advanced Modeling with UML 106
airp1
departing flights
arriving flights
Advanced Modeling with UML 107
108
airline1
airline2
airline3
110
airline1
airline2
airline3
true if there is at least one element in the collection for which the expression expr is true.
Advanced Modeling with UML 112
airline1
airline2
airline3
114
element size: number of elements in collection count(elem): number of occurences of elem in collection includes(elem): true if elem is in collection excludes(elem): true if elem is not in collection includesAll(coll): true if all elements of coll are in collection
Advanced Modeling with UML 115
Result in postcondition
Example pre and postcondition
116
closed
117
Local variables
The Let construct defines variables local to one
constraint:
Let var : Type = <expression1> in <expression2>
118
Iterate
The iterate operation for collections is the most
119
Iterate example
Example iterate:
context Airline inv: flights->select(maxNrPassengers > 150)->notEmpty
Is identical to:
context Airline inv: flights->iterate(f : Flight; answer : Set(Flight) = Set{ } | if f.maxNrPassengers > 150 then answer->including(f) else answer endif )->notEmpty
120
Inheritance of constraints
Guiding principle Liskovs Substitution Principle
(LSP):
T
Whenever an instance of a class is expected, one can always substitute an instance of any of its subclasses.
121
Inheritance of constraints
Consequences of LSP for invariants:
An invariant is always inherited by each subclass. T Subclasses may strengthen the invariant.
T
postconditions:
A precondition may be weakened T A postcondition may be strengthened
T
122
Wrap up
What are constraints Core OCL Concepts Advanced OCL Concepts Wrap up
123
Current Developments
Feedback from several OCL implementors
handled in UML-RTF
e.g. the grammar has some loose ends T typical tool-related issues
T
formalization of OCL
Advanced Modeling with UML 124
OCL Tools
Cybernetics T ww.cybernetic.org University of Dresden T www-st.inf.tu-dresden.de/ocl/ Boldsoft T www.boldsoft.com ICON computing T www.iconcomp.com Royal Dutch Navy Others
Advanced Modeling with UML 125