Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Acceleo] extending Module
[Acceleo] extending Module [message #64986] Sun, 26 July 2009 16:25 Go to next message
Mats Svefors is currently offline Mats Svefors
Messages: 2
Registered: July 2009
Junior Member
Is it possible to use the extension/inheritance functionality to implement
a 'template method design pattern'?

Like so:
[module M1('http://www.eclipse.org/uml2/2.1.0/UML')/]

[template public foo()][bar()/][/template]

[template public bar()]bar[/template]

[module M2('http://www.eclipse.org/uml2/2.1.0/UML') extends M1/]

[template public bar()]SomeOtherBar[/template]

[template public subfoo(c : Class)]
[comment @main /][file ('subfoo.txt', false)][foo()/][/file][/template]

I guess this is one of them, how do you say, rhetorical questions, I tried
it and the contents of subfoo says 'bar'. But Is there a way to fulfil the
goal?

A more telling example of what I am trying to achieve:

[module JavaClass]

[template public txt_contents(c : Class)]
[c.txt_package()/]
[c.txt_imports()/]
[c.txt_classComment()/]
[c.txt_annotations()/]
[c.txt_declaration()/]{
[c.txt_body()/]
}
[/template]

[template public txt_package(c : Class)]package foo;[/template]

[comment ...and the other templates below /]



[module MySpecialTypeOfJavaClass(c : Class) extends JavaClass]

[comment ...and in here override the behavior that you wish to change/]

[template public txt_package(c: Class)]package bar;[/template]

[template public txt_body(c : Class)] lots of special code here [/template]
Re: [Acceleo] extending Module [message #65048 is a reply to message #64986] Mon, 27 July 2009 04:45 Go to previous messageGo to next message
Alexis Muller is currently offline Alexis Muller
Messages: 13
Registered: July 2009
Junior Member
On Sun, 26 Jul 2009 20:25:43 +0000, Mats Svefors wrote:

> Is it possible to use the extension/inheritance functionality to
> implement a 'template method design pattern'?

Hello,

Acceleo is using the first matching template it found.

So you can try something like that :

-------------normal.mtl---------------
[module normal('http://www.eclipse.org/uml2/2.1.0/UML')/]

[template public body(c : Class)]
normal
[/template]


-----------special.mtl----------------
[module special('http://www.eclipse.org/uml2/2.1.0/UML')/]

[template public body(c : Class)]
special
[/template]

---------generate.mtl----------------
[module generate('http://www.eclipse.org/uml2/2.1.0/UML')/]
[import special/]
[import normal/]

[template public generator(c : Class)]
[file (c.name, false)]
class [c.name/]
{
[c.body()/]
}
[/file]
[/template]


Since the special module is imported before the normal one, the special
templates are called in priority.

I hope this can help you.

Regards,

Alexis
Re: [Acceleo] extending Module [message #65112 is a reply to message #65048] Mon, 27 July 2009 05:55 Go to previous message
Laurent Goubet is currently offline Laurent Goubet
Messages: 1622
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------060005050204020801070104
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit

Hi Mats,

Alexis' solution should work ... or you can 'really' use the extension
mechanism (what you pasted here doesn't use the extension mechanism, it
is more or less an import).

Acceleo needs two things to extend templates : the "extends" keyword on
the module itself to tell which module's templates are to be overriden,
and the "overrides" keyword on the templates. Having only the
"overrides" will not compile, having only the "extends" will only have
your extending module "import" the other, making its templates visible
(though protected templates will be visible as well with an "extends" in
stead of an "import".

That being said, your exemple should be modified as such :

----------8<----------
[module M1('http://www.eclipse.org/uml2/2.1.0/UML')/]

[template public foo()][bar()/][/template]

[template public bar()]bar[/template]
---------->8----------

----------8<----------
[module M2('http://www.eclipse.org/uml2/2.1.0/UML') extends M1/]

[template public bar() overrides bar]SomeOtherBar[/template]

[template public subfoo(c : Class) overrides foo]
[comment @main /][file ('subfoo.txt', false)][super()/][/file][/template]
---------->8----------

you will also note the use of "super" instead of directly calling "foo",
though both would work since the name of your overriding template is not
the same as the name of the overriden template (which wouldn't be true
for "bar" in this case).

Laurent Goubet
Obeo

Alexis Muller a écrit :
> On Sun, 26 Jul 2009 20:25:43 +0000, Mats Svefors wrote:
>
>> Is it possible to use the extension/inheritance functionality to
>> implement a 'template method design pattern'?
>
> Hello,
>
> Acceleo is using the first matching template it found.
>
> So you can try something like that :
>
> -------------normal.mtl---------------
> [module normal('http://www.eclipse.org/uml2/2.1.0/UML')/]
>
> [template public body(c : Class)]
> normal
> [/template]
>
>
> -----------special.mtl----------------
> [module special('http://www.eclipse.org/uml2/2.1.0/UML')/]
>
> [template public body(c : Class)]
> special
> [/template]
>
> ---------generate.mtl----------------
> [module generate('http://www.eclipse.org/uml2/2.1.0/UML')/]
> [import special/]
> [import normal/]
>
> [template public generator(c : Class)]
> [file (c.name, false)]
> class [c.name/]
> {
> [c.body()/]
> }
> [/file]
> [/template]
>
>
> Since the special module is imported before the normal one, the special
> templates are called in priority.
>
> I hope this can help you.
>
> Regards,
>
> Alexis


--------------060005050204020801070104
Content-Type: text/x-vcard; charset=utf-8;
name="laurent_goubet.vcf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="laurent_goubet.vcf"

YmVnaW46dmNhcmQNCmZuOkxhdXJlbnQgR291YmV0DQpuOkdvdWJldDtMYXVy ZW50DQpvcmc6
PGEgaHJlZj0iaHR0cDovL3d3dy5vYmVvLmZyLyI+T2JlbzwvYT4NCmVtYWls O2ludGVybmV0
OmxhdXJlbnQuZ291YmV0QG9iZW8uZnINCnVybDpodHRwOi8vd3d3Lm9iZW8u ZnINCnZlcnNp
b246Mi4xDQplbmQ6dmNhcmQNCg0K
--------------060005050204020801070104--
Previous Topic:[Acceleo] init rules don't work
Next Topic:[Acceleo] Unable to make a query with collection as return type
Goto Forum:
  


Current Time: Fri Aug 01 02:01:43 EDT 2014

Powered by FUDForum. Page generated in 0.01707 seconds