Difference between revisions of "OCL"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 108: Line 108:
 
If people were not unique to a department, then a '''''bag''''' of people would be returned.
 
If people were not unique to a department, then a '''''bag''''' of people would be returned.
  
 +
=== Names and Invariants ===
 +
 +
* An OCL invariant need not be applicable to any instance:
 +
* e.g.
 +
<syntaxhighlight lang="java5">
 +
context bart: Person inv
 +
bart.age == 10
 +
</syntaxhighlight>
 +
* TODO: clarify what is bart - a Class, what is Person ?
 +
 +
* An invariant can be given a name:
 +
<syntaxhighlight lang="java5">
 +
context bart: Person inv how_old:
 +
bart.age == 10
 +
</syntaxhighlight>
  
 
[[Category:OODE]]
 
[[Category:OODE]]

Revision as of 09:21, 30 October 2011

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" ?]

Navigation Expressions

  • We can refer to object that are linked to the context object.
  • We need to start from the context object. Consider the below example:

Ocl-navigation.jpg

  • 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.
  • e.g. below OCL will retrieve one object
context Department
self.company

Iterated Traversals

  • Navigations can be composed of multiple references - we can denote more paths through a diagram.
  • e.g All the people who work for a comapany
context Company
self.department.staff

self.department will give a set of departments, and then .staff is applied to each department producing a set of people. If people were not unique to a department, then a bag of people would be returned.

Names and Invariants

  • An OCL invariant need not be applicable to any instance:
  • e.g.
context bart: Person inv
bart.age == 10
  • TODO: clarify what is bart - a Class, what is Person ?
  • An invariant can be given a name:
context bart: Person inv how_old:
bart.age == 10