import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import org.eclipse.compare.structuremergeviewer.DiffElement;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.compare.diff.merge.service.MergeService;
import org.eclipse.emf.compare.diff.metamodel.ComparisonResourceSnapshot;
import org.eclipse.emf.compare.diff.metamodel.DiffFactory;
import org.eclipse.emf.compare.diff.metamodel.DiffModel;
import org.eclipse.emf.compare.diff.service.DiffService;
import org.eclipse.emf.compare.match.metamodel.MatchModel;
import org.eclipse.emf.compare.match.service.MatchService;
import org.eclipse.emf.compare.util.ModelUtils;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
/**
* This application will try and launch an headless model comparison.
*
* @author Cedric Brun <a href="mailto:cedric.brun@obeo.fr">cedric.brun@obeo.fr</a>
*/
public final class ExampleLauncher {
/**
* This class doesn't need to be instantiated.
*/
private ExampleLauncher() {
// prevents instantiation
}
/**
* Launcher of this application.
*
* @param args
* Arguments of the launch.
*/
public static void main(String[] args) {
if (args.length == 2 && new File(args[0]).canRead() && new File(args[1]).canRead()) {
// Creates the resourceSets where we'll load the models
final ResourceSet resourceSet1 = new ResourceSetImpl();
final ResourceSet resourceSet2 = new ResourceSetImpl();
// Register additionnal packages here. For UML2 for instance :
// Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION,
// UMLResource.Factory.INSTANCE);
// resourceSet1.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
// resourceSet2.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
try {
System.out.println("Loading resources.\n"); //$NON-NLS-1$
// Loads the two models passed as arguments
final EObject model1 = ModelUtils.load(new File(args[0]), resourceSet1);
final EObject model2 = ModelUtils.load(new File(args[1]), resourceSet2);
// Creates the match then the diff model for those two models
System.out.println("Matching models.\n"); //$NON-NLS-1$
final MatchModel match = MatchService.doMatch(model1, model2, Collections
.<String, Object> emptyMap());
System.out.println("Differencing models.\n"); //$NON-NLS-1$
final DiffModel diff = DiffService.doDiff(match, false);
System.out.println("Merging difference to args[1].\n"); //$NON-NLS-1$
final EList<DiffElement> differences = new BasicEList(diff.getOwnedElements());
// This will merge all references to the right model (second argument).
MergeService.merge(differences, true);
// Prints the results
try {
System.out.println("MatchModel :\n"); //$NON-NLS-1$
System.out.println(ModelUtils.serialize(match));
System.out.println("DiffModel :\n"); //$NON-NLS-1$
System.out.println(ModelUtils.serialize(diff));
} catch (final IOException e) {
e.printStackTrace();
}
// Serializes the result as "result.emfdiff" in the directory this class has been called from.
System.out.println("saving emfdiff as \"result.emfdiff\""); //$NON-NLS-1$
final ComparisonResourceSnapshot snapshot = DiffFactory.eINSTANCE
.createComparisonResourceSnapshot();
snapshot.setDate(Calendar.getInstance().getTime());
snapshot.setMatch(match);
snapshot.setDiff(diff);
ModelUtils.save(snapshot, "result.emfdiff"); //$NON-NLS-1$
} catch (final IOException e) {
// shouldn't be thrown
e.printStackTrace();
} catch (final InterruptedException e) {
e.printStackTrace();
}
} else {
System.out.println("usage : ExampleLauncher <Model1> <Model2>"); //$NON-NLS-1$
}
}
}
Matching models.
Differencing models.
Merging difference to args[1].
MatchModel :
org.eclipse.emf.ecore.resource.Resource$IOWrappedException: The object 'evo.impl.VeraenderungImpl@a761fe (querprofil: 12, schrittweite: -0.15858634204784403)' is not contained in a resource.
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.endSave(XMLSaveImpl.java:306)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.save(XMLSaveImpl.java:235)
at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doSave(XMLResourceImpl.java:350)
at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.save(XMLResourceImpl.java:325)
at org.eclipse.emf.compare.util.ModelUtils.serialize(ModelUtils.java:429)
at evo.presentation.ExampleLauncher.main(ExampleLauncher.java:79)
Caused by: org.eclipse.emf.ecore.xmi.DanglingHREFException: The object 'evo.impl.VeraenderungImpl@a761fe (querprofil: 12, schrittweite: -0.15858634204784403)' is not contained in a resource.
at org.eclipse.emf.ecore.xmi.impl.XMLHelperImpl.handleDanglingHREF(XMLHelperImpl.java:760)
at org.eclipse.emf.ecore.xmi.impl.XMLHelperImpl.getURIFragment(XMLHelperImpl.java:731)
at org.eclipse.emf.ecore.xmi.impl.XMLHelperImpl.getIDREF(XMLHelperImpl.java:753)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveIDRefSingle(XMLSaveImpl.java:1972)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveFeatures(XMLSaveImpl.java:1318)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveElementID(XMLSaveImpl.java:2685)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveElement(XMLSaveImpl.java:1174)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveElement(XMLSaveImpl.java:1035)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveContainedMany(XMLSaveImpl.java:2386)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveFeatures(XMLSaveImpl.java:1533)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveElementID(XMLSaveImpl.java:2685)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.writeTopObject(XMLSaveImpl.java:680)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.traverse(XMLSaveImpl.java:588)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.save(XMLSaveImpl.java:230)
... 4 more
saving emfdiff as "result.emfdiff"
org.eclipse.emf.ecore.resource.Resource$IOWrappedException: The object 'evo.impl.VeraenderungImpl@a761fe (querprofil: 12, schrittweite: -0.15858634204784403)' is not contained in a resource.
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.endSave(XMLSaveImpl.java:306)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.save(XMLSaveImpl.java:270)
at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doSave(XMLResourceImpl.java:302)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(ResourceImpl.java:1409)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(ResourceImpl.java:991)
at org.eclipse.emf.compare.util.ModelUtils.save(ModelUtils.java:405)
at evo.presentation.ExampleLauncher.main(ExampleLauncher.java:93)
Caused by: org.eclipse.emf.ecore.xmi.DanglingHREFException: The object 'evo.impl.VeraenderungImpl@a761fe (querprofil: 12, schrittweite: -0.15858634204784403)' is not contained in a resource.
at org.eclipse.emf.ecore.xmi.impl.XMLHelperImpl.handleDanglingHREF(XMLHelperImpl.java:760)
at org.eclipse.emf.ecore.xmi.impl.XMLHelperImpl.getURIFragment(XMLHelperImpl.java:731)
at org.eclipse.emf.ecore.xmi.impl.XMLHelperImpl.getIDREF(XMLHelperImpl.java:753)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveIDRefSingle(XMLSaveImpl.java:1972)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveFeatures(XMLSaveImpl.java:1318)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveElementID(XMLSaveImpl.java:2685)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveElement(XMLSaveImpl.java:1174)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveElement(XMLSaveImpl.java:1035)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveContainedMany(XMLSaveImpl.java:2386)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveFeatures(XMLSaveImpl.java:1533)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveElementID(XMLSaveImpl.java:2685)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveElement(XMLSaveImpl.java:1174)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveElement(XMLSaveImpl.java:1035)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveContainedSingle(XMLSaveImpl.java:2372)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveFeatures(XMLSaveImpl.java:1527)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveElementID(XMLSaveImpl.java:2685)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.writeTopObject(XMLSaveImpl.java:680)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.traverse(XMLSaveImpl.java:588)
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.save(XMLSaveImpl.java:256)
... 5 more