问题
I created a DSL via Xtext and now I need to transform the models created in the editor into another model(s). I suppose the most straightforward way is to employ some kind of M2M transformation framework, but i need to access the model behind the textual file. Question: how can I get a reference to the model?
回答1:
The models created by Xtext have also a resource factory created for them. If you try to load the textual file as an EMF model, EMF will look for resource factories available for the extension of your textual file. From there, Xtext will transform the textual file in an EMF model and give EMF the model created. You can see this mechanism in action by right clicking on your textual file and selecting "open with" and "sample reflective ecore model editor". So something like this should work:
ResourceSet rs = new ResourceSetImpl();
Resource r = rs.getResource(uriOfYourTextualFile, true);
List<EObject> contentOfYourFile = r.getContents();
来源:https://stackoverflow.com/questions/11824710/accessing-xtexts-runtime-emf-model