[Xtend] Accessing EClass of a model element? [message #543725] |
Wed, 30 June 2010 12:26 |
Abdull Messages: 32 Registered: May 2010 |
Member |
|
|
Hello everybody,
within an Xtend transformation, is there an easy way to access a model element's EClass?
Imagine I have an .ecore file that contains EClasses A, B, C, and D.
Further imagine I have an instance of said .ecore file with model elements a1, a2, a3, b1, c1, c2.
...a1, a2, and a3 are of type A
...b1 is of type B and
...c1 and c2 are of type C.
Within an Xtend transformation I would like to identify all used EClasses. For instance, with the set myInstances = {a1, a2, a3, b1, c1, c2}, I'd like to derive a new set {A, B, C}.
Currently, I'm using the rather cumbersome way of
myInstances.collect(e|e.metaType).toSet()
then an element "aMetaType" of this derived set is processed the following way:
let typeName = aMetaType.toString() :
switch (typeName) {
case 'myPackage::A' : someExpression1
case 'myPackage::B' : someExpression2
case 'myPackage::C' : someExpression3
case 'myPackage::D' : someExpression4
default : expressionForUnknownType
};
(... e.metaType is of type xpand2::Type)
This solution depends on string equality which is not nice imho.
So, coming back to my initial question: what is the easiest and nicest way to get a model element's EClass?
Thanks, Abdull
|
|
|
Re: [Xtend] Accessing EClass of a model element? [message #543731 is a reply to message #543725] |
Wed, 30 June 2010 12:44 |
|
Hi, why don't you make use of a (Java) Extension?
ecore::EClass eClass(emf::EObject o) :
JAVA template.Helper.eClass(org.eclipse.emf.ecore.EObject);
package template;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
public class Helper {
public static EClass eClass(EObject o) {
return o.eClass();
}
}
«DEFINE main FOR Model»
«FILE "test.txt"»
«this.eRootContainer.eAllContents.eClass().toSet().name»
«ENDFILE»
«ENDDEFINE»
Regards,
Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
[Updated on: Wed, 30 June 2010 12:47] Report message to a moderator
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03757 seconds