The OCL implementation provides support for models defined using either the Ecore or the UML metamodel (as implemented by the Eclipse EMF and UML2 projects), and an extensibility API that allows additional EMF-based metamodels to be plugged in.
The direct and indirect coupling of the Ecore and UML2 meta-models to Ecore makes exact compliance with the OMG specification very difficult, particularly in the area of reflection. Eclipse OCL is therefore migrating to a new potentially 100% OMG compliant Pivot metamodel that hides the differencess between OMG’s UML and EMOF and Eclipse’s UML and Ecore. The Pivot binding is described in the Pivot Programmers Guide.
The OCL API implements support for different target metamodels via the
EnvironmentFactory
interface. An implementation of this interface binds the metamodel’s metaclasses to the generic type parameters of the
OCL
class. The metamodel-specific
Environment
implementation constructed by this factory implements the reflection capability required by OCL to discover the elements of the model being constrained and the relationships between them.
An OCL binding for the Ecore metamodel is provided by the
org.eclipse.ocl.ecore
plug-in. It is best suited to parsing and evaluating OCL constraints on Ecore models. Evaluation of constraints is supported on instances of the EMF-generated Java API (Ecore as the source for the genmodel) and on dynamic EObjects.
As is illustrated by most of the examples in this documentation, the Ecore binding is provided by the
EcoreEnvironmentFactory
class. By default, the Ecore environment uses the static
EPackage
registry to look up package names. It can also be supplied with an alternative package registry (for example, one local to a
ResourceSet
) but it will always use the static registry as a backup. Aside from the package registry, the Ecore environment factory maintains no state. So, when the shared registry is to be used, the static
EcoreEnvironmentFactory.INSTANCE
is most practical.
The Ecore binding for OCL provides the following capabilities, reflecting the subset of Ecore’s modeling constructs with respect to UML:
Capability | Parse | Evaluate |
---|---|---|
Classifier invariant constraints | Y | Y |
Operation precondition and postcondition constraints and body conditions | Y | N |
Property constraints (initial-value and derivation) | Y | Y* |
Attribute and operation definitions (def: expressions) | Y | Y |
Package context declaration | Y | n/a |
Basic values and types, mapped from the standard EDataTypes to OCL’s primitive types | Y | Y |
Collection types | Y | Y |
Navigation of attributes and references | Y | Y |
Operation invocation | Y | Y |
Iteration expressions (all standard iterators) | Y | Y |
Let expressions | Y | Y |
If expressions | Y | Y |
Tuples | Y | Y |
Message expressions, including unspecified values | Y | N |
Operations predefined by OCL: allInstances() | Y | Y |
Operations predefined by OCL: oclIsKindOf(), oclIsTypeOf(), oclAsType() | Y | Y |
Operations predefined by OCL: oclIsNew() | Y | N |
@pre expressions | Y | N |
* derivation only |
Ecore metamodel capability matrix
Because Ecore does not define analogues of some of the UML metaclasses required by the OCL Abstract Syntax Model, the Ecore binding defines these on its behalf, in the
platform:/plugin/org.eclipse.ocl.ecore/model/OCLEcore.ecore
metamodel. These include:
Constraint
: the model of an OCL constraint (when the
language
is OCL)
CallOperationAction
: used in the model of message expressions
SendSignalAction
: used in the model of message expressions
ExpressionInOCL
: it is this metaclass’s general class
OpaqueExpression
that Ecore does not define. It is elided in the Ecore binding
State
: Ecore provides no behavior modeling capabilities. The Ecore binding simply substitutes
EObject
For applications that work exclusively with the Ecore binding for OCL, the
org.eclipse.ocl.ecore
package defines a subclass of the
OCL
class that supplies all of the generic type parameter bindings to simplify typing (in the absence of type aliasing in Java). It also provides Ecore-specific convenience factory methods for the
OCL
, itself, and narrows the return type of the factory methods for the
OCLHelper
and
Query
interfaces. These specialized interfaces likewise supply the generic type parameter bindings for Ecore.
An OCL binding for the UML metamodel is provided by the
org.eclipse.ocl.uml
plug-in. It is best suited to parsing and evaluating OCL constraints on UML models. Evaluation of constraints is supported on instances of the UML2-generated Java API (UML as the source for the genmodel), on dynamic EObjects (using an Ecore model created by the UML-to-Ecore converter), and on
InstanceSpecification
elements in the UML model.
The UML binding is provided by the
UMLEnvironmentFactory
class. By default, the UML environment factory and all of the environment contexts that it creates use a private
ResourceSet
to look up the corresponding UML model(s) against which OCL constraints are parsed.
It is the client’s responsibility to ensure that the UML model is loaded in the resource set used by the UML environment factory instance.
The UML environment factory can alternatively be initialized with a resource set of the client’s choosing. Ordinarily, the UML environment uses its resource set’s local
EPackage
registry to look up EMF-generated
EPackage
names corresponding to UML models. A custom package registry may be provided by the client if necessary.
The UML binding for OCL provides the following capabilities:
Capability | Parse | Evaluate |
---|---|---|
Classifier invariant constraints | Y | Y |
Operation precondition and postcondition constraints and body conditions | Y | N |
Property constraints (initial-value and derivation) | Y | Y* |
Attribute and operation definitions (def: expressions) | Y | Y |
Package context declaration | Y | n/a |
Basic values and types | Y | Y+ |
Collection types | Y | Y |
Operation invocation | Y | Y- |
Navigation of attributes and references | Y | Y |
Navigation of non-navigable association ends (including those that are owned by the association) | Y | Y |
Qualified association end navigation | Y | Y= |
Navigation to association classes, including source qualifiers | Y | Y= |
Iteration expressions (all standard iterators) | Y | Y |
Let expressions | Y | Y |
If expressions | Y | Y |
Tuples | Y | Y |
Message expressions, including unspecified values | Y | N |
Operations predefined by OCL: allInstances() | Y | Y |
Operations predefined by OCL: oclIsKindOf(), oclIsTypeOf(), oclAsType() | Y | Y |
Operations predefined by OCL: oclIsInState() | Y | N |
Operations predefined by OCL: oclIsNew() | Y | N |
@pre expressions | Y | N |
* derivation only | ||
+ OCL defines the Real primitive type that is missing from UML, but not a LiteralReal | ||
- with InstanceSpecifications, only where body constraints are defined | ||
= only with InstanceSpecifications |
UML metamodel capability matrix
A special case of the UML environment’s support for dynamic EObjects, mentioned
above, is stereotype applications. The Eclipse UML2 component uses dynamic
EMF in the implementation of stereotype applications, by converting UML
Profile
s to
EPackage
s.
Constraints parsed in the context of a UML
Stereotype
can be evaluated on applications (instances) of that stereotype or on model
elements to which the stereotype is applied. This applies only to UML models,
themselves, as instances of the UML metamodel (stereotyping is only available
in the UML metamodel).
For applications that work exclusively with the UML binding for OCL, the
org.eclipse.ocl.uml
package defines a subclass of
the
OCL
class that supplies all of the generic type
parameter bindings to simplify typing (in the absence of type aliasing in Java).
It also provides UML-specific convenience factory methods for the
OCL
, itself, and narrows the return type of the factory
methods for the
OCLHelper
and
Query
interfaces. These specialized interfaces likewise
supply the generic type parameter bindings for UML.
h2. Content Assist Support
The Content Assist facilities described here are used by the Interactive OCL Console. They are not used by the new Xtext-based Editors or the Interactive Xtext OCL Console.
The
OCLHelper
API provides support for content-assist in rich editors, by parsing partial OCL
expressions and supplying completion suggestions. The
List<Choice> getSyntaxHelp(ConstraintKind, String)
operation
returns a list of suggestions for the next token to follow the end of the
expression fragment.
The
Choice
objects returned by the helper include some convenient text strings (name and
description) to formulate basic JFace content-assist proposals. Each choice
also carries a reference to the element that it represents, the kind of element
indicated by the
ChoiceKind
enumeration, for a more
sophisticated content assist that might inlude context information, documentation,
etc. as in Eclipse JDT. The list of choices depends in part on the kind of
constraint expression that is to be completed, as for example, the
oclIsNew()
operation is only permitted in operation
post-conditions.
helper.setContext(EXTLibraryPackage.Literals.BOOK);
List<Choice> choices = helper.getSyntaxHelp(ConstraintKind.INVARIANT,
"Book.allInstances()->collect(author)->");
for (Choice next : choices) {
switch (next.getKind()) {
case OPERATION:
case SIGNAL:
// the description is already complete
System.out.println(next.getDescription());
case PROPERTY:
case ENUMERATION_LITERAL:
case VARIABLE:
System.out.println(next.getName() + " : " + next.getDescription();
break;
default:
System.out.println(next.getName());
break;
}
}
The computation of
Choice
s is supported for the
following tokens, which may be used by a client as auto-assist triggers:
Token | Completion choices |
---|---|
.
|
Features applicable to the type of the expression to the left of the dot, or its element type if it is a collection. association classes (in the UML environment only) |
->
|
Collection operations and iterators |
::
|
Packages, types, enumeration literals, and states (in the UML environment only) |
^
|
Operations and signals (in the UML environment only) |
^^
|
|
In other situations, the choices the current context variables and implicit references to features of the
self variable. For example, if the input is something like
"" or
"self.isOrdered and "
|
Content-assist triggers
The completion of partially specified identifiers is also supported, by backtracking to look for one of these triggering tokens. This supports interactively narrowing the choices while the content-assist window is active.
choices = helper.getSyntaxHelp(ConstraintKind.POSTCONDITION,
"self.author.oclIs");