A small, text-based formal language for object modelling, using only ASCII characters.
Descended from Z via the SyntropyMethodology, it is part of the UnifiedModelingLanguage, in which it is used to clarify the semantics of the meta-model, as well as enabling users of the UML to produce precise models.
Described in detail and very understandably in ISBN 0-32-117936-6 .
See also:
Is it the ultimate intent with OCL that code will be generated? Or is it that the constraints described in OCL must be manually put into the code?
You could use OCL to describe pre- and postconditions which could be later translated into code, but that's not the real intention:
The OCL is declarative rather than imperative. (It tells you what you should have rather than how something should be done.) Although I wouldn't like to say its impossible to generate code, it's likely to be impractical for most nontrivial models. If you want to generate code, you need an action language not a constraint specification language. (See UmlActionSemantics for details of a UML-based action language.)
See also: SingletonInUmlForJava
You can certainly use OCL in post-conditions to assert changes in state. But that won't help you overly much with code generation because it's hard automatically to convert from predicate logic to imperative code.
The OCL spec I found via the U.R.L. above implies that the OCL is not intended to be used for generating code. It may not even be possible to manually translat it into code! The idea is that it will tell you if the code you invent fits what the author of the OCL wants. It can state that the balance is always equal to the sum of the transactions on an account. It doesn't say that you have to add them up every time... or if you keep a running total... or if you do some clever combination of these. It merely tells someone reading the code, and thinking about it, whether the code is correct or not. Again you can easily state that a linked list should always by in increasing order -- but you can do this by sorting the list every time it changes, or by carefully inserting items in the right place. The OCL doesn't care.
Its the old what vs how distinction.
Wouldn't it be nice if I could actually use OCL in any of the so-called UML tools? --JohnDaniels
For what?
Try Poseidon from Gentleware, or the Boldsoft tools (now part of Borland) They both supportb OCl, including code generation
--Jos Warmer
I'm concerned that the emphasis on this page seems to be about how useful OCL would/would not be for code generation. How long it takes us to implement a specification is often less important than how many times we have to go back and re-implement it again (the amount of rework). There are many instances where what's needed is clarity - something that plain old ambiguous vanilla UML doesn't offer. Precise UML (UML + OCL constraints) can be productively applied in business strategy definition (measures, goals, relationships between goals etc), business modeling, systems analysis and design, test analysis and a whole heap of other stuff that is not necessarily about generating code but arguably has much more value. Such a shame about the lack of decent tool support, though. I don't know about you, but I find it hard work validating syntax and executing scenarios in my head. I see formal specification as a means of potentially dramattically reducing requirements defects and therefore an essential part of an effective defect-prevention strategy.
-- JasonGorman
Having said that, I've posted a tutorial on OCL specifically for .NET developers at http://www.parlezuml.com/tutorials/ocl/index_files/frame.htm :-)
-- JasonGorman
As far as tool support is concerned: for Rational Rose you might consider EmPowerTecs? OCL-add in (http://www.empowertec.de/products/rational-rose-ocl.htm).
-- Andreas Awenius