OCL(Object Constraint Language)
OCL is a textual language to define constraints OCL can
specify queries, constraints and query operations OCL enables to add extra
information to a UML model OCL is a standard extension to UML for,
- Writing
queries to access model elements and their values
- Stating
constraints on model elements
- Defining
query operations
OCL expressions have no side effects because,
- OCL can’t
change the value of a model element
- OCL can’t
define an operation other than a query operation
- OCL can’t
be used to specify business rules dynamically
Features of OCL,
- can store
OCL expressions in files with UML model
- can attach
OCL expressions to UML model elements as notes
Uses of OCL
- OCL allows
suitably enabled modelling tool to reason about UML models
- OCL allows
suitably equipped modelling tools to generate code based on OCL
expressions
- OCL allows
us to be more precise in our modelling
Negatives of OCL:
- Complex
because syntaxes are irregular, have lots of “shortcut” forms
- few
modellers and even fewer programmers know OCL
- The level
of precision that OCL offers is high, mostly not used
OCL expression syntax
package<packagePath>
context<contextualINstanceName><modelElement>
<expressionType><expressionName>:
<expressionBody>
<expressionType><expressionName>:
….
endpackage
OCL keyword are represented in bold, optional elements
are shown in underlined italics. The angle brackets (<….>) indicate a
placeholder that we must replace with the appropriate thing.
OCL expression is divided into3 parts:
- The
package context(optional)
- The
expression context(mandatory)
- One or
more expressions
Package context and pathnames
The package context defines the namespace for the OCL
expression. It has the following rules,
- If we
don’t specify a package context the namespace for the expression defaults
to the whole model
- If we
attach an OCL expression directly to a model element the namespace for the
expression defaults to the owing package of the element.
E.g.: we could specify a package context as
packageBankAccountExample::SimpleAccounts
……………..
endpackage
The OCL syntax for path name is:
Package1::package2::…packageN::ElementName
The expression context
- The
expression context indicates the UML model element to which the OCL
expression is attached.
- The
expression context defines a contextual instance that has an
optional name and a mandatory type. The type of contextual instance
depends on expression context. i.e;- If the expression context is a
classifier the contextual instance is always an instance of that
classifier and if an expression is an operation or an attribute the
contextual instance is generally an instance of that classifier that owns
the attribute or the operation.
Types of OCL expressions
There are 8 different types in 2 categories
- Those
specifying constraints (inv:, pre:, and post:)
- Those
specifying attributes, operation bodies, and local variables (init:,
body:, def:, let, and derive:)
We can give the expressionName to the operations that
constrain (inv:, pre:, and post:). This allows us to refer to them by name,
perhaps to link them to use case specifications.
Good OCL style ensures that
- All
constraints are named
- Choose
descriptive name that summarizes the semantics of the constraint
- Ensure
that the constraint names are unique within the model
- Use
lowerCamelCase for constraint names.
Expressions that specify constraints
Invariant
|
Precondition
|
Postcondition
|
Syntax-inv:
|
Syntax-pre:
|
Syntax-post:
|
Applies to-classifier and it should be met by all
instances of the classifier
|
Applies to operation and it should be true before
execution starts
|
Applies to behavioural features and operations and must
be true after the execution
|
Expressions that specif attribute, operation and local
variables
Query operation
|
Initial value
|
Define
|
Syntax-body:
|
Syntax-init:
|
Syntax-def:
|
Applies to query operation
|
Applies to attributes and association end
|
Applies to classifier
|
Let
|
Derived value
|
Syntax-let
|
Syntax-derive:
|
Applies to OCL expression
|
Applies to attribute
|