我发现 Papyrus 真正有用的特性之一是能够以编程方式询问它通过在 Eclipse UI 之外使用 UML2 运行时创建的 UML 模型。这非常适合运行简单的工具,例如使用 POI 生成文档或为 Talend MDM 工具编写模型驱动的配置。然而,虽然通过加载资源集中的资源很容易实现遍历和处理模型树,但事实证明,在 .notation 文件中操作图表更具挑战性。
我已经到了org.eclipse.papyrus.infra.export.ExportAllDiagrams
可以加载所有资源并Diagram
从 .notation 文件中找到元素的地方(通过检查 .notation 的源代码):
File uml = new File(model + ".uml");
File di = new File(model + ".di");
File notation = new File(model + ".notation");
URI umlUri = URI.createFileURI(uml.getAbsolutePath());
URI diUri = URI.createFileURI(di.getAbsolutePath());
URI notationUri = URI.createFileURI(notation.getAbsolutePath());
final ModelSet resourceSet = new ModelSet();
resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
resourceSet.getPackageRegistry().put(NotationPackage.eNS_URI, NotationPackage.eINSTANCE);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("notation", new XMIResourceFactoryImpl());
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
try {
resourceSet.getLoadOptions().put(XMLResource.OPTION_DEFER_IDREF_RESOLUTION, true);
resourceSet.getLoadOptions().put(XMLResource.OPTION_DEFER_ATTACHMENT, true);
resourceSet.getResource(diUri, true);
resourceSet.getResource(umlUri, true);
resourceSet.getResource(notationUri, true);
List<Diagram> diagrams = new ArrayList<Diagram>();
for (Iterator<Notifier> i = resourceSet.getAllContents(); i.hasNext();) {
Notifier n = i.next();
if (n instanceof Diagram) {
diagrams.add((Diagram) n);
}
}
//export(diagrams);
} finally {
// Unload the resource set so that we don't leak loads of UML content in the CacheAdapter
unload(resourceSet);
}
但是,ExportAllDiagrams
该类最终用于org.eclipse.gmf.runtime.diagram.ui.render.util.CopyToImageUtil
呈现图表,此时它会失败,因为它依赖于DiagramUIRenderPlugin
并DiagramUIRenderPlugin.getInstance()
返回 null。
然后我看了看,org.eclipse.gmf.runtime.diagram.ui.render.clipboard.DiagramSVGGenerator
但在需要初始化各种 Eclipse 插件时遇到了类似的问题。
我没有 Eclipse 插件系统的经验,但我假设平台加载和初始化插件,因此,到目前为止尝试的方法需要在 Eclipse GUI 环境中运行才能工作。是否有任何其他方法可用于轻松地将图表呈现为 SVG 而无需依赖整个 Eclipse 运行时?