Object Modeling with OMG UML Tutorial Series
Advanced Modeling with UML
Karin Palmkvist, Bran Selic, Jos Warmer
and Nathan Dykman
UML Revision Task Force
© 1999-2001 OMG and Contributors: Crossmeta, EDS, IBM, Enea Data, Hewlett-Packard, IntelliCorp, Kabira Technologies, Klasse Objecten,
Rational Software, Telelogic, Unisys
Overview


Introduction
Advanced Modeling

Part 1: Model Management


Part 2: Extension Mechanisms and Profiles



Karin Palmkvist, Enea Data
Bran Selic, Rational Software
Nathan Dykman, Hewlett Packard
Part 3: Object Constraint Language (OCL)


Jos Warmer, Klasse Objecten
Bran Selic, Rational Software
Advanced Modeling with UML
2
Tutorial Series



Lecture 1: Introduction to UML:
Structural and Use Case Modeling
Lecture 2: Behavioral Modeling with
UML
Lecture 3: Advanced Modeling with UML
[Note: This version of the tutorial series is based on OMG UML
Specification v. 1.4, UML Revision Task Force recommended final draft,
OMG doc# ad/01-02-13.]
Advanced Modeling with UML
3
Tutorial Focus: the Language

language = syntax + semantics




syntax = language elements (e.g. words)
are assembled into expressions (e.g.
phrases, clauses)
semantics = the meanings of the syntactic
expressions
UML Notation Guide – defines UML’s
graphic syntax
UML Semantics – defines UML’s
semantics
Advanced Modeling with UML
4
UML Overview
«metamodel»
UML
Model
Management
Behavioral
Elements
dependency
Foundation
Advanced Modeling with UML
package
5
UML Overview
Behavioral
Elements
Model
Management
Foundation
Extension
Mechanisms
Core
Data
Types
Advanced Modeling with UML
6
UML Overview
Behavioral Elements
Use Cases
Collaborations
State Machines
Activity Graphs
Model
Management
Common
Behavior
Foundation
Advanced Modeling with UML
7
Advanced Modeling with UML



Part 1: Model Management
Part 2: Extension Mechanisms and Profiles
Part 3: Object Constraint Language (OCL)
Advanced Modeling with UML
8
UML Overview
«metamodel»
UML
Model
Management
Behavioral
Elements
Foundation
Advanced Modeling with UML
9
Model Management Overview

Main UML constructs used for
model management:



Package
Subsystem
Model
Advanced Modeling with UML
10
Unifying Concepts

Packages, Subsystems, and Models



Group other model elements together
Each groups elements for a different
reason (providing different semantics)
Other grouping elements in UML
include:


Classes
Components
Advanced Modeling with UML
11
Package





What are Packages?
Core Concepts
Diagram Tour
When to Use Packages
Modeling Tips
Advanced Modeling with UML
12
Package
A package is a grouping of model elements
Advanced Modeling with UML
13
Package – Example
Sales
Customer
Order
Warehouse
Location
Item
Stock Item
Order Item
Advanced Modeling with UML
14
Package

A package can contain model elements
of different kinds



Including other packages to create
hierarchies
A package defines a namespace for its
contents
Packages can be used for various
purposes
Advanced Modeling with UML
15
Core Concepts
Construct
Package
Description
Syntax
A grouping of model elements.
Name
Import
A dependency indicating that the
public contents of the target
package are added to the namespace
of the source package.
Access
A dependency indicating that the
public contents of the target
package are available in the
namespace of the source package.
Advanced Modeling with UML
«import»
«access»
16
Visibility

Each contained element has a visibility
relative to the containing package




A public element is visible to elements outside the
package, denoted by ‘+’
A protected element is visible only to elements
within inheriting packages, denoted by ‘#’
A private element is not visible at all to elements
outside the package, denoted by ‘-’
Same syntax for visibility of attributes and
operations in classes
Advanced Modeling with UML
17
Import
Y
X
+C
B
«import»
A
-D
+E
Y
X
B
Y::C
+C
-D
«import»
A
Y::E
+E
The associations are owned by package X
Advanced Modeling with UML
18
Import – Alias
Y
X
+B
B
«import»
A
-D
+E
same class
Y
X
B
A
+B
+Y::B
-C
«import»
+Y::E
-D
+E
An imported element can be given a local alias and a
local visibility
Advanced Modeling with UML
19
Access
Y
X
+C
B
«access»
A
X
«access»
-D
+E
Y
+C
B
-D
+E
A
The associations are owned by package X
Advanced Modeling with UML
20
Import vs. Access
Y
X
Y::C
B
Y::F
A
Z
+C
«import»
-D
+E
Y::E
+F
+Z::F
«import»
+G
-Z::G
Y
X
Z
+C
B
«access»
A
-H
-D
+E
Advanced Modeling with UML
+F
«access»
-H
+G
21
Package Inheritance

A package with a generalization to
another package inherits public and
protected elements that are


owned or
imported
by the inherited package
Advanced Modeling with UML
22
Diagram Tour


Packages are shown in static diagrams
Two equivalent ways to show containment:
Advanced Modeling with UML
23
When to Use Packages




To create an overview of a large set
of model elements
To organize a large model
To group related elements
To separate namespaces
Advanced Modeling with UML
24
Modeling Tips – Package




Gather model elements with strong cohesion
in one package
Keep model elements with low coupling in
different packages
Minimize relationships, especially associations,
between model elements in different packages
Namespace implication: an element imported
into a package does not “know” how it is used
in the imported package
Advanced Modeling with UML
25
Subsystem





What are Subsystems?
Core Concepts
Diagram Tour
When to Use Subsystems
Modeling Tips
Advanced Modeling with UML
26
Subsystem
Subsystems are used for
system decomposition
Advanced Modeling with UML
27
Subsystem – Example
Traffic
Control
Trunk
Communicating subsystems
constitute a system
Subscription
Advanced Modeling with UML
28
Core Concepts
Construct
Subsystem
Description
A grouping of model elements
that represents a behavioral unit
in a physical system.
Advanced Modeling with UML
Syntax
Name
29
Subsystem Aspects

A subsystem has two aspects:



An external view, showing the services
provided by the subsystem
An internal view, showing the
realization of the subsystem
There is a mapping between the two
aspects
Advanced Modeling with UML
30
Subsystem Aspects
Specification elements
Realization elements
A subsystem has a specification and a realization
to represent the two views
Advanced Modeling with UML
31
Subsystem Realization
Specification elements
Realization elements
?


The subsystem realization defines the actual contents of
the subsystem
The subsystem realization typically consists of classes and
their relationships, or a contained hierarchy of subsystems
with classes as leaves
Advanced Modeling with UML
32
Subsystem Specification
Specification elements
Realization elements
?
The subsystem specification defines the
external view of the subsystem
Advanced Modeling with UML
33
Subsystem Specification

The subsystem specification




describes the services offered by the
subsystem
describes the externally experienced
behavior of the subsystem
does not reveal the internal structure of
the subsystem
describes the interface of the subsystem
Advanced Modeling with UML
34
Specification Techniques




The
The
The
The
Use Case approach
State Machine approach
Logical Class approach
Operation approach
…and combinations of these.
Advanced Modeling with UML
35
Use Case Approach
Specification elements


Realization elements
For subsystem services used in certain sequences
When the specification is to be understood by nontechnical people
Advanced Modeling with UML
36
Use Case Approach – Example
Traffic Control
Specification elements
Operator
Realization elements
Change Digit Analysis Information
Initiate Call
Trunk
Subscription
Receive Digit and Connect
Hook Signal and Disconnect
Advanced Modeling with UML
37
State Machine Approach
Traffic Control
Specification elements
Stopped
Running
Maintenance
Error


Exhausted
For subsystems with state dependent behavior
Focuses on the states of the subsystem and the
transitions between them
Advanced Modeling with UML
38
Logical Class Approach
Traffic Control
Specification elements
Analyzer
Number
Dictionary
Network
Manager


When usage of the subsystem is perceived as
manipulation of objects
When the requirements are guided by a particular
standard
Advanced Modeling with UML
39
Operation Approach
Traffic Control
Operations
initiateConnection (…)
dialledDigit (…)
throughConnect (…)
bAnswer (…)
bOnHook (…)
aOnHook (…)


For subsystems providing simple, “atomic” services
When the operations are invoked independently
Advanced Modeling with UML
40
Mixing Techniques
Traffic Control
Specification elements
Operations
changeDigitAnalysisInformation (...)
Specification elements
Initiate Call
Trunk
Receive Digit and Connect
Subscription
Hook Signal and Disconnect
Advanced Modeling with UML
41
Complete Subsystem Notation
Operations
Realization elements
Specification elements


The complete subsystem symbol has three pre-defined
compartments
Each of the compartments may be optionally omitted
from the diagram
Advanced Modeling with UML
42
Subsystem Interfaces
Trunk
Traffic
Control
Subscription
Trunk
Traffic
Control
Subscription
Advanced Modeling with UML
43
Operations and Interfaces
«Interface»
operation1( )
operation2( ) «realize»
operation4( )
«Interface»
Operations
operation1( ) : Type1
operation2( )
«realize» operation3( )
operation5( )
operation2( ) : Type2
operation3( ) : Type3
operation4( ) : Type4
operation5( ) : Type5
The subsystem must support all operations in the
offered interfaces
Advanced Modeling with UML
44
Subsystem Interfaces
«Interface»
«Interface»
Specification elements
«realize»
«realize»
An interface operation may alternatively be supported
by a specification element of the subsystem
Advanced Modeling with UML
45
Specification – Realization


The specification and the realization
must be consistent
The mapping between the specification
and the realization can be expressed
by:


realization relationships
collaborations
Advanced Modeling with UML
46
Realize Relationship
Operations
operation1( ) : Type1
operation2( ) : Type2
Realization Elements
«realize»
operation1( )
operation3( ) : Type3
operation4( ) : Type4
operation5( ) : Type5
Realization is particularly useful in simple mappings
Advanced Modeling with UML
47
Realize – Example
Traffic Control
Realization elements
Operations
changeDigitAnalysisInformation ( )
Specification elements
Initiate Call
«realize»
changeDigitAnalysisInformation ( )
:
:
Trunk
Receive Digit and Connect
Subscription
Hook Signal and Disconnect
Advanced Modeling with UML
48
Collaboration


A collaboration defines the roles to be played
when a task is performed
The roles are played by interacting instances
Sequence Diagram
:Trunk
:Traffic Control
Collaboration Diagram
:Subscription
2: dialledDigit
3: dialledDigit
6: bAnswer
markBusy
dialledDigit
:Traffic Control
4: throughConnect
dialledDigit
:Trunk
throughConnect
markBusy
:Subscription
bAnswer
1: markBusy
5: markBusy
Advanced Modeling with UML
49
Collaboration – Notation
Collaboration
Role
role name
role name
Class
role name
role name
A collaboration and its participants
Advanced Modeling with UML
50
Collaboration – Example
Specification elements Realization elements
Initiate Call
Network
Interface
Receive Digit and Connect
Coordinator
Analysis
Database
Hook Signal and Disconnect
Collaborations are useful in more complex situations
Advanced Modeling with UML
51
Subsystem Interaction
4:receive
1:transmit
2:send
3:ack
1.4
Advanced Modeling with UML
52
Communicating with Subsystems

Two approaches:


1.4
Open subsystem - public elements are
accessed directly
Closed subsystem - access via the
subsystem itself
Advanced Modeling with UML
53
Open Subsystems
Traffic Control

-B
+A
«import»

Trunk
+TC::A
-C
A subsystem is
called open if its
realization is used
directly by the
environment
The specification
acts


+B
1.4
Advanced Modeling with UML
as an overview of
the subsystem
as a requirements
specification
54
Closed Subsystems
Traffic Control

+A
-B

A subsystem is called
closed if its realization
is not directly used by
the environment
The specification acts

Trunk

-C
+B
1.4

as an overview of the
subsystem
as a requirements
specification
as a specification of
how to use the
subsystem
Advanced Modeling with UML
55
Subsystem Instance
:Traffic Control
A subsystem instance is the runtime
representation of a subsystem. Its task is
to handle incoming stimuli.
1.4
Advanced Modeling with UML
56
Subsystem Inheritance

A subsystem with a generalization to
another subsystem inherits public and
protected elements that are




owned or
imported
by the inherited subsystem
Both specification elements and
realization elements are inherited
Operations are also inherited
Advanced Modeling with UML
57
Diagram Tour


Subsystems can be shown in static diagrams and
interaction diagrams
“Fork” notation alternative for showing contents:
Specification elements Realization elements
Realization elements
Advanced Modeling with UML
58
Diagram Tour – continued

Subsystems can be shown in interaction
diagrams


collaboration diagrams
sequence diagrams
Sequence Diagram
Advanced Modeling with UML
59
When to Use Subsystems




To express how a large system is
decomposed into smaller parts
Distributed development
To express how a set of modules are
composed into a large system
For component based development
Advanced Modeling with UML
60
Component Based Development
ShoppingCart
Specification Elements
Realization Elements
«Interface»
ShoppingCartHome
«call»
ArtStore
Client
«call»
create(…)
findByPrimaryKey(…)
...
«auxiliaryClass»
HomeObject
ShoppingCart
Home
«call»
ShoppingCart
ShoppingCart
Impl
«auxiliaryClass»
«call»
«Interface»
Context
ShoppingCart
getItemCount(…)
setItemCount(…)
getTotal(…)
«focalClass»
«auxiliaryClass»
«auxiliaryClass»
RemoteObject
ContextObject
ShoppingCart
DBbroker
setTotal(…)
...
An EJB component modeled using a subsystem and
classes stereotyped «focalClass» or «auxiliaryClass»
1.4
Advanced Modeling with UML
61
Modeling Tips – Subsystem



Define a subsystem for each separate
part of a large system
Choose specification technique
depending on factors like kind of
system and kind of subsystem
Realize each subsystem independently,
using the specification as a
requirements specification
Advanced Modeling with UML
62
Model





What are Models?
Core Concepts
Diagram Tour
When to Use Models
Modeling Tips
Advanced Modeling with UML
63
Model
A model captures a view of a system, with a certain
purpose. It is a complete description of those
aspects of the system relevant to the purpose of the
model, at the appropriate level of detail.
Advanced Modeling with UML
64
Model – Example
Use Case Model
Design Model
Advanced Modeling with UML
65
Core Concepts
Construct
Model
Trace
Description
A view of a system, with a certain
purpose determining what aspects
of the system are described and at
what level of detail.
A dependency connecting model
elements that represent the same
concept within different models.
Traces are usually non-directed.
Advanced Modeling with UML
Syntax
Name
«trace»
66
Trace
Analysis
Design
«trace»
Advanced Modeling with UML
67
Model Inheritance

A model with a generalization to
another model inherits public and
protected elements that are


owned or
imported
by the inherited model
Advanced Modeling with UML
68
Diagram Tour


Models as such are seldom shown in
diagrams
Two equivalent ways to show containment:
Advanced Modeling with UML
69
Model vs. Diagram
Use Case
Model
Diagrams make up the
documentation of a model
Design
Model
Advanced Modeling with UML
70
When to Use Models



To give different views of a system to
different stakeholders
To focus on a certain aspect of a
system at a time
To express the results of different
stages in a software development
process
Advanced Modeling with UML
71
Modeling Tips – Model



Define the purpose for each model
A model must give a complete picture
of the system, within its purpose
Focus on the purpose of the model;
omit irrelevant information
Advanced Modeling with UML
72
Models and Subsystems
Models and subsystems can be combined
in hierarchies:
Advanced Modeling with UML
73
Wrap Up Model Management

Packages are used to organize a large
set of model elements




Subsystems are used to represent
parts of a system



Visibility
Import
Access
Specification
Realization
Models are used to capture different
views of a system

Trace
Advanced Modeling with UML
74
Advanced Modeling with UML



Part 1: Model Management
Part 2: Extension Mechanisms and Profiles
Part 3: Object Constraint Language (OCL)
Advanced Modeling with UML
75
Semantic Variations in UML



UML contains semantic aspects that are:

undefined (e.g., scheduling discipline), or

ambiguous (multiple choices/interpretations)
Why is this the case?

UML can’t address every domain perfectly

Different domains require different specializations
But, if UML can be extended in arbitrary
ways, what is the value of having a standard?
Advanced Modeling with UML
76
UML as a “Family of Languages”

The standard can be specialized for
different domains

in effect: refinements of the standard
UML 1.4 Standard
UML for Real Time
UML for CORBA
Advanced Modeling with UML
Using
extensibility
mechanisms
etc.
77
Extensibility Mechanisms

Extensibility Mechanisms


Allow Modelers to refine the semantics of
UML for a specific domain
Extensions cannot violate the standard
UML semantics


Enforces a consistent core of concepts and
semantics for every variation
Prevents meta-model explosion (Using UML
to model everything and anything)
Advanced Modeling with UML
78
How UML Extensibility Works

The standard UML semantics can be
viewed as defining a space of possible
interpretations
refined semantics
(valid)
different semantics
(NOT valid)
Standard UML semantics
Advanced Modeling with UML
79
Models

A model is a description of something

“a pattern for something to be made” (MerriamWebster)
blueprint
(model)
building

building
model  thing that is modeled

The Map is Not The Territory
Advanced Modeling with UML
80
Meta-Models

Are Simply Models of Models
<Ben&Jerry’s>
Customer
<sawdust>
<2 tons>
<lard>
<5 tons>
CustomerOrder
item
quantity
id
Class
Association
Advanced Modeling with UML
Objects
(M0)
Model
(M1)
Meta-Model (M2)
81
The UML Meta-Model

Is a UML Model of UML
Meta-Class
GeneralizableElement
isRoot : Boolean
isLeaf : Boolean
isAbstract : Boolean
Feature
visibility : {public, private,
protected}
*
Classifier
Well-formedness constraint (OCL)
Class
isActive : Boolean
not self.isAbstract implies
self.allOperations->forAll(op |
self.allMethods->exists(m |
m.specification includes (op)))
Advanced Modeling with UML
82
The UML Meta-Model

Put another way, UML uses (a part of)
itself as a Meta-language


Other examples: Scheme, Lisp, Smalltalk
This small subset of UML is used to
describe all of UML


The subset contains classes, associations,
operations, constraints, generalization, etc.
This core subset is related to MOF (more
on MOF in Part 4)
Advanced Modeling with UML
83
The Basic Extensibility Mechanisms

Stereotypes


Constraints



used to refine meta-classes (or other stereotypes)
by defining supplemental semantics
predicates (e.g., OCL expressions) that reduce
semantic variation
can be attached to any meta-class or stereotype
Tagged Values


individual modifiers with user-defined semantics
can be attached to any meta-class or stereotype
Advanced Modeling with UML
84
Stereotypes


Used to define specialized model elements
based on a core UML model element
Defined by:

Base metaclasses (or stereotype)


Constraints:


1.4
What values does this stereotype need to know?
icon


What is special about this stereotype?
required tags (0..*)


What element is specialized?
How should I appear in a model?
A model element can be stereotyped in
multiple different ways
Advanced Modeling with UML
85
Example


Capsule: A special type of concurrent
object used in modeling certain realtime systems
By definition, all classes of this type:




are active (concurrent)
have only features (attributes and operations) with
protected visibility
have a special “language” characteristic used for
code generation purposes
In essence, a constrained form of the
general UML Class concept
Advanced Modeling with UML
86
Example: Stereotype Definition

Using a tabular form:
Stereotype
Base Class
Tags
Constraints
isActive = true;
«capsule»
1.4
Class
language
self.feature->select(f |
f.oclIsKindOf(Operation))->
forAll(o |
o.elementOwnership.visibility
= #protected)
Tag
Stereotype
Type
Multiplicity
language
«capsule»
String
0..1
Advanced Modeling with UML
87
Stereotype Notation

Several choices
«capsule»
aCapsuleClass
(a) with guillemets
(“gwee-mays”)
Stereotype
icon
aCapsuleClass
(b) with icon
(c) iconified form
Advanced Modeling with UML
88
Extensibility Method

Refinements are specified at the Model (M1)
level but apply to the Meta-Model level (M2)


avoids need for “meta-modeling” CASE tools
can be exchanged with models
«capsule»
aCapsuleClass
Customer
CustomerOrder
«Capsule»
item
quantity
id
Class
Association
Advanced Modeling with UML
(M1)
(M2)
89
Graphical Definition

Alternative to the tabular form

defined in a user (M1) model
«metaclass»
Class
«stereotype»
«stereotype»
capsule
Tags
language : String
1.4
Advanced Modeling with UML
90
Heuristic: Combining Stereotypes

Through multiple inheritance:
«stereotype»
capsule
«metaclass»
Package
Tags
language : String
«stereotype»
«stereotype»
PackageCapsule
1.4
Advanced Modeling with UML
91
When to Use Stereotypes?


Why not use normal subclassing
instead?
Use stereotypes when:

additional semantic constraints cannot be
specified through standard M1-level
modeling facilities


e.g. “all features have protected visibility”
the additional semantics have significance
outside the scope of UML

e.g. instructions to a code generator
“debugOn = true”
Advanced Modeling with UML
92
Tagged Values



Consist of a tag and value pair
Typed with a standard data type or M1 class
name
Typically used to model stereotype attributes


May also be used independently of
stereotypes

1.4
Additional information that is useful/required to
implement/use the model
e.g., project management data
(“status = unit_tested”)
Advanced Modeling with UML
93
Constrains

Constraints


formal or informal expressions
must not contradict inherited base semantics
ATM_Withdrawal
Account
customer : id
amount : Money
{amount is multiple of $20}
customer : id
balance : Money
«constraint»
{ATM_Withdrawal.customer =
Account.customer}
Advanced Modeling with UML
94
UML Profiles

A package of related extensibility
elements that capture domain-specific
variations and usage patterns


A domain-specific interpretation of UML
Profiles currently being defined by the
OMG:




EDOC
Real-Time
CORBA
...
Advanced Modeling with UML
95
Advanced Modeling with UML



Part 1: Model Management
Part 2: Extension Mechanisms and Profiles
Part 3: Object Constraint Language (OCL)
Advanced Modeling with UML
96
Overview




What are constraints?
Core OCL Concepts
Advanced OCL Concepts
Wrap up
Advanced Modeling with UML
97
Why use OCL ?
What’s in it
for me?
Use the
Object
Constraint
Language
Advanced Modeling with UML
98
Can we make this more precise?
Flight
PassengerFlight
flights
0..*
1
CargoFlight
Airplane
0..*
0..*
1
PassengerPlane
CargoPlane
1
99
Diagram with added invariants
Flight
type :
enum of cargo, passenger
0..*
flights
1
Airplane
type :
enum of cargo, passenger
{context Flight
inv: type = #cargo implies airplane.type = #cargo
inv: type = #passenger implies airplane.type = #passenger}
100
Definition of constraint

“A constraint is a restriction on one or
more values of (part of) an objectoriented model or system.”
Advanced Modeling with UML
101
Different kinds of constraints

Class invariant


Precondition of an operation


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
Postcondition of an operation

a constraint that must always be true
AFTER the execution of the operation
Advanced Modeling with UML
102
Constraint stereotypes

UML defines three standard stereotypes
for constraints:



invariant
precondition
postcondition
Advanced Modeling with UML
103
What is OCL?

OCL is


a textual language to describe constraints
the constraint language used in UML
models


As well as the UML meta-model
Formal but easy to use


unambiguous
no side effects
Advanced Modeling with UML
104
Constraints and the UML model

OCL expressions are always bound to a
UML model

OCL expressions can be bound to any
model element in UML
Advanced Modeling with UML
105
Core OCL Concepts




What are constraints
Core OCL Concepts
Advanced OCL Concepts
Wrap up
Advanced Modeling with UML
106
Example model
Flight
Airport
origin
name: String
destination
departing
Flights
departTime: Time
*
/arrivalTime: Time
duration : Interval
*
maxNrPassengers: Integer
flights
airline
*
arriving
Flights
Airline
passengers
* {ordered}
name: String
Passenger
$minAge: Integer
age: Integer
needsAssistance: Boolean
0..1
book(f : Flight)
CEO
Advanced Modeling with UML
airline
0..1
107
Constraint context and self

Every OCL expression is bound to a
specific context.


The context is often the element that the
constraint is attached to
The context may be denoted within the
expression using the keyword ‘self’.


‘self’ is implicit in all OCL expressions
Similar to`this’ in C++
Advanced Modeling with UML
108
Notation

Constraints may be denoted within the
UML model or in a separate document.

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
Advanced Modeling with UML
109
Elements of an OCL expression

In an OCL expression these elements
may be used:


basic types: String, Boolean, Integer, Real.
classifiers from the UML model and their
features



attributes, and class attributes
query operations, and class query operations
(i.e., those operations that do not have side
effects)
associations from the UML model
Advanced Modeling with UML
110
Example: OCL basic types
context Airline inv:
name.toLower = ‘klm’
context Passenger inv:
age >= ((9.6 - 3.5)* 3.1).floor implies
mature = true
Advanced Modeling with UML
111
Model classes and attributes

“Normal” attributes
context Flight inv:
self.maxNrPassengers <= 1000

Class attributes
context Passenger inv:
age >= Passenger.minAge
Advanced Modeling with UML
112
Example: query operations
context Flight inv:
self.departTime.difference(self.arrivalTime)
.equals(self.duration)
Time
$midnight: Time
month : String
day : Integer
year : Integer
hour : Integer
minute : Integer
difference(t:Time):Interval
before(t: Time): Boolean
plus(d : Interval) : Time
Interval
nrOfDays : Integer
nrOfHours : Integer
nrOfMinutes : Integer
equals(i:Interval):Boolean
$Interval(d, h, m : Integer) :
Interval
Advanced Modeling with UML
113
Associations and navigations



Every association in the model is
a navigation path.
The context of the expression is
the starting point.
Role names are used to identify
the navigated association.
Advanced Modeling with UML
114
Example: navigations
Flight
Airport
origin
name: String
destination
departing
Flights
departTime: Time
*
/arrivalTime: Time
duration : Interval
*
maxNrPassengers: Integer
arriving
Flights
context Flight
inv: origin <> destination
inv: origin.name = ‘Amsterdam’
context Flight
inv: airline.name = ‘KLM’
Advanced Modeling with UML
115
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
Advanced Modeling with UML
Company
name : String
116
The OCL Collection types


What are constraints
Core OCL Concepts



Collections
Advanced OCL Concepts
Wrap up
Advanced Modeling with UML
117
Significance of Collections in OCL

Most navigations return collections
rather than single elements
Flight
type :
enum of cargo, passenger
0..*
flights
1
Airplane
type :
enum of cargo, passenger
Advanced Modeling with UML
118
Three Subtypes of Collection

Set:



Bag:



arrivingFlights(from the context Airport)
Non-ordered, unique
arrivingFlights.duration (from the context
Airport)
Non-ordered, non-unique
Sequence:


passengers (from the context Flight)
Ordered, non-unique
Advanced Modeling with UML
119
Collection operations


OCL has a great number of predefined
operations on the collection types.
Syntax:

collection->operation
Use of the “->” (arrow)
operator instead of the
“.” (dot) operator
Advanced Modeling with UML
120
The collect operation

Syntax:
collection->collect(elem : T | expr)
collection->collect(elem | expr)
collection->collect(expr)

Shorthand:
collection.expr


The collect operation results in the collection
of the values resulting evaluating expr for all
elements in the collection
Shorthand often trips people up. Be Careful!
Advanced Modeling with UML
121
Example: collect operation
context Airport inv:
self.arrivingFlights -> collect(airLine) ->notEmpty
airp1
f1
airline1
f2
f3
airp2
airline2
f4
airline3
f5
departing flights
arriving flights
Advanced Modeling with UML
122
The select operation

Syntax:
collection->select(elem : T | expression)
collection->select(elem | expression)
collection->select(expression)

The select operation results in the
subset of all elements for which
expression is true
Advanced Modeling with UML
123
Example: collect operation
context Airport inv:
self.departingFlights->select(duration<4)->notEmpty
f1
duration = 2
airp1
f2
duration = 5
f3
duration = 3
f4
duration = 5
airp2
departing flights
arriving flights
airline1
airline2
airline3
f5
duration = 2
Advanced Modeling with UML
124
The forAll operation

Syntax:




collection->forAll(elem : T | expr)
collection->forAll(elem | expr)
collection->forAll(expr)
The forAll operation results in true if
expr is true for all elements of the
collection
Advanced Modeling with UML
125
Example: forAll operation
context Airport inv:
self.departingFlights->forAll(departTime.hour>6)
f1
depart = 7
airp1
f2
depart = 5
f3
depart = 8
f4
depart = 9
airp2
airline1
airline2
airline3
f5
depart = 8
departing flights
arriving flights
Advanced Modeling with UML
126
The exists operation

Syntax:
collection->exists(elem : T | expr)
collection->exists(elem | expr)
collection->exists(expr)

The exists operation results in
true if there is at least one
element in the collection for
which the expression expr is
true.
Advanced Modeling with UML
127
Example: exists operation
context Airport inv:
self.departingFlights->exists(departTime.hour<6)
f1
depart = 7
airp1
f2
depart = 5
f3
depart = 8
airp2
f4
depart = 9
airline1
airline2
airline3
f5
depart = 8
departing flights
arriving flights
Advanced Modeling with UML
128
Other collection operations







isEmpty: true if collection has no elements
notEmpty: true if collection has at least one
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
129
Advanced OCL Concepts




What are constraints
Core OCL Concepts
Advanced OCL Concepts
Wrap up
Advanced Modeling with UML
130
Result in postcondition

Example pre and postcondition
context Airline::servedAirports() : Set(Airport)
pre : -- none
post: result = flights.destination->asSet
Advanced Modeling with UML
131
Statechart: referring to states

The operation oclInState returns true if
the object is in the specified state.
Bottle
filled : enum {empty, half, full}
closed
open
context Bottle inv:
self.oclInState(closed) implies filled = #full
Advanced Modeling with UML
132
Local variables

The let construct defines variables local
to one constraint:
Let var : Type = <expression1> in
<expression2>

Example:
context Airport inv:
Let supportedAirlines : Set (Airline) =
self.arrivingFlights -> collect(airLine) in
(supportedAirlines ->notEmpty) and
(supportedAirlines ->size < 500)
Advanced Modeling with UML
133
Iterate

The iterate operation for collections is
the most generic and complex building
block.
collection->iterate(elem : Type;
answer : Type = <value> |
<expression-with-elem-and-answer>)
Advanced Modeling with UML
134
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
Advanced Modeling with UML
135
Inheritance of constraints

Guiding principle Liskov’s Substitution
Principle (LSP):

“Whenever an instance of a class is
expected, one can always substitute an
instance of any of its subclasses.”
Advanced Modeling with UML
136
Inheritance of constraints

Consequences of LSP for invariants:



An invariant is always inherited by each
subclass.
Subclasses may strengthen the invariant.
Consequences of LSP for preconditions
and postconditions:


A precondition may be weakened
(contravariance)
A postcondition may be strengthened
(covariance)
Advanced Modeling with UML
137
Wrap Up




What are constraints
Core OCL Concepts
Advanced OCL Concepts
Wrap up
Advanced Modeling with UML
138
Current Developments

Feedback from several OCL implementers
handled in UML-RTF



Development of OCL metamodel



e.g. the grammar has some loose ends
typical tool-related issues
currently concrete syntax only
will result in abstract syntax
UML 2.0 RFP


meta-model as part of the UML meta-model
additional capabilities (e.g., business rules)
Advanced Modeling with UML
139
OCL Tools

Cybernetics


University of Dresden



www.boldsoft.com
ICON computing


www-st.inf.tu-dresden.de/ocl/
Boldsoft


ww.cybernetic.org
www.iconcomp.com
Royal Dutch Navy
Others … …
Advanced Modeling with UML
140
Conclusions and Tips

OCL invariants allow you to



OCL pre- and postconditions allow you to



model more precisely
remain implementation independent
specify contracts (design by contract)
specify interfaces of components more precisely
OCL usage tips



keep constraints simple
always combine natural language with OCL
use a tool to check your OCL
Advanced Modeling with UML
141
Further Resources for OCL

The Object Constraint Language


ISBN 0-201-37940-6
OCL home page

www.klasse.nl/ocl/index.htm
Advanced Modeling with UML
142
References


[UML 1.3] OMG UML Specification v. 1.3,
OMG doc# ad/06-08-99
[UML 1.4] OMG UML Specification v. 1.4, UML
Revision Task Force recommended final draft,
OMG doc# ad/01-02-13.
Advanced Modeling with UML
143
Further Info

Web:





UML 1.4 RTF: www.celigent.com/omg/umlrtf
OMG UML Tutorials: www.celigent.com/omg/umlrtf/tutorials.htm
UML 2.0 Working Group:
www.celigent.com/omg/adptf/wgs/uml2wg.htm
OMG UML Resources: www.omg.org/uml/
Email


[email protected]
Contributors





Karin Palmkvist: [email protected]
Bran Selic: [email protected]
Jos Warmer: [email protected]
Nathan Dykman: [email protected]
Conferences & workshops



UML World 2001, New York, June 11-14, 2001
UML 2001, Toronto, Canada, Oct. 1-5, 2001
OMG UML Workshop 2001, San Francisco, Dec. 3-6, 2001
Advanced Modeling with UML
144
Descargar

Behavioral Modeling with UML