Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Xpand/Xtend] how to organize templates for multiple output languages
[Xpand/Xtend] how to organize templates for multiple output languages [message #500395] Wed, 25 November 2009 22:01 Go to next message
hoschi is currently offline hoschi
Messages: 6
Registered: November 2009
Junior Member
Hi,

I want to writa a code generator for the targe languages java, C# and C++. I'm not sure how to organize my templates to make it readable and DRY.

First of all I see no possability to call or use another xpand template from a xpand template. I want this to split big template files in more small files and keep it more readable.

My first idea was to set a global var "language" in the workflow file with the name of the target language. Create 3 workflows, for each target language one workflow with the global var set to the language. So the xpand template can hold only the logic and the xtend file can hold the text generation like this example

Xtend
getTargetLang() : ((String)GLOBALVAR language);

buildClassName(String name) :
	switch (getTargetLang()) {
   		case 'java' : 'public class '+name
   		case 'c#' : 'class '+name
   		default : ''
}; 


Xpand
«EXTENSION template::lib»

«DEFINE main FOR model::DataHolder»
«EXPAND main FOREACH items»  
«ENDDEFINE»

«DEFINE main FOR model::DataA»
«FILE this.name+".dataa"»
«buildClassName(this.name)»
«ENDFILE»	
«ENDDEFINE»

«DEFINE main FOR Object»
«ENDDEFINE»



Feel free to post your ideas Wink
Re: [Xpand/Xtend] how to organize templates for multiple output languages [message #500402 is a reply to message #500395] Wed, 25 November 2009 22:29 Go to previous messageGo to next message
Miles Parker is currently offline Miles Parker
Messages: 1340
Registered: July 2009
Senior Member
Hi Hoschi,

I wrote an article a while back on just this issue..

http://milesparker.blogspot.com/2009/04/meta-models-for-arbi trary-targets-iii.html

Some more current examples to take a look at are:

http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.amp/org .eclipse.amp.amf/plugins/org.eclipse.amp.amf.gen/src/model/? root=Modeling_Project
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.amp/org .eclipse.amp.amf/plugins/org.eclipse.amp.amf.gen/src/metaabm /tmpl/?root=Modeling_Project

And then:

http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.amp/org .eclipse.amp.amf/plugins/org.eclipse.amp.amf.gen.escape/src/ model/?root=Modeling_Project
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.amp/org .eclipse.amp.amf/plugins/org.eclipse.amp.amf.gen.escape/src/ metaabm/escape/tmpl/?root=Modeling_Project

But the basic idea is to use Aspects. I'd really argue against the idea of using globals and switches -- the whole idea is that you want to be able to mix in new features independently. I'd be happy to go into greater detail if you have more questions about this approach.
Re: [Xpand/Xtend] how to organize templates for multiple output languages [message #500426 is a reply to message #500402] Thu, 26 November 2009 05:49 Go to previous messageGo to next message
Christian Dietrich is currently online Christian Dietrich
Messages: 6292
Registered: July 2009
Senior Member
Hello,

here some additional hints:

It is no problem at all to call one template (file) from another:

«EXPAND my::templates::TemplateFile::definitionName
        FOR myModelElement»


second you can give additional paramaters to templates

«DEFINE(String language)  main FOR My::Class»
«FILE this.name+".dataa"»
«buildClassName(this.name, language)»
«EXPAND my::templates::TemplateFile::definitionName(language)
        FOR myModelElement»
«ENDFILE»	
«ENDDEFINE»


and pass the parameter from the workflow

<expand value="example::my::template('Java') FOR myModel"/>


so you won't need a global var at all
Re: [Xpand/Xtend] how to organize templates for multiple output languages [message #500785 is a reply to message #500395] Sat, 28 November 2009 17:16 Go to previous message
hoschi is currently offline hoschi
Messages: 6
Registered: November 2009
Junior Member
thank you both for the examples und advices. The AOP feature is really cool and in fact the solutiont for my problem.

@Miles
In your article you say that Xtend has also aspects. Do you have a link for an exapmle how to use it? I don't find it in the docu Sad
Previous Topic:Xpand Workspace Setup
Next Topic:[Acceleo]Collections of Collections on Queries
Goto Forum:
  


Current Time: Tue Sep 16 09:30:41 GMT 2014

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

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