Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Acceleo] Support for generate once
[Acceleo] Support for generate once [message #664050] Thu, 07 April 2011 06:06 Go to next message
Cedric Moonen is currently offline Cedric Moonen
Messages: 269
Registered: August 2009
Senior Member
Hello,

I'm trying to implement a system where some part of the code will be generated only if the file doesn't exist: this allows the user to modify those changes and regenerate the code without lossing those changes. Of course, the generation produces several files, some of them which will be recreated even if they exist.
However, I'm not sure if such a behavior can be implemented using Acceleo.
Do you have any idea how I can achieve something like that ?

Thanks,
C├ędric
Re: [Acceleo] Support for generate once [message #664078 is a reply to message #664050] Thu, 07 April 2011 08:44 Go to previous messageGo to next message
Stephane Begaudeau is currently offline Stephane Begaudeau
Messages: 406
Registered: April 2010
Location: Nantes (France)
Senior Member

Hi Cedric,

If you want to protect some parts of your generated code in order to let your users the possibility to add some code without losing this code after a new generation, Acceleo has two mechanisms for you:

1- Protected Areas
You create a protected area in Acceleo (with a unique identifier in a given module). If your users add some code in those protected area, it will not be lost during a new generation. This allow you to plan the area where your users will be able to contribute their code. You can see an example in a picture here
You can find an example of this mechanism in the UML to Java example (File -> New -> Example -> Acceleo -> UML to Java).
With this mechanism, we can define the methods and let the users do the implementation.

2- JMerge
If you are generating some Java, Acceleo will also support JMerge. With JMerge in Java, if your Java code has a "@generated" tag in its documentation the documented block will be override. If it has a "@generated NOT" tag, it will not be overridden during the next generation. You can see an example here.
This is the mechanism used by Acceleo to let the users customize the Java launcher class (the class created with the main module like "GenerateJava.java" in the UML to Java example).

If you want to see those example "in motion", you can see a video of those features here. (Video 7 starting at 7min25)
Or you can see some explanation in the wiki, here.

Stephane Begaudeau, Obeo

--
Twitter: @sbegaudeau
Acceleo wiki: http://wiki.eclipse.org/Acceleo
Blogs: http://stephanebegaudeau.tumblr.com & http://sbegaudeau.tumblr.com



[Updated on: Thu, 07 April 2011 08:45]

Report message to a moderator

Re: [Acceleo] Support for generate once [message #664262 is a reply to message #664078] Fri, 08 April 2011 03:15 Go to previous messageGo to next message
Cedric Moonen is currently offline Cedric Moonen
Messages: 269
Registered: August 2009
Senior Member
Hi Stephane,

Yes I'm aware of those two methods, however I'm generating C++ code so only the option with protected sections will work for me.

Furthermore, what is a bit annoying with this method is the fact that our users will have to SVN duplicate information: the real model and the generated code (which contains their modifications). Since there is not clean separation between generated code and user code, everything has to be commited.
This could cause some troubles when the generation template changes: in that case all thegenerated files will be modified but in fact the only thing that really changed is the generation template.

There could be a solution to this problem: we could keep an "artifact" file which contains only the protected sections (so, the code from the user) and then supply it in some way to Acceleo when the code is generated (I guess if we simply copy remove the contents of the generated file and copy the protected sections into it, Acceleo will accept that).
We will then need to be able to extract the protected sections from a file, does Acceleo provide some public classes to that ? It does it internal, but is there a way for us to easily retrieve that information ?

Another requirement that we have is the following: suppose that we generate protected section for each element in a list of our model (the name of the protected section will be the name of the element for instance). Take for example that in our model we specify the methods of a class: when the code is generated, there will be a protected section inside every method body. If the user removes such a method from the model, Acceleo will automatically remove the corresponding protected section.
What we need to have here is a way to warn the user that some of his code will be removed.

This can also be achieved in some way if we are able to retrieve the protected sections of a generated document.
Re: [Acceleo] Support for generate once [message #664275 is a reply to message #664262] Fri, 08 April 2011 03:50 Go to previous message
Laurent Goubet is currently offline Laurent Goubet
Messages: 1623
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------010202080402020109050304
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Cedric,

You could also define your own conventions as to how "user code" should
be added to the "generated code". Here is an example of what we do with
EMF generated code :

EMF generates a number of interfaces and implementations, along with a
"XyzPackageImpl" and "XyzFactoryImpl". We never add user code (and
"@generated NOT") to the generated implementations : we subclass them so
that we have "MyClass" the generated interface, "MyClassImpl" the
generated implementation, and "MyClassSpec extends MyClassImpl" the
subclass where we put our user code. With such a scheme, the only place
where we need to add "@generated NOT" annotations is the
"XyzFactoryImpl" in order for it to return "*Spec" instead of "*Impl".

This way, we can clearly separate "generated code" in its own source
folder (src-gen) and "user code" in its own source folder and classes
(src). Except for the little exception of the factory.

I don't really know what you are generating, but as long as you are in
charge of defining the generation templates, you could try and put
something of the sort in practice for your own generators.

Other than that ... well the generation strategies of Acceleo can be
extended, so you could make your own "JMerge-like" code merging
facilities. Take a look at the IAcceleoGenerationStrategy interface and
the AcceleoService constructor (that is called from the Java class we
generate along you "main" template).

Laurent Goubet
Obeo

On 08/04/2011 09:15, Cedric wrote:
> Hi Stephane,
>
> Yes I'm aware of those two methods, however I'm generating C++ code so
> only the option with protected sections will work for me.
>
> Furthermore, what is a bit annoying with this method is the fact that
> our users will have to SVN duplicate information: the real model and the
> generated code (which contains their modifications). Since there is not
> clean separation between generated code and user code, everything has to
> be commited.
> This could cause some troubles when the generation template changes: in
> that case all thegenerated files will be modified but in fact the only
> thing that really changed is the generation template.
>
> There could be a solution to this problem: we could keep an "artifact"
> file which contains only the protected sections (so, the code from the
> user) and then supply it in some way to Acceleo when the code is
> generated (I guess if we simply copy remove the contents of the
> generated file and copy the protected sections into it, Acceleo will
> accept that).
> We will then need to be able to extract the protected sections from a
> file, does Acceleo provide some public classes to that ? It does it
> internal, but is there a way for us to easily retrieve that information ?
>
> Another requirement that we have is the following: suppose that we
> generate protected section for each element in a list of our model (the
> name of the protected section will be the name of the element for
> instance). Take for example that in our model we specify the methods of
> a class: when the code is generated, there will be a protected section
> inside every method body. If the user removes such a method from the
> model, Acceleo will automatically remove the corresponding protected
> section. What we need to have here is a way to warn the user that some
> of his code will be removed.
>
> This can also be achieved in some way if we are able to retrieve the
> protected sections of a generated document.


--------------010202080402020109050304
Content-Type: text/x-vcard; charset=utf-8;
name="laurent_goubet.vcf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="laurent_goubet.vcf"

YmVnaW46dmNhcmQNCmZuOkxhdXJlbnQgR291YmV0DQpuOkdvdWJldDtMYXVy ZW50DQpvcmc6
PGEgaHJlZj0iaHR0cDovL3d3dy5vYmVvLmZyIj5PYmVvPC9hPg0KZW1haWw7 aW50ZXJuZXQ6
bGF1cmVudC5nb3ViZXRAb2Jlby5mcg0KdXJsOmh0dHA6Ly93d3cub2Jlby5m cg0KdmVyc2lv
bjoyLjENCmVuZDp2Y2FyZA0KDQo=
--------------010202080402020109050304--
Previous Topic:[xtend] HowTo: Enum as a parameter in switch-case
Next Topic:[Xtext] syntax errors while running the generator workflow
Goto Forum:
  


Current Time: Thu Aug 21 02:36:42 EDT 2014

Powered by FUDForum. Page generated in 0.01945 seconds