Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Acceleo 3] Howto use a model in memory
[Acceleo 3] Howto use a model in memory [message #638932] Sat, 13 November 2010 13:24 Go to next message
Frank Connor is currently offline Frank ConnorFriend
Messages: 2
Registered: November 2010
Junior Member
Hello,

I am new to Acceleo, but so far, I like it a lot. I am, however, struggeling with a concept concerning the workflow:

I have a File for which an editor exists, for example, a C-Header-File file.h or some description language (Avro description). The editors for these files always create an abstract syntax tree, which can be viewed in the editor in the outline section, e.g.

Now I want to use Acceleo to generate code from this syntax tree from within Eclipse. So, in addition to my mtl-Files I provided an autogenerated Acceleo UI, so I can just click on the file with the right mouse button and select the Acceleo M2T Code Generator.

The bad thing is, that the constructor of the main projects generate.java file gets called (with the URI file name "file.h") instead of the constructor that takes an EObject model (of the AST) as a parameter.

So, my question is: how can I access the model associated with the file file.h? Do I need to modify the editor (I'd prefer if that's not the case)? Or is that something, which can be handled in Acceleo (or even better: the UI)? Or is there a better way to apply Acceleo to the Abstract Syntax Tree instead of selecting the file fromwhich the AST was created? Or do I have to look to ANT-Files or MWE to solve the project in an elegant fashion?

How does this work?

Thank you in advance, folks!

Bye,
F.
Re: [Acceleo 3] Howto use a model in memory [message #639085 is a reply to message #638932] Mon, 15 November 2010 09:11 Go to previous messageGo to next message
Stephane Begaudeau is currently offline Stephane BegaudeauFriend
Messages: 458
Registered: April 2010
Location: Nantes (France)
Senior Member

Hi Frank,

Acceleo generates a Java class to launch the code generation from an EMF model. If you look carefully that Java class load the model in memory and then launch the generation. If you want to use an EObject in memory to launch the generation you can just observe what we are doing with our generated class:

1- we find the model URI and the target folder from the argument given to the main method
2- we create an AbstractAcceleoGenerator and we call initialize. this method will load the EMF model in memory with the call to org.eclipse.acceleo.common.utils.ModelUtils.load(URI, ResourceSet)
3- we launch the generation with the EObject loaded from the model (the root of the model)

If you want to use an "in memory" EObject, you can overload the initialize method and then "find/compute" your EObject, then you assign that EObject to the field model. and you can launch the generation with doGenerate.

Now for your problem with the editor, if the abstract syntax tree of the content of your editor is an EMF object, then you can use it as an input for the generation. If it is not an EMF object (and if your editor is not something from the eclipse modeling community or something build to use EMF, there is a good chance that the AST is not an EMF object) then you will have to transform the AST of the editor into an EMF based AST.

For example, the Java editor has an AST but it is not using EMF (because that would mean that the Java project would have to depend on the EMF project and they don't need EMF for a data structure like their AST, it's a bit overkill). even if I'm not familiar with the CDT project (if you are using their C/C++ editor), I pretty sure that they are not using EMF for their AST either. The Acceleo editor uses EMF for its AST and a couple project in the modeling community too.

"how can I access the model associated with the file file.h?"
- it completely depends on the editor, if it is the C/C++ editor of the CDT, you should ask the CDT community and if their AST cannot be accessed with their public API, you may have to download their source code to access it.

Do I need to modify the editor (I'd prefer if that's not the case)?
- As I said, if it is not coming from the modeling community, I would say that there is a 99% chance that you will have to modify the editor or at least create a connector to create an EMF model (even in memory) of your AST. But keep in mind that it would involve creating a metamodel of your whole language, it is not trivial.

Or is that something, which can be handled in Acceleo (or even better: the UI)? Or is there a better way to apply Acceleo to the Abstract Syntax Tree instead of selecting the file fromwhich the AST was created?
- If you give Acceleo an EObject or an EMF model and an Acceleo module, we can launch the generation, but we cannot "find" that EObject for you, sorry :/
But if you have your AST as an EObject, you can plug Acceleo anywhere. For example, if you create an Acceleo UI project from your acceleo project, and then if you deploy it, you can launch the generation from a simple right click on your model. If you want to plug Acceleo somewhere, it is simple Acceleo can be used as a stand alone component with no dependencies with Eclipse (you can use it in a server, in an embedded application, etc.) so If you can call a Java program, you can use Acceleo. Even the Acceleo parser/compiler is a stand alone component.

Or do I have to look to ANT-Files or MWE to solve the project in an elegant fashion?
- I don't a way in which Ant or MWE could help you here (for the editor / AST /EMF part). But if you had access to that EMF based AST you could very easily use "Ant/MWE/Maven/Anything else" to then call Acceleo. Our API can be called from a simple Java program (the generated java class is launched with a simple "main") so if you can use Java, you can use Acceleo.


There are two parts in your problem:
1- find an EMF representation of your file
2- generate code from it

The second part, as I told you is simple you just have to modify the generated class and its done, Acceleo can be called with an EObject, from anywhere, it's easy.
For the first part, if you cannot access an EMF version of your AST, I would suggest you to see the MoDisco project. The goal of their project is to create an EMF model of a given file (I am not sure if the input file as to be in a specific language like Java or if you can take any kind of input language). You may have to write an adapter for your editor. And then after that you can easily plug Acceleo in MoDisco.


Stephane Begaudeau, Obeo
Re: [Acceleo 3] Howto use a model in memory [message #639156 is a reply to message #639085] Mon, 15 November 2010 12:51 Go to previous messageGo to next message
Laurent Goubet is currently offline Laurent GoubetFriend
Messages: 1902
Registered: July 2009
Senior Member
Hi Frank,

To complete Stephane's post on the "MoDisco" part, you can take a look at the work from Angelo Zerr on JM2T where he used MoDisco to create a model from a Java file in order to launch Acceleo generations directly from these models.

I don't know, however, whether MoDisco provides a model for C/C++.

Laurent Goubet
Obeo
Re: [Acceleo 3] Howto use a model in memory [message #639250 is a reply to message #638932] Mon, 15 November 2010 18:05 Go to previous message
Frank Connor is currently offline Frank ConnorFriend
Messages: 2
Registered: November 2010
Junior Member
Thank you both very much for clearing up the situation!

I will take closer a look at the editors I want to use and MoDisco.

Thanks again,
F.
Previous Topic:[acceleo]Accessing UML elements in SysML Model
Next Topic:[Acceleo] Problem when generating code with GMF
Goto Forum:
  


Current Time: Fri Apr 19 23:43:51 GMT 2024

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

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

Back to the top