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:

  1. Install a plug-in metamodel in Eclipse and create the transformation.
  2. 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.

  1. 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.
    ABC metamodel
  2. Create a new “EMF Generator Model” (right click the metamodel in the package explorer, “New->Other” and “Eclipse Modeling Framework->EMF Generator Model”).
  3. 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.
  4. 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).
  5. 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.
  6. Write the following QVTo code:
    modeltype ABC uses ABC('http:///ABC.ecore');
    
    transformation HelloWorld(in source:ABC, out target:ABC);
    
    main() {
    	source.rootObjects()[Root]->map Root2Root();
    }
    
    mapping Root :: Root2Root() : Root {
    	element += self.element->select(a |
    		a.oclIsKindOf(A))[A]->map A2B();
    }
    
    mapping A :: A2B() : B
    	when {
    		self.id > 0
    	}
    	{
    	result.id := self.id;
    	result.b := self.a + " World!";
    }

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

  7. 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”
  8. 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.
  9. 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.

  1. 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.
  2. Close Eclipse. Copy the files that were generated in the “plugins” (“eclipse/plugins”) directory of Eclipse. Restart Eclipse.
  3. 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…

47 thoughts on ““Hello world” in Eclipse QVTo

  1. I found this text very useful. Thank you very much 🙂

    P.S.
    In Eclipse Helios it behaves differently (not as described cause to GUI changes) so this text needs some upgrades 😉

  2. I kept getting errors in the transformation file.

    The very first line
    modeltype ABC uses ABC(‘http:///ABC.ecore’);

    would give me a “failed to resolve metamodel”

    After some searching I found the fix was:
    from the project folder go right click->properties
    QVT Settings/Metamodel Mappings
    Click “Add”

    The source model URI should match the same uri in your transformation file.
    The target model URI will be the ecore file somewhere in your workspace.

    Good luck.

    -Mark

  3. Ok, I didn’t see your last post about
    DO NOT add the metamodel in the project settings (“QVT Settings->Metamodel Mappings”).

    Apparently I was trying to execute the transformation from my original instance of eclipse and not from the one that contained the generated editor for my “*.abc” files.

    After launching a new instance of eclipse from my original one, everything went smooth and I didn’t have to add anything to the QVT Settings.

    -Mark

    1. Hi Mark,

      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.

      Wish you all the best!

  4. Hi,

    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?

    1. Hello R,

      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.

  5. Hi,

    It is good starting point thank you 🙂

    could you pleas tell us how to get the XMI file of out put model?

    In fact, I need to see the result visually. Please advice

    Thanks again

    1. Hi mokhayesh;

      The transformation in fact generates a XMI file – i.e. the “.abc” file is a XMI. To see the source, right click it in the Project Explorer and select “Open With->Text Editor”.

      About the visual representation of your model, you can use either GMF or Graphiti to create a concrete syntax (it seems the Graphiti project is now the recommended starting point).

  6. Hi Fabiols,

    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 ?

    Thanks a lot 🙂

    1. Hi mokhayesh;

      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!

  7. Dear Fabiols,

    Many thanks for the post…

    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.

    Regards,
    Ahmed Elsawi

    1. Hi Ahmed;

      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.

      Regards;

  8. Hi,

    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.

    Thanks.
    Regards
    Charu

    1. Hi Charu;

      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.

  9. Thank you so much Fabio,

    It is working but I have 2 more questions to ask

    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. Hi Charu;

      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.

      Good luck!

  10. Hello Fabio,

    Thank you so much . This cleared lot of doubts for me. 🙂
    Ya, the new instance gets created. All the steps executes correctly as per direction , I just have problem executing step 7.

    1. Hi Charu;

      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.

      You can also try to erase the projects and reexecute the steps…

  11. Hello Fabio,

    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 .

    Thank you once again

    1. Hi Charu;

      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.

      Good luck!

  12. Hello Fabio.

    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.

    Please help me

    Thanks in advance

  13. Hello Fabio,

    I have some doubt in this article, can you please help me

    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. Hi Silwaya;

      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.

      3) I’m sorry, but I didn’t understand your question. What is “Qi.e”?

  14. 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?

    1. Hello Ben;

      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.

      Best regards;

  15. Hello,

    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??

    Thanks in advance.
    Regards

    1. Hello Conchi;

      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.

      Best regards

      1. Hello,

        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.

        Thanks again for the tutorial and you answer.
        Kind regards.

  16. Thank you for your quick answer.
    I had already done what you suggest with no effect. I do not know what else to do.
    I’m using the first option, an Eclipse instance, no plug-in.

  17. think you for your tutorial.

    please i have problem to the step 4 . i can’t create a plugin

    “Export Plug-ins ” has encountered a problem.

    can you help me .Thanks in advance.

      1. thank you fabiols,

        I think the problem was the version of JVM, once spent his walk-d32, I have a new eclipse instance that opens.

        on step 5 you created the QVT project in the new open instanace eclipse, either.

        thank you in advance.

  18. Hello Fabio,

    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

    1. Hello Hatim;

      On the window opened, you must right click on the Root element and select “new child” to create the 3 childs. I’ll update the tutorial to explain that.

      Good luck!

  19. 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:

    mapping inout A :: A2B() : B
    when {
    self.id > 0
    }
    {

    result.removeElement(self);

    }

    But it seems such a pre-defined function does not exist. Any idea?

    1. Hello Mani;

      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”.

      Good luck!

  20. Hello,

    perhaps already said, but if the children are defined so, the example don’t work as it should:

    A: id=1, A=”Hello”
    A: id=-1, A=”World”
    C: id=1, C=”Something”

    These should be defined at the gollowing way:

    A: id=1, A=”Hello”
    B: id=-1, A=”World”
    C: id=1, C=”Something”

    Regards.

    1. Hello Gonzalo;

      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.

      Regards.

  21. Hello,

    basically that i need is define a java program, then generate a model for it, transform it and finally obtain the java program again.

    I would make the transformation with QVTo.

    I should define a MoDisco Java metamodel as modeltype?

    What is the most appropriate way to do this according to you?

    Best regards.

    1. Hello Sergio;

      It seems that your project doesn’t have the correct plug-in dependencies. But I don’t know how you could generate the models and end up with this problem.

      Try creating a new project, following the steps I’ve presented. If it does not work, try using my ABC metamodel. If it still does not work, the last resort is reinstalling Eclipse…

      Good luck!

      Fabio

  22. Hi Fabio,

    i’m writing a transformation and i need write conditions in some mappings about others mappings.

    What is the correct way of to do this?

    Thanks.

Deixe uma resposta para fabiols Cancelar resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *