简体   繁体   English

如何遍历Xtext生成的EMF对象树?

[英]How can I traverse the EMF object tree generated by Xtext?

I'm using Xtext to define my DSL. 我正在使用Xtext来定义我的DSL。 Xtext generates a parser that lets me traverse the EMF model of my input DSL. Xtext生成一个解析器,让我遍历输入DSL的EMF模型。 I'd like to translate this EMF model into some other tree. 我想将这个EMF模型翻译成其他树。 To do this translation, I need to traverse the tree. 要做这个翻译,我需要遍历树。 But, I couldn't find a visitor class for the EMF model generated by Xtext. 但是,我找不到Xtext生成的EMF模型的访问者类。 The closest thing that I've found is a Switch class that visits a single node. 我发现最接近的是访问单个节点的Switch类。 I can traverse the EMF model myself and invoke the Switch class on each node that I visit. 我可以自己遍历EMF模型,并在我访问的每个节点上调用Switch类。 But, I wonder if there exists a visitor functionality in Xtext that implements the model traversal. 但是,我想知道Xtext中是否存在实现模型遍历的访问者功能。

If you consider this thread , Switch ( used here for instance) is pretty much the official visitor pattern for XText. 如果你考虑这个线程 ,Switch( 在这里使用)几乎就是XText的官方访问者模式。
You also have the custom Validation process : 您还有自定义验证过程

The purpose of the AbstractDeclarativeValidator is to allow you to write constraints in a declarative way – as the class name already suggests. AbstractDeclarativeValidator的目的是允许您以声明方式编写约束 - 正如类名已经建议的那样。
That is instead of writing exhaustive if-else constructs or extending the generated EMF switch you just have to add the @Check annotation to any method and it will be invoked automatically when validation takes place. 这不是编写详尽的if-else结构或扩展生成的EMF开关,只需将@Check注释添加到任何方法,它将在验证发生时自动调用。

That may be not exactly what you need, though. 但这可能不是你所需要的。

I guess EcoreUtil.getAllProperContents(Resource, false) might be what you are looking for. 我想EcoreUtil.getAllProperContents(Resource,false)可能就是你要找的东西。 You'll get a TreeIterator that will traverse the contents of the resource. 您将获得一个将遍历资源内容的TreeIterator。 Pass the result of iter.next() into a Switch and perform the transformation logic depending on the element type. 将iter.next()的结果传递给Switch,并根据元素类型执行转换逻辑。

Hmm, is it not sufficient to use EMF API? 嗯,使用EMF API还不够吗? If you want to transform the model, is using some of the transformation langauges like Xtend not possible? 如果你想转换模型,是不是可以使用像Xtend这样的一些转换语言?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM