Title: Object Constraint Language OCL
1Object Constraint Language (OCL)
1
The case for formality
The following diagram depicts the stages in
software development and highlight the cost
delay involved in correcting errors made at
different stages.
year(s)
project brief
commissioning
months
Requirements specification
acceptance testing
weeks
System specification
integration testing
days
move to formality
program design
unit testing
structured methods formal methods
code
Moving to a formal representation early allows
mathematical proof of correctness of development
processes. Diagrammatic notations need more
detailed specification.
2Object Constraint Language (OCL)
2
Overview of OCL
Origins The Object Constraint Language was
designed to be used alongside UML supplement the
design specification. Originally developed by
Object Designers for the Syntropy method, it has
been further developed by IBM. It has a strong
similarity to VDM in that it provides an object
based modelling language. Usage To provide a
formal specification of a system design. To
express constraints formally. To express the
semantics of methods formally through pre post
conditions.
3Object Constraint Language (OCL)
3
Overview of OCL
Components of OCL OCL provides a rich set of
data types to express system specifications
and these form an inheritance heirarchy as
follows -
OclAny
scalar
collection
real
string
boolean
sequence
bag
set
integer
The OclAny type provides some basic operations
for all objects and each data type adds its own.
4Object Constraint Language (OCL)
4
Example System - Lending Library
Library
acquisition-num
stock_item
borrower
Book author title
onLoan
0,1
Example constraint Member self.loan-gtsize lt 10
5Object Constraint Language (OCL)
5
Overview of OCL
OCL expressions OCL is used to define
invariants (constraints) on the state of classes
and to express the semantics of methods
through pre and post conditions.
Objects of the class containing the expression
are referred to as self, and related
objects in the class diagram are referenced
through navigation expressions which make
extensive use of role names. When role
names are missing, the class name referred to
(with lower case first letter can
sometimes be used). When navigating
through qualified associations, the key is
attached to the expression in square
brackets, e.g. - stock-itemacquisition-num.aut
hor When referring to a property in
another package, the property name is
prefixed with the package name -
packageproperty
6Object Constraint Language (OCL)
6
OclType
All types in OCL are instances of the type
OclType. This is therefore the meta-type and
provides access to type characteristics
- Operation Returned value name name of
the type as a string e.,g. object.name
attributes the set of attribute names as
strings associationEnds the set of
associationEnds as strings operations the set
of operation names as strings supertypes the
set of all the direct supertypes as
OclTypes allSupertypes the set of all
supertypes as OclTypes allInstances the set of
all instances of the type
7Object Constraint Language (OCL)
7
OclAny Type
This is the super-class of all OCL types and
provides some fundamental operations which are
inherited by all sub-classes. Operation Meanin
g true if objects are the same
object ltgt true if objects are not the
same oclType the type of the
object oclIsKindOf(type) true if object is a
sub-class of type oclIsTypeOf(type) true if
object is of type oclAsType(type) cast to
type - downcasting disallowed
8Object Constraint Language (OCL)
8
Scalar types
Type Values
Operations defined Boolean true, false
and, or, xor, not, implies,
if-then-else e.g. Boolean a, b if a
or b then ltocl expressiongt
else ltocl
expressiongt Real 1.5, -23.6
, /, , -, floor, abs, max, min, lt, lt, gt,
gt e.g. Real x, y
a x lt y Integer
-5, 4, 0 (as real), abs, div, mod,
-floor, String hello
toUpper, toLower, size, substring(lo,up),
concat(string) e.g.
String s, t
if s.size t.substring(1,5).size then Note -
There is no assignment operation
9Object Constraint Language (OCL)
9
Collection type
Operation Returns collect(property) a new
collection of the designated
property e.g. stock_item-gtcollect(author)
returns a collection of author names Note - a)
may abbreviate this to - stock_item.author
b) do not need to use index
in qualified associations select(boolean
expression) a collection of items for which
select( c expression containing c) expression
is true e.g. borrower-gtselect(b b.numLoans
0) reject(boolean expression) reject( c
boolean expression with c) a collection
containing items for which expression is
false
10Object Constraint Language (OCL)
10
Collection type
Operation Returns includes(object) true if
object is an element of collection count(object)
the number of times object occurs includesAll(co
llection2) true if collection2 is a
sub-collection exists(oclExpression) true if
expression is true for at least one
item forAll(oclExpression) true if expression is
true for all iterate(oclExpression) iterates
over collection also have - sum (used only
with collections of integers or reals) isEmpty
notEmpty
11Object Constraint Language (OCL)
11
Set type
Operation Returns union(set2) the union of both
sets or a set and a bag union(bag2)
true if sets contain the same
elements intersection(set2) the intersection of
two sets or set and bag intersection(bag2) - th
e elements of set1 not in set2 including(object)
set with object added excluding(object) set with
object removed symmetricDifference(set2) set
with items from each not in both also,
operations on collections and asSequence and asBag
12Object Constraint Language (OCL)
12
Bag type
Operation Returns true if bag contains same
elements same number of times union(bag2) un
ion of two bags or bag and set union(set2) inters
ection(bag2) intersection of two bags or bag and
set intersection(set2) including(object) bag
with object added excluding(object) bag with
object removed count(object) occurrences of
object in bag also, operations on collections
plus asSequence and asSet
13Object Constraint Language (OCL)
13
Sequence type
Operation Returns true if sequences
identical count(object) occurrences of object
in sequence union(seq2) sequences combined end
to end append(object) object added to end of
sequence prepend(object) object added to start
of sequence subSequence(low,high) subsequence
of sequence at(integer) element at
position also, operations on collections plus
first, last, including(object), excluding(object)
14Object Constraint Language (OCL)
14
Design by Contract - Specifying methods
The semantics of methods are specified in OCL by
using pre post conditions which describe the
state of the world before and after an
operation. The pre condition represents a
contract which the client of the method
must agree to, e.g. - Library borrowBook(Member
m, BookAqNo bid) pre - self.borrower-gtincludes
(m) and self.stock_itembid-gtsize 1 and
m.loan -gtsize lt 6 post - nl new
Loan(today) and m.loan m.loan_at_pre-gtincluding(nl
) and self.stock_itembid.onLoan
ln Or pre - true post - if
self.borrower_at_pre-gtincludes(m) and
self.stock_item_at_prebid-gtsize 1 and
m_at_pre.loan_at_pre -gtsize lt 6
then --specification of post
condition as above else
self self_at_pre
15Object Constraint Language (OCL)
15
Design by Contract - Specifying methods
In the first version, the pre-condition specifies
the state of the world that must exist before the
operation is carried out. It is the clients
responsibility to ensure this. If the
pre-condition is false, the effect of the
operation is undefined In the second version,
the operation is always defined and deals
appropriately with all eventualities. Defensive
programming In defensive programming, the second
approach is usually taken even if a pre-condition
is imposed which it is the clients responsibility
to verify.
16Object Constraint Language (OCL)
16
Example System - Petri-Net Simulation
transitions
outputs
Petri-Net
inputs
places