Home » Modeling » M2T (model-to-text transformation) » [Acceleo] Generate a complex inheritance list
[Acceleo] Generate a complex inheritance list [message #665684] |
Fri, 15 April 2011 13:51  |
Cedric Moonen Messages: 274 Registered: August 2009 |
Senior Member |
|
|
Hello,
I am currently using Acceleo to generate C++ code. I have a case in which I need to generate a C++ which inherits from some other classes. The classes it is inheriting from are not all contained in one sequence of my object but rather should be composed depending of different conditions.
It becomes quite difficult to manage the code in that case. Let's take a simple example: suppose that for a certain model, all the conditions end up that my class doesn't inherit from any interface. In that specific case, I can't generate a ':' character before the list of inherited classes (this will end up in a compilation error). Already for this simple example, this becomes difficult because for that simple character, I have to make a lot of checks to see if I have to print it.
Then another problem is the fact that I have to generate a ',' between each classes. Here also, it becomes very complex to write the checks.
A solution would be to be able to build a list of strings beforehand (each string corresponds to a class name) depending on the different checks, and then generate the code based on that list. It then becomes much easier to write: I simply have to check if my list is empty or not (for the ':' character) and then write a simple for loop with a ',' separator.
Unfortunately, building such a list seems impossible since variables in Acceleo are final.
Is there another way to achieve something like this without having to use a java service ?
|
|
|
Re: [Acceleo] Generate a complex inheritance list [message #665927 is a reply to message #665684] |
Mon, 18 April 2011 08:17   |
|
Hi Cedric,
I understand perfectly the kind of problem that you are facing, here are some solutions for you:
>>> Then another problem is the fact that I have to generate a ',' between each classes. Here also, it becomes very complex to write the checks.
- You want to write a collection of element separated by "," except for the last one like "A, B, C, D, E". You have two solutions :
[Sequence{'A', 'B', 'C', 'D', 'E'}->sep(', ')/]
[for (str : String | Sequence{'A', 'B', 'C', 'D', 'E'}) seperator(', ')][str/][/for]
In a Java generator, in order to write something like this:
public final MyClass implements MyInterface1, MyInterface2, MyInterface3 {
}
Which is a similar problem, we are doing something like this:
[template protected genBody(aClass : Class)]
[genVisibility()/][genAbstract()/][genFinal()/]class [genName()/][genInheritance()/][genInterfaceRealization()/] {
[genClassMembers()/]
}
[/template]
[template public genInterfaceRealization(aClass : Class)]
[if (not aClass.getAllInterfaces()->isEmpty())] implements [for (namedElement : NamedElement | aClass.getAllInterfaces()) separator(', ')][namedElement.genName()/][/for][/if]
[/template]
You should try something similar with ":" instead of "implements" and with the same use of the "separator" keyword. In our case, the "getAllInterface()" is a query in order to simplify the "genInterfaceRealization" template. The result of a query being stored in a cache it also improve performances since we are calling it several times.
Stephane Begaudeau, Obeo
--
Twitter: @sbegaudeau
Acceleo wiki: http://wiki.eclipse.org/Acceleo
Blogs: http://stephanebegaudeau.tumblr.com & http://sbegaudeau.tumblr.com
[Updated on: Mon, 18 April 2011 08:22] Report message to a moderator
|
|
| | | |
Re: [Acceleo] Generate a complex inheritance list [message #666133 is a reply to message #665684] |
Tue, 19 April 2011 09:09   |
|
Hello
I'll have to admit this organization is not very easy to manipulate. It should work with something like this:
[let seq : Sequence(String) = Sequence{}]
[seq->union(if(true) then Sequence{'a'} else Sequence{} endif)->union(if(true) then Sequence{'b'} else Sequence{} endif)/]
[/let]
or
[let seq : Sequence(String) = (if true then if false then Sequence{'a'} else Sequence{'b'} endif else Sequence{} endif)]
[seq->sep(', ')/]
[/let]
>>> If you had to "translate" that piece of code in Acceleo, would you be able to do so ?
I can translate it but if I really had to do it, I would see if I can change the metamodel (not always possible since it may be used by other software and in that case any changes could be expensive) and if I really had to manipulate this, I think I would choose a Java service because it would be easier to maintain than a complex expression. So if you just need this for a small problem, go for it, if you need several lines of "if then else endif' to solve your problem, use a Java service.
Stephane Begaudeau, Obeo
--
Twitter: @sbegaudeau
Acceleo wiki: http://wiki.eclipse.org/Acceleo
Blogs: http://stephanebegaudeau.tumblr.com & http://sbegaudeau.tumblr.com
[Updated on: Tue, 19 April 2011 09:14] Report message to a moderator
|
|
|
Re: [Acceleo] Generate a complex inheritance list [message #666679 is a reply to message #665684] |
Thu, 21 April 2011 14:51  |
Niels Brouwers Messages: 80 Registered: July 2009 |
Member |
|
|
Hi Cedric,
It seems to me that the meta-model is too far away from the target meta-model. When writing code generators, always make sure the gap to cross is relatively small. The abstraction level of the meta-model language should be almost the same as the target programming language. Proceed the M2T transformation with a M2M transformation, which actually performs the translation from the problem domain to the software domain.
In my opinion there are three things you can try to make the code templates easier:
1) Refactor the meta-model by adding the missing relationships. Create the instance model by hand, or preceed the M2T transformation with a M2M transformation, or
2) Create derived attributes in your meta-model that create the sequence of abstract classes, or
3) Create queries which populate the sequence (I assume this is possible in Acceleo)
Regards,
Niels.
Kind regards,
Niels Brouwers.
|
|
|
Goto Forum:
Current Time: Tue Jun 06 01:15:24 GMT 2023
Powered by FUDForum. Page generated in 0.02660 seconds
|