Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Xpand] Access to root element from within DEFINE block [SOLVED](Xpand Template: Accessing the root element of the uderlying model from within a DEFINE block)
[Xpand] Access to root element from within DEFINE block [SOLVED] [message #551629] Sun, 08 August 2010 13:51 Go to next message
jemurphy  is currently offline jemurphy
Messages: 4
Registered: August 2010
Junior Member
I wish to access the root element of the underlying model from within a DEFINE block that has been defined for the meta-class of a sub-element of the model. Can the root element be referenced directly, by global variable or passed to the DEFINE block with the sub-element? I am expanding the DEFINE block recursively in a manner that does not allow for simply passing the root element alone. If this is possible, could a simple example be provided.

[Updated on: Fri, 13 August 2010 03:44]

Report message to a moderator

Re: [Xpand] Access to root element from within DEFINE block [message #551630 is a reply to message #551629] Sun, 08 August 2010 13:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6315
Registered: July 2009
Senior Member
Hello,

depending on the metamodel you are using (e.g. for emf based ones) you can ask each model element for it's root in the model (eRootContainer )but this may not be what you want if the root is just a semantic one but not the physical root.

But of course you can add parameters to a definition and thereby pass the root.

Here a adapted sample created with the new xpand project wizard

«DEFINE main FOR Model»
«EXPAND javaClass(this) FOREACH entities()»
«ENDDEFINE»

«DEFINE javaClass(Model m) FOR Entity»
	«FILE name+".java"»
		public class «name» {
			«FOREACH features AS f»
				private «f.type.name» «f.name»;
				
				public void «f.setter()»(«f.type.name» «f.name») {
					this.«f.name» = «f.name»;
				}
				
				public «f.type.name» «f.getter()»() {
					return «f.name»;
				}
			«ENDFOREACH»
		}
	«ENDFILE»
«ENDDEFINE»

i don't see there is a usecase where this should not work.

and you can use globalvar as described in the docs. (std lib doc - part Globalvar extensions)

Regards
Christian

[Updated on: Sun, 08 August 2010 14:28]

Report message to a moderator

Re: [Xpand] Access to root element from within DEFINE block [message #551974 is a reply to message #551630] Tue, 10 August 2010 11:30 Go to previous messageGo to next message
jemurphy  is currently offline jemurphy
Messages: 4
Registered: August 2010
Junior Member

Christian,

Thank you for your response setting out the three alternatives to solving this problem. Unfortunately I had little success. The problems encountered are described below but first I will give some background. I am using the Helios build and the project was set up using the XText example with generation with an Ecore meta model. The pertinent parts of the current working Xpand template and Workflow are as follows:
============================================================ ===
register = org.webontologies.lineagedsl.LinDslStandaloneSetup {}
load = {
slot = "config"
type = "ConfigItem"
}

component = org.eclipse.xpand2.Generator {
expand = "templates::Template::main FOREACH config"
outlet = {
path = targetDir
}
------------------------------------------------------------ --

«DEFINE main FOR ConfigItem -»
«FILE "output.txt"-»
«EXPAND upperLineage FOR this.definitions -»
«ENDFILE-»
«ENDDEFINE»


«DEFINE upperLineage FOR List[Definition]-»
«IF this.size > 1»
---Some statements using the implicit this of List[Definition]
«EXPAND upperLineage FOREACH partition(this) -»
---Further Statements wishing to use ConfigItem
«ELSE»
«EXPAND bottomLineage FOR this.get(0) -»
«ENDIF»
«ENDDEFINE»


«DEFINE bottomLineage FOR Definition-»
---Some statements using the implicit this of Definition

--- Statement wishing to use ConfigItem
«ENDDEFINE»

------------------------------------------------------------ ---
Function partition(List[Definition]) returns a List of two List[Definition] in which the length of each List[Definition] is reduced in size by one from the original input.
============================================================ ====

FIRST SOLUTION

The preferred solution of passing the element by parameter produced the error (EvaluationException : No Definition 'upperLineage for List' found!)
In this approach I altered the template as below:
«EXPAND upperLineage(this) FOR this.definitions -»
«DEFINE upperLineage(ConfigItem conf) FOR List[Definition]-»
I also tried
«DEFINE upperLineage(List[ConfigItem] conf) FOR List[Definition]-»

I also get a similar error if FOR instead of FOREACH is used in the workflow expand statement even though config is a property with an upper bound of 1. (No Definition 'templates::Template::main for List' found!)

SECOND SOLUTION

I had difficulty with the second solution because I did not know how to correctly reference the config slot in the globarVar statement without getting errors such as "Couldn't resolve reference to Referrable 'config'". I did however manage to pass the String "config" into the Xpand template using the following statement.

globalVar = {
name = "__configItems"
value = "config"
}

THIRD SOLUTION

I also had difficulty with the third and least favoured solution of using the Ecore API to get the root container. Using the following syntax:

«IMPORT org::eclipse::emf::ecore::util::EcoreUtil»
«EcoreUtil.getRootContainer(this)»

I get the error "Couldn't find type or property 'EcoreUtil'"
============================================================ =====

Thank you very much for your previous reply. Any assistance or guidance you can give to resolve this problem would be very much appreciated.

Regards

John Murphy
SOLVED: Access to root element from within DEFINE block [message #552135 is a reply to message #551974] Wed, 11 August 2010 04:11 Go to previous message
jemurphy  is currently offline jemurphy
Messages: 4
Registered: August 2010
Junior Member
The problem experienced with passing the root element using parameters was due to the simple oversight of not including the parameter in the recursive call of upperLineage. I've become overly dependent on compiler checks.

Once again, thank you for your assistance,

Regards
John Murphy
Previous Topic:[Xpand] Multiple meta models and ClassCastException
Next Topic:XPAND OR MOFSCRIPT?
Goto Forum:
  


Current Time: Tue Sep 23 02:26:52 GMT 2014

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

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