如何以编程方式在两个Eclipse资源之间执行合并操作

时间:2017-12-21 08:31:48

标签: eclipse eclipse-rcp eclipse-emf emf

截至目前,我有两个Eclipse资源对象(org.eclipse.emf.ecore.resource.Resource),现在我想使用emf在两个资源对象之间实现合并操作。

1 个答案:

答案 0 :(得分:0)

您可以使用EMF-Compare比较和合并EMF模型。

来自FAQ的代码段,以编程方式启动EMF-Compare:

public void compare(File model1, File model2) {
    URI uri1 = URI.createFileURI("path/to/first/model.xmi");
    URI uri2 = URI.createFileURI("path/to/second/model.xmi");

    Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());

    ResourceSet resourceSet1 = new ResourceSetImpl();
    ResourceSet resourceSet2 = new ResourceSetImpl();

    resourceSet1.getResource(uri1, true);
    resourceSet2.getResource(uri2, true);

    IComparisonScope scope = new DefaultComparisonScope(resourceSet1, resourceSet2);
    Comparison comparison = EMFCompare.builder().build().compare(scope);

    List<Diff> differences = comparison.getDifferences();
    // Let's merge every single diff
    IMerger.Registry mergerRegistry = new IMerger.RegistryImpl();
    IBatchMerger merger = new BatchMerger(mergerRegistry);
    merger.copyAllLeftToRight(differences, new BasicMonitor());
}