|
Re: which metamodell to use [message #499442 is a reply to message #499421] |
Sun, 22 November 2009 16:23 |
|
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
which is what i expect
any questions left?
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04777 seconds