Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Exception serializing cross-references
Exception serializing cross-references [message #1809536] Wed, 17 July 2019 17:04 Go to next message
L R is currently offline L RFriend
Messages: 2
Registered: July 2019
Junior Member
I am having several difficulties serializing a model that contains cross-references. I am attempting to translate from one Xtext grammar to another and then to serialize the resulting model. The translation appears to work fine, but I receive an exception when attempting to serialize the translated model. This exception appears to be related to the serialization of cross-references.

This issue can be reproduced using the Domainmodel grammar from the Xtext 15 minute tutorial: (https://www.eclipse.org/Xtext/documentation/102_domainmodelwalkthrough.html).

To perform serialization I use the following function:
public String serialize(EObject obj) {
	DomainmodelStandaloneSetupGenerated setup = new DomainmodelStandaloneSetupGenerated();
	Injector injector = setup.createInjectorAndDoEMFRegistration();
	ISerializer serializer = injector.getInstance(ISerializer.class);
	String result = serializer.serialize(obj);
	return result;
}


A small model with a cross-references is as follows:
entity Test{
}

entity Test2{
	test_feature : Test
}


In this model entity 'Test2' has a feature named 'test_feature' with a type cross-referenced to 'Test'. If I create a model of this test model using the parser and then call serialize, everything works fine. However, if I create what I think is an equivalent model using the Xtext generated EFactory (DomainmodelFactory), I receive an exception. If the 'test_feature' is not included, then everything serializes as expected.

Here is the code for creating the model using the EFactory:
Domainmodel result = DomainmodelFactory.eINSTANCE.createDomainmodel();
Entity en1 = DomainmodelFactory.eINSTANCE.createEntity();
en1.setName("Test");
result.getElements().add(en1);

Entity en2 = DomainmodelFactory.eINSTANCE.createEntity();
en2.setName("Test2");
Feature f1 = DomainmodelFactory.eINSTANCE.createFeature();
f1.setName("test_feature");
Type t1 = DomainmodelFactory.eINSTANCE.createType();
t1.setName("Test");
f1.setType(t1);
en2.getFeatures().add(f1);
result.getElements().add(en2);


Here is the relevant portion of the exception:
java.lang.NullPointerException
	at org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider.getResourceScope(ImportedNamespaceAwareLocalScopeProvider.java:103)
	at org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider.getScope(ImportedNamespaceAwareLocalScopeProvider.java:97)
	at org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider.getScope(ImportedNamespaceAwareLocalScopeProvider.java:95)
	at org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider.getScope(ImportedNamespaceAwareLocalScopeProvider.java:95)
	at org.eclipse.xtext.scoping.impl.DelegatingScopeProvider.delegateGetScope(DelegatingScopeProvider.java:42)
	at org.eclipse.xtext.scoping.impl.DelegatingScopeProvider.getScope(DelegatingScopeProvider.java:38)
	at org.eclipse.xtext.serializer.tokens.CrossReferenceSerializer.serializeCrossRef(CrossReferenceSerializer.java:90)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.getToken(SequenceFeeder.java:482)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.accept(SequenceFeeder.java:245)
	at org.eclipse.xtext.serializer.sequencer.BacktrackingSemanticSequencer.accept(BacktrackingSemanticSequencer.java:451)
	at org.eclipse.xtext.serializer.sequencer.BacktrackingSemanticSequencer.createSequence(BacktrackingSemanticSequencer.java:511)
	at org.example.domainmodel.serializer.DomainmodelSemanticSequencer.sequence_Feature(DomainmodelSemanticSequencer.java:117)
	at org.example.domainmodel.serializer.DomainmodelSemanticSequencer.sequence(DomainmodelSemanticSequencer.java:50)
	at org.eclipse.xtext.serializer.sequencer.AbstractSemanticSequencer.createSequence(AbstractSemanticSequencer.java:67)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.acceptEObjectRuleCall(SequenceFeeder.java:326)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.acceptRuleCall(SequenceFeeder.java:353)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.accept(SequenceFeeder.java:264)
	at org.eclipse.xtext.serializer.sequencer.BacktrackingSemanticSequencer.accept(BacktrackingSemanticSequencer.java:444)
	at org.eclipse.xtext.serializer.sequencer.BacktrackingSemanticSequencer.createSequence(BacktrackingSemanticSequencer.java:511)
	at org.example.domainmodel.serializer.DomainmodelSemanticSequencer.sequence_Entity(DomainmodelSemanticSequencer.java:105)
	at org.example.domainmodel.serializer.DomainmodelSemanticSequencer.sequence(DomainmodelSemanticSequencer.java:47)
	at org.eclipse.xtext.serializer.sequencer.AbstractSemanticSequencer.createSequence(AbstractSemanticSequencer.java:67)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.acceptEObjectRuleCall(SequenceFeeder.java:326)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.acceptRuleCall(SequenceFeeder.java:353)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.accept(SequenceFeeder.java:264)
	at org.eclipse.xtext.serializer.sequencer.BacktrackingSemanticSequencer.accept(BacktrackingSemanticSequencer.java:444)
	at org.eclipse.xtext.serializer.sequencer.BacktrackingSemanticSequencer.createSequence(BacktrackingSemanticSequencer.java:511)
	at org.example.domainmodel.serializer.DomainmodelSemanticSequencer.sequence_Domainmodel(DomainmodelSemanticSequencer.java:91)
	at org.example.domainmodel.serializer.DomainmodelSemanticSequencer.sequence(DomainmodelSemanticSequencer.java:44)
	at org.eclipse.xtext.serializer.sequencer.AbstractSemanticSequencer.createSequence(AbstractSemanticSequencer.java:67)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:118)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:131)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:184)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:58)


Additionally, I receive the same exception if I make a copy (using EcoreUtil2.copy) of the originally parsed test model and attempt to serialize the copy of the model.
Re: Exception serializing cross-references [message #1809580 is a reply to message #1809536] Thu, 18 July 2019 13:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13119
Registered: July 2009
Senior Member
do you add the model to a resource/resourceset?

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Exception serializing cross-references [message #1809604 is a reply to message #1809580] Thu, 18 July 2019 16:55 Go to previous messageGo to next message
L R is currently offline L RFriend
Messages: 2
Registered: July 2019
Junior Member
I was not adding the model to a resource/resourceset. I added a resource and resourceSet and it now serializes as expected (after I fixed how the feature's type was assigned). Here is the resulting code that serialized as expected (with the path of outuri populated).
URI outuri = URI.createFileURI("...path defined here...");

Domainmodel result = DomainmodelFactory.eINSTANCE.createDomainmodel();
Entity en1 = DomainmodelFactory.eINSTANCE.createEntity();
en1.setName("Test");
result.getElements().add(en1);

Entity en2 = DomainmodelFactory.eINSTANCE.createEntity();
en2.setName("Test2");
Feature f1 = DomainmodelFactory.eINSTANCE.createFeature();
f1.setName("test_feature");
f1.setType(en1);
en2.getFeatures().add(f1);
result.getElements().add(en2);
		
DomainmodelStandaloneSetupGenerated setup = new DomainmodelStandaloneSetupGenerated();
Injector injector = setup.createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);				
Resource resource = resourceSet.createResource(outuri);
resource.getContents().add(result);


This solved my problem, so thank you for the response. I am still curious as to why a resource is required when serializing cross-references and would like to understand this better. Do you happen to know of any further reading on this topic or related to using the EFactory?
Re: Exception serializing cross-references [message #1809605 is a reply to message #1809604] Thu, 18 July 2019 17:02 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13119
Registered: July 2009
Senior Member
resourceset / resource are used for validating scoping

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Absolute Beginner
Next Topic:osgi logging fragment configuration not working
Goto Forum:
  


Current Time: Tue Sep 17 12:17:51 GMT 2019

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

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

Back to the top