Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [xtext] Serializing parsed DSL Model
[xtext] Serializing parsed DSL Model [message #56730] Thu, 09 July 2009 15:14 Go to next message
Eclipse UserFriend
Hi everyone.

It's the first time I'm using the xtext framework.

I've already read most parts of the XText User Guide and the "Getting
Started" works well
( http://www.eclipse.org/Xtext/documentation/latest/xtext.html #getting-started).

But I have a problem with serialization reusing the generated fragments
from the getting Started Example:

First I saved the following content to a file which is named "test.entity":

type String
type Bool

entity Session {
property Title: String
property IsTutorial : Bool
}

entity Conference {
property Name : String
property Attendees : Person[]
property Speakers : Speaker[]
}

entity Person {
property Name : String
}

entity Speaker extends Person {
property Sessions : Session[]
}

Then I tried to reuse the the generated Implementation from the "Getting
Started" Example.

- I parse the content from test.entity using the appropriate IParser.
-> It seems to be that it works well.
- Then I try to serialize the parsed model using resource.save(...)
-> That doesn't work. I get the following error log messages:

org.eclipse.xtext.parsetree.reconstr.XtextSerializationExcep tion:
Serialization failed
<# of serialized tokens>: "<serializable fragment, starting from the end>":
-> <possible reasons for not continuing>
8: "entity Session { }":
-> Can not leave rule 'Entity' since the current object 'Entity' has
features with unconsumed values: 'properties':2
7: "}":
-> Property_ManyAssignment_4: Property.many is not set.
-> Property_TypeAssignment_3: Property.type is not set.
7: "{ }":
-> Entity_ExtendsAssignment_2_1: Entity.extends is not set.

at
org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeC onstructor.serialize(AbstractParseTreeConstructor.java:432)
at
org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeC onstructor.serialize(AbstractParseTreeConstructor.java:378)
at
org.eclipse.xtext.parsetree.reconstr.SerializerUtil.serializ e(SerializerUtil.java:47)
at
org.eclipse.xtext.parsetree.reconstr.SerializerUtil.serializ e(SerializerUtil.java:52)
at org.eclipse.xtext.resource.XtextResource.doSave(XtextResourc e.java:188)
at
org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(Resour ceImpl.java:1406)
at
org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(Resour ceImpl.java:993)
at ParserTestNG.main(ParserTestNG.java:49)


Here, all "is not set" log messages focusing DSL constructs with Cross
References (See Getting Started Example).

My source code for parsing and serializing is as follows:

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xtext.parser.IParseResult;
import org.eclipse.xtext.parser.IParser;
import org.eclipse.xtext.parser.antlr.IAntlrParser;
import org.xtext.example.EntitiesStandaloneSetup;

import com.google.inject.Injector;

public class ParserTestNG {

public static void main(String[] args) {
InputStream fis;
IParseResult ipr;
try {
EntitiesStandaloneSetup.doSetup();
EntitiesStandaloneSetup ess = new EntitiesStandaloneSetup();
Injector injector = ess.createInjector();

//-------------From here...Parsing
IParser ip = injector.getInstance(IAntlrParser.class);

fis = new FileInputStream("D:/myTmp/test.entity");
ipr = ip.parse(fis);

EObject eo2 = ipr.getRootASTElement();

//-------------From here...Serialization
Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
Map<String, Object> m = reg.getExtensionToFactoryMap();

m.put("entity",
injector.getInstance(org.eclipse.xtext.resource.IResourceFac tory.class));

ResourceSet resSet = new ResourceSetImpl();

Resource resource =
resSet.createResource(URI.createURI("entities/My2.entity"));
resource.getContents().add(eo2);

try {
resource.save(Collections.EMPTY_MAP);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}catch (Exception e){
System.out.println(e.toString());
e.printStackTrace();
}
}
}


Has anyone an idea whats wrong?

Thanks in advance, Helko
Re: [xtext] Serializing parsed DSL Model [message #56757 is a reply to message #56730] Thu, 09 July 2009 15:37 Go to previous messageGo to next message
Eclipse UserFriend
Perhaps, I've forgotten to call a methode to start the Linking Phase (for
establishing cross references)?

The User Guide says in the Cross References Part:
"A unique feature of Xtext is the ability to declare crosslinks in the
grammar. In traditional compiler construction the crosslinks are not
established during parsing but in a later linking phase."

Hope that somebody can set me on the right track.

Helko
Re: [xtext] Serializing parsed DSL Model [message #56785 is a reply to message #56730] Thu, 09 July 2009 16:21 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Helko,

please do not use the parser directly but the resource instead.
Something like this should work fine:

// instanciate a resource set
XtextResourceSet set = inject.getInstance(XtextResourceSet.class);
// create a uri
URI uri = URI.createFileURI("D:/myTmp/test.entity");
Resource res = set.getResource(uri, true);
// res.getContents() contains the model
Resource newRes = set.createResource(URI.createURI("..."));
newRes.getContents().add(res.getContents().get(0));
newRes.save(Collections.emptyMap());

The code is not tested and I'm sure it contains a bunch of errors, but
you get the basic idea :-)

The resource encapsulates a lot of fance stuff and will provide some
detailled information. For example you can use res.getErrors() to obtain
any syntax error that occured during the parsing.

Hope that helps,
Sebastian


Am 09.07.2009 17:14 Uhr, schrieb Helko Glathe:
> Hi everyone.
>
> It's the first time I'm using the xtext framework.
>
> I've already read most parts of the XText User Guide and the "Getting
> Started" works well
> ( http://www.eclipse.org/Xtext/documentation/latest/xtext.html #getting-started).
>
>
> But I have a problem with serialization reusing the generated fragments
> from the getting Started Example:
>
> First I saved the following content to a file which is named "test.entity":
>
> type String
> type Bool
>
> entity Session {
> property Title: String
> property IsTutorial : Bool
> }
>
> entity Conference {
> property Name : String
> property Attendees : Person[]
> property Speakers : Speaker[]
> }
>
> entity Person {
> property Name : String
> }
>
> entity Speaker extends Person {
> property Sessions : Session[]
> }
>
> Then I tried to reuse the the generated Implementation from the "Getting
> Started" Example.
>
> - I parse the content from test.entity using the appropriate IParser.
> -> It seems to be that it works well.
> - Then I try to serialize the parsed model using resource.save(...)
> -> That doesn't work. I get the following error log messages:
>
> org.eclipse.xtext.parsetree.reconstr.XtextSerializationExcep tion:
> Serialization failed
> <# of serialized tokens>: "<serializable fragment, starting from the end>":
> -> <possible reasons for not continuing>
> 8: "entity Session { }":
> -> Can not leave rule 'Entity' since the current object 'Entity' has
> features with unconsumed values: 'properties':2
> 7: "}":
> -> Property_ManyAssignment_4: Property.many is not set.
> -> Property_TypeAssignment_3: Property.type is not set.
> 7: "{ }":
> -> Entity_ExtendsAssignment_2_1: Entity.extends is not set.
>
> at
> org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeC onstructor.serialize(AbstractParseTreeConstructor.java:432)
>
> at
> org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeC onstructor.serialize(AbstractParseTreeConstructor.java:378)
>
> at
> org.eclipse.xtext.parsetree.reconstr.SerializerUtil.serializ e(SerializerUtil.java:47)
>
> at
> org.eclipse.xtext.parsetree.reconstr.SerializerUtil.serializ e(SerializerUtil.java:52)
>
> at org.eclipse.xtext.resource.XtextResource.doSave(XtextResourc e.java:188)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(Resour ceImpl.java:1406)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(Resour ceImpl.java:993)
>
> at ParserTestNG.main(ParserTestNG.java:49)
>
>
> Here, all "is not set" log messages focusing DSL constructs with Cross
> References (See Getting Started Example).
>
> My source code for parsing and serializing is as follows:
>
> import java.io.FileInputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.util.Collections;
> import java.util.Map;
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.EObject;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.eclipse.xtext.parser.IParseResult;
> import org.eclipse.xtext.parser.IParser;
> import org.eclipse.xtext.parser.antlr.IAntlrParser;
> import org.xtext.example.EntitiesStandaloneSetup;
>
> import com.google.inject.Injector;
>
> public class ParserTestNG {
>
> public static void main(String[] args) {
> InputStream fis;
> IParseResult ipr;
> try {
> EntitiesStandaloneSetup.doSetup();
> EntitiesStandaloneSetup ess = new EntitiesStandaloneSetup();
> Injector injector = ess.createInjector();
>
> //-------------From here...Parsing
> IParser ip = injector.getInstance(IAntlrParser.class);
>
> fis = new FileInputStream("D:/myTmp/test.entity");
> ipr = ip.parse(fis);
>
> EObject eo2 = ipr.getRootASTElement();
>
> //-------------From here...Serialization
> Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
> Map<String, Object> m = reg.getExtensionToFactoryMap();
>
> m.put("entity",
> injector.getInstance(org.eclipse.xtext.resource.IResourceFac tory.class));
>
> ResourceSet resSet = new ResourceSetImpl();
>
> Resource resource =
> resSet.createResource(URI.createURI("entities/My2.entity"));
> resource.getContents().add(eo2);
>
> try {
> resource.save(Collections.EMPTY_MAP);
> } catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
> }catch (Exception e){
> System.out.println(e.toString());
> e.printStackTrace();
> }
> }
> }
>
>
> Has anyone an idea whats wrong?
>
> Thanks in advance, Helko
>
Re: [xtext] Serializing parsed DSL Model [message #56812 is a reply to message #56785] Thu, 09 July 2009 16:59 Go to previous messageGo to next message
Eclipse UserFriend
Hi Sebastian.

Thank you. Your code has no errors. But anyway, the serialization fails.

The new code:

EntitiesStandaloneSetup.doSetup();
EntitiesStandaloneSetup ess = new EntitiesStandaloneSetup();
Injector injector = ess.createInjector();

// instanciate a resource set
XtextResourceSet set = injector.getInstance(XtextResourceSet.class);
// create a uri
URI uri = URI.createFileURI("D:/myTmp/test.entity");
Resource res = set.getResource(uri, true);
List<Diagnostic> lod = res.getErrors();
Iterator<Diagnostic> diaIter = lod.iterator();
while (diaIter.hasNext()){
Diagnostic dia = diaIter.next();
System.err.println(dia.getMessage());
}

// res.getContents() contains the model
Resource newRes =
set.createResource(URI.createFileURI("D:/myTmp/testRefined.entity "));
newRes.getContents().add(res.getContents().get(0));
newRes.save(Collections.emptyMap());



And my short example for this code:

type String
type Bool

entity Session {
property Title: String
}

And the error output:

Error serializing CrossRefs: Unable to create a string represenation for
reference 'Type' using
org.eclipse.xtext.linking.impl.DefaultLinkingService EReference: type
Context:org.xtext.example.entities.impl.PropertyImpl@1fbfd6 (name: Title,
many: false) Target:org.xtext.example.entities.impl.TypeImpl@2db19d
(eProxyURI:
file:/D:/myTmp/test.entity#xtextLink_:://@elements.2/@proper ties.0::http://www.xtext.org/example/Entities#//Property/typ e::/6)
org.eclipse.xtext.parsetree.reconstr.XtextSerializationExcep tion: Error
serializing CrossRefs: Unable to create a string represenation for
reference 'Type' using
org.eclipse.xtext.linking.impl.DefaultLinkingService EReference: type
Context:org.xtext.example.entities.impl.PropertyImpl@1fbfd6 (name: Title,
many: false) Target:org.xtext.example.entities.impl.TypeImpl@2db19d
(eProxyURI:
file:/D:/myTmp/test.entity#xtextLink_:://@elements.2/@proper ties.0::http://www.xtext.org/example/Entities#//Property/typ e::/6)
at
org.eclipse.xtext.parsetree.reconstr.impl.DefaultCrossRefere nceSerializer.serializeCrossRef(DefaultCrossReferenceSeriali zer.java:37)
at
org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeC onstructor$AssignmentToken.serializeThis(AbstractParseTreeCo nstructor.java:222)
at
org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeC onstructor.serialize(AbstractParseTreeConstructor.java:380)
at
org.eclipse.xtext.parsetree.reconstr.SerializerUtil.serializ e(SerializerUtil.java:47)
at
org.eclipse.xtext.parsetree.reconstr.SerializerUtil.serializ e(SerializerUtil.java:52)
at org.eclipse.xtext.resource.XtextResource.doSave(XtextResourc e.java:188)
at
org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(Resour ceImpl.java:1406)
at
org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(Resour ceImpl.java:993)
at ParserTestNG.main(ParserTestNG.java:36)

Thanks in advance, helko
Re: [xtext] Serializing parsed DSL Model [message #56865 is a reply to message #56812] Thu, 09 July 2009 21:46 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Helko,

sorry, the code snippet missed an important point. You'll have to
resolve any EMF proxy before you can serialize your model.

Please change your code slightly and insert the following lines:

XtextResourceSet set = injector.getInstance(XtextResourceSet.class);
set.setClasspathURIContext(getClasspathURIContext());
set.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);

This will try to resolve any proxy in your resource (and thereby trigger
the linker).

Sorry for inconvenience,
Sebastian

Am 09.07.2009 18:59 Uhr, schrieb Helko Glathe:
> Hi Sebastian.
>
> Thank you. Your code has no errors. But anyway, the serialization fails.
>
> The new code:
>
> EntitiesStandaloneSetup.doSetup();
> EntitiesStandaloneSetup ess = new EntitiesStandaloneSetup();
> Injector injector = ess.createInjector();
>
> // instanciate a resource set
> XtextResourceSet set = injector.getInstance(XtextResourceSet.class);
> // create a uri
> URI uri = URI.createFileURI("D:/myTmp/test.entity");
> Resource res = set.getResource(uri, true);
> List<Diagnostic> lod = res.getErrors();
> Iterator<Diagnostic> diaIter = lod.iterator();
> while (diaIter.hasNext()){
> Diagnostic dia = diaIter.next();
> System.err.println(dia.getMessage());
> }
>
> // res.getContents() contains the model
> Resource newRes =
> set.createResource(URI.createFileURI("D:/myTmp/testRefined.entity "));
> newRes.getContents().add(res.getContents().get(0));
> newRes.save(Collections.emptyMap());
>
>
>
> And my short example for this code:
>
> type String
> type Bool
>
> entity Session {
> property Title: String
> }
>
> And the error output:
>
> Error serializing CrossRefs: Unable to create a string represenation for
> reference 'Type' using
> org.eclipse.xtext.linking.impl.DefaultLinkingService EReference: type
> Context:org.xtext.example.entities.impl.PropertyImpl@1fbfd6 (name:
> Title, many: false)
> Target:org.xtext.example.entities.impl.TypeImpl@2db19d (eProxyURI:
> file:/D:/myTmp/test.entity#xtextLink_:://@elements.2/@proper ties.0::http://www.xtext.org/example/Entities#//Property/typ e::/6)
>
> org.eclipse.xtext.parsetree.reconstr.XtextSerializationExcep tion: Error
> serializing CrossRefs: Unable to create a string represenation for
> reference 'Type' using
> org.eclipse.xtext.linking.impl.DefaultLinkingService EReference: type
> Context:org.xtext.example.entities.impl.PropertyImpl@1fbfd6 (name:
> Title, many: false)
> Target:org.xtext.example.entities.impl.TypeImpl@2db19d (eProxyURI:
> file:/D:/myTmp/test.entity#xtextLink_:://@elements.2/@proper ties.0::http://www.xtext.org/example/Entities#//Property/typ e::/6)
>
> at
> org.eclipse.xtext.parsetree.reconstr.impl.DefaultCrossRefere nceSerializer.serializeCrossRef(DefaultCrossReferenceSeriali zer.java:37)
>
> at
> org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeC onstructor$AssignmentToken.serializeThis(AbstractParseTreeCo nstructor.java:222)
>
> at
> org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeC onstructor.serialize(AbstractParseTreeConstructor.java:380)
>
> at
> org.eclipse.xtext.parsetree.reconstr.SerializerUtil.serializ e(SerializerUtil.java:47)
>
> at
> org.eclipse.xtext.parsetree.reconstr.SerializerUtil.serializ e(SerializerUtil.java:52)
>
> at org.eclipse.xtext.resource.XtextResource.doSave(XtextResourc e.java:188)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(Resour ceImpl.java:1406)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(Resour ceImpl.java:993)
>
> at ParserTestNG.main(ParserTestNG.java:36)
>
> Thanks in advance, helko
>
Re: [xtext] Serializing parsed DSL Model [message #56893 is a reply to message #56865] Fri, 10 July 2009 05:30 Go to previous messageGo to next message
Eclipse UserFriend
Ah, thank you :) I will try it in an hour.

Are such useful informations documented somewhere? In the user guide i did
not find something like this.

Regards, Helko
Re: [xtext] Serializing parsed DSL Model [message #56946 is a reply to message #56865] Fri, 10 July 2009 07:13 Go to previous messageGo to next message
Eclipse UserFriend
Ok. That is working well.

One hint on the Getting Started Example in the User guide
( http://www.eclipse.org/Xtext/documentation/0_7_0/xtext.html# getting-started):

The DSL Instance Example contains a little typo...

It should be

...
property Speakers : Speaker[]
...

instead of

...
property Speakers : Speakers[]
...


Thanks again and regards, Helko
Re: [xtext] Serializing parsed DSL Model [message #57024 is a reply to message #56946] Fri, 10 July 2009 11:38 Go to previous message
Knut Wannheden is currently offline Knut WannhedenFriend
Messages: 298
Registered: July 2009
Senior Member
Hi Helko,

Thanks for pointing out the typo. The documentation has been corrected
and will soon be published to the web site.

Regards,

--knut

Helko Glathe wrote:
> Ok. That is working well.
>
> One hint on the Getting Started Example in the User guide
> ( http://www.eclipse.org/Xtext/documentation/0_7_0/xtext.html# getting-started):
>
>
> The DSL Instance Example contains a little typo...
>
> It should be
>
> ...
> property Speakers : Speaker[]
> ...
>
> instead of
>
> ...
> property Speakers : Speakers[]
> ...
>
>
> Thanks again and regards, Helko
>
Previous Topic:Using existing .uml file in textuell modell?
Next Topic:[Xtext] A very generic terminal definition
Goto Forum:
  


Current Time: Thu Jul 25 16:37:14 GMT 2024

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

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

Back to the top