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

发布于 2024-08-31 15:55:25 字数 295 浏览 5 评论 0 原文

我使用 Xtext 来定义我的 DSL。 Xtext 生成一个解析器,让我可以遍历输入 DSL 的 EMF 模型。我想将此 EMF 模型转换为其他树。为了进行这个翻译,我需要遍历这棵树。但是,我找不到 Xtext 生成的 EMF 模型的访问者类。我发现的最接近的是访问单个节点的 Switch 类。我可以自己遍历 EMF 模型并在我访问的每个节点上调用 Switch 类。但是,我想知道 Xtext 中是否存在实现模型遍历的访问者功能。

I'm using Xtext to define my DSL. Xtext generates a parser that lets me traverse the EMF model of my input DSL. I'd like to translate this EMF model into some other tree. 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. The closest thing that I've found is a Switch class that visits a single node. I can traverse the EMF model myself and invoke the Switch class on each node that I visit. But, I wonder if there exists a visitor functionality in Xtext that implements the model traversal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫瑾少女 2024-09-07 15:55:25

如果您考虑此线程,请切换 (此处使用)几乎是 XText 的官方访问者模式。< br>
您还拥有 自定义验证流程

AbstractDeclarativeValidator 的目的是允许您以声明性方式编写约束 - 正如类名称所暗示的那样。
也就是说,无需编写详尽的 if-else 结构或扩展生成的 EMF 开关,您只需将 @Check 注释添加到任何方法,并且在验证发生时将自动调用它。

但这可能并不完全是您所需要的。

If you consider this thread, Switch (used here for instance) is pretty much the official visitor pattern for 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.
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.

That may be not exactly what you need, though.

定格我的天空 2024-09-07 15:55:25

我想 EcoreUtil.getAllProperContents(Resource, false) 可能就是您正在寻找的。您将获得一个 TreeIterator 来遍历资源的内容。将 iter.next() 的结果传递到 Switch 中,并根据元素类型执行转换逻辑。

I guess EcoreUtil.getAllProperContents(Resource, false) might be what you are looking for. You'll get a TreeIterator that will traverse the contents of the resource. Pass the result of iter.next() into a Switch and perform the transformation logic depending on the element type.

悲歌长辞 2024-09-07 15:55:25

嗯,使用 EMF API 还不够吗?如果你想转换模型,使用一些像Xtend这样的转换语言是不可能的吗?

Hmm, is it not sufficient to use EMF API? If you want to transform the model, is using some of the transformation langauges like Xtend not possible?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文