As we saw in the
Parsing Constraints and Queries topic, the OCL parser provides an
OCLHelper
API for parsing constraints embedded in models. OCL also permits constraints
to be specified in a text document, as an adjunct to the model. In this case,
the concrete syntax for context declarations indicates the context of
constraints, equivalent to their placement in models.
As an example, consider the following Complete OCL document: "
The import on the first line is an extension supported by the Complete OCL editor for use with the Pivot meta-model. The import is ignored by the parsers for the Ecore or UML bindings, which assume that the relevant metamodels have been registered in either the global EPackage.Registry or the local EPackage.Registry passed to the EnvironmentFactory..
The
OCLInput
class encapsulates an OCL document. An input can be created from a string or an
input stream.
Given an
OCLInput
, simply ask an
OCL
to parse it: "
The
OCL
returns the list of constraints if they were
successfully parsed. They are retained by the OCL (available via the
getConstraints()
method at any time), and in particular,
any definitions of additional operations or attributes are available for
subsequent constraint parsing. Any number of OCL documents may be parsed by
the same
OCL
instance, combined also with constraints
parsed by
OCLHelpers
. All of these constraints are
retained by the
OCL
environment.
The source for these examples may be found in the org.eclipse.ocl.ecore.tests plugin in model/parsingDocumentsExample.ocl and in src/org/eclipse/ocl/ecore/tests/DocumentationExamples.java.