Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to load a model conformant to the generated metamodel of an XText DSL?(How to configure an Xtext generated DSL to process models (xmi format)?)
How to load a model conformant to the generated metamodel of an XText DSL? [message #664761] Mon, 11 April 2011 21:12 Go to next message
LD  is currently offline LD Friend
Messages: 28
Registered: March 2011
Junior Member
Hi all.

I'm wondering to use a DSL, generated with xtext, to load a model that conforms to the metamodel generated of the DSL and convert it to a textual definition according to the predefined grammar. This model may come in XMI.

And additionally I would like to know how to generate an xmi model from a textual definition of the DSL. Which is the opposite of the first question.


Thank you.
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 Go to previous messageGo to next message
Horacio Hoyos is currently offline Horacio HoyosFriend
Messages: 242
Registered: October 2009
Location: Mexico
Senior Member

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 Go to previous messageGo to next message
Horacio Hoyos is currently offline Horacio HoyosFriend
Messages: 242
Registered: October 2009
Location: Mexico
Senior Member

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 Go to previous message
Horacio Hoyos is currently offline Horacio HoyosFriend
Messages: 242
Registered: October 2009
Location: Mexico
Senior Member

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
Previous Topic:Get Xtext log4j stack traces
Next Topic:refactoring in xtext 2.0 - getting started
Goto Forum:
  


Current Time: Sat Apr 27 01:01:25 GMT 2024

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

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

Back to the top