“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:

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

This entry was posted in QVT, QVTo, Tutorial. Bookmark the permalink.

12 Responses to “Hello world” in Eclipse QVTo

  1. Some student says:

    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. Mark S says:

    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. Mark S says:

    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

  4. R says:

    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?

    • fabiols says:

      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. R says:

    Thank you so much.
    But the version I am using is 3.1.0!

  6. mokhayesh says:

    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

    • fabiols says:

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

  7. mokhayesh says:

    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 :)

    • fabiols says:

      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!

  8. Ahmed Elsawi says:

    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

    • fabiols says:

      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;

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>