Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » get a model from a grammar expression(how can i get a model from its corresponding grammar expression)
get a model from a grammar expression [message #1745066] Tue, 04 October 2016 18:03 Go to next message
Aran A is currently offline Aran AFriend
Messages: 30
Registered: January 2011
Member
Hi,

When a new dsl (its grammar) is created, an Ecore model (i.e. a metamodel) is inferred (we can see it in the model/generated folder).

If I write an expression conforming to the grammar (e.g. in the example.mydsl file), how can I get the model conforming to the corresponding metamodel?

In a different context (a dsl defined using an imported Ecore model), if I have the model I can get the corresponding grammar expression (e.g. example.mydsl file). But now using a similar code I cannot get it (I guess that one of the problems might be that the extension of the model file is the same as the file of the grammar expression, but I'm not sure, and I don't know how I could change it).

Any clue? Thanks in advance.

Best regards,

Aran
Re: get a model from a grammar expression [message #1745072 is a reply to message #1745066] Tue, 04 October 2016 18:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13898
Registered: July 2009
Senior Member
where/in which context are you when "when you want to get a model"?
inside eclipse, inside a standalone application, .....

a standalone app it could look like

public class Main {
	
	public static void main(String[] args) throws IOException {
		Injector i = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
		ResourceSet rs = i.getInstance(ResourceSet.class);
		Resource r = rs.getResource(URI.createURI("test.mydsl2"), true);
		r.load(null);
		Model m = (Model)r.getContents().get(0);
		for (Greeting g : m.getGreetings()) {
			System.out.println(g.getName());
		}
	}

}


in eclipse it could look similar to the approaches under https://christiandietrich.wordpress.com/2011/10/15/xtext-calling-the-generator-from-a-context-menu/

any yes if you you reuse the file extension you might get a problem unless you customize your imported emf and your dsl e.g. regarding supporting content types.
i am not sure if you intend to have the same file extension or not or if you dont know how to change the file extension for what? the xtext dsl or the emf/xmi based one


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: get a model from a grammar expression [message #1745137 is a reply to message #1745072] Wed, 05 October 2016 15:06 Go to previous messageGo to next message
Aran A is currently offline Aran AFriend
Messages: 30
Registered: January 2011
Member
Hi,

I'm not developing a big project, but trying to learn which things I can do with dsls, metamodels, and so on. So, in this moment I'm just playing with Eclipse.

I've been trying and at the end, the hint has been the '.xmi' extension for the model file (I wanted another extension, because when I create the metamodels in EMF (without grammars and XText) I'm used to define my own extension for the model files, but anyway, for this example I don't mind to use xmi).

So, the working code to get the model file is as following:

		  // Initialization for the grammar expression 
		  Injector injector = new CusEvDslStandaloneSetup().createInjectorAndDoEMFRegistration();
		  ResourceSet rs = injector.getInstance(ResourceSet.class);
		  
		  // Load the grammar expression
		  // mydsl is the extension defined for the grammar expressions
		  // It is defined in the GenerateWTDsl.mwe2 file
		  Resource r1 = rs.getResource(URI.createURI("example/pro1.mydsl"), true);
		  r1.load(null);
		  System.out.println("Grammar expression loaded...");
		  
		  // Model is the name of the grammar root
		  Model m= (Model) r1.getContents().get(0);
		  
		  // Save the model in a *.xmi file
		  Resource r2 = rs.createResource(URI.createURI("example/res1.xmi")); 
		  r2.getContents().add(m);
		  r2.save(null);
		  System.out.println("model saved...");


By the way, to get it I've had to simplify my grammar. The original one has some problems, and when I try to save the file (r2.save(null)) I get this error (Error: java.lang.RuntimeException: Could not serialize EObject via backtracking. ...). Now I'll have to investigate that....

Thanks a lot,

Aran
Re: get a model from a grammar expression [message #1745155 is a reply to message #1745137] Wed, 05 October 2016 18:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13898
Registered: July 2009
Senior Member
Where do you call this code from. It is important that you don't call standalonesetup from
A eclipse context e.g. A command or a action


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: get a model from a grammar expression [message #1745212 is a reply to message #1745155] Thu, 06 October 2016 14:54 Go to previous messageGo to next message
Aran A is currently offline Aran AFriend
Messages: 30
Registered: January 2011
Member
I'm just playing, so I'm inside Eclipse with a toy application with 'main'.
I've read some previous advices about StandaloneSetup and the need of injectors, but I haven't got every detail and I need more time to research on it.
I've saved these two references, something else that you can recommend?

http://koehnlein.blogspot.com.es/2012/11/xtext-tip-how-do-i-get-guice-injector.html
http://christiandietrich.wordpress.com/2011/10/15/xtext-calling-the-generator-from-a-context-menu/

Thanks a lot, regards,

Aran
Re: get a model from a grammar expression [message #1745222 is a reply to message #1745212] Thu, 06 October 2016 16:05 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13898
Registered: July 2009
Senior Member
No these should cover all you need as start

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:INode of unresolvable proxy EObjects
Next Topic:Limit scope provider to few files
Goto Forum:
  


Current Time: Fri Apr 23 15:24:54 GMT 2021

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

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

Back to the top