How to find the cross-references of an Eobject across files? [message #1835491] |
Fri, 04 December 2020 11:18  |
Eclipse User |
|
|
|
I have the following grammar. I have simplified it for the sake of demonstrating my issue.
Head:
model=Model;
Model:
ActivityModel | ComponentModel;
ActivityModel:
activities+=Activity+
;
Activity:
name=ID
;
ComponentModel:
'Component' name=ID
activities+=ComponentActivity+
;
ComponentActivity:
name=ID ':'
activityReference=[Activity]
;
I have these two files:
activityModel.myDsl
componentModel.myDsl
Component system
a : MyActivity
b : MyActivity
My goal is to navigate to ComponentModel instance from the Activity instance.
So, I tried accessing the "eCrossReferences" property of the activity but that was thrown out quickly since the list was empty.
Next, I started looking into "EcoreUtil.UsageCrossReferencer" but for that I need to give a resource as a parameter so it knows where to look. Is that the right direction? How do I find the resource, which is basically another file?
// Xtend code
val resource = ???;
val settings = EcoreUtil.UsageCrossReferencer.find(activity,
resource);
for (setting : settings) {
if (setting.EObject instanceof ComponentActivity) {
return setting.EObject.eContainer() as TranslationModel;
}
}
|
|
|
|
|
|
|
|
Re: How to find the cross-references of an Eobject across files? [message #1835576 is a reply to message #1835572] |
Mon, 07 December 2020 08:26   |
Eclipse User |
|
|
|
Hello Giray,
The following code snippet demonstrates the usage of the IReferenceFinder interface from the ActivityGenerator class:
package org.xtext.example.mydsl.generator
import com.google.inject.Inject
import com.google.inject.Provider
import org.eclipse.core.runtime.NullProgressMonitor
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EReference
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.xtext.findReferences.IReferenceFinder
import org.eclipse.xtext.findReferences.IReferenceFinder.IResourceAccess
import org.eclipse.xtext.findReferences.TargetURIs
import org.eclipse.xtext.resource.IReferenceDescription
import org.eclipse.xtext.resource.IResourceDescriptions
import org.xtext.example.mydsl.myDsl.Activity
import org.xtext.example.mydsl.myDsl.ActivityModel
class ActivityGenerator {
@Inject IReferenceFinder referenceFinder
@Inject Provider<TargetURIs> targetURIProvider
@Inject IResourceDescriptions resourceDescriptions
def generate(ActivityModel activityModel) '''
«FOR activity : activityModel.activities»
Activity «activity.name» is referenced by «getReferenceCount(activity)» component activities.
«ENDFOR»
'''
def getReferenceCount(Activity activity) {
val acceptor = new MyDslReferenceAcceptor
val uri = EcoreUtil.getURI(activity)
val targetURIs = targetURIProvider.get
targetURIs.addURI(uri)
referenceFinder.findAllReferences(targetURIs, null, resourceDescriptions, acceptor, new NullProgressMonitor)
return acceptor.count
}
}
class MyDslReferenceAcceptor implements IReferenceFinder.Acceptor {
int count = 0
override accept(IReferenceDescription description) {
count++
}
override accept(EObject source, URI sourceURI, EReference eReference, int index, EObject targetOrProxy, URI targetURI) {
}
def getCount() {
count
}
}
Hope that helps,
Tamás
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.06650 seconds