Writing Queries and Interpreted Expressions

  1. Writing Queries and Interpreted Expressions
    1. Introduction
    2. General Rules
    3. Using the Specialized interpreters
    4. Using Acceleo
    5. Using Raw OCL
    6. Providing a Custom Language
    7. Writing Java Services

Introduction

Many parts of a VSM require you to provide interpreted expressions, which will be evaluated at runtime to provide a behavior specific to your domain and representations. Some of these expressions return model elements (in which case we call them queries), while others simply produce text (for example the expressions used for labels), but the principles stay the same.

Sirius does not force the use of a specific language to write these queries. Currently, three different languages are provided by default, and if you have specific needs, you can extend the system and provide your own. You can use several different languages inside the same VSM (but only one in each expression).

Whatever the language(s) you use, because they are all optional from Sirius’s point of view, make sure the Viewpoint Specification Project which contains your VSMs explicitly depends on the Sirius plug-in which provides supports for these languages. Otherwise when you modelers are deployed to end-users you can not be sure the proper support will be available. The Viewpoint Specification Projects which are created using the standard wizard are pre-configured to use Acceleo 3. See the sections below for each of the standard languages for the name of the Sirius support plug-ins to depend on if you use the language.

General Rules

Some general rules are independent of the language details.

Auto-completion. First, in the VSM editor, any field in the property view which has a yellow background is an interpreted expression. In these fields, auto-completion can be triggered by hitting Ctrl+Space.

The completion will correspond to the empty expression for the installed query language (e.g. [/] for Acceleo 3). It will also place the cursor at the expected place where it is ready to start typing the expression.

When hitting auto-completion on an empty expression, the first completion proposal will correspond to the variables which are available in the expression’s context, and the rest correspond to all the features, services, etc. which are available on the current element.

Sometimes Sirius is not able to statically determine the precise type of the model elements on which an expression will be evaluated. In that case, it assumes a plain EObject. This means you will not get completion proposals specific to your custom types, and that if you use features or services which are not available on EObject, the VSM’s validation will fail. To prevent this, if you know the precise type of the elements and the language supports it, add a cast operation to tell Sirius the actual type (e.g. [filter(ExpectedType).theRestOfTheExpression/] if using Acceleo 3.

The Interpreter view. The Interpreter view can be used to develop complex expressions interactively. The view can work in either the native Acceleo mode (corresponding to Acceleo 3/MTL) or in the Sirius mode. In the Sirius mode, you can use any of the languages supported by Sirius, but you lose the benefits of syntax coloring and auto-completion.

To use the Interpreter view, simply open a Sirius representation, select and element in it (for example a shape on a diagram), and type some expression in the view’s upper text field. The result of the expression (a set of model elements or a string) will appear in the lower part of the view. If you select a different element, the result will automatically be re-evaluated in the context of the new element.

Warning: When using the Interpreter view from an element selected in a Sirius representation, the context of the expression is not the semantic element, but the view model element used internally by Sirius. To access the semantic element you must use the view’s target reference. More concretely, imagine you have a Sirius diagram representing UML classes with nodes. If you select a class and enter [self/] in the interpreter view, the result will not be a UML Class element, but a DNode (the type used internally by Sirius to represent graphical nodes). To get the UML class, you must enter [self.target/]. Keep this in mind when using the Interpreter view to develop expressions you want to use inside a VSM: most expressions defined in the VSM will be evaluated in the context of a semantic element, so you will need to add/remove target references when switching between the two.

Determinism. In general, you should try to ensure that your queries are deterministic, i.e. from the same input model they should always return the same result, and in the same order (if returning collections of elements). Otherwise, each time a representation is refreshed it might become dirty (as Sirius will see the order difference as a change) or even visually unstable (i.e. elements shown in a different order). There are several things to be aware of in order to ensure determinism in your queries:

Using the Specialized interpreters

Sirius provides several specialized interpreters to handle more efficiently some forms of simple expressions which can occur very often in a typical VSM. Each interpreter focuses on a very limited case, and none supports complex expressions. This allows them to be very fast, but it requires that you use them explicitly when possible. All these interpreters are available by default in Sirius, you do not need to declare additional dependencies in your VSM project to be able to use them.

Using Acceleo

Acceleo is the recommended language to use for new VSMs. It provides better static validation than the rest, and more precise auto-completion when editing your expressions. Viewpoint Specification Projects created with the default wizard will be already setup to use it. Otherwise you need to add a dependency to the org.eclipse.sirius.common.acceleo.mtl plug-in to ensure Acceleo support will be available wherever your modelers are used.

Acceleo expressions are enclosed in brackets: [theExpression/]. Inside the brackets you can write any valid Acceleo expression, including using if and let statements, for example. See the Acceleo documentation for the exact syntax and semantics of the language.

Note that currently Acceleo expressions used inside VSMs must be enclosed inside a single bracket. It is not currently possible to mix Acceleo expressions and fixed text (like "prefix[someExpression/]suffix"). Instead, you can use Acceleo’s string manipulation operations to obtain the same result: [‹prefix› + someExpression + ‹suffix›/]). Related to this, auto-completion only works if you are inside a well-formed bracket (i.e. [<cursor>/]); if you have simply opened the bracket but not closed it (i.e. [<cursor>), completion will not be available.

Acceleo expressions can transparently invoke methods from Java classes which follow the service methods conventions and have been properly declared in the VSM. Note that currently this only works if the Java service class is in the same project as the VSM. You can also invoke Acceleo queries defined in .mtl files in your Viewpoint Specification Project. For this to work, your Viewpoint Specification Project should be an Acceleo Generator project. This is the default for projects created with the wizard. If you want to use queries from .mtl files in you Sirius project, the parent Viewpoint element must have a Java Extension element which references the MTL file, using the com::example::domain::design::module syntax. Also make sure to read http://www.obeonetwork.com/page/building-an-acceleo-generator to ensure the project is built correctly, or the queries defined in the .mtl files may not be available when the project is deployed as a plug-in.

Acceleo is more precise than the legacy language, but also more demanding, about the types of elements your queries are executed on. This generally means you get better auto-completion and more useful validation and diagnostics. However sometimes Sirius is not able to statically determine the precise type of the model elements on which an expression will be evaluated. In that case, it assumes a plain EObject. If you know the actual type which will be used, prefix your expression with filter(ExpectedType) to help Sirius. Note that if the actual type at runtime is not compatible with ExpectedType, the rest of your expression will silently be ignored.

In the context of Sirius, you have access to a special feature which can be used to follow «back-links» or «cross-references». From a given model element, this allows you to easily (and efficiently) find all the elements which refer to it in the scope of the models and representations in the same modeling project. This feature is available through the eInverse() method, which can be used on any model element inside an Acceleo expression.

Note that in most cases, expressions in Sirius are evaluated in a context where variables are defined. For example, most expressions inside tool definitions have access to variables telling them which elements the tool has been applied on. In some cases, the names of these variable can conflict with names of features in your meta-model. The evaluation rules of Acceleo (OCL actually) give precedence to variables, so it is recommended to always prefix accesses to your meta-model features with self (or another expression) to avoid ambiguity. As a concrete example, say you have an expression "[target/]" that you expect to access the target feature of one of your object. If evaluated in a context where a target variable exist, the value of the expression will be the value of the variable, not the value of the object’s property. To avoid ambiguity, always use expressions of the form "[self.target/]" in such cases.

Using Raw OCL

Sirius also supports raw OCL expressions, but this support is deprecated and will be removed in future versions. OCL expressions must be prefixed with ocl:. If you use it your Sirius project must declare a dependency to the org.eclipse.sirius.common.ocl plug-in to ensure OCL support will be available wherever your modelers are used.

It is highly recommended that you use Acceleo, which implemented the MTL standard and is a super-set of the OCL language, instead of raw OCL.

Providing a Custom Language

Sirius allows you to provide your own language implementations, if you have very specific needs or want to reuse an existing custom language. Note that Sirius must be able to syntactically and unambiguously determine from an expression which language it is written in (to send it to the appropriate interpreter). The simplest way to achieve this is to define a prefix (like ocl: for OCL) so that expressions written in your language can be distinguished from expressions written in any of the others.

To provide your custom language, you must at least implement the org.eclipse.sirius.common.expressionInterpreter extension point, and provide an implementation of the org.eclipse.sirius.common.tools.api.interpreter.IInterpreter interface. The example below shows how the Acceleo interpreter is registered. The AcceleoMTLInterpreterProvider is the one which implements the IInterpreter interface.

   <extension point="org.eclipse.sirius.common.expressionInterpreter"
              id="org.eclipse.sirius.common.acceleo.mtl.AcceleoMTLInterpreter">
      <expressionInterpreterProvider
            interpreterProviderClass="org.eclipse.sirius.common.acceleo.mtl.business.internal.interpreter.AcceleoMTLInterpreterProvider" />
   </extension>

You can also optionally provide support for auto-completion for your language by implementing the org.eclipse.sirius.common.proposalProvider extension point and providing an implementation of org.eclipse.sirius.common.tools.api.contentassist.IProposalProvider interface. For example, here is how the completion support for Acceleo is registered ( AcceleoProposalProvider is the class which implements the IProposalProvider interface):

   <extension point="org.eclipse.sirius.common.proposalProvider">
      <proposalProvider
            class="org.eclipse.sirius.common.acceleo.mtl.ide.AcceleoProposalProvider"
            interpreter="org.eclipse.sirius.common.acceleo.mtl.AcceleoMTLInterpreter" />
   </extension>

Writing Java Services

Both Acceleo and the legacy language (which is an older, incompatible version of Acceleo) support the notion of Java services, which are methods written in Java that can be transparently invoked from interpreted expressions in Sirius. All you need to do is to create a Java class whose methods follow some conventions (described below) in your Viewpoint Specification Project, and declare the class (using its fully qualified Java name) in the VSM. You can then use the services defined in that class in any of you interpreted expressions written in either Acceleo or the legacy language.

A service is simply a public Java methods which follows some conventions:

Here is an example of a Java service taken from the com.obeodesigner.sample.family.design example (you can obtain the project’s sources using New... > Example... > Obeo Designer > Family Designer):

public class EObjectServices {
    public List<EClass> getEClasses(EPackage ePackage) {
        List<EClass> eClasses = new ArrayList<EClass>();
        // The service code. 
        return eClasses;
    }
}

Once a service has been defined and its class is registered in your VSM, you can invoke it in the expressions of languages which support it (Acceleo and the legacy language) like this: [aFamily.getFamiliesContainingParents()/]. The invocation looks as if the service was a normal feature of the Family type. When the service is invoked, the model element on which it is invoked is used as the first argument to the Java method. If arguments are passed in the expression, they are mapped to the second, third, etc. parameters of the Java method, assuming the types are compatible. The result of evaluating a service invocation is the result of the Java method.

Warning: Java service methods should be stateless. There is no guarantee that two successive invocations of the same service method on two model elements (or even on the same one) will use the same instance of the service class.

Warning: To test a Java service invoked through a legacy language expression, you have to launch an Eclipse runtime or to type its parameters with EObject and then cast them to your domain types in the service body.The legacy language interpreter is not able to retrieve non-deployed service with domain types parameters.

Warning: There are currently some limitations on the use of Java services from Acceleo: