|
Re: How to load a model conformant to the generated metamodel of an XText DSL? [message #664768 is a reply to message #664761] |
Mon, 11 April 2011 22:24 |
|
Hello,
I was about to ask the second question. In XText 1.0 you could use the mwe workflow and use the org.eclipse.xtext.MweWriter (if I am not wrong) to generate an xmi file (ecore model) of the text file. Maybe this is still supported in the new mwe2 engine.
However I am interested in doing it from java. So I was wondering if once having the XtextResource you could serialize the EObjects in it to an xmi file (and the other way round to answer the first post). I was looking at this post: http://www.vogella.de/articles/EclipseEMFPersistence/article .html, but was wondering if there is an XMI resource factory already present or if following the Xtext 2.0 model one has to inject it instead of declaring it.
I am doing some tests and will post any progress.
Regards,
Horacio
Horacio Hoyos Rodriguez
Kinori Tech
Need professional support for Epsilon, EMF?
Go to: https://kinori.tech
|
|
|
Re: How to load a model conformant to the generated metamodel of an XText DSL? [message #664778 is a reply to message #664768] |
Mon, 11 April 2011 23:24 |
|
Hello,
So far so good. I think I am generating the xmi model correctly. However, for some reason, it is not being saved (i.e. no file generated). This is what I have so far (this is invoked form a pop up menu):
public class ModelGenerator extends AbstractHandler implements IHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
System.out.println("ModelGenerator execute");
XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event);
if (xtextEditor != null) {
// The right click was done over an opened dsl file
IXtextDocument xtextDocument = XtextDocumentUtil.get(xtextEditor);
System.out.println(xtextDocument.toString());
String elementDescription = xtextDocument.readOnly(
new IUnitOfWork<String, XtextResource>() {
@Override
public String exec(XtextResource resource) throws Exception {
//File file = new File("c:/myfile.xmi");
System.out.println("Saving file");
Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
Map<String, Object> m = reg.getExtensionToFactoryMap();
m.put("xmi", new XMIResourceFactoryImpl());
// Obtain a new resource set
ResourceSet resSet = new ResourceSetImpl();
// Create a resource
// File name
String name = resource.getURI().segment(1)+ "/myfile.xmi";
Resource xmiResource = resSet.createResource(URI
.createURI(name));
// Get the first model element and cast it to the right type, in my
// example everything is hierarchical included in this first node
xmiResource.getContents().add(resource.getContents().get(0));
// Now save the content.
try {
xmiResource.save(null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
});
}
return this;
}
But no file is generated. Additionally, it seems the add method removes the EObject form the xtext resource, so I think you need to make a copy before passing it to the xmi resource.
Horacio
Horacio Hoyos Rodriguez
Kinori Tech
Need professional support for Epsilon, EMF?
Go to: https://kinori.tech
|
|
|
Re: How to load a model conformant to the generated metamodel of an XText DSL? [message #664781 is a reply to message #664778] |
Tue, 12 April 2011 00:17 |
|
Ok, I got it working. I was missing the output stream, which now I create to correctly save the file in the current project:
public class ModelGenerator extends AbstractHandler implements IHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
System.out.println("ModelGenerator execute");
XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event);
if (xtextEditor != null) {
// The right click was done over an opened rsk file
IXtextDocument xtextDocument = XtextDocumentUtil.get(xtextEditor);
System.out.println(xtextDocument.toString());
String elementDescription = xtextDocument.readOnly(
new IUnitOfWork<String, XtextResource>() {
@Override
public String exec(XtextResource resource) throws Exception {
System.out.println("Saving file");
Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
reg.getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());
// Obtain a new resource set
ResourceSet resSet = new ResourceSetImpl();
// Create a resource
// File name
System.out.println(resource.getURI().toString().substring(0, resource.getURI().toString().indexOf(resource.getURI().lastSegment())));
String name = resource.getURI().toString().substring(0, resource.getURI().toString().indexOf(resource.getURI().lastSegment())) + "myfile.xmi";
// Output stream to the file
ExtensibleURIConverterImpl converter = new ExtensibleURIConverterImpl();
OutputStream os = converter.createOutputStream(URI.createURI(name));
Resource xmiResource = resSet.createResource(URI.createURI(name));
// Get the first model element and cast it to the right type, in my
// example everything is hierarchical included in this first node
EObject root = resource.getContents().get(0);
xmiResource.getContents().add(root);
// Now save the content.
try {
xmiResource.save(os, null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
os.close();
}
return null;
}
});
}
return this;
}
}
I hope you can figure out the inverse process to generate the text file from the xmi.
Cheers,
Horacio
Horacio Hoyos Rodriguez
Kinori Tech
Need professional support for Epsilon, EMF?
Go to: https://kinori.tech
|
|
|
Powered by
FUDForum. Page generated in 0.03556 seconds