[xtext] Serializing parsed DSL Model [message #56730] |
Thu, 09 July 2009 11:14  |
Eclipse User |
|
|
|
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 #56785 is a reply to message #56730] |
Thu, 09 July 2009 12:21   |
Eclipse User |
|
|
|
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 #56865 is a reply to message #56812] |
Thu, 09 July 2009 17:46   |
Eclipse User |
|
|
|
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
>
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03847 seconds