Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Strange Serialization(Model is flawed after xtext serialization)
Strange Serialization [message #1745511] Tue, 11 October 2016 20:01 Go to next message
Matt Se is currently offline Matt SeFriend
Messages: 29
Registered: February 2016
Junior Member
Hi,

I came across a serialization error, which I simply can't figure out to understand/solve.
It seems like a specific Type gets interpreted as another one, during serialization.

I want to create a Xtext Embedded Editor in an other Editor.
First I need create a XtextResource based on my ecore Model.
My code looks like this:

		EObject semanticElement = EcoreUtil.copy(originalSemanticElement.eResource().getContents().get(0));
		XtextResource xtextResource = createVirtualXtextResource(originalSemanticElement.eResource().getURI(), semanticElement);
		semanticElementFragment = originalResource.getURIFragment(originalSemanticElement);
		
		
	protected XtextResource createVirtualXtextResource(URI uri, EObject semanticElement) throws IOException {
			IResourceFactory resourceFactory = xtextInjector.getInstance(IResourceFactory.class);
			// TODO use the synthetic scheme.
			XtextResourceSet rs = xtextInjector.getInstance(XtextResourceSet.class);
			rs.setClasspathURIContext(getClass());
			// Create virtual resource
			XtextResource xtextVirtualResource = (XtextResource) resourceFactory
				.createResource(URI.createURI(uri.toString()));
			rs.getResources().add(xtextVirtualResource);

			// Populate virtual resource with the given semantic element to edit
			xtextVirtualResource.getContents().add(semanticElement);

			// Save and reparse in order to initialize virtual Xtext resource
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			xtextVirtualResource.save(out, Collections.emptyMap());
			xtextVirtualResource.reparse(new String(out.toByteArray()));

			return xtextVirtualResource;
	}


In my Scoping provider I only inserted one case, because I use some primitive EClassifiers (EString, EInt, EDouble...) from the EcorePackage.eINSTANCE as reference in my project. So
PrimitiveDatatypes.getPrimitivesEClassifiers
returns them in a List

override getScope(EObject context, EReference reference) {
		if (reference == DatatypesPackage.eINSTANCE.variable_EType) {
			return MultimapBasedScope.createScope(super.getScope(context, reference),
				Scopes.scopeFor(PrimitiveDatatypes.getPrimitivesEClassifiers).allElements, false)
		}


The Xtext grammar was created based on an ecore model.
The relevant part looks like this:

Wrapper returns wrapper::Wrapper:
	{wrapper::Wrapper}
	(name=EString)?
	'{'
	('ModulWrappers' '{' ModulWrappers+=ModulWrapper ( "," ModulWrappers+=ModulWrapper)* '}' )?
	'}';

ModulWrapper returns wrapper::ModulWrapper:
	{wrapper::ModulWrapper}
	(name=EString)?
	'{'
	('Connectors' '{' connectors+=WrapperConnector ( "," connectors+=WrapperConnector)* '}' )?
	'}';

WrapperFunction returns wrapper::WrapperFunction:
	{wrapper::WrapperFunction}
	(name=EString)?
	'Input' ':'
	(inputVars+=[wrapper::VariablePort|EString])?
	'Output' ':'
	(outputVars+=[wrapper::VariablePort|EString])? 
	('inputVars' '(' inputVars+=[wrapper::VariablePort|EString] ( "," inputVars+=[wrapper::VariablePort|EString])* ')' )?
	('outputVars' '(' outputVars+=[wrapper::VariablePort|EString] ( "," outputVars+=[wrapper::VariablePort|EString])* ')' )?
'}';

VariablePort returns wrapper::VariablePort:
	{wrapper::VariablePort}
	(name=EString)?
	'{'
		('attribute' attribute=[datatypes::Attribute|EString])?
		('direction' direction=[wrapper::WrapperConnector|EString])?
	'}';

WrapperConnector returns wrapper::WrapperConnector:
	WrapperFunction | VariablePort
;


Now if I try to create a XtextResource my Wrapperfunction gets surprisingly serialized to a Modulwrapper:

I fromatted the Wrapper Object in the original resource :

		0: Wrapper {
                            cref ModulWrapper ModulWrappers [
                                0: ModulWrapper {
                                    cref WrapperConnector connectors [
                                        0: VariablePort {
                                            attr EString name 'w_DWord'
                                            ref Attribute attribute ref: Attribute@//@DeskAreas.0/@FunctionGroups.0/@Wrapper.0/@Modul/@Aktors.0/@attributes.0
                                        }
                                        1: WrapperFunction {
                                            attr EString name 'WrapperFunc_1'

                                        }
                                    ]
                                }
                            ]
                        }
                    ]



and the same node formatted from the XtextResource after serialization:

		0: Wrapper {
                            cref ModulWrapper ModulWrappers [
                                0: ModulWrapper {

                                    cref WrapperConnector connectors [
                                        0: VariablePort {
                                            attr EString name 'w_DWord'
                                            ref Attribute attribute ref: Attribute@(unresolved proxy platform:/resource/ilm.example/Test.mfa#|3)
                                        }
                                    ]
                                }
                                /*this is wrong!!*/
                                1: ModulWrapper {
                                    attr EString name 'WrapperFunc_1'
                                }
                            ]
                        }
                    ]
                }


So the WrapperFunction is now indeed a ModulWrapper...
I have absolutly no idea why this happens...

What did I miss?

Many thanks in advance
Re: Strange Serialization [message #1745522 is a reply to message #1745511] Wed, 12 October 2016 03:12 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 13879
Registered: July 2009
Senior Member
I have no idea as well.

But I have some questions

(1) where does the injector come from
(2) why do you do this this save reparse thing
(3) the interesting part: how does the model string look like before and after serialization
Maybe there is something wrong with brackets so the the reparse is actually correct or a error recovery only

So could you nail down this to a unit test that does serialization only?


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Wed, 12 October 2016 04:16]

Report message to a moderator

Re: Strange Serialization [message #1745572 is a reply to message #1745522] Wed, 12 October 2016 15:45 Go to previous message
Matt Se is currently offline Matt SeFriend
Messages: 29
Registered: February 2016
Junior Member
Hi,

Thx for the quick response.

<<1) where does the injector come from
The Injector is the default injector generated by xtext for my given ecore model
<<(2) why do you do this this save reparse thing
I want to embedd a xtext editor in my sirius diagram, Obeo provides a xtext-sirius-integration plugin which I mostly stick to.

<<(3) the interesting part: how does the model string look like before and after serialization
Here was the catch. I spent the whole day getting deeper into xtext and found my issue.

I had to solve several issues:
Firstly, I use an EObject - JavaObject Wrapper in my mode (a simple EClass which holds a plain EJavaObject as attribute), this caused a problem. I bound an IValueConverter for the Rule and now it's fine.

The second problem was that I used attribute=[datatypes::Attribute|EString] in the grammar to resolve corssreferences, but the qualifiedname was no EString but rather: ID ('.' ID)*.
After doing some JUnit parsing test I recognized, that this caused a flaw in the model string and resulted in a misinterpretation.
And finally after inserting the right qualifiedname in the grammar everything seems to work fine.

Previous Topic:Backslashes as line ending characters
Next Topic:Default Ordering of XText Content Assistant
Goto Forum:
  


Current Time: Mon Apr 12 12:11:01 GMT 2021

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

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

Back to the top