Wednesday, January 30, 2013

QVT transformations with Eclipse [tutorial]

Eclipse has a running plugin for QVT Operational transformations. So, let's use it!

I suggest you start with the Eclipse Modeling Tools plus the Model-to-Model Operational QVT plugins (check how to download/install it).

Also, QVT requires a metamodel of the source and target languages being used (in case you don't know, a metamodel is a model that describes another model). In this example we'll use this simplified use case diagram metamodel. Lastly, of course, we need an actual model to be used as input! We will use this piece of a Meeting Scheduler use case diagram.

Ugly, right? Agreed...

If you want to create your own use case models, you can use the default editor that Eclipse can create, which allow you to edit models in a tree-based view. You can create an editor for our use case metamodel by following the steps I and II from this tutorial by Vitor Souza (in step II.2, you can use "usecasepackage" as the base package name). Then follow step IV for actually creating the model (the model object for the step IV.4 is just "Model").

But now, let's go the transformations!


Quick and Dirty version:

  1. Run Eclipse with QVT
  2. Create a new Operational QVT Project (File/New/Other.../Model to Model Transformation/Operational QVT Project). In the wizard, be sure to select "Create a simple project".
  3. Import the metamodel (.ecore) and the model (.usecase)
  4.   My favorite way of doing this is by just drag-and-dropping the files into the Project Explorer. Alternatively, right click your project, select 'Import...', General/File System
  5. Create a new Operational QVT Transformation (File/New/Other.../Model to Model Transformation/Operational QVT Transformation). In the Module name you write the name of the transformation you are creating.
  6. Now it is time for the obscure Eclipse configuration: go to Project/Properties/QVT settings/Metamodel Mappings, click 'Add' and define the source/target metamodels. In the source field you need to write the URI of the metamodel (in our example it's http://www.cin.ufpe.br/useCaseUri ). In the target field you can browse the current project and select the metamodel file.
  7. Code! Or use this transformation example.
  8. Now, more configuration. Right-click the transformation file and select Run As/Run Configurations...  , select 'Operational QVT Interpreter' and click the 'New launch configuration' button. Go to the Model URI field and Browse the project to select the input model file (.usecase)
  9. Press "Apply", to save these settings
  10. Run!


Now if everything is right you will see... nothing. Just go check the transformation results in your target model and be happy! Once these settings are done, all you need to run the transformation again is to select the transformation in the 'Run' dropdown button.

Thorough version:

  1. Run  Eclipse with QVT
  2.    Please make it wear a good tennis as not to harm its feet.
  3. Create a new Operational QVT Project. To do so, go to File/New/Other... (Ctrl+N) and then select 'Operational QVT Project' in the 'Model to Model Transformation' folder. In the wizard, be sure to select "Create a simple project", as shown in the pictures below. 
  4. File/New/Other...
    New Operational QVT Project

    Write the project name, select 'Create a simple project' and click Next

    Just finish
  5. Import the metamodel (.ecore) and the model (.usecase).  There are several ways to do this. My favorite one is to simply drag the files and drop them into Eclipse's Project Explorer (or Package Explorer, depending on the version of Eclipse).


  6. Create a new Operational QVT Transformation (File/New/Other.../Model to Model Transformation/Operational QVT Transformation). In the Module name you write the name of the transformation you are creating.

  7. Now it is time for the obscure Eclipse configuration: go to Project/Properties/QVT settings/Metamodel Mappings, click 'Add' and define the source/target metamodels. In the source field you need to write the URI of the metamodel (in our example it's http://www.cin.ufpe.br/useCaseUri ). In the target field you can browse the current project and select the metamodel file.


  8. We're almost done with the configuration, now you already can code your transformations. These slides may help you with that. Or, use this example based on the paper "Towards Architectural Evolution through Model Transformations", from SEKE2012:
    modeltype UseCase uses UseCase('http://www.cin.ufpe.br/useCaseUri');
    transformation AddActor(inout useCaseModel : UseCase);
    
    main()
    {
      useCaseModel.rootObjects()[Model].map applyAddActor();
    }
    
    mapping inout Model::applyAddActor()
    {
       self.actor += new Actor("newActor");
    }
    
    constructor Actor::Actor(myName : String)
    {
       name := myName;
    }
    
    
  9. Now, more configuration. Right-click the transformation file and select Run As/Run Configurations...  , select 'Operational QVT Interpreter' and click the 'New launch configuration' button. Go to the Model URI field and Browse the project to select the input model file (.usecase)


  10. Browse and select your input model
    After selecting the input model Eclipse may show an error like this: "Invalid source URI 'platform:/resource/UseCaseTransformation/MeetingScheduler.usecase' for parameter 'useCaseModel'". If this happens, you need to check if your model has the following attribute in its root node: xsi:schemaLocation="http://www.cin.ufpe.br/useCaseUri UseCase.ecore". This attribute is required for Eclipse to know which file to load for the given URI (even though we already defined that in step 5, go figure...)

  11. Press "Apply", to save these settings
  12. Press Run!
You can check the model to see if the new Actor was created:

5 comments:

  1. Thanks for this simple example! Its a good starting point.

    ReplyDelete
  2. Hello,

    I can't find the sources,

    ReplyDelete
  3. Hi,
    I Can´t download the sources. Help me please

    ReplyDelete
  4. You can find more Use case diagram examples in the diagram community of Creately diagram software. There are 100s of use case diagrams drawn on various scenarios.

    ReplyDelete
  5. You can refer to different types of online diagramming tools which have use case pages for different aspects. This will help you to easily figure out what exactly you want and at the meantime you can use those templates to draw your preferred Use case diagrams.

    ReplyDelete