Exception when referencing between Resources in same ResourceSet [message #1809794] |
Tue, 23 July 2019 09:43  |
Eclipse User |
|
|
|
Hello,
I'm adding two (in this small example, otherwise a lot more) objects to my "main" Model programmatically (with MyDslFactory). One of them is saved in a different Resource which is in the same ResourceSet as the original one (I checked). The other one is saved in the "main" Model and contains a reference to the first object. Here an example grammar with model files:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
Model:
elements+=Element*;
Element:
First | Second;
First:
'first' name=ID;
Second:
'second' item=Reference;
Reference:
'refers' reference=[First];
initial model (example.mydsl):
first object
second refers object
how the model should look like afterwards (example.mydsl):
first object
second refers object
second refers newObject
and newObject.mydsl :
I'm able to create and save 'newObject' to a new resource 'newObject.mydsl' but when I try to save the "main" model with the added 'second refers newObject' I get a RuntimeException:
java.lang.RuntimeException: No EObjectDescription could be found in Scope Reference.reference for Model.elements[0]->First'newObject'
Semantic Object: Model.elements[2]->Second.item->Reference
URI: file:/C:/Users/flug/runtime-EclipseXtext/example/example.mydsl
EStructuralFeature: myDsl::Reference.reference
Here the code I'm executing (see also the whole project attached)
public static void createNewObject(IPath path) {
Injector injector = MydslActivator.getInstance().getInjector(MydslActivator.ORG_XTEXT_EXAMPLE_MYDSL_MYDSL);
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
URI modelURI = URI.createFileURI(path.toString());
Resource resource = resourceSet.getResource(modelURI, true);
Model model = (Model) resource.getContents().get(0);
Model modelForObject = MyDslFactory.eINSTANCE.createModel();
First newObject = MyDslFactory.eINSTANCE.createFirst();
newObject.setName("newObject");
modelForObject.getElements().add(newObject);
URI objectURI = modelURI.trimSegments(1).appendSegment("newObject.mydsl");
Resource objectResource = resourceSet.createResource(objectURI);
objectResource.getContents().add(modelForObject);
try {
objectResource.save(Collections.EMPTY_MAP);
} catch (IOException e) {
e.printStackTrace();
}
Reference reference = MyDslFactory.eINSTANCE.createReference();
reference.setReference(newObject);
Second second = MyDslFactory.eINSTANCE.createSecond();
second.setItem(reference);
model.getElements().add(second);
try {
resource.save(Collections.EMPTY_MAP);
} catch (IOException e) {
e.printStackTrace();
}
}
To trigger the exception you have to start 'org.xtext.example.mydsl' as an eclipse plugin, add the 'example' project attached to your workspace (or create a new file and copy the "initial model" above), right click on 'example.mydsl' -> 'Create New Object'. The Exception won't be visibly thrown (at least not for me). To actually read it I have to set a breakpoint at the second save() call and "walk" to the exception in debug mode (tips for printing it in the console are very much appreciated).
So my question is: Why is the Reference not working and how do I get it to work?
Please ask for further explanations if my question isn't clear enough. Thanks in advance for your help.
Saphira
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03320 seconds