Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » Running template just once for an Ecore model
Running template just once for an Ecore model [message #1780239] Thu, 18 January 2018 23:40 Go to next message
Gonzalo Sintas is currently offline Gonzalo SintasFriend
Messages: 3
Registered: January 2018
Junior Member
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 #1780331 is a reply to message #1780239] Sat, 20 January 2018 12:21 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7679
Registered: July 2009
Senior Member
Hi

I don't really understand your problem. Surely you have one file per whatever so your once per whatever goes at the start of the file? Nothing to do with models.

The Ecore counterpart of a Model is an EResource which is second class model element (an EDataType rather than an EClass). This prevents EResource being used in many contexts.

For many Ecore models there is only one EPackage or at any rate only one root EPackage so you can use this as the 'Model'.

Regards

Ed Willink
Re: Running template just once for an Ecore model [message #1780342 is a reply to message #1780331] Sat, 20 January 2018 17:38 Go to previous messageGo to next message
Gonzalo Sintas is currently offline Gonzalo SintasFriend
Messages: 3
Registered: January 2018
Junior Member
Hi Ed. Thanks for your reply

I have only one file. I process each of the elements in the Ecore Model and that adds a line in my file.

That works fine. What I need is to use acceleo to write something before that, that is written only once when the transformation is executed.

Right now I have this template that goes through each EObject in the model and writes one line based on the EObject it is processing

[template public generate(aObject : EObject) post (trim())]
[comment @main /]
[file ('TEST1.txt', true)]
Write something with the information of the aObject
[/file]
[/template]

What I'm looking for is a way to do the same for a Model. I have tried EResource and EPackage like this:

[template public generate(aPackage: EPackage)]
[comment @main /]
[file ('TEST1.txt', true)]
Write Header
[/file]
[/template]

[template public generate(aResource: EResource)]
[comment @main /]
[file ('TEST1.txt', true)]
Write Header
[/file]
[/template]

and run this before the first part where the EObjects are processed, si that a header is written in my Text file (TEST1.txt), but none of those work (the template isn't executed)

Maybe there is a way in Acceleo to write something at the begining of the file withouht depending on the Model and/or it's elements, but I haven't found a way to do it.

I hope this is a better explanation of the problem. Sorry If I'm mixing concepts, I have recently started with Acceleo and some things aren't clear yet.
Re: Running template just once for an Ecore model [message #1780356 is a reply to message #1780342] Sun, 21 January 2018 07:08 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7679
Registered: July 2009
Senior Member
Hi

As I explained before there is no Ecore Model. You provide no repro so I'm left to guess what a Model is to you.

Perhaps if you provided a trivial input model and what you are trying to get as output and your best attempt, what you want might be clearer.

Regards

Ed Willink
Re: Running template just once for an Ecore model [message #1780467 is a reply to message #1780356] Mon, 22 January 2018 22:04 Go to previous messageGo to next message
Gonzalo Sintas is currently offline Gonzalo SintasFriend
Messages: 3
Registered: January 2018
Junior Member
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 09:05 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7679
Registered: July 2009
Senior Member
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

Previous Topic:[Acceleo] Problem with the deleted protected zones
Next Topic:Mulitple input models to Acceleo
Goto Forum:
  


Current Time: Sat Nov 09 01:25:14 GMT 2024

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

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

Back to the top