ocl

OCL: Specify pre and postconditions for constructor and destructor


Hi I'm new to OCL and was wondering if the following can be done using OCL:

  1. check if certain conditions hold before an object can be created
  2. specify operations that must be performed after an object is destructed

If yes, can we achieve this by specifying preconditions in the constructor (for no.1) and postconditions in the destructor (no.2) or is there any other way?

Thanks!


Solution

  • OCL is a side effect free language so nothing changes consequently there is no object construction or destruction. Neither UML nor Ecore provides any modeling of constructors or destructors.

    The Pivot-based OCL prototypes a solution to the no type-construction limitation by introducing shadow objects for which an OCL expression could guard the construction. But to avoid side effects shadow objects are just a lazy reification from the universe of all possible objects, so there is no destruction.

    Complete OCL adds an ability to reason about the changes between two system states using @pre or oclIsNew() so you could use oclIsNew() somewhere to detect that something bad happened.

    The OMG OCL standard defines a history model that could allow reasoning about many system states, but I am not aware of any attempt to implement it. Much more recently the USE group have published papers on a filmstrip approach that is much more promising. It could probably be elaborated to support construction/destruction.

    Informally you are free to use an OCL expression in the documentation of your constructor / destructor.

    My view of programming is that all systems are state machines and all transitions are model transformations. You may therefore use OCL in the guard conditions of your state transition/transformation with the action performing the state mutation.