Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Access a reference's feature(Given an EReference, how to access the referenced object's name?)
Access a reference's feature [message #786638] Mon, 30 January 2012 17:07 Go to next message
Joey Mink is currently offline Joey MinkFriend
Messages: 87
Registered: July 2009
Location: Centreville, VA, USA
Member

Hi,

I hope the following description and code snippets makes my problem clear. I have a ForceDefinition:

ForceDefinition:
	keyword=ForceDefKeyword
	name=STRING
	(
		side=[SideDefinition|STRING]
		& attributes+=Attribute*
		& attributes+=EnemyAttribute*
		& attributes+=UnitAttribute*
		& attributes+=SatelliteAttribute*
	)
	=>EndKeyword
;


Which, as you saw above, can reference a SideDefinition:

SideDefinition:
	keyword=SideDefKeyword
	name=STRING
	attributes+=Attribute*
	=>EndKeyword
;


I test things out with DSL text that looks like this:

Plan "hierarchy"

SideDef "Good guys"
End

SideDef "Bad guys"
End

ForceDef "Good force"
	Side	"Good guys"
End

End


I am accessing Xtext's global index in my Java code, and I manage to obtain an EReference from a ForceDefinition instance:

		for (IEObjectDescription eObjectDesc : 
			desc.getExportedObjectsByType(TplXPackage.Literals.FORCE_DEFINITION))
		{
		// Force's name?
		QualifiedName qName = eObjectDesc.getQualifiedName();
		String forceName = qName.getLastSegment();
		
		// Does this Force reference a Side?
		String sideName = null;
		for (EReference eRef : eObjectDesc.getEClass().getEReferences())
		{
			if (eRef.getName().equals("side"))
				// TODO: Wish I could access the name of this side...
		}
		}


How can I achieve the TODO above? What approach would allow me to determine the name of the SideDefinition being referenced?
Re: Access a reference's feature [message #787179 is a reply to message #786638] Tue, 31 January 2012 08:48 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
(This is really an EMF question.)

You mean to obtain the value of the EReference's target, right? You need an EObject for that, which is given by IEObjectDescription.getEObjectOrProxy(), so this should work (untested):
((SideDefinition) desc.getEObjectOrProxy().eGet(eRef)).getName()


Re: Access a reference's feature [message #787196 is a reply to message #787179] Tue, 31 January 2012 09:01 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Meinte, hi Joey,

this will likely return the proxy which has no values set (usually).
You'd have to resolve that with a resource set.

XtextResourceSet rs = resourceSetProvider.get(myProject);
ForceDef fd = rs.getEObject(description.getEObjectURI(), true);
fd.get(..)

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 31.01.12 09:48, schrieb Meinte Boersma:
> (This is really an EMF question.)
>
> You mean to obtain the value of the EReference's target, right? You need
> an EObject for that, which is given by
> IEObjectDescription.getEObjectOrProxy(), so this should work (untested):
>
> ((SideDefinition) desc.getEObjectOrProxy().eGet(eRef)).getName()
>
Re: Access a reference's feature [message #787441 is a reply to message #787179] Tue, 31 January 2012 15:32 Go to previous message
Joey Mink is currently offline Joey MinkFriend
Messages: 87
Registered: July 2009
Location: Centreville, VA, USA
Member

Meinte,

I expanded your one-line suggestion and found that the call to eGet(...) returns null:

		// Does this Force reference a Side?
		String sideName = null;
		for (EReference eRef : eObjectDesc.getEClass().getEReferences())
		{
			if (eRef.getName().equals("side"))
			{
				EObject objOrProxy = eObjectDesc.getEObjectOrProxy();
				Object obj = objOrProxy.eGet(eRef);	// <-- returns null
				SideDefinition sideDef = (SideDefinition)obj;
				sideName = sideDef.getName();
			}
		}


I'm not sure if this is what Sebastian was referring to when he said, "the proxy which has no values set (usually)."

Sebastian,

Thanks for the advice - I obtained an XtextResourceSetProvider the following way:

Class MyClass {
...
	@Inject
	private XtextResourceSetProvider xtextResourceSetProvider;
...
	public MyClass ()
	{
		Injector injector = 
				TplXActivator.getInstance().getInjector("com.exoanalytic.seas.tplx.TplX");
		injector.injectMembers(this);
	}
...
}


And used the following code to obtain the SideDefinition referenced by a ForceDefinition:

                ResourceSet xtextResourceSet = xtextResourceSetProvider.get(currentProject);
		for (IEObjectDescription eObjectDesc : 
			desc.getExportedObjectsByType(TplXPackage.Literals.FORCE_DEFINITION))
		{
		    ForceDefinition forceDef = (ForceDefinition)
				    xtextResourceSet.getEObject(eObjectDesc.getEObjectURI(), true);
		
		    SideDefinition referencedSideDef = forceDef.getSide();
		    String referencedSideName = referencedSideDef.getName();
                    ...
		}


Thanks much for the replies!
Previous Topic:Generic access for QualifiedName
Next Topic:Conditional formatter
Goto Forum:
  


Current Time: Fri Apr 26 01:17:42 GMT 2024

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

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

Back to the top