Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Acceleo] Can I modify a section of a template from another section?(Can I avoid multiple passes over the model?)
[Acceleo] Can I modify a section of a template from another section? [message #791991] Mon, 06 February 2012 08:49 Go to next message
Seref Arikan is currently offline Seref Arikan
Messages: 61
Registered: August 2010
Member
Greetings,
I've used stringtemplate before, and I'm used to adding content to fields on a template as a go along. An example would be walking a tree structure based on EMF: as I encounter new types, I'd be adding them to imports section of the file that must be generated.

It appears that with Acceleo, if I have a call to a java service wrapper at the beginning of the template, that call is performed, and that is it.

I want to make sure that I understand the usage of Acceleo correctly. In that case, I'd have to explore the tree structure for imports (possibly through a template that produces imports only), then I'd have to repeat the walk over the tree again, this time for different output. There is no "state" to keep among calls contained in a template.

Is this the correct approach?

Best regards
Seref
Re: [Acceleo] Can I modify a section of a template from another section? [message #793579 is a reply to message #791991] Wed, 08 February 2012 05:03 Go to previous messageGo to next message
Laurent Goubet is currently offline Laurent Goubet
Messages: 1621
Registered: July 2009
Senior Member
Hi Seref,

Unfortunately, Acceleo does not provide yet an easy way to manage the "imports" of a generated java file : we do not provide post-processing of any sort on a "file" block, they are generated on-the-fly on disk. You can work around it by making use of the fact that Java services called from Acceleo are stateful (we do not recommand this, but it can serve as a workaround until we introduce the necessary functionnality).

Basically, what you would do is :

- create a "main" template that will hold your "[file]" block.
- create a "generate" template that generates a "placeholder" for the imports and the body of your class while registering the necessary imports in a set/list through a java service
- From your "[file]" block, call the "generate" template as argument of a "replace" operation that replaces the "placeholder" by the content of the import list saved in the java service.

Laurent Goubet
Obeo
Re: [Acceleo] Can I modify a section of a template from another section? [message #793673 is a reply to message #793579] Wed, 08 February 2012 07:25 Go to previous messageGo to next message
Seref Arikan is currently offline Seref Arikan
Messages: 61
Registered: August 2010
Member
Hi Laurent,
This is exactly the kind of work around I had in mind, thanks for letting me know that it is possible.
However, you're referring to a usage of templates I have not seen before. What kind of syntax would you use in [file] to access [generate] and modify the placeholder (some text value?) ? And which documentation should I be reading to learn about this type of syntax rules/capabilities. I don't think I've seen this in the online user guide

Kind regards
Seref
Re: [Acceleo] Can I modify a section of a template from another section? [message #793705 is a reply to message #793673] Wed, 08 February 2012 08:07 Go to previous messageGo to next message
Laurent Goubet is currently offline Laurent Goubet
Messages: 1621
Registered: July 2009
Senior Member
Seref,

I don't remember where ATM, but this shoud be in the user-guide. There are a number of ways you can turn your template. The first is to simply use "replace" with your placeholder string :

[template public main(...)]
[file ...]
[generate(...).replace("<PLACEHOLDER>", getImports(...))/]
[/file]
[/template]

[template public generate(...)]
<PLACEHOLDER>

public class MyClass {
  MyClassBody
}
[/template]

[query public getImports(...) : String = invoke(<invoke java service to retrieve import list and convert it to a String>)/]


A second way would be to use post-processing on the template to call the replace :

[template public main(...)]
[file ...]
[generate(...)/]
[/file]
[/template]

[template public generate(...) post (replace("<PLACEHOLDER>", getImports(...)))]
<PLACEHOLDER>

public class MyClass {
  MyClassBody
}
[/template]

[query public getImports(...) : String = invoke(<invoke java service to retrieve import list and convert it to a String>)/]


Laurent Goubet
Obeo
Re: [Acceleo] Can I modify a section of a template from another section? [message #793731 is a reply to message #793705] Wed, 08 February 2012 08:42 Go to previous message
Seref Arikan is currently offline Seref Arikan
Messages: 61
Registered: August 2010
Member
Thanks Laurent,
I think this is one of those moments where you feel like an idiot for missing the obvious, but at least a happy idiot, knowing he has a way out Smile

Many thanks for your help, and by the way, this is one cool piece of work!

Kind regards
Seref

[Updated on: Wed, 08 February 2012 08:42]

Report message to a moderator

Previous Topic:Syntax clarification.
Next Topic:[XPand2] Splitting one big xpt-file
Goto Forum:
  


Current Time: Tue Jul 29 22:58:11 EDT 2014

Powered by FUDForum. Page generated in 0.02228 seconds