Jairson Vitorino, Cin UFPE May, 2nd 2005 OCL in a small nutshell Jairson Vitorino, Cin UFPE May, 2nd 2005
Contents Motivation What is OCL? Uses of OCL Examples References
Are you serious about your UML models? (source: www.empowertec.de) Motivation Are you serious about your UML models? (source: www.empowertec.de)
What is OCL? The Object Constraint Language (OCL) is a query language that can be used to define expressions on object oriented models (UML). OCL enrich the models with unambiguous information, formalizing knowledge about the underlying business domain.
It is a formal language, but Yes! You can understand it and even use it!
Uses of OCL Constraints are the most prominent use. Initialization expressions can be used to specify an initial value for an attribute or an association. OCL expressions can be used to specify the algorithm that is to be used for derived attributes, i.e. values that are calculated from other attributes or associations. OCL expressions can be used to specify the implementation of methods as long as they do not have side effects (i.e. the method is 'read only').
A trivial Example context Stack::pop(): Object optional You can refer to the previous state of na object context Stack::pop(): Object pre notEmpty: isEmpty() = false post topElementReturned: result = self@pre.top() post topElementRemoved: top() <> self@pre.top() context Stack::top(): Object pre notEmpty: isEmpty() = false context Stack::push(anObject: Object): void post pushedObjectIsOnTop: top() = anObject
A more typical example
And typical constraints... No employee may be older than 65 years OCL equivalent context Company inv: self.employee->forAll(age <= 65) And what about this one? What does it mean? context Company inv: employee->exists(isUnemployed) = false
More examples in the PDF
Tools Octopus : Plug-in for Eclipse OCLE: (syntatical checking on class diagrams, cool editor and generates java code!) http://www.klasse.nl/english/research/octopus-intro.html OCLE: From Romenia, syntantical checking, model checking, comes with full UML meta-model and well-formdness rules for diagrams consistency in OCL! Generates java code) http://lci.cs.ubbcluj.ro/ocle/overview.htm OCL-AddIn for Rational Rose, similar to Octopus but do not generate code. http://www.empowertec.de/products/rational-rose-ocl.htm
Conclusions and further motivation UML without OCL is only useful for extreme programming, disposable models OCL is key for formal methods, UML models as first-class, strategic, reusable artefacts and (future) automated code generation
References http://www.omg.org http://www.empowertec.de Presentation Object Constraint Language (OCL) by Tevfik Bultan (www.cs.ucsb.edu/~bultan/courses/272/OCL.ppt) Introduction to OCL, Andreas Roth (i12www.ira.uka.de/~aroth/ lehre/keypraktWS0304/oclIntro.pdf)