How to programmatically use the MoDisco query set catalog

Here a MoDisco query API example is provided. For more information please refer to the JavaDoc.

import org.eclipse.emf.ecore.EObject;
import org.eclipse.modisco.infra.query.ModelQuery;
import org.eclipse.modisco.infra.query.ModelQuerySet;
import org.eclipse.modisco.infra.query.core.AbstractModelQuery;
import org.eclipse.modisco.infra.query.core.ModelQuerySetCatalog;
import org.eclipse.modisco.infra.query.runtime.ModelQueryResult;

public class Example {
  public Integer main(EObject context) throws Exception {
    // Get the model query set catalog.
    ModelQuerySetCatalog catalog = ModelQuerySetCatalog.getSingleton(); 
    // Get the query set named "My".
    ModelQuerySet modelQuerySet = catalog.getModelQuerySet("My");
    // Select in the "My" query set a query named "myQuery".
    // modelQueryDescription is a model element.
    ModelQuery modelQueryDescription = null;
    for (ModelQuery modelQuery : modelQuerySet.getQueries()) {
      if (modelQuery.getName().equals("myQuery")) {
        modelQueryDescription = modelQuery;
        break;
      }
    }
    if (modelQueryDescription == null) {
      throw new Exception();
    }
    //Get a java instance of the querySet
    AbstractModelQuery myModelQuery = catalog
        .getModelQueryImpl(modelQueryDescription);
    //the model query set evaluation
    ModelQueryResult result = myModelQuery.evaluate(context);
    if (result.getException() != null) {
      throw new Exception();
    }
    return (Integer) result.getValue();
  }
}

How to package a query set in a plug-in

To package a query set in a plug-in, an extension must be added in the file plugin.xml (contained in the query set's project). The extension point to use is: org.eclipse.modisco.infra.query.registration. Here is an example of a query set declaration:

 <plugin>
    <extension
          point="org.eclipse.modisco.infra.query.registration">
       <modelqueryset
             file="_example_jdkAndEclipseQuerySet.querySet">
       </modelqueryset>
    </extension>
 </plugin>

Thanks to this extension declaration, The MoDisco project is ready to be exported as a plug-in.

Query Meta-model Description

ModelQuerySet is the root of the query model. One root per query model is expected.

ModelQuerySet::name must be unique (in the Eclipse platform) and equal to the containing file name. We recommend to use a name prefix with a namespace, for example: org.eclipse.modisco.example1.querySet1.

ModelQuerySet::isEditable is true if the query can be edited. If the query is stored into a compiled file then ModelQuerySet::isEditable is false.

ModelQuerySet::getQuery(EString) returns the contained ModelQuery having the requested name.

ModelQuerySet::associatedMetamodels points to the meta-models used by the contained queries.

ModelQuerySet::queries refers to the contained queries.

ModelQuery is an abstract class representing a query.

ModelQuery::name is the query name. In each query set the query names must be unique.

ModelQuery::parameters a set of ModelQueryParameter instances describing the query parameters.

ModelQuery::returnType is an ecore::DataType representing the query return type.

ModelQuery::scope is the set of types on which the query is applicable. Those types are represented by ecore::DataType instances.

ModelQueryParameter is a class representing the query parameters.

ModelQueryParameter::type is an ecore::DataType representing the query parameter type.

ModelQueryParameter::name is the parameter name. In each query the parameter names must be unique.

ModelQuery::isExternalContextDependent must be true if two evaluations of the same query with the same context and parameters can return different results.

JavaModelQuery is a sub class of ModelQuery which points to a Java implemented query.

JavaModelQuery::implemenationClassName contains the qualified name of the class implementing the query. This class must be a sub class of org.eclipse.modisco.infra.query.core.java.IJavaModelQuery. The Java implementation class must be stored in the same plug-in (or plug-in project) as the query model.

OCLModelQuery::query is the OCL query string.