"Hello world" in Eclipse QVTo

Although QVT isn’t a brand new specification (its first version is from April 2008), it is very difficult to find basic information about it and about how to use it. Since I’ve been working with QVT (Relations and Operational Mapping languages) in the last few months, I’ll post some things about it – maybe it will be useful to some QVT users…

To start, I’ll present a brief tutorial of how to execute a simple transformation in Eclipse QVTo with a custom metamodel. Nowadays we have 2 options to execute a transformation in this scenario:

Install a plug-in metamodel in Eclipse and create the transformation.

Use the QVTo environment in an inner Eclipse instance.

Normally we should use the option 1, as a stable metamodel is a pre-requirement for a transformation project. In some projects you may correct the metamodel during the transformation development, but it is just a matter of regenerating a plug-in. The option 2 is more suitable for tests or unstable metamodels (whatever this means).

First of all, install Eclipse Modeling Tools and go to “Help->Install Modeling Components” and choose “Model to Model->Operational QVT”. Install it and restart Eclipse when asked.

To present the rest of the tutorial, I will start with the option 2 (Use the QVTo environment in an inner Eclipse instance). I will not discuss how to create a metamodel using EMF (an interesting step-by-step tutorial is available here). So, I will use a simple metamodel that I will use in future examples. And before we start, just notice that some steps may be executed differently from what is shown here. For instance, the step 8 (to execute the transformation) can be done using the “Run” menu. But the outcome is the same.

Create a new EMF Project (in “File->New->Project” and “Eclipse Modeling Framework->Empty EMF Project”). Create your metamodel or copy-it. I’ll use this metamodel in this tutorial.

Open the genmodel (it is normally opened automatically) and right click in its root and select “Generate All”. The source code for a basic model editor will be generated.

Go in the generated “plugin.xml” and select “Launch an Eclipse application” under “Testing” section. A new eclipse instance will be opened with the plug-in working. Leave the other Eclipse instance open (the new instance is running from it).

Now let’s create a transformation. In the new instance(edit) go in “File->New->Project” and select “Model to Model Transformation->Operational QVT Project”. Choose a name and select “Create a simple project”. In the next window select “Create artifacts in new QVT Operational Project” and then “Operational QVT Transformation” to create a basic transformation file.

This code only transform an A to B, using the same id and concatenating the “a” attribute to ” World!” to create the “b” attribute.

Create a new model to execute in the transformation. Go in the Project Folder in the Project Explorer and right click “New->Other” and “Example EMF Model Creation Wizards->ABC Model”. Select the model root object for the model (Root in the ABC model) and add three childs – on the window opened right click the “Root” element and select “new Child”. Use the “Properties View” to inform the needed data: right click in an element and select “Show Properties View”. (Don’t forget to save)edit

A: id=1, A=”Hello”

A: id=-1, A=”World”

C: id=1, C=”Something”

Let’s execute the transformation. Select the transformation file in the “Project Explorer” and go “Run->Transformation”. Select the transformation file. Select for the “In Source” the Root for the model you’ve just created as source. For the “OUT target” you can leave the default option. Click Finish.

Now you will see the result in the file defined as “OUT target”. Only one element inside Root will be defined, a B with and B=”Hello World!”.

As I said before, another option is to create a plug-in and use it, instead of initiating a new instance of Eclipse. This seems to be the recommended option.

Create a plug-in for a metamodel. To do this, execute the steps 1 to 3 above and in the step 4 select “Export Wizard” in the “Export” section of the “plugin.xml”. Select all elements, define a directory for its destination, and click finish.

Close Eclipse. Copy the files that were generated in the “plugins” (“eclipse/plugins”) directory of Eclipse. Restart Eclipse.

Now we can go back to Step 5 of the previous option (create a Model to Model Transformation) and create our transformation.

That’s it. But be careful: DO NOT add the metamodel in the project settings (“QVT Settings->Metamodel Mappings”). If you do this, you will get an empty result no matter what you do. This seems to happen because the metamodel registered in this option conflicts with the metamodel defined in the plug-in (they define different implementation for the metaclasses in EMF). For now, this option is only used to register the target metamodel when the source and the target metamodel are different. I expect in future version of QVTo we will be able to use the source metamodel without creating a plug-in (similarly to what happens in Medini QVT). However, when this options is available, probably you will have to see the result in an “unpleasant” textual representation – as there won’t be a plug-in. But it doesn’t seem to be a problem for developers…

I have one question. Have you any troubles adding source and target meta models in QVT Setiings so you decided to launch a new instance of Eclipse? Or you just wanted to point that you didn’t see what was the last comment about to not add anything in QVT settings?
I tried to launch my transformation from original eclipse instance and i am getting an empty target file.

Thank you. I found this post very useful. However, since I am very new to QVT I don’t understand how it exactly works. For now the problem I have with this code is that when I run it several times for the same output file, one more instance of Root is added to the target file after each execution. Of course, one solution is to use different output files for each execution or to remove the output file after each execution. But, is there any way to fix it in the code? For example, is there any way to clear the content of the target file before performing the transformation?

This problem seems to be a bug (see the information about it here and here). Apparently, it was fixed in QVTo 3.1.0 (at least this bug was corrected in 2011-06-27) – but I haven’t tried this new version yet… When I do, I’ll post it here.

in your example you create the meta-model first then you apply transformation. My question is: How I can import any UML meta-model i.e sequence , activity diagram … etc to transformation project instead of building it from scratch?

I mean

Is their any built-in standard UML meta-model in eclipse EMF as repository ? if yes How I can use it please ?

I’ve never used UML models in my transformations, but I think the Eclipse UML2 project has an EMF implementation of UML. I also heard that some people use the Papyrus meta-model in transformations. The use of Eclipse UML2 meta-model should be very straightforward – and probably the Papyrus too. Good luck!

I am new in QVT. I went through the steps provided however, I failed to find some of the mentioned steps in the tools I am currently use (medini QVT Version: 1.6.0.25263). I appreciate if you can support these steps of screen snapshots. Also it will be great if you advice me regarding the tool I am currently use. Thanks in advance.

You can’t use Eclipse QVTo in Medini QVT. Eclipse QVTo is an Eclipse plug-in to work with QVT-Operational. On the other hand, Medini QVT is a proprietary tool to work with QVT-Relations (a different language specified in the QVT standard). Even though Medini QVT is built using Eclipse, you can’t install plug-ins in it.

Therefore, you should install Eclipse (the Eclipse Modeling Tools package, specifically) if you want to use Eclipse QVTo – as I wrote in my post. Although the post was written using an older version of Eclipse, the steps should be the same nowadays. Nevertheless, if you have any problems following the steps, I will be glad to help.

Your article helped me a lot but I am facing one problem . I have Eclipse (version:Kepler Release) . I am unable to copy the metamodel that you have provided. Can you please help me on this . I am very new to QVT. I need a working simple example so as to get more clearer idea on it.

There are several options to copy the metamodel to your project. The easiest way is to drag and drop the ecore file (abc.ecore) to the project’s “model” folder. You can also copy the ecore file directly to the real “model” folder (in the file system). To do this, find the real folder – right click on the project created and see the folder on the item “Resource”, with the label “Location” – and copy the file.

If you still can’t copy the file, give more information about your problem.

1. In step 7 you have mentioned go to EMF model creation Wizard -> ABC model ,But when we try to execute this step it shows many models except the ABC model. How do i solve this?
2. Why and how is the use of QVT better? I mean why must one use QVT ?

1. If it does not appear, it means that the plug-in for the ABC metamodel is not correctly installed. Are you sure you executed the steps 2 to 4? And be sure to use the new instance of Eclipse (as described in step 4) for the other steps!

2. That’s a tough question! It is like asking why should I use Java and not another programming language… QVT Operational has some advantages, such as being an OMG standard, having a good open-source support tool (other languages also have nice tools), and having syntax and semantic similar to object-oriented languages. But it has some problems, for instance, not allowing a bi-directional transformation out of the box (you have to create the transformation for the other direction, if you need it). Furthermore, some people say it is easier to write transformation in QVTr or ATL, but I personally think it is easier using QVTo. So a more precise answer to your question is “depends”. You have to analyze and choose the transformation language that is better for your problem and you like the most… In here you can see a list of some languages you can choose from.

If you followed the previous steps correctly, you should be able to create an ABC model in the new instance of Eclipse. Be sure that in the original Eclipse instance there are 3 projects: “YourProjectName.edit”, “YourProjectName.editor” and “YourProjectName.tests”. If they were not created, be sure to select “Generate All” in step 3.

Thank you so much for your help. The example runs just perfectly fine and gives the desired output.
Thanks again for your such a wonderful article.

I have a last question to ask I have read in some paper that there are two kinds of implementations on QVT (operational Mapping)
1.Eclipse M2M
2.Smart Qvt

I would like to know which one are we exactly using in these. And what exactly does the output signify ( its clear to me that it is result of concatenation of two strings which is stored in B ). I was very much eager to understand the transformation part .

In this post I describe the use of the Eclipse M2M implementation for QVTo. About the transformation, it is a very simple example. It only creates a B object for each A with id > 0. The b attribute of B is the a atribute of A concatenated with the String ” World!”.

If you want to learn about the QVTo language I suggest you read the QVT specification. It is a very good start.

First of all thank you for the article its of great help to new learners of QVT.
I have a question to ask I have developed a small program of my own.How can i share the Ecore Diagram and metamodel with you . I need some Help on that.

I’m glad to help. However, I don’t have much time to dig into someone else’s code, so I really prefer that you make general questions – without sending me the metamodel and transformation. Having said that, my e-mail is fabio@levysiqueira.com.br.

1. Why is concatenation done using QVT coz there are many other simple ways to do it. ?
2. Where is the transformation i.e model to model transformation ?
3. I want to know how is Qi.e Query of QVT used in this article ?

1) This post only explains how you can configure and use Eclipse QVTo. It was not my intention to explain the QVTo language. That’s why I created a very simple example. I agree this transformation does not seem very useful – but the important part is the idea behind it. For example, you can use this idea to create a transformation that does a refactoring in a class model, or that transforms a petri net into a BPMN model (BPDM, in fact).

2) The transformation is described in step 6 (the QVTo code). I made an endogenous transformation (i.e., a transformation between models using the same metamodel), but you can easily create an exogenous transformation if you want.

Thanks, a very good tutorial. Everything works as explained. But at the last point you mentioned “DO NOT add the metamodel in the project settings”. I want to extend the transformation with two different metamodels (source and target). Is it still a bad idea to add the metamodels in project settings?

The last statement seems to be still true. If you start a new Eclipse instance, do not use the “metamodel mapping” setting as it makes the transformation generate an empty model.

I also tried to use the “metamodel mapping” setting in the original Eclipse instance. Even though it makes the warnings in the QVTo code disappear (i.e., it recognizes the metamodel), I couldn’t execute the transformation. The problem is that couldn’t select the “root” of the metamodel in the “Run->Transformation” screen. When executing the transformation, it shows the message “Invalid source URI ‘platform:/resource/QVTTest/A.abc’ for parameter ‘source'”. So, I think you still need to open a new instance of Eclipse OR create a plug-in and “install” it in your Eclipse.

Regarding your transformation, you need to create two plug-ins and “install” them in Eclipse – as you have two different metamodels.

first of all, thanks for this tutorial. I have been able to reproduce your example in a short time. After that, I have modified the initial ecore to include two subpackages inside the package ABC and distribute the classes between these two packages. Unfortunately I got the error “failed to resolve metamodel”. If I add the ecore in the settings, error disappears but the transformation is null. Is there anything different in the transformation that must be done??

As far as I know, there is nothing different you should do when using subpackages. The only thing you must always do when you change the metamodel is to reexecute the steps 2 to 4 (regenerate the plug-in and launch a new Eclipse instance). If you don’t do this, the Eclipse will use the old plug-in.

If you have done that, try to delete the folders created by the Generator Model (i.e., the “src” folder and the projects “ABC.edit”, “ABC.editor”, and “ABC.tests”), and regenerate it. As I said in the post, avoid adding the Ecore in the project settings. It does not seem to work correctly.

I have just found the solution. I have had to modify the modeltype sentence to call each particular subpackage, instead of the whole package, which means that I have to add a mdeltype sentence for each one of the subpackages involved in the QVT transformation.

Thank you for this Tutorial. It helped me so much. However, I still find a problem Concerning the step number 7 : I don’t get this sentence :
” Select the model root object for the model (Root in the ABC model) and add three child (use the “Properties View” for that: right click in an element and select “Show Properties View”).
A: id=1, A=”Hello”
A: id=-1, A=”World”
C: id=1, C=”Something” ”
Can you give me more clarifications or tell me how to “add three child”?
Thanks In Advance

I am trying to modify your example by removing a node, I googled and seems model.removeElement(self) should work. But when I type in this code, it does not find removeElement as one of the available functions. I am writing the code like below and changed nothing else:

First of all, the “removeElement” operation only works on a model – and not on a result. The result in your mapping is just a B object. So, you must do something like “source.removeElement(self);” (using my QVTo code in the example). I recommend you look in the QVT specification for the available operations and the explanation of how they work.

You are probably testing the QVT capabilities, but I hope you understand that what you intend to do by using source.removeElement is to change the source model by removing an element from it when the mapping is executed. For this to work, you must make the source model of the transformation “inout”.

The tutorial is correct. As the second A has id = -1, it will not be processed due to the constraint “self.id > 0” in the “when” clause of A2B. Therefore, only the first A will be transformed. Your solution would also work, as B’s are not transformed.