Live querying over UML models with EMF-IncQuery

Starting with EMF-IncQuery version, it is possible to formulate patterns which query non-trivial structural features of UML models such as qualified name, which didn't work before. Let's see this feature in action through a small example that validates UML models!

Install the UML support feature and add the UML support plugin to the dependencies of your plugin as described here.

Write a constraint pattern that matches when a class has multiple superclasses. For more details about writing validation constraints, refer to the documentation of the Validation Framework.

package org.incquery.uml.demo

import ""

 key = {"class"},
 severity = "warning",
 message = "$$ has multiple superclasses"
pattern multipleSuperclasses(class: Class) {
 N == count find superclass(class, _);
 check (N > 1);

private pattern superclass(class, superclass) {
 Class.superClass(class, superclass);

Notice that the Class.superClass path expression has an information marker saying:

The derived/volatile feature superClass of class Class used in the path expression has a surrogate query org.eclipse.incquery.uml.derivedfeatures.classSuperClass which will be used by EMF-IncQuery.

This means that this otherwise ill-behaving feature will be substituted with an IncQuery pattern which has the same semantics. This technique is called surrogate patterns.

Let's try out our newly created validator! Launch a runtime Eclipse, create an UML model, open it with the Sample Reflective Ecore Model Editor, right-click the editor contents and select EMF-IncQuery Validation > Initialize EMF-IncQuery Validators on Editor.


That's great, but clearly not too convenient for the end user. We can do much better if we write some glue code that initializes the validators automatically whenever a Papyrus diagram editor is opened! This Papyrus-specific mechanism is out of the scope of this blog post, but if you are intereseted, you can check it out here. To test this feature in the runtime Eclipse, create a Papyrus Model and create a new Class Diagram under it in the Model Explorer view.


Now you have live validation in your graphical UML editor! You can find the whole source code among the IncQuery examples.
This opens up many possibilities if your domain is UML - for example, you can also perform incremental transformation of your models. The CPS example demonstrates multiple methods of achieving this.