Load EObject with all cross-references from file path [message #1723512] |
Tue, 16 February 2016 10:21  |
Andreas Stange Messages: 2 Registered: February 2016 |
Junior Member |
|
|
Hello,
I have a model A with references model B. Both are of the same DSL and in the same project and the project has the Xtext nature added to it. So in the Eclipse editor window everything is fine (no error markers).
Now I want to load an EObject of A where also B is resolved, as part of a project launch. Therefore I know the location of the file in which A is saved (lets say "src/A.mydsl").
This works if I create a ResourceSet that contains both, A and B:
// Get resource of A
val uriOfA = URI.createFileURI(fullPathOfA)
val resSet = new XtextResourceSet()
val resource = resSet.getResource(uriOfA, true)
// Get resource of B as well
resSet.getResource(URI.createFileURI(fullPathOfB)
// Load EObject for A where also B is resolved
if(!resource.getContents().isEmpty) {
return resource.getContents().get(0)
} else {
return null
}
But in general I don't know the location of the files that are referenced by A. I tried the following commands, because I thought they might find the referenced objects and resolve them:
resSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE)
EcoreUtil2.resolveAll(resource)
However, they do not. If I don't get the resource of B manually, the reference to it in the loaded EObject of A is null.
I read about an index that is maintained by Xtext and also found https://www.eclipse.org/forums/index.php/t/261245/ where a resourceDescriptionsProvider is used to find the referenced models.
However, when I do
@Inject
private ResourceDescriptionsProvider resourceDescriptionsProvider;
the resourceDescriptionsProvider is null, which is propably because I do this in a simple class that has nothing to do with Guice otherwise.
As result I have a few questions:
1. What does the XtextResource.OPTION_RESOLVE_ALL option and EcoreUtil2.resolveAll(resource) do, if not resolving all referenced EObjects?
2. Is there a predefined method that does what I want? IMO it does seem to be a normal use-case.
3. If there is no such method, how do I get a reference to the resourceDescriptionsProvider?
|
|
|
Re: Load EObject with all cross-references from file path [message #1723540 is a reply to message #1723512] |
Tue, 16 February 2016 13:39   |
|
XtextResource.OPTION_RESOLVE_ALL option and EcoreUtil2.resolveAll(resource) do resolve all cross references of a resource, but therefore all potential cross-referenced resources have to be part of the ResourceSet.
In a standalone environment you will have to populate the ResourceSet programmatically. Within Eclipse, all potential resources are known within the workspace or referenced libraries. In a standalone environment not.
Within a MWE workflow, the XtextReader component does such a job. Based on the configured platform URI it searches for files for which a DSL file extension is registered.
Outside an MWE workflow you will have to populate your ResourceSet yourself.
Consider that it is usually not necessary to resolve all cross references at once (although there are certain special use cases), since they will resolve lazily and this is usually the desired way.
Need professional support for Xtext, EMF, Eclipse IDE?
Go to: http://devhub.karakun.com
Twitter : @kthoms
Blog : www.karsten-thoms.de
|
|
|
|
Powered by
FUDForum. Page generated in 0.01703 seconds