Accessing Xtext's runtime EMF model

假如想象 提交于 2019-12-07 16:05:29

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!