OCL
From Suhrid.net Wiki
Contents
Introduction
- How can we express constraints in UML ?
- E.g. the balance attribute in a SavingsAccount class can never be less than zero and more than 100000.
- UML only has a notion of an informal constraint in terms of a note - which can be attached to a model. However its just text.
- To write formal and machine checkable constraints - we use the Object Constraint Language (OCL).
Assertions
- In a program, assertions express constraints on program state that must be true at a specified point during execution.
- In a model/diagram, they document what must be true of an implementation of a modelling element.
- Can have optional preconditions - (must be true when operation is invoked). for e.g. an argument to a certain operation cannot be null.
- Can have optional postconditions - (must be true when operation completes). for e.g. the operation must update the database when it completes.
Design by contract
- A pre and post condition can be viewed as a contract between an operation and its invokers.
- Something like - "If the client promises to invoke the operation with the precondition satisfied, then the operation guarantees that its implementing methods will deliver a final state in which the postcondition is true".
- So the invoker doesn't care about how final state is produced
- And the implementer doesn't need to worry about cases where the precondition is not true.
[TODO] : How about handling errors for invalid preconditions ?
- So a contract can be many things:
- A specification of what a method should do
- Documentation - more precise than class interfaces [TODO : e.g. ]
- Offers runtime checks for correctness.
- Are a basis for testing.
- Are a basis for formal proofs of programs correctness.
OCL
- OCL is the constraint language for UML.
- Used to write general constraints on UML as well as DBC.
- Can apply to any modeling element, not only classes.
- OCL Essential Capabilities:
- Specifying which model element is to be constrained (the context).
- Navigating through models to identify objects that are relevant to a constraint.
- Asserting properties about links between objects.
OCL Context
- Consider a BankAccountclass with an integer balance.
- OCL constraint
context BankAccount inv:
self.balance>=0 and self.balance<=250000
- The constraint is an invariant that applies in the context of the class BankAccount.
- This means the balance for any BankAccount object must be within 0 and 250000.
- self means the same thing as "this".
[TODO: check when you are using invariants, why would you use "self" ?]
- We can refer to object that are linked to the context object.
- We need to start from the context object. Consider the below example:
- To get the set of employees working in a department :
context Department
self.staff
- The association name has to be followed
- Think of it something like the below code in Java, to access the Person object,
class Department {
private Set<Person> staff;
public Set<Person> getStaff() {
return this.staff;
}
}
- If there is no association name, then the class name at the destination end of the context object can be used.
- The class name has to be in lower case
- Eg to get the department from the company:
context Company
self.department
- The multiplicity of the assocation will tell us how many objects are retrieved.