Object Constraint Language
The Object Constraint Language is a declarative language describing rules applying to Unified Modeling Language models developed at IBM and is now part of the UML standard. Initially, OCL was merely a formal specification language extension for UML. OCL may now be used with any Meta-Object Facility Object Management Group meta-model, including UML. The Object Constraint Language is a precise text language that provides constraint and object query expressions on any MOF model or meta-model that cannot otherwise be expressed by diagrammatic notation. OCL is a key component of the new OMG standard recommendation for transforming models, the Queries/Views/Transformations specification.
Description
OCL is a descendant of Syntropy, a second-generation object-oriented analysis and design method. The OCL 1.4 definition specified a constraint language. In OCL 2.0, the definition has been extended to include general object query language definitions.OCL statements are constructed in four parts:
- a context that defines the limited situation in which the statement is valid
- a property that represents some characteristics of the context
- an operation that manipulates or qualifies a property, and
- keywords that are used to specify conditional expressions.
Relation
OCL and UML
OCL supplements UML by providing expressions that have neither the ambiguities of natural language nor the inherent difficulty of using complex mathematics. OCL is also a navigation language for graph-based models.OCL and MOF
OCL makes a Meta-Object Facility model more precise by associating assertions with its meta-elements.OCL and QVT
Of particular importance to Model Driven Engineering or model-driven architecture is the notion of Model transformation. The OMG has defined a specific standard for model transformation called MOF/QVT or in short QVT. Several model transformation languages like GReAT, VIATRA, or Tefkat are presently available, with different levels of compliance with the QVT standard. Many of these languages are built on top of OCL, which is the main part of the QVT-compliance.Alternatives
Being a rule-based validation language, Schematron may be considered an alternative to OCL. However Schematron works for Extensible Markup Language trees while OCL makes it possible to navigate MOF-based models and meta-models. In other words, OCL relates to UML or MOF similarly to how Schematron relates to XML.Being a model specification language permitting designers to decorate a model or a meta-model with side-effect-free annotations, OCL could be replaced by languages like Alloy. Automated OCL generation is in principle possible from natural language.
Constraint | OCL Equivalent |
The age of a person is not negative. | context Person inv: self.age >=0 |
A person is younger than its parents. | context Person inv |
After a birthday, a person becomes one year older. | context Person::hasBirthday post: self.age=self.age@pre+1 |
A Person has 2 parents at max. | context Person inv: self.parents->size<=2 |
After somebody has a child, his/her child-set is not empty, and it is larger than before. | context Person::getsChild post: self.childs->notEmpty and self.childs->size > self.childs@pre->size |
Only an adult can be owner of a car. | context Person inv: self.age<18 implies self.cars->isEmpty |
The first registration of a car can not be before it is built. | context Auto inv: self.registration>=self.constructionYear |
Every Person that has a car has at least one car which is younger than the Person. | context Person inv: self.cars->notEmpty implies |
Nobody can be his/her own parent. | context Person inv: self.parents->excludes |
There's at least one Person which owns a car. | context Person inv |