Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Serialize DSL model from with out Xtext Web project classes(null reference attempting to serialize our DSL model from classes in the our DSL web project.)
Serialize DSL model from with out Xtext Web project classes [message #1761057] Fri, 05 May 2017 18:57 Go to next message
Alan Frye is currently offline Alan FryeFriend
Messages: 41
Registered: January 2017
Member
When trying to serialize our DSL model from within our DSL web project, I am getting a null pointer exception when calling resourceProvider.get method to get the resource. It appears the ResourceProvider is not getting injected properly.

       @Inject Provider<ResourceSet> resourceSetProvider 
	@Inject extension ISerializer

	val resourceSet = resourceSetProvider.get
	val resource =resourceSet.createResource(URI.createURI("dummy.stext"))
	resource.contents += model	
    resource.contents.head.serialize(SaveOptions.newBuilder().format.options)


Also in the our DSLWebSetup Class we are injecting the our DSLRuntimeModule.

Are there are examples on how properly inject the resource provider?

Thanks,
Alan Frye
Re: Serialize DSL model from with out Xtext Web project classes [message #1761058 is a reply to message #1761057] Fri, 05 May 2017 19:02 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

am not sure if i can follow you. a reproducible example would help.
anyway you should inject IWebResourceSetProvider i think

or dont use a resourceset at all and inject iserializer
and call serializer.serialize(object)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Serialize DSL model from with out Xtext Web project classes [message #1761063 is a reply to message #1761058] Fri, 05 May 2017 19:44 Go to previous messageGo to next message
Alan Frye is currently offline Alan FryeFriend
Messages: 41
Registered: January 2017
Member
No Message Body
Re: Serialize DSL model from with out Xtext Web project classes [message #1761064 is a reply to message #1761058] Fri, 05 May 2017 20:02 Go to previous messageGo to next message
Alan Frye is currently offline Alan FryeFriend
Messages: 41
Registered: January 2017
Member
I have tried to inject the Iserializer and call the serialize method but I am getting the same null reference exception.

I also running this via a unittest and the unittest is calling into a class located in the Web project and that class is where I am trying to inject the serializer and serialize the object.

I am going to try and put together an example that demonstrates the issue and post it here.
Re: Serialize DSL model from with out Xtext Web project classes [message #1761065 is a reply to message #1761058] Fri, 05 May 2017 20:17 Go to previous messageGo to next message
Alan Frye is currently offline Alan FryeFriend
Messages: 41
Registered: January 2017
Member
Does it matter that the class that I trying to inject the serializer into is defined as a singleton class.

Re: Serialize DSL model from with out Xtext Web project classes [message #1761067 is a reply to message #1761065] Fri, 05 May 2017 20:23 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
No it does not.
As I said I need some guidance how to reproduce


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Serialize DSL model from with out Xtext Web project classes [message #1761071 is a reply to message #1761067] Fri, 05 May 2017 23:21 Go to previous messageGo to next message
Greg Lusk is currently offline Greg LuskFriend
Messages: 36
Registered: February 2017
Location: USA
Member
Our unit test is nearly identical to the one in the SerializerTest class in the Xtext Homeautomation example project. We are essentially trying to serialize the model after parsing it from our web editor. The extension methods for creating model class instances are not available and we get a NullPointerException; same goes for the injection of Provider<ResourceSet> and ISerializer. So it appears that none of the instances are being injected. I assume that we need to set up the module class to make the right injection, but I'm not even sure that an Injector is available to do the job.
Re: Serialize DSL model from with out Xtext Web project classes [message #1761076 is a reply to message #1761071] Sat, 06 May 2017 05:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
As I said. Since I have zero idea what you do (I have no greeting example provided by you that reproduces the problem)
I cannot support you


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Serialize DSL model from with out Xtext Web project classes [message #1761096 is a reply to message #1761076] Sat, 06 May 2017 14:17 Go to previous messageGo to next message
Greg Lusk is currently offline Greg LuskFriend
Messages: 36
Registered: February 2017
Location: USA
Member
We are not able to share the exact code, but we will work up an example, as Alan stated earlier.
Re: Serialize DSL model from with out Xtext Web project classes [message #1761148 is a reply to message #1761096] Mon, 08 May 2017 07:49 Go to previous messageGo to next message
Akos Kitta is currently offline Akos KittaFriend
Messages: 25
Registered: November 2015
Junior Member
// bind your customized web resource set provider in the web module

def Class<? extends IWebResourceSetProvider> bindIWebResourceSetProvider() {
	MyResourceSetProvider
}



// your resource set provider for the web: `MyResourceSetProvider.xtend`

import com.google.inject.Inject
import com.google.inject.Provider
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.xtext.web.server.IServiceContext
import org.eclipse.xtext.web.server.model.IWebResourceSetProvider

class MyResourceSetProvider implements IWebResourceSetProvider {
	
	@Inject Provider<ResourceSet> provider
	
	// ...

	@Override
	override get(String resourceId, IServiceContext serviceContext) {
                // ...
		return provider.get
	}
	
	// ...
}


Or if you do not have a customized resource set provider for the web, just inject the `IWebResourceSetProvider` as Christian suggested.
Re: Serialize DSL model from with out Xtext Web project classes [message #1762503 is a reply to message #1761148] Fri, 12 May 2017 03:09 Go to previous messageGo to next message
Greg Lusk is currently offline Greg LuskFriend
Messages: 36
Registered: February 2017
Location: USA
Member
Just to close the loop here, we found the root cause and solution to the problem. The class from which we were calling the serialization methods and extensions had not been injected, so it was not able to rely on the Injector to resolve all the dependencies that it needed. As soon as we injected that class, everything worked.
Re: Serialize DSL model from with out Xtext Web project classes [message #1800049 is a reply to message #1762503] Wed, 19 December 2018 10:57 Go to previous messageGo to next message
Mykola Makhin is currently offline Mykola MakhinFriend
Messages: 50
Registered: July 2018
Member
BTW, does anyone know a way to serialize produced ECore model?

After parsing input into and EObject via "resource.load(new StringInputStream(body), null); resource.getContents().get(0)" I'd like to see what's in the AST, e.g. what's in that EObject and it's children - but I can't do that using some generic serializers because these run into circular references (supposedly by going up via eContainer() method). Is there any helper tool that can do that? Or should I implement serialization as a custom code by using semantics of my particular grammar?
Re: Serialize DSL model from with out Xtext Web project classes [message #1800053 is a reply to message #1800049] Wed, 19 December 2018 11:10 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Can you be more specific on what you want to achieve and what is not working

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Serialize DSL model from with out Xtext Web project classes [message #1800056 is a reply to message #1800053] Wed, 19 December 2018 11:28 Go to previous messageGo to next message
Mykola Makhin is currently offline Mykola MakhinFriend
Messages: 50
Registered: July 2018
Member
Sure.

I have implemented a custom DSL.
I have some text that I parse into AST (which as I understand is an EMF/ECore model), represented by EObject

To be very specific, I use this code to parse the text in DSL into an AST, which returns me an EObject for astRoot:
Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
IResourceFactory resourceFactory = injector.getInstance(IResourceFactory.class);
Provider<XtextResourceSet> xtextResourceSetProvider = injector.getInstance(new Key<Provider<XtextResourceSet>>() {});

XtextResourceSet resourceSet = xtextResourceSetProvider.get();

Resource resource = resourceFactory.createResource(URI.createURI(UUID.randomUUID() + ".mydsl"));
resourceSet.getResources().add(resource);
resource.load(new StringInputStream(body), null);
final EObject astRoot = resource.getContents().isEmpty() ? null : resource.getContents().get(0);


I knot that EObject and it's children are actually some classes that are generated by XText for my DSL and represent particular grammar rules (and implement EObject interface).
I'd like to serialize it into JSON or XML so that I could inspect the resulting AST and see if it's what I expected it to be.

I can use some generic serialization libraries like Jackson and give them the astRoot EObject:
new ObjectMapper().writeValueAsString(astRoot)

But this results in StackOverflow, because there are circular references in the AST EObjects (I'm guessing that at least reference from parent to child and from child to parent e.g. container will definitely create a loop - so for parser AST "tree" is not 100% a tree/DAC, but more of a tree representation).

Thus I'm curious if there's any way to serialize it without writing custom code that would account for specific classes/fields generated by Xtext for my specific grammar.

[Updated on: Wed, 19 December 2018 11:30]

Report message to a moderator

Re: Serialize DSL model from with out Xtext Web project classes [message #1800060 is a reply to message #1800056] Wed, 19 December 2018 11:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
are you ok with arbitrary to string representation (then use org.eclipse.xtext.util.EmfFormatter)
if you want to use emf xml/xmi and root to a xmi/xml resource and save.
about json you might have to configure to ignore some refs/how to deal with cross refs


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Serialize DSL model from with out Xtext Web project classes [message #1800079 is a reply to message #1800060] Wed, 19 December 2018 15:44 Go to previous message
Mykola Makhin is currently offline Mykola MakhinFriend
Messages: 50
Registered: July 2018
Member
Thanks, that's exactly what I was looking for.
Previous Topic:Disable DocumentSymbolService for Binary Files
Next Topic:Standalone setup parsing threadsafety
Goto Forum:
  


Current Time: Thu Apr 25 01:23:33 GMT 2024

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

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

Back to the top