Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Serialization of xtext grammar models
Serialization of xtext grammar models [message #1407282] Tue, 12 August 2014 08:45 Go to next message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Hi,

I want to perform some serializations of .xtext --> .xmi and wanted to know if it is
possible the way I want to do it:

1. Get the semantic model to a xtext grammar file (.xtext, not a xtext-DSL)
2. Save it in a XMI-Resource
3. Load the .xmi resource in an XextResourceSet
4. Save the content of the .xmi resource again back to an .xtext grammer file

1,2,3 are working without any problems, but when I want to do the serialization back to the .xtext file I'm getting the following exception:

org.eclipse.xtext.parsetree.reconstr.XtextSerializationException: Could not serialize cross reference from Grammar'org.xtext.example.mydsl.MyDsl'.metamodelDeclarations[0]->GeneratedMetamodel'myDsl'.ePackage to EPackage
	at org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeConstructor$AssignmentToken.serializeInternal(AbstractParseTreeConstructor.java:257)
	at org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeConstructor$AbstractToken.serialize(AbstractParseTreeConstructor.java:158)
	at org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeConstructor.write(AbstractParseTreeConstructor.java:771)
	at org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeConstructor.write(AbstractParseTreeConstructor.java:766)
	at org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeConstructor.write(AbstractParseTreeConstructor.java:766)
	at org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeConstructor.serializeSubtree(AbstractParseTreeConstructor.java:732)
	at org.eclipse.xtext.parsetree.reconstr.Serializer.serialize(Serializer.java:64)
	at org.eclipse.xtext.parsetree.reconstr.Serializer.serialize(Serializer.java:70)
	at org.eclipse.xtext.resource.XtextResource.doSave(XtextResource.java:342)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(ResourceImpl.java:1430)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(ResourceImpl.java:999)


Is it evep possible to handle .xtext files like other .dsl files?

~Alex



Re: Serialization of xtext grammar models [message #1407292 is a reply to message #1407282] Tue, 12 August 2014 09:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13264
Registered: July 2009
Senior Member
hi,

did you load the epackages to the resourceset as well?


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Serialization of xtext grammar models [message #1407301 is a reply to message #1407292] Tue, 12 August 2014 09:37 Go to previous messageGo to next message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Hi,

no I didn't load any epackages by hand. I just used EcoreUtil2.resolveAll(xtextResource); before serializing .xtext to .xmi
I wanted to implement this mechanism for every xtext-DSL and the xtext-grammar-dsl.

My code for saving the .xtext file to xmi looks like this:

	public void serializeXtext(URI modelURI) {
		// load the xtext model to an xtextresourceset
		XtextResourceSet resourceSet = new XtextResourceSet();
		Resource xtextResource = resourceSet.getResource(modelURI, true);

		EcoreUtil2.resolveAll(xtextResource, null);
		EObject newMod = xtextResource.getContents().get(0);
		// path to xmi model
		String xmiPath = EditorUtils.getActiveXtextEditor().getResource().getProject().getFolder("product").getFullPath() + "/"
				+ "xtextResourceSerialization.xmi";
		// store in a xmi-resoure
		Resource xmiResource = resourceSet.createResource(URI.createURI(xmiPath));
		xmiResource.getContents().add(newMod);

		try {
			xmiResource.save(null);
		} catch (IOException e) {
			e.printStackTrace();

		}

	}


And the code for loadnig the .xmi to .xtext looks like this:

   private void XMItoXtext(URI uri){
                XtextResourceSet xrs = new XtextResourceSet();
		xrs.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);

		// load the xmi resource
		Resource xmiRes = xrs.getResource(uri, true);
		// load the model from the xmi-resource
		EObject model= xmiRes.getContents().get(0);

		/*
		 * save the xmi-model to the xtext-resource
		 */
		// xtext file path
		String xtextFile = "test.xtext";
				

		System.out.println(xtextFile);
		// create the resource
		XtextResource a = (XtextResource) xrs.createResource(URI.createURI(xtextFile, true));

		// add the model to the xtext resource
		a.getContents().add(model);

		// save the new resource with the model
		try {
			a.save(null);
		} catch (IOException e) {
			e.printStackTrace();

		}
}


Or do I have to handle the xtext-grammar file in a different, special way?
Re: Serialization of xtext grammar models [message #1407321 is a reply to message #1407301] Tue, 12 August 2014 10:21 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13264
Registered: July 2009
Senior Member
Hi,

i think all relevant resources (incl the epackages) have to be in the xtext 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: Serialization of xtext grammar models [message #1407353 is a reply to message #1407321] Tue, 12 August 2014 12:06 Go to previous messageGo to next message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Hi,

you were right. I had to register all the packages of the referenced metamodels. So here my solution:

// loading the packages
		XtextPackage.eINSTANCE.eClass();
		EcorePackage.eINSTANCE.eClass();

		XtextResourceSet xrs = (XtextResourceSet) getGlobalXtextResource().getResourceSet();
		xrs.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);

		// load the ecore resource
		Resource ecoreResource = xrs.getResource(URI.createURI(EcoreFactory.eINSTANCE.getEPackage().getNsURI()), true);
		ecoreResource.load(null);
		EcoreUtil2.resolveAll(ecoreResource);

		// load the xtext resource
		Resource xtextResource = xrs.getResource(URI.createURI(EcoreFactory.eINSTANCE.getEPackage().getNsURI()), true);
		xtextResource.load(null);
		EcoreUtil2.resolveAll(xtextResource);



Do you maybe know, how I can acces the metamodel (.ecore) of the Terminals.xtext file?

Thanks a lot!

Re: Serialization of xtext grammar models [message #1407360 is a reply to message #1407353] Tue, 12 August 2014 12:33 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13264
Registered: July 2009
Senior Member
Terminals.xtext does not have a metamodel afaik

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Serialization of xtext grammar models [message #1407365 is a reply to message #1407360] Tue, 12 August 2014 12:53 Go to previous message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Ok, thanks.
Previous Topic:Manually add errors to a resource
Next Topic:Cross-file references as in 15-min tutorial not working
Goto Forum:
  


Current Time: Sat Dec 07 01:43:04 GMT 2019

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

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

Back to the top