The MoDisco query component allows executing model queries independently of any technology. To offer this feature, the component provides a meta-model to describe queries and a set of tools to manage the queries and their descriptions.
MoDisco provides drivers for Java, OCL and JXPath. Other drivers can be written for other query technologies.
The first step is to create a MoDisco project.
Then right-click on the MoDisco project and select New > Other ....
Select QuerySet Model and press the Next button
Choose a name for your model query set ( My.querySet in the example) and press Finish.
Open the "My.querySet" file and open the "Properties View"
Set the description of the query set and make sure that the query set name is the same as the containing file name ("My" in the example).
Right-click on a query set, and choose Create Query.
This opens the Create Query wizard, with the parent query set already selected.
Fill in the query name, description, scope and return type. Then click Finish.
A Java editor opens on the Java class that was created for your query. Complete the "evaluate" method body with your code:
Then do File > Save in the query set editor.
The wizard does everything for you, but it is still interesting to know how to create and edit a query manually. Also, the wizard only handles creating Java queries for now, so if you want to create an OCL query for example, then you have to do it manually.
Load the resources containing the meta-models used by the model queries that will be owned by the query set. To load a meta-model resource use the Load meta-model resource action in the query set editor.
In this example, we choose to use the KDM meta-model.
Fill the "Associated Metamodels" field with the ePackages containing the meta-models used by the model queries that will be owned by the query set.
In this example we use the "core" package.
Right click on the ModelQuerySet element and choose the New Child > Java Model Query action to create a "Java Model Query".
Set the name, the description and the return type of the query. Set the "Implementation Class Name" field with the qualified name of the class that will contain the query's Java implementation.
Fill the "Scope" field with the meta-class to which the query will be applicable.
A MoDisco project is also a "Plug-in project". To be able to refer to the API of the used meta-model, we have to add the "org.eclipse.modisco.infra.query.core" plug-in and the meta-model implementation plug-in to the plug-in dependencies.
Create the query's implementation class.
The Java Query implementation class must implement org.eclipse.modisco.query.core.java.IJavaModelQuery.
If a query set model is valid, it is registered in the model query set catalog. To consult the model query set catalog, we can use the "Model Query Set" view.
To get this view select Windows > Show view > Other ... in the menu bar and choose "Query Sets" in the list.
The "Query Sets" view presents the available query sets and their queries. And for each query, its description and model location are displayed.
A JXPathModelQuery is a ModelQuery describing a JXPath expression.
The JXPath expressions are stored into the JXPathModelQuery::query attribure.
The jxpathQueryTest.querySet model provides an example of a JXPath query. The JXPathQuery stored into jxpathQueryTest.querySet returns the name of the EPackage passed in the query context.
If you want to log errors or messages in a Java query, you can use the MoDisco logger, like this (where Activator is the class that is already generated by default in your MoDisco project):
MoDiscoLogger.logError("Error message", Activator.getDefault());
MoDiscoLogger.logError(exception, Activator.getDefault());
MoDiscoLogger.logInfo("Error message", Activator.getDefault());