|
Re: get a model from a grammar expression [message #1745072 is a reply to message #1745066] |
Tue, 04 October 2016 18:40 |
|
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
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 |
Aran A 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
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02998 seconds