Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Acceleo] How to make a type cast?
[Acceleo] How to make a type cast? [message #1018839] Thu, 14 March 2013 14:58 Go to next message
Iban Ayestaran is currently offline Iban AyestaranFriend
Messages: 58
Registered: September 2011
Member
Hi,

I am using Acceleo with a meta-model with some inherited classes. Imagine classes 'child1' and 'child2' inherit from 'parent'.

I want to do this:

[for (_par : Parent | aModel.eAllContents()->filter(Parent))]
    _par.parentattribute1 [comment write parent attributes of the class/]
    [if (_par.oclIsTypeOf(child1))]
	[(child1) _par;/]     [comment this does not work!!/]
        _par.child1attribute1 [comment write child1 attributes of the class/]
    [/if]
    [if (_par.oclIsTypeOf(child2))]
	[(child2) _par;/]     [comment this does not work!!/]
        _par.child2attribute1 [comment write child2 attributes of the class/]
    [/if]
[/for]


As far as I know, to be able to read an attribute defined in a child class I need to cast my object to be seen by acceleo as a child class, and I do not know how to do this. I tried this in different ways, for example the one above, but this is not accepted by acceleo.

Do you know how to do it?
Re: [Acceleo] How to make a type cast? [message #1021074 is a reply to message #1018839] Tue, 19 March 2013 13:38 Go to previous messageGo to next message
Stephane Begaudeau is currently offline Stephane BegaudeauFriend
Messages: 413
Registered: April 2010
Location: Nantes (France)
Senior Member

Hi,

[mySupertype.oclAsType(MySubType).mySubTypeAttribute]


"oclIsTypeOf(MyType)" returns true if your object has precisely the type "MyType".
"oclIsKindOf(MyType)" returns true if your object has or inherits from the type "MyType".
"oclAsType" cast the object into one of its inherited type.

Regards,

Stephane Begaudeau, Obeo

--
Twitter: @sbegaudeau
Google+: +stephane.begaudeau
Blog: http://stephanebegaudeau.tumblr.com | Eclipse Java Development Tools Tips and Tricks



[Updated on: Tue, 19 March 2013 13:38]

Report message to a moderator

Re: [Acceleo] How to make a type cast? [message #1021108 is a reply to message #1021074] Tue, 19 March 2013 14:28 Go to previous messageGo to next message
Iban Ayestaran is currently offline Iban AyestaranFriend
Messages: 58
Registered: September 2011
Member
Hi Stephane,
that is exactly what I was looking for, but it does not work for me. I typed this:

[if (_par.oclIsTypeOf(Child_Class))]
	[(_par.oclAsType(Child_Class)).ChildAttribute/]
[/if]


It does not work, in fact when Eclipse builds the code I get the "Unrecognized variable: (ChildAttribute)" error. That is, the if works well (at least it builds), but then I still cannot access the child attribute.

Do you know why?
Re: [Acceleo] How to make a type cast? [message #1021663 is a reply to message #1021108] Wed, 20 March 2013 13:27 Go to previous message
Stephane Begaudeau is currently offline Stephane BegaudeauFriend
Messages: 413
Registered: April 2010
Location: Nantes (France)
Senior Member

Hi,

How is your meta-model structured? Can you reproduce the issue with a minimal example or could you attach your meta-model and a minimal Acceleo module showing the issue?

Regards,

Stephane Begaudeau, Obeo

--
Twitter: @sbegaudeau
Google+: +stephane.begaudeau
Blog: http://stephanebegaudeau.tumblr.com | Eclipse Java Development Tools Tips and Tricks



Previous Topic:[Acceleo] Invalid attributes during template invocation
Next Topic:Xpand Workflow file
Goto Forum:
  


Current Time: Wed Dec 17 21:34:35 GMT 2014

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

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