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"

 
(4 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
=== PreReqs  ===
 
=== PreReqs  ===
  
Eclipse Galileo. Create a new project with the folder structure defined above (we'll create the files as we trundle along).  
+
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  ===
 
=== What we're aiming for  ===
Line 61: Line 61:
 
«ENDDEFINE»
 
«ENDDEFINE»
 
</pre>  
 
</pre>  
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!
+
<br>
  
 
=== Create generate.xml  ===
 
=== Create generate.xml  ===
Line 85: Line 85:
  
 
#The template name first2csv::Main, first2csv has to match the name of the template we have in our template folder.  
 
#The template name first2csv::Main, first2csv has to match the name of the template we have in our template folder.  
#The #/ at the end of the inputURI is no typo, we are specifying where in the data go.
+
#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  ===
 
=== Validate Xpand Ant tasks are installed  ===
  
Under Window-&gt;Preferences-&gt;ANT, have a look at the runtime node at the tasks tab. In the name column you want to see something like  
+
Under Window-&gt;Preferences-&gt;ANT, have a look at the runtime node in the tasks tab. In the name column you want to see something like  
 
<pre>eclipse.org/gmf/2008/xpand:template</pre>  
 
<pre>eclipse.org/gmf/2008/xpand:template</pre>  
 
.  
 
.  
  
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 [http://www.eclipse.org/downloads/download.php?file=/modeling/gmf/gmf/downloads/drops/2.2.0/R200906220658/gmf-xpand-2.2.0.zip gmf-xpand-2.2.0.zip], just copy this plugin over and launch eclipse.exe -clean to make sure it picks up the plugin.  
+
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 [http://www.eclipse.org/downloads/download.php?file=/modeling/gmf/gmf/downloads/drops/2.2.0/R200906220658/gmf-xpand-2.2.0.zip 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  ===
 
=== Generate data.csv  ===
Line 105: Line 105:
 
*If you don't have it already, you need "eclipse modeling project: A Domain-Specific Language (DSL) Toolkit" book.  
 
*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:  
 
*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/generate.xml?root=Modeling_Project&view=markup 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]
 
**[http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.amalgam/releng/org.eclipse.amalgam.releng.builder/?root=Modeling_Project Various templates and transforms]

Latest revision as of 08:47, 22 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 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:

Back to the top