Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » which metamodell to use
icon5.gif  which metamodell to use [message #499421] Sun, 22 November 2009 10:59 Go to next message
hoschi is currently offline hoschi
Messages: 6
Registered: November 2009
Junior Member
Hi,

I must write a code generator for C#, Java and C++. So I thought I can do this with Xpand. I worked with this a year ago. But I'm not sure which way is the easiest to access my data with xpand.

Here is my situation:
I have a bunch of java interfaces and implementations of the inferfaces which hold the data. In the beginning the application lodas the data and create instances from the data classes. Then I have all my data which I need for the code generation in this data instances.

First of all I thought I have to use EMF and annotate the inferfaces to generate EMF classes which can persist the model to xmi. Then make a workflow file and load the xmi to use this with xpand. But I'm not sure how I connect the generated EMF classes with my own implementations of the intefaces, so EMF can get the data to persist it.

I found also the JavaMetaModel in the xpand docu, but don't find out how I must configure the workflow to get this to work.

Any suggestions for the problem? I really need help on this problem.

best regards,
hoschi
Re: which metamodell to use [message #499442 is a reply to message #499421] Sun, 22 November 2009 16:23 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6315
Registered: July 2009
Senior Member
hello hoschi,

the javametamodel should be the right choice for your purpose.
here is a little example

my model interfaces:

package model;
public interface DataA {
	String getName();
	DataB getDataB();
}

package model;
public interface DataB {
	String getName();
}


and their implementations

package model.impl;
import model.DataA;
import model.DataB;
public class DataAImpl implements DataA {
	private DataB dataB;
	private String name;
	@Override
	public DataB getDataB() {
		return dataB;
	}
	@Override
	public String getName() {
		return name;
	}
	public DataAImpl(DataB dataB, String name) {
		super();
		this.dataB = dataB;
		this.name = name;
	}
}
package model.impl;
import model.DataB;
public class DataBImpl implements DataB {
	private String name;
	@Override
	public String getName() {
		return name;
	}
	public DataBImpl(String name) {
		super();
		this.name = name;
	}
}


and a class that consructs the model (DataModelProvider )
package model.provider;
import model.DataA;
import model.DataB;
import model.impl.DataAImpl;
import model.impl.DataBImpl;
public class DataModelProvider {
	public static DataA getModel() {
		DataB dataB = new DataBImpl("b");
		DataA dataA = new DataAImpl(dataB , "a");
		return dataA;
	}
}


now i want to generate some code out of the model provided by the DataModelProvider using the interfaces as metamodel
therefore i write some template using the javametamodel.

«DEFINE main FOR model::DataA»
«FILE name+".dataa"»
-> datab: «dataB.name»
«ENDFILE»	
«ENDDEFINE»


and configure the xpand generator to work with the metamodel
and my template

<component class="org.eclipse.xpand2.Generator">
		<metaModel class="org.eclipse.internal.xtend.type.impl.java.JavaMetaModel"/>
		<fileEncoding value="Cp1252"/>
		<expand value="templates::template::main FOR model"/>
		<genPath value="src-gen"/>
	</component>


so there is only one problem left. how to get the modelto the slot called model

therefore i wrote a very small workflow component like this one:

package model.provider;
import org.eclipse.emf.mwe.core.WorkflowContext;
import org.eclipse.emf.mwe.core.issues.Issues;
import org.eclipse.emf.mwe.core.lib.WorkflowComponentWithModelSlot;
import org.eclipse.emf.mwe.core.monitor.ProgressMonitor;
public class ModelLoader extends WorkflowComponentWithModelSlot {
	@Override
	protected void invokeInternal(WorkflowContext ctx, ProgressMonitor monitor,
			Issues issues) {
		ctx.set(getModelSlot(), DataModelProvider.getModel());
	}
}


and configured theworkflow to use it

<component class="model.provider.ModelLoader" >
		<modelSlot value="model" />
	</component>


running the workflow i get one resultfile

a.dataa

with the content

-> datab: b


which is what i expect

any questions left?
Re: which metamodell to use [message #499448 is a reply to message #499421] Sun, 22 November 2009 17:05 Go to previous messageGo to next message
hoschi is currently offline hoschi
Messages: 6
Registered: November 2009
Junior Member
thank you for the great example! I try this on my own and come back if questions rise up Wink
Re: which metamodell to use [message #499729 is a reply to message #499421] Mon, 23 November 2009 21:14 Go to previous messageGo to next message
hoschi is currently offline hoschi
Messages: 6
Registered: November 2009
Junior Member
Hmmm, in the xpand template are errors like "couldn't find model::DataA". Code completion also don't work Sad
But all work fine with the erorrs ..
Is this always a problem with the java metamodel or did I make a mistake?
Re: which metamodell to use [message #499778 is a reply to message #499729] Tue, 24 November 2009 04:59 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6315
Registered: July 2009
Senior Member
Hello, you have to tell the xpand / xtend ui plugins to use the Java(Beans)MetaModel. therefore go to the projects properties. there is a section for Xtend/Xpand. Choose JavaBeans MetaModel there.
Re: which metamodell to use [message #499845 is a reply to message #499421] Tue, 24 November 2009 11:10 Go to previous message
hoschi is currently offline hoschi
Messages: 6
Registered: November 2009
Junior Member
great, it works Smile
Previous Topic:[Xpand] Error: XMLFeatureMap is not responsible for java type org.eclipse.emf.ecore.util.FeatureMap$
Next Topic:Editor gives problems, when using external metamodel, eventhough MM is registered.
Goto Forum:
  


Current Time: Wed Sep 24 02:26:21 GMT 2014

Powered by FUDForum. Page generated in 0.02154 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software