Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Model2Text using Xpand and QVT for Query"

(New page: This is a guide on how to configure and exectue an Xpand template which is using QVT for lookup. What your project will look like: *MyEclipseProject **model ***First.ecore ''...)
 
Line 1: Line 1:
 
This is a guide on how to configure and exectue an [[Xpand]] template which is using [[QVT]] for lookup.  
 
This is a guide on how to configure and exectue an [[Xpand]] template which is using [[QVT]] for lookup.  
 +
 +
=== PreReqs  ===
 +
 +
Eclipse Galileo. Create a new project with the folder structure defined above (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:  
 
What your project will look like:  
Line 13: Line 21:
 
***MyClass.xmi ''(an instance of your metamodel data)''  
 
***MyClass.xmi ''(an instance of your metamodel data)''  
 
**generate.xml ''(ant script that executes temaplte)''
 
**generate.xml ''(ant script that executes temaplte)''
 
=== PreReqs  ===
 
 
Eclipse Galileo. Create a new project with the folder structure defined above (we'll create the files as we trundle along).
 
  
 
=== Create First.ecore  ===
 
=== Create First.ecore  ===
Line 96: Line 100:
  
 
Refresh your out folder, you should see data.csv with some data  
 
Refresh your out folder, you should see data.csv with some data  
<pre>First, Second</pre>
+
<pre>First, Second</pre>  
 +
=== 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:
 +
**[http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.amalgam/releng/org.eclipse.amalgam.releng/generate.xml?root=Modeling_Project&view=markup generate.xml generate.xml]
 +
**[http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.amalgam/releng/org.eclipse.amalgam.releng.builder/?root=Modeling_Project Various templates and transforms]

Revision as of 23:57, 21 July 2009

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

PreReqs

Eclipse Galileo. Create a new project with the folder structure defined above (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»

Now to nudge Eclipse, in the editor window hit (CTRL+SPACE) and Eclipse should ask you "Do you want to add Xtend nature to project test?", heck yeah!

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

Validate Xpand Ant tasks are installed

Under Window->Preferences->ANT, have a look at the runtime node at 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

Back to the top