get a model from a grammar expression [message #1745066] |
Tue, 04 October 2016 14:03  |
Eclipse User |
|
|
|
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 #1745137 is a reply to message #1745072] |
Wed, 05 October 2016 11:06   |
Eclipse User |
|
|
|
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.03978 seconds