Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [XPAND] Traverse all elements of a class([XPAND] Traverse all elements of a class)
[XPAND] Traverse all elements of a class [message #873604] Fri, 18 May 2012 16:28 Go to next message
Søren Qvist is currently offline Søren QvistFriend
Messages: 3
Registered: May 2012
Junior Member
Hey there,

I'm looking for a feature in XPAND to list/access all attributes of a given class.
I could only find old entries suggesting the following statement: "«FOREACH allElements() ...", but it seems to be obsolete.

Is there any way to achieve this functionallity with the current release?

Thank you,
Søren
Re: [XPAND] Traverse all elements of a class [message #873611 is a reply to message #873604] Fri, 18 May 2012 16:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

the allElements method was oAW Xtext specific. never the less there are methods like eAllAttributes,eAllReferences,eAllStructuralFeatures...
available on EClass

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [XPAND] Traverse all elements of a class [message #873640 is a reply to message #873611] Fri, 18 May 2012 18:32 Go to previous messageGo to next message
Søren Qvist is currently offline Søren QvistFriend
Messages: 3
Registered: May 2012
Junior Member
Hi Christian,

thanks for your answer!

I tried a few things, but unfortunately I didn't get things done.
Could you please provide me with further instructions/documentation to keep going on?

In particular, after I type «ecore::EClass.» there's no code-completion anymore and the suggested method(s) you mentioned can't be found.

The relevant template-part looks like this:
..
«DEFINE buildWidget FOR Widget»
<«this.keyword»>
// this is where the magic should happen
</«this.keyword»>
«ENDDEFINE»
..

Maybe some background information:
I have modeled an abstract superclass "Widget" that has LOTS of subclasses. Some common attributes (id, height, width, ...) are modeled in the class Widget. It'd be a horror-scenario to write a template for every subclass, so I'm looking for a way to approach something similar with XPand:
<pseudo-code>
for (attribute : getAllAttributes()) {
print(attribute, getValue(attribute))
}
</pseudo-code>

Thanks a lot,
Søren
Re: [XPAND] Traverse all elements of a class [message #873652 is a reply to message #873640] Fri, 18 May 2012 19:11 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

your snippet makes no sense to me. btw wich metamodel kind do you use? (java / emf(registry) = dynamic emf)
dynamic emf does not allow this kind of reflection. so you may have to deal with xpands reflection

this.metatype.....

or you use a javaextension

ecore::EClass getEClass(emf::EObject o):
	JAVA template.Helper.getEClass(org.eclipse.emf.ecore.EObject);	
	
Object eGet(emf::EObject o, ecore::EStructuralFeature f) :
	JAVA template.Helper.eGet(org.eclipse.emf.ecore.EObject, org.eclipse.emf.ecore.EStructuralFeature);	



package template;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;

public class Helper {
	
	public static EClass getEClass(EObject o) {
		return o.eClass();
	}
	
	public static Object eGet(EObject o, EStructuralFeature f) {
		return o.eGet(f);
	}

}


«FILE name+".txt"»
«FOREACH this.getEClass().eAllAttributes AS a»
«a.name» «eGet(a)»
«ENDFOREACH»
«ENDFILE»


alternatively: if you can use the javabeansmetamodel it will work out of the box

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:[Acceleo]I don't want to chang the timestamp when the content is not changed
Next Topic:[Acceleo] Unstable behavior when using a query from an imported module
Goto Forum:
  


Current Time: Fri Mar 29 14:02:07 GMT 2024

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

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

Back to the top