Model2Text using Xpand and QVT for Query

From Eclipsepedia

Revision as of 08:47, 22 July 2009 by Refux.hotmail.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This is a guide on how to configure and exectue an Xpand template which is using QVT for lookup.

Contents

PreReqs

Eclipse Galileo. Create a new project with the folder structure defined below (we'll create the files as we trundle along).

What we're aiming for

Starting with a simple model, populating it with some data and then performing a Model2Text transform to create a CSV file.

What your project will look like:

  • MyEclipseProject
    • model
      • First.ecore (main metamodel)
    • out (folder for output)
      • data.csv (what will be generated)
    • templates
      • first2csv.xpt (transformation from metamodel to csv)
    • test
      • MyClass.xmi (an instance of your metamodel data)
    • generate.xml (ant script that executes temaplte)

Create First.ecore

Right Click on model->new->other->model Name the top level package First. Edit its properties

Name	        First
Ns Prefix	First
Ns URI		http:///First.ecore

Add a subpackage called Second. Edit its properties

Name	        Second
Ns Prefix	First.Second
Ns URI		http:///First/Second.ecore

Create an EClass in package Second called MyClass. Create two EAttributes both of type EString called: attribute1 and attribute2

The ecore editor should look something like this

  • First
    • Second
      • MyClass
        • attribute1 : EString
        • attribute2 : EString

Create MyClass.xmi

Right click on MyClass (in the ecore editor) and select 'Create Dynamic Instance' point it to your test (data) folder and call it MyClass.xmi

The default editor is probably wrong, so close it. Right click on MyClass.xmi and select Open With->'Generic EMF Form Editor'.

Edit My Class's attribute1 and attribute2 and put in some test data. I used First and Second as the data.

Create first2csv.xpt

Right click on templates and select New->Other->xPand Template (GMF-Xpand) Select the templates folder and name it first2csv.xpt.

Paste this text in:

«IMPORT 'http:///First.ecore'»

«DEFINE Main FOR First::Second::MyClass -»
«attribute1», «attribute2»
«ENDDEFINE»


Create generate.xml

Right Click on MyEclipseProject and select New->Other->File and call it generate.xml

Paste this text in:

<?xml version="1.0" encoding="UTF-8"?>
<project name="pmw" default="main" xmlns:zzz="eclipse.org/gmf/2008/xpand">
	<target name="main">
		<zzz:template name="first2csv::Main" 
			inputURI="platform:/resource/MyEclipseProject/test/MyClass.xmi#/" 
			templateroot="file:/${basedir}/templates/"
			outfile="out/data.csv">
			<MetamodelRegistry>
				<Metamodel nsURI="http:///First.ecore" location="platform:/resource/MyEclipseProject/model/First.ecore" />
			</MetamodelRegistry>
		</zzz:template>
	</target>
</project>

Couple of interesting things to note:

  1. The template name first2csv::Main, first2csv has to match the name of the template we have in our template folder.
  2. The #/ at the end of the inputURI is no typo, we are specifying where in the data to go.

Validate Xpand Ant tasks are installed

Under Window->Preferences->ANT, have a look at the runtime node in the tasks tab. In the name column you want to see something like

eclipse.org/gmf/2008/xpand:template

.

If you don't, you need to get the org.eclipse.gmf.xpand.ant plugin. You can find it included in the GMF Xpand download gmf-xpand-2.2.0.zip, just copy this plugin over and launch eclipse.exe -clean to make sure it picks up the plugin.

Generate data.csv

To run this script, right click on generate.xml select Run as 'Ant Build...'. Select the JRE tab and pick 'Run in the same JRE as the workspace'. Click 'Run'.

Refresh your out folder, you should see data.csv with some data

First, Second

Further Reading

  • If you don't have it already, you need "eclipse modeling project: A Domain-Specific Language (DSL) Toolkit" book.
  • The Amalgam project has some much more sophisticaed templates and models to generate text files: