在XTend中加载EMF模型实例 [英] Load EMF Model instance in XTend
问题描述
我正在XTend中构建代码生成器,这里我已经具有输入模型和元模型.也就是说,我使用ATL为我的XTend代码生成器生成输入模型(作为逐渐降低抽象级别的转换序列的一部分,而不是一次;这是我不使用xtext创建语法的原因)
I am building a code generator in XTend where I already have an input model and meta model. That is, I use ATL to generate the input model for my XTend code generator (as part of a transformation sequence to gradually lower the abstraction level, instead of at once; this is the reason i'm not using xtext to create the syntax).
非常清楚,我的代码生成器输入模型是XMI格式的文件,而不是xtext项目语法中的文件(甚至不使用它)!而且我认为这导致了我的问题/困惑.
So to be very clear, my input model for the code generator is a file in XMI format and NOT in the grammar of the xtext project (not even using that)! And i think this is causing me problems/confusion.
我使用现有模型创建了一个新的XText项目,右键单击.text文件,运行为,生成伪像,然后对mwe2文件执行相同的操作.
I created a new XText project using Existing models, right clicked on the .text file, run as , generate artefacts, and then i did the same for the mwe2 file.
下一步是什么,我做对了吗?如何启动我的代码生成器?所有示例均来自使用XText创建DSL的POV.我有一个EMF元模型,以及一个基于XMI的实例.如何使用XTend进一步处理该问题?
What is the next step, am I doing it right? How can I start my code generator? All the examples are from the POV that you use XText to create a DSL. I have an EMF meta model, and an XMI based instance of that. How to process that further using XTend?
任何提示或教程指南都是有帮助的.
Any hint or pointer to a tutorial is helpful.
解决方案:
该解决方案正如Sven在我接受的答案中所建议的那样,但是我还要指出,您需要使用genmodel从元模型生成Java工件.该链接显示了如何: http://www.vogella.com/articles/EclipseEMF/article. html ,请参阅第4节.这看起来似乎太合逻辑了,但是无论如何,我认为还是值得注意的.
The solution was as Sven suggested in my accepted answer, but also I would like to note that you need to use a genmodel to generate Java artifacts from your meta model. This link shows how: http://www.vogella.com/articles/EclipseEMF/article.html , see section 4. This may appear all too logical, but i think it's worth noting anyway.
推荐答案
如果您有XMI,并且只想从中生成代码,则根本不需要Xtext. 只需从Java项目开始(我将使用一个插件项目,以重用依赖关系管理)并开始编码:
If you have an XMI and just want to generate code from it, you don't need Xtext at all. Just start with a Java project (I'd use a plug-in project, to reuse the dependency management) and start coding:
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.ecore.resource.Resource$Factory$Registry
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl
class MyCodeGenerator {
def static void main(String[] args) {
new MyCodeGenerator().generate("mymodel.xmi")
}
def generate(String file) {
doEMFSetup
val resourceSet = new ResourceSetImpl
val resource = resourceSet.getResource(URI.createURI(file), true)
for (content : resource.contents) {
generateCode(content)
}
}
def dispatch generateCode(MySpecialType it) '''
public class «name» {
«FOR member : members»
«ENDFOR»
}
'''
def dispatch generateCode(MyMember it) '''
private «type» «name»;
...
'''
def doEMFSetup() {
// EPackage$Registry.INSTANCE.put(MyPackage.eINSTANCE.nsURI, MyPackage.eINSTANCE)
Resource$Factory.Registry.INSTANCE.extensionToFactoryMap.put("xmi", new XMIResourceFactoryImpl);
}
}
您需要添加到清单中的依赖项:
The dependencies you need to add to your Manifest :
Require-Bundle: org.eclipse.xtend.lib,
com.google.guava,
org.eclipse.xtext.xbase.lib,
org.eclipse.emf.common,
org.eclipse.emf.ecore,
org.eclipse.emf.ecore.xmi
这篇关于在XTend中加载EMF模型实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!