Running template just once for an Ecore model [message #1780239] |
Thu, 18 January 2018 18:40  |
Eclipse User |
|
|
|
Hi!
I'm new to Acceleo, been working with it for a couple of weeks, and have been able to create a simple M2T transformation from an Ecore Model by iterating though the model's EObjects.
[template public generate(aClass : EObject) post (trim())]
[comment @main /]
[file ('TEST1.txt', true)]
Do something...
[/file]
[/template]
Now I need my transformation to generate a "header" before processing all objects. I've seen examples using UML models where they use the Model class as the elements to iterate in the template definition
[template public generate(aModel : Model) post (trim())]
[/template]
which allows to generate text once for each model defined (my example has just one Ecore Model, so something similar would work).
I haven't been able to find a way to do something like this for an Ecore Model. Does anyone know how to do this using Acceleo?
There is one ugly solution I know would solve the problem that is using a Java Wrapper so that, when iterating through the EObjects, I can use a boolean flag so that the content of the template is written just once
[i][template public generate(aClass : EObject) post (trim())]
[comment @main /]
[file ('TEST1.txt', true)]
[if (firstIteration())]
Write Header...
[/if]
[/file]
[/template][/i]
but I would like to solve this using just Acceleo if possible.
Regards,-
|
|
|
|
|
|
Re: Running template just once for an Ecore model [message #1780467 is a reply to message #1780356] |
Mon, 22 January 2018 17:04   |
Eclipse User |
|
|
|
I will show you everything I have, maybe I'm misunderstanding something.
This is my Ecore Metamodel:
package model : model = 'http://www.example.org/model'
{
class Entity
{
attribute id : ecore::EInt[1];
property animals: Animal[*|1] { ordered composes };
}
class Animal
{
attribute id : ecore::EInt[1];
attribute legs : ecore::EInt[1];
}
class Mammal extends Animal
{
attribute name : String[?];
}
class Dog extends Mammal
{
attribute breed : String[?];
attribute age : ecore::EInt[1];
}
}
Based on this metamodel, I've built a simple model
<model:Entity
id="100">
<animals
xsi:type="model:Dog"
legs="4"
id="1"
name="Keoni"
breed="Labrador"
age="12"/>
<animales
xsi:type="model:Dog"
legs="4"
id="2"
name="Hasso"
breed="Golden Retriever"
age="2">
</animales>
</model:Entidades>
I'm working on a project that transforms these types of Models and Metamodels into a Haskell representation. The whole representation is in a single file.
When processing the Model, I need to come out with an output similar to this one:
example = Model [
(ModelElement 0 (AnimalCh (Animal 4 1 (MammalCh (Mammal "Keoni" (DogCh (Dog "Labrador" 12)))))))
, (ModelElement 1 (AnimalCh (Animal 4 2 (MammalCh (Mammal "Hasso" (DogCh (Dog "Golden Retriever" 2)))))))
, (ModelElement 2 (EntityCh (Entity 100 [0, 1])))
]
That represents the way the model is defined in the Haskell representation I have.
So, I have the code that generates the representation for the three elements in the Model. I can get that just by iterating in the different EObjects in the Model, what I need is to write the "example = Model [" at the beginign and the "]" at the end (there is a little more text before and after, but I think the example is good enough to describe the problem).
[comment encoding = UTF-8 /]
[module generate('http://www.eclipse.org/emf/2002/Ecore', 'http://www.example.org/model')]
[template public generateHeader(model : EPackage)]
[comment @main /]
[file ('TEST1.txt', true)]
example = Model ['['/]
[/file]
[/template]
[template public generate(aObject : EObject) post (trim())]
[comment @main /]
[file ('TEST1.txt', true)]
(ModelElement ([for (parent : EClass | aObject.eClass().getParents())] .... [/for]
[/file]
[/template]
[template public generateFooter(model : EPackage)]
[comment @main /]
[file ('TEST1.txt', true)]
[']'/]
[/file]
[/template]
Above is the Acceleo transformation I have. As I said before, the part where the elements (EObjects) are processed works fine, what I can't manage to achieve is running the Headers and Footers, so that I can get the output shown before. I belive there should be a way to do this using an acceleo transformation, but I'm clearly missing something. I'm open to other suggestions as to how to do this. I know I can "trick" the tranformation using a Java Wrapper, but I would like to avoid that if possible.
I hope the situation is clear now, and thanks a lot for your help Ed.
Regards,-
|
|
|
Re: Running template just once for an Ecore model [message #1780486 is a reply to message #1780467] |
Tue, 23 January 2018 04:05  |
Eclipse User |
|
|
|
Hi
Ok, it's a bit clearer, though it would be more useful to have your files in a ZIP to avoid cut and paste and guessing at .project and MANIFEST.MF and of course guessing at "...." and at what your actual output is. What you think is enough very rarely is and so wastes a lot of everyone's time. See https://wiki.eclipse.org/OCL/ForumNetiquette With full initial details, you might have had an answer 4 days ago.
Normally if you want to generate one file, e.g. Test1.txt, you have one template that contains a [file ...] clause. You have three. I don't know whether "...." calls generateHeader/generateFooter. But if they do they will overwite the file. If not they do nothing.
Regards
Ed Willink
|
|
|
Powered by
FUDForum. Page generated in 0.03537 seconds