ModelJoin/Getting Started
Introduction
ModelJoin is a textual SQL-like language written in xText which allows users to specify queries over already defined meta-models. The queries generate a QVT transformation which allows for applying queries on actual model instances. In this step by step tutorial we will show how to create your own meta-models, specify actual instances of those, and use ModelJoin to define queries on them.
In order to get started, first make sure you have already installed the following:
- A running version of eclipse for java – we recommend the “eclipse for modeling” release as it comes with pre-installed modeling tools
- Modeling plugins: Xtext, QVT-Operational and Palladio
- The ModelJoin Plug-Ins (see ModelJoin/Setup)
Detailed installation instructions for all necessary dependencies can be found here: ModelJoin/Setup
The tutorial will assume you are working with the modelling version of eclipse. The first two modelling plugins can be found under Help/Install Modeling Components. Palladio can be installed through the following update site: http://sdqweb.ipd.kit.edu/eclipse/workflowengine/releases/2.0.3/
Creating a meta-model
The scenario is as following: Our first meta-model will represent restaurants with the food they sell and our second meta-model will define review pages for restaurants. We will construct a new meta-model with ModelJoin which will represent the restaurants associated with the average rating taken from the user reviews. The class diagrams of the input meta models are shown in Figure 1 and 2. Firstly, create a new empty EMF project, by right-clicking: New/Project/Eclipse Modeling Framework/Empty EMF Project. Name the Project RestaurantModel.
Next, we will create a new meta-model. The meta-model should be located in the model folder which is already available for us. To do this, create a new package by right-clicking, select New/Other/Eclipse Modeling Framework/Ecore Model. We will name the meta-model 'restaurant.ecore' and set the Model Object field in the next window to EPackage. Afterwards, click on the newly created package and fill out its properties as shown in the Properties tab. Your eclipse instance should look like in Fig 3. Note that the meta-model attributes were also filled in, that is the name and namespace of the meta-model.
Next, we will add classes and attributes to our meta-model. The meta-model will consist of two classes Restaurant and Food. Restaurant will contain basic information about restaurants and Food will represent various kinds of food that is sold in them. See Fig 4. for the resulting meta-model.
These two components can be added by right-clicking on the meta-model icon and selecting New Child\EClass. The properties of the components can be set through the properties panel below. For the classes (EClass) you have to fill out the Name property. For the attributes, fill out Name and EType. And for the EReference reviews, fill out Name, EType, and Lower Bound (0) as well as Upper Bound (-1) to make it an 1:* relation. Fill out the same fields for the Food class.
In order to generate a Java implementation of the meta-model we will need to add a .genmodel file. This is done by right-clicking on our meta-model in the project explorer and selecting New/Eclipse Modeling Framework/EMF Generator Model. If asked for a Model Importer, choose Ecore model and if asked for a model URL, choose platform:/resource/RestaurantModel/model/restaurant.ecore and load it afterwards. Open the restaurant.genmodel file and select the Restaurant node. Set the base package property to RestaurantModel. Now right click the top-level node and select Generate Model Code. You can now find the generated code inside the src folder.
We can define the meta-model for our review webpage analogously. Fig. 5 shows the corresponding meta-model. We have a base class ReviewPage which contains the restaurant name and reviews from different users, and the Review class representing the actual reviews.
Use the interactive ModelJoin editor
To use the interactive ModelJoin editor, launch an Eclipse target instance. Import the example project from the SVN or create a test project of your own:
- Create new Java project
- Create an *.mj file with your example query
- If asked, let Eclipse add the Xtext nature to the project
- Add a folder src-gen. The target meta-model and the transformation will be created in this folder. (This is hard-coded at the moment).
- Play around with the query and have fun. :-)
Create the restaurant example
For this example, you need to start a new target instance to make sure that the Eclipse plugins are loaded. To start, we will create a new Java Project inside the target instance's workspace named ModelJoinTest that contains our example. Inside the project, create a new folder named src-gen. The src-gen folder is the predefined destination where ModelJoin saves the generated meta-model and model-transformations. As we're working with our meta-model, make sure that the RestaurantModel project as well as the ReviewPageModel project are in the workspace. If they are not, import both of them by clicking File/Projects/Import/General\Existing Projects into Workspace . Now create the file query.mj inside the src folder. Note that .mj is the extension for ModelJoin files. If you are asked to add Xtext nature to your project, click on yes. We will write a ModelJoin query which will generate a meta-model which contains all restaurants with their corresponding average ratings over all reviews. Restaurants with no reviews will not be listed. The query is shown on Figure 6 and listed below.
import "platform:/resource/RestaurantModel/model/restaurant.ecore"
import "platform:/resource/ReviewPageModel/model/reviewpage.ecore"
target "http://mjtest/test"
natural join restaurant.Restaurant with reviewpage.ReviewPage as jointarget.RatedRestaurants {
keep aggregate avg(reviewpage.Review.rating)
over reviewpage.ReviewPage.reviews as RatedRestaurants.rating
keep outgoing restaurant.Restaurant.sells
}
The resulting meta-model is automatically generated (i.e. after saving) and can be found in the scr-gen folder, along with the QVT transformation for generating actual model instances.