Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [acceleo] Strange behavior when accessing name field
[acceleo] Strange behavior when accessing name field [message #640683] Mon, 22 November 2010 19:48 Go to next message
Glenview Jeff is currently offline Glenview Jeff
Messages: 79
Registered: September 2010
Member
I'm a bit of a newbie with acceleo as you probably have read. I have around 100 lines of functional acceleo code in my project. I recently ran into this strange problem.

I have an Eclass, call it "child class" that is derived from another in a second ecore meta-model. The parent class has a name field.

In my mtl file, I access the child class's name via [childclass.name/].

In the output file, this is what I get instead of the name:
org.eclipse.emf.ecore.impl.DynamicEObjectImpl@7f3ca730 (eClass: org.eclipse.emf.ecore.impl.EClassImpl@67048192 (name: Invalid_Class) (instanceClassName: null) (abstract: false, interface: false))


Has anyone seen anything like this? Other fields in the same mtl file work fine.

Thanks!
Jeff
Re: [acceleo] Strange behavior when accessing name field [message #640685 is a reply to message #640683] Mon, 22 November 2010 20:06 Go to previous messageGo to next message
Glenview Jeff is currently offline Glenview Jeff
Messages: 79
Registered: September 2010
Member
Another bit of quite useful troubleshooting information is that the name is accessed successfully to generate the output filename. The problem occurs in the middle of a for loop that is accessing another field of the class.

Specifically, i.name in the [file] line works fine as below:
[template public ChildClass(i : ChildClass)]
[file ('/path/to/folder/'.concat(i.name.concat('.java')), false, 'Cp1252')]


But later in the same mtl file, the i.name in this code is where i get the strange output. anItem.name works fine.

	[for (anItem : Item | i.list.items)]
                      [i.name/]_[anItem.name/]:
	[/for]

[Updated on: Mon, 22 November 2010 20:13]

Report message to a moderator

Re: [acceleo] Strange behavior when accessing name field [message #640691 is a reply to message #640685] Mon, 22 November 2010 20:12 Go to previous messageGo to next message
Glenview Jeff is currently offline Glenview Jeff
Messages: 79
Registered: September 2010
Member
I worked around this by surrounding the for loop with a [let] where I accessed the member as follows:

[let myName : String = i.name]	         
[for (anItem : Item | i.list.items)]
                      [myName/]_[anItem.name/]:
[/for]
[/let]


Is this a bug or a feature that I have to do this? Also, please note that the missing slash in the previous post was only a typo in the message, not in the actual code.

[Updated on: Mon, 22 November 2010 20:13]

Report message to a moderator

Re: [acceleo] Strange behavior when accessing name field [message #640786 is a reply to message #640691] Tue, 23 November 2010 07:54 Go to previous messageGo to next message
Laurent Goubet is currently offline Laurent Goubet
Messages: 1632
Registered: July 2009
Senior Member
Hi Jeff,

We define an implicit "i" variable that olds the iteration count for each of our "for" loops. If you define your "for" loop with the iteration named "i", then "i" inside the for will have the value contained in the Sequence :

[for (i : String | Sequence{'a', 'b', 'c'}][i/][/for]


will generate "abc".

If your iteration variable has another name, then "i" will be the iteration count :

[for (s : String | Sequence{'a', 'b', 'c'}][i/][/for]


will generate "123".

With that said, you are in the second case : your iteration variable is not named "i", so we override it for the iteration count, and "integer.name" yield an invalid result (which is what got generated in your case).

Change the name of your template argument for a meaningful name, and you will not have this error anymore :

[template public ChildClass(class : ChildClass)]


as the problem exists only with the "i" variable.

Laurent Goubet
Obeo

[Updated on: Tue, 23 November 2010 07:56]

Report message to a moderator

Re: [acceleo] Strange behavior when accessing name field [message #640952 is a reply to message #640786] Tue, 23 November 2010 16:33 Go to previous message
Glenview Jeff is currently offline Glenview Jeff
Messages: 79
Registered: September 2010
Member
Thanks very much for explaining this Laurent. The choice of the variable name "i" was, interestingly enough, created from the auto-generated template Acceleo itself created based upon the template name "Item." As I'm sure you know, Acceleo when chooses by default for a parameter name a single lower-case letter variable name that corresponds to the first letter in the name of the template.

I think I will submit an enhancement request to change this syntax to something unique like i$. It seems almost without modern precedent and is most unexpected for a programming language to hard code a valid variable name to have special meaning. I recognize that this might cause some backwards incompatibility, but you could optionally issue a warning when using the ambiguous variable "i."
Previous Topic:[Acceleo 3] Iteration over the types of a model
Next Topic:Where did my console go?
Goto Forum:
  


Current Time: Sat Oct 25 18:19:20 GMT 2014

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

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