OCL
Summary
- Object Constraint Language
- Overview
- Uses
- Syntax
- Changes to Attribute Values
- Built-ins
- Other Features
Object Constraint Language
Official part of UML
Provides details that cannot be described by diagrams
- Specifically semantics
Strongly typed, declarative specification of system properties
Constraints + collection classes + UML diagram navigation
Extends UML with:
- Class invariants
- Operation pre and post conditions
- Guards on state-machine transitions
Overview
Declarative, not procedural
- Pure expression language
No assignments or other side effects
Strongly typed
- built-in types
Highest level mechanism is constraint
- Formal assertion of system properties
Uses
- Specify invariants
- Describe pre and post conditions
- Specify derivation rules for derived attributes
- Describe guards on transitions in statecharts
- Specify targets for messages and actions
- Use as a query language
Syntax
context <identifier> <constraintType>: <Boolean expression>
The context+identifier describe where you are in a diagram, typically a class
Invariants
Statement of a property that is always true
Express key system requirement
inv
keyword
context LargeCompany inv: numberOfEmployees > 50
Integrity constraints
Pre and Post conditions
context Real::squareRoot() : Real
pre: self >= 0
post: self = result * result
Changes to Attribute Values
Post conditions can also be used to describe the results of changes in the value of the attribute
context Account::deposit(Real : amount)
pre: amount > 0
post: balance = balance@pre + amount
Built-ins
Types | Literals | Operations |
---|---|---|
Boolean | true, false | and, or, xor, not, implies, if-then-else |
Integer | 1, -5, 10000, … | *, +, -, /, abs(), … |
Real | 1.5, 3.14, … | *, =, -, /, floor(), … |
String | ‘to be or not to be’ | toUpper(), concat() |
Keyword | Description |
---|---|
inv, pre, post | Introduces constraints |
if-then-else-endif | Conditional |
not, or, and, xor, implies | Boolean operators |
package, endpackage | Packages |
context | Introduces constraints |
def | Global definition |
let, in | local definition |
derive | Attribute derivation |
init | Intial value description |
result | Result value from an operation |
self | Object being constrained |
Let clause
Introduces an abbreviation to a constraint
let incoome : Integer = self.job.salary->sum() in
if isUnemployed then income < 100
else income >= 100
endif
Navigation
To refer to features of a non-context class, you must use a qualified name
Navigation is done by walking through a diagram from the context class to the other class using intermediary relationship lines and class rectangles
context Customer
self.order.date
Multiplicity means a collection
Collections
Other Features
- Tuples
- Similar to structs
- Enumerations
- Message expression
- denoting the sending of a signal or the invoking of an operation
- Access to metamodel
- Automatic flattening
- A set of sets is just a set