【问题标题】:EMF/UML and OCL API for ScalaScala 的 EMF/UML 和 OCL API
【发布时间】:2015-07-09 16:28:16
【问题描述】:
我在 Scala 中开发的应用程序的一部分需要读取和解析 EMF/UML 模型以及在这些模型上定义的 OCL 表达式。我的 OCL 表达式几乎是在这些 EMT/UML 模型上定义的查询表达式。
我的问题:
1) 读取和解析 EMF/UML 模型的 API 选项有哪些?
2) 有哪些 API 选项可以解析和评估 EMF/UML 模型上的 OCL 表达式(查询)。
【问题讨论】:
标签:
scala
uml
eclipse-emf
metamodel
ocl
【解决方案1】:
要开始使用 EMF 和 UML,您至少需要对以下 jars 的依赖:
- org.eclipse.emf.common
- org.eclipse.emf.ecore
- org.eclipse.uml2.uml
然后您可以使用以下代码加载您的第一个 EMF 模型:
File file = new File("path")
ResourceSet resourceSet = new ResourceSetImpl();
// Register the various metamodels that will be used, here we are using UML
resourceSet.getPackageResgitry().put(UMLPackage.NS_URI, UMLPackage.eINSTANCE);
// Load the resource
URI uri = URI.createFileURI(file.getAbsolutePath());
Resource resource = resourceSet.getResource(uri, false);
// Iterate on the content of the whole resource
TreeIterator<EObject> iterator = resource.getAllContents();
while (iterator.hasNext()) {
EObject eObject = iterator.next();
}
在 EObjects(EMF 基本元素)上解析和评估 OCL 代码会稍微复杂一些,您可以查看 OCL 的文档和 wiki 以获取更多信息:https://wiki.eclipse.org/OCL#Example_Code