Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Xtend] Accessing EClass of a model element?
[Xtend] Accessing EClass of a model element? [message #543725] Wed, 30 June 2010 12:26 Go to next message
Abdull is currently offline 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 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6388
Registered: July 2009
Senior Member
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

[Updated on: Wed, 30 June 2010 12:47]

Report message to a moderator

Re: [Xtend] Accessing EClass of a model element? [message #543799 is a reply to message #543731] Wed, 30 June 2010 15:27 Go to previous messageGo to next message
Abdull is currently offline Abdull
Messages: 32
Registered: May 2010
Member
Great, this works - thank you Christian!

I guess I cannot use a property "myobject.getEClass()" directly (that is, I have to use your given JAVA extension), as the EMF public interface EObject exposes a method of name "eClass()", and not of name "getEClass()".
Re: [Xtend] Accessing EClass of a model element? [message #543803 is a reply to message #543799] Wed, 30 June 2010 15:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6388
Registered: July 2009
Senior Member
Hi,

the eClass() Method is not directly available cause the Xtend EMF Metamodel Implementation does not support this. feels free to file an enhancement request to bugzilla.

Regards,
Christian

[Updated on: Wed, 30 June 2010 15:48]

Report message to a moderator

Re: [Xtend] Accessing EClass of a model element? [message #544654 is a reply to message #543799] Mon, 05 July 2010 08:10 Go to previous message
Sven Efftinge is currently offline Sven Efftinge
Messages: 1769
Registered: July 2009
Senior Member
Abdull schrieb:
> Great, this works - thank you Christian!
>
> I guess I cannot use a property "myobject.getEClass()" directly (that
> is, I have to use your given JAVA extension), as the EMF public
> interface EObject exposes a method of name "eClass()", and not of name
> "getEClass()".

You could use the JavaBeansMetamodel instead of the EMF Metamodel.
To do so change the corresponging configuration in your workflow file as
well as in the preferences of your Eclipse workbench.
The main difference is that you use the java qualified names instead of
the shorter EMF namespaces (which boils down to a different import
statement in your Xtend and Xpand files).

Using JavaBeans Metamodel is better since it integrates better with
other Java types and Java extensions. Also the tooling is slightly
better since it is based on JDT and doesn't need the long running EMF
analyzers.

Sven


--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : blog.efftinge.de


--
Need professional support on Xtext or Xtend?
Mail to: xtext (at) itemis.com
Twitter : @svenefftinge
Blog : blog.efftinge.de
Previous Topic:embedded XPand tools in an application
Next Topic:[Xtend] Editor shows wrong errors
Goto Forum:
  


Current Time: Sun Oct 26 08:04:01 GMT 2014

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

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