Skip to main content



      Home
Home » Modeling » M2T (model-to-text transformation) » [Xpand] How to save local variables in nested for loop
[Xpand] How to save local variables in nested for loop [message #648022] Mon, 10 January 2011 16:33 Go to next message
Eclipse UserFriend
Hi,

I have a nested for loop that traverses a hierarchy of custom uml profile stereotypes. When I get to stereotyped operation parameters, I need to save off the names of the parameters depending on what stereotype property is set and output them as part of logic of an earlier for loop.

I tried looking into the "LET" capability of Xpand, but it seems that string variables with this approach are immutable, which wouldn't help me in this scenario.

Any suggestions on alternative ways of approaching this?

Thanks,

Lourdes
Re: [Xpand] How to save local variables in nested for loop [message #648024 is a reply to message #648022] Mon, 10 January 2011 16:39 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

one possibiliy are global vars (see std lib docs) but i doubt this is neccessary - you should be able to achive the same by a single expression but without having you problem code i cannot give futher advice. have a look at the list operations like select and collect.

~Christian
Re: [Xpand] How to save local variables in nested for loop [message #648036 is a reply to message #648024] Mon, 10 January 2011 17:22 Go to previous messageGo to next message
Eclipse UserFriend
Hi Christian,

Here's a example of the loop I'm trying to use in my Xpand template:

«DEFINE Root FOR MyProfile::MyPackageStereotype»
«FOREACH ownedType.typeSelect(MyProfile::MyInterfaceStereotype) AS p»
«FOREACH p.ownedOperation.typeSelect(MyProfile::MyOperationStereotype ) AS e»
«FOREACH e.ownedParameter.typeSelect(MyProfile::MyParam) AS param»
«REM» «IF param.level == "LOW"»
//would like to save to local var here
«ELSEIF param.level == "MEDIUM"»
//would like to save to local var here
«ENDIF» «ENDREM»
«ENDFOREACH»
«name», «p.name», «e.name», ... //would like to output "LOW" param followed by "MEDIUM" param
«ENDFOREACH»
«ENDFOREACH»
«ENDDEFINE»

I did try out the select statement in my sandbox Xpand template:

«DEFINE MyParamRoot FOR MyProfile::MyParam»
«FILE name+".txt"»
«LET this.ownedParameter.select(param | (param.level == "LOW")) AS foo»
«foo.name»
«ENDLET»
«ENDFILE»
«ENDDEFINE»

The output for this was:
[ ]

when I was expecting the name of the parameter.

Hopefully this provides sufficient detail for more specific guidance.

As a point of clarification, the two template samples provided were separate tests that do not depend on each other.

Thanks again!

[Updated on: Mon, 10 January 2011 17:24] by Moderator

Re: [Xpand] How to save local variables in nested for loop [message #648037 is a reply to message #648036] Mon, 10 January 2011 17:31 Go to previous messageGo to next message
Eclipse UserFriend
hi, what about

«DEFINE Root FOR MyProfile::MyPackageStereotype»
«FOREACH ownedType.typeSelect(MyProfile::MyInterfaceStereotype) AS p»
«FOREACH p.ownedOperation.typeSelect(MyProfile::MyOperationStereotype ) AS e»
«name», «p.name», «e.name», «e.ownedParameter.typeSelect(MyProfile::MyParam).select(x|x.level == "LOW").collect(x|x.name).toString(",")»
, «e.ownedParameter.typeSelect(MyProfile::MyParam).select(x|x.level == "MEDIUM").collect(x|x.name).toString(",")»
«ENDFOREACH»
«ENDFOREACH»
«ENDDEFINE»


~Christian
Re: [Xpand] How to save local variables in nested for loop [message #648061 is a reply to message #648037] Mon, 10 January 2011 22:37 Go to previous message
Eclipse UserFriend
I had to make a slight modification to what you suggested to get it to work.

For example:
select(x|x.level == "LOW")

changed to:
select(x|x.level.name == "LOW")

so that the correct output would be produced.


Thanks again for the quick response! This was very helpful. Smile
Previous Topic:[Acceleo 3] Protected section ERROR ?
Next Topic:[Acceleo 3]Hibernate support?
Goto Forum:
  


Current Time: Tue Jul 01 17:57:29 EDT 2025

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

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

Back to the top