ICS 121: Object Constraint Language
Overview
- What is OCL?
- Why use OCL
- OCL Syntax
- Examples
What is OCL?
- OCL is the Object Constraint Language. It is a standard
notation that is closely related to UML. It can make UML models
more precise.
- OCL constraints are logical conditions for:
- Operations: pre- and post-conditions
- Attributes: conditions on the value of that attribute
- Association: conditon on membership in that association
- Class: conditions on all attributes and associations of that class
Why use OCL?
- Use OCL for Design by Contract in UML
- OCL can express constraints that cannot be handled with just
multiplicity and attribute types
- OCL conditions in a UML model help make the meaning of the model
more clear.
- UML tools can assert statements from OCL conditions.
OCL Syntax
- Each constraint applies to some UML model element called its
"context". If you draw the constraint directly on a UML diagram,
the context should be clear.
- You can reference attributes and properties of the context. You
can follow associations by using dot-notation.
- Since associations are sets, most values in OCL expressions are sets.
- OCL provides basic math operators: >, <, =, <>, +, -, *, /, mod, abs, ...
- OCL provides basic logical operatiors: and, or, not, xor, implies
- OCL provides basic set operatiors: size, includes, includesAll, union, intersection, sum, ...
- OCL provides first-order-predicate logic operators:
- exists: true if there is one element of the set that satisfied the condition
- forAll: true if all elements of the set satisfy the condition
- For more details see the OCL specification
Examples
- context Student::add(c: Course)
- pre: not enrolled->includes(c) and
transcript->includesAll(c.prereqs())
- post: enrolled->includes(c)
- context Course
- inv: enrolled->size() <= capacity
- inv: (not waitlist->isEmpty()) implies (enrolled->size() = capacity)
- inv: allInstances()->isUnique(courseID)
- inv: honors implies enrolled->forAll(s | s.isHonors() )
example use case templatesample test plan templateProject plan template