1

我在 Scala 中开发的应用程序的一部分需要读取和解析 EMF/UML 模型以及在这些模型上定义的 OCL 表达式。我的 OCL 表达式几乎是在这些 EMT/UML 模型上定义的查询表达式。

我的问题:

1) 读取和解析 EMF/UML 模型的 API 选项有哪些?

2) 有哪些 API 选项可以解析和评估 EMF/UML 模型上的 OCL 表达式(查询)。

4

1 回答 1

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

于 2015-04-30T07:46:19.227 回答