Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtend: generate source from multiple resources
Xtend: generate source from multiple resources [message #685807] Mon, 20 June 2011 09:06 Go to next message
Christian Gudrian is currently offline Christian GudrianFriend
Messages: 11
Registered: May 2011
Junior Member
Hello!

Given a set of source files each of which defines one or more entities: how can I have my Xtend template generate a list of all entities across all resources?

Christian
Re: Xtend: generate source from multiple resources [message #685812 is a reply to message #685807] Mon, 20 June 2011 09:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
Hi,

this is done by the default setup automatically. the workflow reads all the files an the calls the generator foreach resource.

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtend: generate source from multiple resources [message #685813 is a reply to message #685812] Mon, 20 June 2011 09:35 Go to previous messageGo to next message
Christian Gudrian is currently offline Christian GudrianFriend
Messages: 11
Registered: May 2011
Junior Member
Hi!

As you've pointed out the generator is called once for each resource. So generating a list of all entities within that one resource is trivial.

However, I would like to have one file generated which contains all entities in all resources. To achieve that the generator must be passed a list of all resources instead of just one resource at a time.

I suppose that currently that's not possible (at least not with the default workflow) since the generator gets only called once for each resource that has been modified.

Christian
Re: Xtend: generate source from multiple resources [message #685814 is a reply to message #685813] Mon, 20 June 2011 09:36 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
Hi,

yes this usecase is currently not supported.

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtend: generate source from multiple resources [message #685817 is a reply to message #685814] Mon, 20 June 2011 09:43 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

in the generator call, you can simply use EXPAND ... *FOR modelslot* instead of *FOREACH modelslot*. You template then gets the list of all resources. Then you are free to do with the list whatever you want, e.g. generate into one file code for all the slot entries.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Xtend: generate source from multiple resources [message #685832 is a reply to message #685817] Mon, 20 June 2011 10:19 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
Hi,

@Alex this is true for the Generator of XPand but not for the one in Xtend 2 / IGenerator Interface

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtend: generate source from multiple resources [message #685838 is a reply to message #685832] Mon, 20 June 2011 10:39 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
OK, so Xtend was not a typo but referred to Xtend2 Wink

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Xtend: generate source from multiple resources [message #685842 is a reply to message #685838] Mon, 20 June 2011 11:02 Go to previous messageGo to next message
Christian Gudrian is currently offline Christian GudrianFriend
Messages: 11
Registered: May 2011
Junior Member
Quote:
Xtend was not a typo but referred to Xtend2


I referred to the file extension, actually. Smile

Sorry for the confusion.

Christian
Re: Xtend: generate source from multiple resources [message #687595 is a reply to message #685842] Wed, 22 June 2011 20:01 Go to previous message
Laurent Kempeneers is currently offline Laurent KempeneersFriend
Messages: 2
Registered: June 2011
Junior Member
Hi Christian,

Here is a solution (maybe not the best) to this issue.

1) I create a specific function with XTend 2 to merge the models contained in the different resources into one merged model:

package be.etnic.dsl.datamodel.util.xtend

import java.util.List
import com.google.inject.Inject
import static extension org.eclipse.xtext.xtend2.lib.ResourceExtensions.*

// The model behind the language (named DataModel)
import be.etnic.dsl.datamodel.dataModel.*

class DataModelHelper {

	// Merge function that creates a new model instance (DataModel) and fills it with the others model's contents
	def create result : DataModelFactory::eINSTANCE.createDataModel merge2(List<DataModel> models) {
		// The resulting name of the merged model is the one of the first model that has a name    
		result.name = models.findFirst(e|e.name != null).name 
		// The types are the union of all the others model's types
		result.types.addAll(models.map(e | e.types).flatten.toList)
		// Idem for packageElements
		result.packageElements.addAll(models.map(e | e.packageElements).flatten.toList)
		//...
	}
}


2) I create an action on a project that reads all of its resources and makes a 'List' of all the 'DataModel' instances. Then I call the Xtend 'merge' function (through the generated Java method) with those 'DataModel' instances.
3) Finally I put the resulting merged 'DataModel' into a new Resource that I can save into one single XMI file for example or that I can pass to the standard IGenerator function.


I must confess I'm new to Xtend 2. I have a first experience with Xtend, but I'm still a bit lost with 2.0... however I'm excited to learn it! Then, if someone has a better solution, I'll take it ;o)

Laurent
Previous Topic:Use EMF outline instead of Xtext outline
Next Topic:Building XText Projects with Maven Sample
Goto Forum:
  


Current Time: Fri Apr 26 19:37:18 GMT 2024

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

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

Back to the top