By Pierre-Charles David on Monday, 02 May 2022
Category: Blog

Using Java Services in Sirius Modelers

When creating your own graphical modeler using Eclipse Sirius Desktop, you need to tell Sirius:

This is done using a combination of interpreted expressions (in general using AQL) and model operations (for the tool's behavior) that can be configured directly inside the modeler’s configuration (the .odesign file).

For example, on the first screenshot below the interpreted expressions (always presented with a yellow background in the properties view) configure an edge mapping by telling Sirius how to navigate inside the user’s model to find the model objects to be represented as edges (computed from the model with the Semantic Candidate Expression) and which elements it should connect on the diagram (computed with the Source Finder Expression and the Target Finder Expression).

In the second screenshot, another AQL expression is used to configure a Move operation as part of the definition of the behavior of a Delete tool.

 

You can go a long way just using only these mechanisms, but for more advanced modelers these tools can become limited compared to the power of a full-blown programming language like Java. Fortunately, Sirius makes it very easy to invoke your own Java code from inside your odesign file, and thus greatly expands the range of things your modelers can do. Java services are plain Java methods that follow a specific calling convention and which, once they are registered in your modeler, can be invoked transparently from any AQL expression.

For example Flow Designer defines the following simple Java service:

public int sizeFromCapacity(Processor p) {
return Double.valueOf(Math.sqrt(p.getCapacity()) * 2).intValue();
}

and then uses it in the Size Computation Expression in some elements’ style configuration with aql:self.sizeFromCapacity(). The result is that the size of the processor elements on a diagram is recomputed by invoking this Java code on each diagram refresh, and thus always adapts to the changes in their capacity attribute.

 

Typical use-cases for Java services include:

Java services can be used anywhere you can place an AQL expression inside your odesign. In the odesign editor, they can be easily identified by the yellow background on the corresponding text fields in the properties view (see the screenshots above). This includes things like Semantic Candidate Expression used to find the model elements to display, preconditions or conditional styles, etc. Inside a tool’s body, if you need to call a Java service to perform the required model transformation, the idiom is to use the Change Context operation with an expression that invokes your service. For example to support the direct edition (the possibility for the user to change the label directly from the diagram) of two different attributes (name and capacity) of a Flow element, Flow Designer delegates the tool’s implementation to a custom Java service like this:


Writing and using a Java service

To create your own Java services:

  1. Create a Java class inside your modeler project. The class should be public and have a default constructor (implicit or explicit) with no arguments.
  2. Add your service as public methods following the required calling convention (see below).
  3. Register the class inside your odesign file using a Java Extension element with the fully qualified name of the class.
  4. Inside your odesign, invoke your service from an AQL expression like any of the standard AQL services.

If you have used the Viewpoint Specification Project wizard to create your modeler, you are actually already set up, as it automatically creates and registers an example service ready to be filled in:

 

Of course, if you end up having lots of services, you can split them into different classes. Just make sure they are all registered in the odesign.

The rules for writing a service method are simple:

Once written and properly registered on a Viewpoint element in your odesign model, you can invoke your Java services from any interpreted expression inside that viewpoint.

There are a few things to be aware of when using Java services inside a Sirius modeler:

In this article, we showed that it’s very easy to extend the capabilities of a Sirius modeler by calling into Java code. This is very easy to do and opens up a lot of possibilities, in particular on the behavior of the tools. To go further, you can follow the Advanced Tutorial which includes a section with step by step instructions to add a Java service to the sample modeler, refer to the reference documentation for more details, or study more advanced use cases in Ecore Tools, UML Designer or even Capella to get concrete examples to take inspiration from.

If you have any questions, do not hesitate to ask on the Sirius forum and we will do our best to guide you.

 

Related Posts