Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » navigating inverse of references?
navigating inverse of references? [message #804629] Wed, 22 February 2012 20:51 Go to next message
Scott Finnie is currently offline Scott FinnieFriend
Messages: 94
Registered: October 2011
Member
Apologies, am sure this must be answered somewhere but can't find it.

Is there an easy way to navigate the inverse of a reference?

For example (from tutorial):

Feature:
  many?='many'? name = ID ':' type = [Type]
;


given an instance f1 of Feature, the type is accessible as f1.getType(). But what about the other way? i.e. given an instance of Type, how to find all Features that use it?

Thanks.
Re: navigating inverse of references? [message #804652 is a reply to message #804629] Wed, 22 February 2012 21:25 Go to previous messageGo to next message
Alex Tugarev is currently offline Alex TugarevFriend
Messages: 14
Registered: July 2011
Junior Member
Hi,

you can use the index for this kind of tasks. Have a look at the following snippet.

@Inject protected ResourceDescriptionsProvider resourceDescriptionsProvider;

Resource elementResource = element.eResource();

IResourceDescriptions resourceDescriptions = resourceDescriptionsProvider.getResourceDescriptions(elementResource);
IResourceDescription resourceDescription = resourceDescriptions.getResourceDescription(EcoreUtil2.getURI(element));
Iterable<IReferenceDescription> referenceDescriptions = resourceDescription.getReferenceDescriptions();

for (IReferenceDescription referenceDescription : referenceDescriptions) {
	// set a breakpoint here and inspect "referenceDescription" in debugger
}


Regards,
Alex
Re: navigating inverse of references? [message #805126 is a reply to message #804652] Thu, 23 February 2012 11:07 Go to previous message
Scott Finnie is currently offline Scott FinnieFriend
Messages: 94
Registered: October 2011
Member
Alex, thanks for the reply. Tried your suggestion but getting NPE on call to eResource().
Here's the code:

@Test
public void testFindInverse() {
	Class cls = OoaFactory.eINSTANCE.createClass();
	cls.setName("TheClass");
	Domain domain = OoaFactory.eINSTANCE.createDomain();
	domain.setName("TheDomain");
	domain.getElements().add(cls);

	Resource elementResource = cls.eResource();
	ResourceDescriptionsProvider resourceDescriptionsProvider = new ResourceDescriptionsProvider();
	IResourceDescriptions resourceDescriptions = resourceDescriptionsProvider.getResourceDescriptions(elementResource);
	IResourceDescription resourceDescription = resourceDescriptions.getResourceDescription(EcoreUtil2.getURI(cls));
	Iterable<IReferenceDescription> referenceDescriptions = resourceDescription.getReferenceDescriptions();
		
	for (IReferenceDescription referenceDescription : referenceDescriptions) {
		@SuppressWarnings("unused")
		int hashcode = referenceDescription.hashCode();
		// set a breakpoint here and inspect "referenceDescription" in debugger
	}	
}


Just getting to grips with EMF/xtext APIs so not very familiar with them. Wonder if it's something to do with running as unit test?

Thanks,
Scott.
Previous Topic:Using "weird" character literals
Next Topic:ignore some errors
Goto Forum:
  


Current Time: Fri Apr 19 22:01:42 GMT 2024

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

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

Back to the top