Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Saving model in DSL format(How to save an EMF model as DSL)
Saving model in DSL format [message #921393] Mon, 24 September 2012 04:36 Go to next message
Andy Gotz is currently offline Andy GotzFriend
Messages: 32
Registered: July 2010
Member
Hi,

we are using Xtext 2.2 to do implement our DSL. We manipulate the model in memory and persist the model as an xmi file using the EMF Resource.save() method. We would like to save the model in memory as text file using the DSL grammar we have specified with Xtext. How can we do this in Xtext 2.2? What is the code? I have not found an example in the documentation.

All help appreciated

Thanks

Andy
Re: Saving model in DSL format [message #921463 is a reply to message #921393] Mon, 24 September 2012 06:12 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6544
Registered: July 2009
Senior Member
Hi it is 100% the same as with xmi

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Saving model in DSL format [message #921774 is a reply to message #921463] Mon, 24 September 2012 12:13 Go to previous messageGo to next message
Andy Gotz is currently offline Andy GotzFriend
Messages: 32
Registered: July 2010
Member
Hi Christian,

thanks for your quick answer but I do not completely understand. The code I have right now generates an xmi file. What do I have to change to generate the dsl file.

My xmi generation code is:

ResourceSet resourceSet = new ResourceSetImpl();
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl());
URI fileURI = URI.createFileURI(new File(xmiFileName).getAbsolutePath());
Resource resource = resourceSet.createResource(fileURI);
resource.getContents().add(sys);
try {
resource.save(Collections.EMPTY_MAP);
System.out.println(xmiFileName + " generated");
} catch (IOException e) {

Where do I specify the format to write?
Re: Saving model in DSL format [message #921845 is a reply to message #921774] Mon, 24 September 2012 13:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6544
Registered: July 2009
Senior Member
Call your dslstandalonrsetup.do setup and use a DSL URI when creating
the resource

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Saving model in DSL format [message #922151 is a reply to message #921845] Mon, 24 September 2012 19:46 Go to previous messageGo to next message
Andy Gotz is currently offline Andy GotzFriend
Messages: 32
Registered: July 2010
Member
If I understand correctly all I should do is create a file with the extension of my registered DSL. In my case this is called "pogo". So if I create a file "myexample.pogo" instead of "myexample.xmi" I will have the model in the format of my DSL's grammar.

Sorry to be slow in understanding !
Re: Saving model in DSL format [message #922220 is a reply to message #922151] Mon, 24 September 2012 21:10 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6544
Registered: July 2009
Senior Member
Here a greeting example

package org.xtext.example.mydsl;

import java.io.IOException;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.xtext.example.mydsl.myDsl.Greeting;
import org.xtext.example.mydsl.myDsl.Model;
import org.xtext.example.mydsl.myDsl.MyDslFactory;

import com.google.inject.Injector;

public class Main {

	public static void main(String[] args) {
		Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
		ResourceSet rs = injector.getInstance(ResourceSet.class);
		Resource r = rs.createResource(URI.createURI("test.mydsl"));
		Model m = MyDslFactory.eINSTANCE.createModel();
		Greeting g1 = MyDslFactory.eINSTANCE.createGreeting();
		g1.setName("g1");
		Greeting g2 = MyDslFactory.eINSTANCE.createGreeting();
		g2.setName("g2");
		m.getGreetings().add(g1);
		m.getGreetings().add(g2);
		r.getContents().add(m);
		try {
			r.save(null);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
Re: Saving model in DSL format [message #923028 is a reply to message #922220] Tue, 25 September 2012 14:45 Go to previous message
Aaron Digulla is currently offline Aaron DigullaFriend
Messages: 258
Registered: July 2009
Location: Switzerland
Senior Member
The code is the same, no matter in which format you save your model. The trick is the file extension. It tells the framework in which format you want to save. If you use ".xml", you get an ecore XMI model. If you use ".foo" (if the extension for your DSL is "foo"), then the model will be saved in the "Foo" DSL.
Previous Topic:registered resource factory is needed
Next Topic:Context sensitive implicit namespace imports
Goto Forum:
  


Current Time: Thu Dec 18 23:19:52 GMT 2014

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

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