[Acceleo] Difficulties with variables [message #633900] |
Tue, 19 October 2010 14:11  |
Eclipse User |
|
|
|
Hi,
I am pretty new to Acceleo, but after some initial difficulties I have been able to use it quite successfully so far.
However, the fact that all variables in Acceleo are final, i.e. cannot be modified after they are declared, is giving me quite a hard time. I understand that part of my problem is that I am new to OCL as well, but I hope you will be able to help. Despite I looked quite thoroughly through Acceleo and OCL documentation and MTL specification, I couldn't find anything similar to this.
Even something as simple as a counter is not so simple in Acceleo; I have tried what is described here:
http://www.eclipse.org/forums/index.php?t=msg&&th=19 986&goto=64949
but I couldn't get it working. Even the suggested solution of using the let statement inside the for loop does not seem to have the side effect of incrementing the general variable. Moreover, I don't see how this could work, given that variables are final.
Now, on to my specific problem. Let's say I have a Collection and I need to do something with each member of the Collection and store the result in a different variable in order to reuse it later. E.g. let's say I have a UML model of a Java class, with the list of getters and setters. I want to store the names of all the operations in a different variable Set(String). I have been able to retrieve all the setters, for example like this:
setters : Set(Operation) = (c.getOperations()->select(name.startsWith('set')));
but now how do I assign their names to a Set(String)?
A slightly more complicated thing would be if I also need to check some condition on the names. Or, if I need to compute something that depends on the value of each variable: a counter or something similar would be of use here, but values of variables cannot be modified...
Any ideas?
Thank you!
[Updated on: Tue, 19 October 2010 16:58] by Moderator
|
|
|
|
Re: [Acceleo] Difficulties with variables [message #634642 is a reply to message #633978] |
Fri, 22 October 2010 10:39   |
Eclipse User |
|
|
|
Hi Stephane,
first of all, thank for your quick reply. I couldn't test this until today. So, I had tried what you say before, i.e.:
c.getOperations()->select(name.startsWith('set'))->collect(oper | oper.name)
or
c.getOperations()->select(name.startsWith('set')).name
and it works.
However, I finally found my problem: there seem to be something wrong when I try to assign the result of that function to a Set(String), through a "let" statement or an initialization statement.
In the case of the let statement, I don't get any compile-time or run-time error but the code inside the let statement is never executed:
[let attributes : Set(String) = c.getOperations()->select(name.startsWith('set')).name]
This statement is never entered.
[for (s : String | attributes)]
[s/]
[/for]
[/let]
In the case of the initialization statement:
[template public generatePropertySourceWoAttributeNames(c : Class){
getters : Set(Operation) = (c.getOperations()->select(name.startsWith('get') or name.startsWith('is')));
setters : Set(Operation) = (c.getOperations()->select(name.startsWith('set')));
attributes : Set(String) = c.getOperations()->select(name.startsWith('get')).name;
}]
[for (s : String | attributes)]
[s/]
[/for]
I get an error on "attributes": "Init expression type does not conform to type of variable (attributes)."
However, a for loop on "getters" or "setters" above works perfectly.
Finally, putting the code in a query as you suggested works, unless I try to assign the result of the query to a Set(String) variable. It seems to be some issue with the Set(String) type, or the way the attribute is collected. I am a little unclear of why this happens, could you please explain? I am quite sure I am missing something.
Thank you.
[Updated on: Fri, 22 October 2010 11:38] by Moderator
|
|
|
|
|
|
|
|
Re: [Acceleo] Difficulties with variables [message #1695078 is a reply to message #1695073] |
Mon, 11 May 2015 16:34  |
Eclipse User |
|
|
|
Hi
No. Your model has an Employee class and an Employee::name attribute.
Your generated Java API has setName/getName methods.
Regards
Ed Willink
On 11/05/2015 20:40, Rafael Dantas wrote:
>
> Yes. I am new to java / model. Sorry if I was unclear.
>
> Also I asked a question at the wrong topic. I saw the examples mentioned
> above and made my question here.
>
> I need to know if there is any method other than 'get' or 'set'. I
> believe that not just whether begins with 'get' or 'set'. Think of the
> scenario, I have a simple class named Employee, which has the name
> attribute, getName and setName besides these have getSalary. Checking
> the memento generation m2t is getSalary is not a get an attribute?
|
|
|
Powered by
FUDForum. Page generated in 0.06989 seconds