Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Serializing Instances based on the Domainmodel (15 minutes tutorial)
Serializing Instances based on the Domainmodel (15 minutes tutorial) [message #1806740] Tue, 14 May 2019 14:39 Go to next message
Martin Jürgens is currently offline Martin JürgensFriend
Messages: 3
Registered: May 2019
Junior Member
Hi,
I want to programmatically create EMF instances based on the Domainmodel / Entity DSL out of the 15 minutes tutorial (https://www.eclipse.org/Xtext/documentation/102_domainmodelwalkthrough.html#write-the-grammar) and serialize them back into their textual Xtext representation.

E.g., I want to create an EMF instances according to this Xtext representation:

datatype String
 entity Blog {
title: String
 }


My solution is as follows:

@Inject Serializer serializer

  def void serializer() {
    val f = DomainmodelFactory.eINSTANCE
    
    val domainModel = f.createDomainmodel
    val dataType = f.createDataType
    dataType.setName("String")
    domainModel.getElements.add(dataType)
    
    val entity = f.createEntity
    entity.setName("Blog")
    domainModel.getElements.add(entity)
    
    val feature = f.createFeature
    feature.setName("title")
    feature.setType(dataType)
    entity.getFeatures.add(feature)

    println(serializer.serialize(domainModel))
  }


Unfortunately, I get a NullPointerException. It seems to me as if the cross references in square brackets are the problem, because without them everything works just fine. Does anyone have a solution for this problem / know whats wrong?:

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)
Re: Serializing Instances based on the Domainmodel (15 minutes tutorial) [message #1806748 is a reply to message #1806740] Tue, 14 May 2019 16:15 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13071
Registered: July 2009
Senior Member
You should create a new resourceset and resource and add the model to the resource
Does it work then?


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Serializing Instances based on the Domainmodel (15 minutes tutorial) [message #1806752 is a reply to message #1806748] Tue, 14 May 2019 17:12 Go to previous messageGo to next message
Martin Jürgens is currently offline Martin JürgensFriend
Messages: 3
Registered: May 2019
Junior Member
It works. Thanks! I stumbled across the same issue on Stackoverflow and would also post the solution code there, if that's okay..

About justification why the ResourceSet is needed: Do I get it right that the serialization method needs access to all elements to map the cross references and that this is made possible by attaching the model to the ResourceSet (e.g. it provides the method getAllContents())?

Here's the solution:
    @Inject Serializer serializer
	
//new
    @Inject
    ResourceSet resourceSet

 def void serializer() {
  	
//new
    val resource = resourceSet.createResource(URI.createURI("dummy/test.dmodel"))
    
    val f = DomainmodelFactory.eINSTANCE
    
    val domainModel = f.createDomainmodel
    val dataType = f.createDataType
    dataType.setName("String")
    domainModel.getElements.add(dataType)
    
    val entity = f.createEntity
    entity.setName("Blog")
    domainModel.getElements.add(entity)
    
    val feature = f.createFeature
    feature.setName("title")
    feature.setType(dataType)
    entity.getFeatures.add(feature)
    
     //new
    resource.getContents.add(domainModel)

    println(serializer.serialize(domainModel))

  }

Re: Serializing Instances based on the Domainmodel (15 minutes tutorial) [message #1806754 is a reply to message #1806752] Tue, 14 May 2019 17:44 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13071
Registered: July 2009
Senior Member
yes and no. the resourceset specifies the "space" / container where objects for references are searched.
btw you should use

@Inject
Provider<ResourceSet> rsp

and rsp.get to obtain the rs
(if you want to follow the ususal pattern)


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Serializing Instances based on the Domainmodel (15 minutes tutorial) [message #1806759 is a reply to message #1806754] Tue, 14 May 2019 18:58 Go to previous message
Tamas Miklossy is currently offline Tamas MiklossyFriend
Messages: 50
Registered: February 2016
Member
See also https://github.com/eclipse/xtext-eclipse/blob/master/org.eclipse.xtext.xtext.ui.examples/projects/homeautomation/org.eclipse.xtext.example.homeautomation.tests/src/org/eclipse/xtext/example/homeautomation/tests/SerializerTest.xtend
Previous Topic:Keep the Grammar structure same as before saving the file
Next Topic:Check for byte array in model inferrer
Goto Forum:
  


Current Time: Fri Aug 23 16:07:14 GMT 2019

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

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

Back to the top