Skip to main content



      Home
Home » Modeling » Compare » EMF Compare from API(How do i get the differences of two Model by using the EMF Compare API?)
EMF Compare from API [message #871033] Thu, 10 May 2012 11:11 Go to next message
Eclipse UserFriend
Hello!

I am trying to use the EMF Compare diffing from a Java Program using the provided API.

So far i have this code:
public static void compare(String file1, String file2) {
	if (new File(file1).canRead() && new File(file2).canRead()) {

		 //register XMI resource factory for all other extensions
		 Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap(
		   ).put(Resource.Factory.Registry.DEFAULT_EXTENSION,
		         new XMIResourceFactoryImpl());
		 
		 ResourceSet resourceSet = new ResourceSetImpl();
		 
		 try {
			System.out.println("Loading resources.\n");

			EObject model1 = ModelUtils.load(new File(file1), resourceSet);
			EObject model2 = ModelUtils.load(new File(file2), resourceSet);

			System.out.println("Matching models.\n");
			MatchModel match = MatchService.doMatch(model1, model2, Collections.<String, Object> emptyMap());
			
			match = MatchService.doContentMatch(model1, model2, Collections.<String, Object> emptyMap());
			
		/*	GenericMatchEngine eng = new GenericMatchEngine();
			match = eng.contentMatch(model1, model2, Collections.<String, Object> emptyMap());
		*/	
			System.out.println("Differencing models.\n"); 
			DiffModel diff = DiffService.doDiff(match);
			
			System.out.println("In the model:\n"); 
			System.out.println(diff.getLeftRoots().toString());
			System.out.println(diff.getRightRoots().toString());
			
			System.out.println("\n---------------------\n"); 
			
			System.out.println(ModelUtils.serialize(diff));
			
			EList<DiffElement> diffs = diff.getOwnedElements();
			
			diffs = diff.getDifferences();
			
			
			
			ComparisonResourceSnapshot snapshot = DiffFactory.eINSTANCE.createComparisonResourceSnapshot();
			
			snapshot.setDate(Calendar.getInstance().getTime());
			//snapshot.setMatch(match);
			snapshot.setDiff(diff);
			
			
			TreeIterator<EObject> tree = snapshot.eAllContents();
			
			
			System.out.println("Tree:\n");
			while(tree.hasNext()){
				EObject next = tree.next();
				System.out.println(next.getClass() + " : : " + next); 
			}
			System.out.println("\n--------------------------------\n"); 
			
			System.out.println(diffs.size() + " differences found");
			
			printDiff(diffs, "|");
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	} else {
		System.out.println("Can NOT read the Files!"); 
	}
}

private static void printDiff(EList<DiffElement> diffs, String depth){
	for(DiffElement diff : diffs){
		System.out.println(depth + "Kind: " + diff.getKind() + " hidden: " + diff.getIsHiddenBy() + ", " + " , subelements: " + diff.getSubDiffElements().size());
		System.out.println(depth + "(" + diff + ")");
		System.out.println(depth + "(" + diff.getClass() + ")");
		if(diff.getSubDiffElements().size() > 0){
			printDiff(diff.getSubDiffElements(), depth+"----|");
		}
	}
}


So i compared 2 Models that had 1 Change when i compared them with the user interface.
And i get this output in the console:
Loading resources.

Matching models.

Differencing models.

In the model:

[org.eclipse.emf.ecore.impl.DynamicEObjectImpl@5fb9c5cd (eClass: org.eclipse.emf.ecore.impl.EClassImpl@49b510b8 (name: CompilationUnit) (instanceClassName: null) (abstract: false, interface: false)), org.eclipse.emf.ecore.impl.DynamicEObjectImpl@669b675b (eClass: org.eclipse.emf.ecore.impl.EClassImpl@7029245b (name: Field) (instanceClassName: null) (abstract: false, interface: false)), org.eclipse.emf.ecore.impl.DynamicEObjectImpl@714812a6 (eClass: org.eclipse.emf.ecore.impl.EClassImpl@7029245b (name: Field) (instanceClassName: null) (abstract: false, interface: false)), org.eclipse.emf.ecore.impl.DynamicEObjectImpl@7ebdc543 (eClass: org.eclipse.emf.ecore.impl.EClassImpl@7029245b (name: Field) (instanceClassName: null) (abstract: false, interface: false)), org.eclipse.emf.ecore.impl.DynamicEObjectImpl@7605ba5d (eClass: org.eclipse.emf.ecore.impl.EClassImpl@4a349f8a (name: AnnotationAttribute) (instanceClassName: null) (abstract: false, interface: false))]
[org.eclipse.emf.ecore.impl.DynamicEObjectImpl@720e6c33 (eClass: org.eclipse.emf.ecore.impl.EClassImpl@49b510b8 (name: CompilationUnit) (instanceClassName: null) (abstract: false, interface: false)), org.eclipse.emf.ecore.impl.DynamicEObjectImpl@63c8e097 (eClass: org.eclipse.emf.ecore.impl.EClassImpl@7029245b (name: Field) (instanceClassName: null) (abstract: false, interface: false)), org.eclipse.emf.ecore.impl.DynamicEObjectImpl@2e57c204 (eClass: org.eclipse.emf.ecore.impl.EClassImpl@7029245b (name: Field) (instanceClassName: null) (abstract: false, interface: false)), org.eclipse.emf.ecore.impl.DynamicEObjectImpl@78031613 (eClass: org.eclipse.emf.ecore.impl.EClassImpl@7029245b (name: Field) (instanceClassName: null) (abstract: false, interface: false)), org.eclipse.emf.ecore.impl.DynamicEObjectImpl@61935511 (eClass: org.eclipse.emf.ecore.impl.EClassImpl@4a349f8a (name: AnnotationAttribute) (instanceClassName: null) (abstract: false, interface: false))]

---------------------

<?xml version="1.0" encoding="Cp1252"?>
<diff:DiffModel xmi:version="2.0" xmlns:xmi="h_t_t_p://www.omg.org/XMI" xmlns:xsi="h_t_t_p://www.w3.org/2001/XMLSchema-instance" xmlns:diff="h_t_t_p://www.eclipse.org/emf/compare/diff/1.1">
  <ownedElements xsi:type="diff:DiffGroup">
    <subDiffElements xsi:type="diff:DiffGroup">
      <subDiffElements xsi:type="diff:DiffGroup">
        <subDiffElements xsi:type="diff:DiffGroup">
          <subDiffElements xsi:type="diff:DiffGroup">
            <subDiffElements xsi:type="diff:DiffGroup">
              <subDiffElements xsi:type="diff:DiffGroup">
                <subDiffElements xsi:type="diff:DiffGroup">
                  <subDiffElements xsi:type="diff:DiffGroup">
                    <subDiffElements xsi:type="diff:UpdateReference">
                      <reference href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/java.ecore#/14/ElementReference/target"/>
                      <rightElement href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/ver01/MyArrayList.java.xmi#/0/@classifiers.0/@members.9/@statements.0/@statement/@statements.0/@condition/@next"/>
                      <leftElement href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/MyArrayList.java.xmi#/0/@classifiers.0/@members.8/@statements.0/@statement/@statements.0/@condition/@next"/>
                      <leftTarget href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/src/src/MyArrayList.java#EMFTEXT_INTERNAL_URI_FRAGMENT_43_equals"/>
                      <rightTarget href="file:/C:/Users/däumling/JamoppTranslator/src/src/MyArrayList.java#EMFTEXT_INTERNAL_URI_FRAGMENT_43_equals"/>
                    </subDiffElements>
                    <rightParent href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/ver01/MyArrayList.java.xmi#/0/@classifiers.0/@members.9/@statements.0/@statement/@statements.0/@condition/@next"/>
                  </subDiffElements>
                  <rightParent href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/ver01/MyArrayList.java.xmi#/0/@classifiers.0/@members.9/@statements.0/@statement/@statements.0/@condition"/>
                </subDiffElements>
                <rightParent href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/ver01/MyArrayList.java.xmi#/0/@classifiers.0/@members.9/@statements.0/@statement/@statements.0"/>
              </subDiffElements>
              <rightParent href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/ver01/MyArrayList.java.xmi#/0/@classifiers.0/@members.9/@statements.0/@statement"/>
            </subDiffElements>
            <rightParent href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/ver01/MyArrayList.java.xmi#/0/@classifiers.0/@members.9/@statements.0"/>
          </subDiffElements>
          <rightParent href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/ver01/MyArrayList.java.xmi#/0/@classifiers.0/@members.9"/>
        </subDiffElements>
        <subDiffElements xsi:type="diff:ModelElementChangeRightTarget">
          <leftParent href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/MyArrayList.java.xmi#/0/@classifiers.0"/>
          <rightElement href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/ver01/MyArrayList.java.xmi#/0/@classifiers.0/@members.3"/>
        </subDiffElements>
        <rightParent href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/ver01/MyArrayList.java.xmi#/0/@classifiers.0"/>
      </subDiffElements>
      <rightParent href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/ver01/MyArrayList.java.xmi#/0"/>
    </subDiffElements>
  </ownedElements>
  <leftRoots href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/MyArrayList.java.xmi#/0"/>
  <leftRoots href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/MyArrayList.java.xmi#/1"/>
  <leftRoots href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/MyArrayList.java.xmi#/2"/>
  <leftRoots href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/MyArrayList.java.xmi#/3"/>
  <leftRoots href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/MyArrayList.java.xmi#/4"/>
  <rightRoots href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/ver01/MyArrayList.java.xmi#/0"/>
  <rightRoots href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/ver01/MyArrayList.java.xmi#/1"/>
  <rightRoots href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/ver01/MyArrayList.java.xmi#/2"/>
  <rightRoots href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/ver01/MyArrayList.java.xmi#/3"/>
  <rightRoots href="file:/C:/Users/däumling/JamoppTranslator/CompareExample/models/ver01/MyArrayList.java.xmi#/4"/>
</diff:DiffModel>

Tree:

class org.eclipse.emf.compare.diff.metamodel.impl.DiffModelImpl : : org.eclipse.emf.compare.diff.metamodel.impl.DiffModelImpl@36217796
class org.eclipse.emf.compare.diff.metamodel.impl.DiffGroupImpl : : org.eclipse.emf.compare.diff.metamodel.impl.DiffGroupImpl@4f32349d (conflicting: false, kind: Addition, remote: false)
class org.eclipse.emf.compare.diff.metamodel.impl.DiffGroupImpl : : 2 change(s) in CompilationUnit MyArrayList.java
class org.eclipse.emf.compare.diff.metamodel.impl.DiffGroupImpl : : 2 change(s) in Class MyArrayList
class org.eclipse.emf.compare.diff.metamodel.impl.DiffGroupImpl : : 1 change(s) in ClassMethod indexOf
class org.eclipse.emf.compare.diff.metamodel.impl.DiffGroupImpl : : 1 change(s) in ForLoop []
class org.eclipse.emf.compare.diff.metamodel.impl.DiffGroupImpl : : 1 change(s) in Block Block
class org.eclipse.emf.compare.diff.metamodel.impl.DiffGroupImpl : : 1 change(s) in Condition []
class org.eclipse.emf.compare.diff.metamodel.impl.DiffGroupImpl : : 1 change(s) in IdentifierReference []
class org.eclipse.emf.compare.diff.metamodel.impl.DiffGroupImpl : : 1 change(s) in MethodCall []
class org.eclipse.emf.compare.diff.metamodel.impl.UpdateReferenceImpl : : Reference target in [] changed from org.eclipse.emf.ecore.impl.DynamicEObjectImpl@2ca9f04e (eProxyURI: file:/C:/Users/däumling/JamoppTranslator/CompareExample/src/src/MyArrayList.java#EMFTEXT_INTERNAL_URI_FRAGMENT_43_equals eClass: org.eclipse.emf.ecore.impl.EClassImpl@4a349f8a (name: AnnotationAttribute) (instanceClassName: null) (abstract: false, interface: false)) to org.eclipse.emf.ecore.impl.DynamicEObjectImpl@7a774652 (eProxyURI: file:/C:/Users/däumling/JamoppTranslator/src/src/MyArrayList.java#EMFTEXT_INTERNAL_URI_FRAGMENT_43_equals eClass: org.eclipse.emf.ecore.impl.EClassImpl@4a349f8a (name: AnnotationAttribute) (instanceClassName: null) (abstract: false, interface: false))
class org.eclipse.emf.compare.diff.metamodel.impl.ModelElementChangeRightTargetImpl : : org.eclipse.emf.ecore.impl.DynamicEObjectImpl@3f279472 (eClass: org.eclipse.emf.ecore.impl.EClassImpl@7029245b (name: Field) (instanceClassName: null) (abstract: false, interface: false)) has been removed

--------------------------------

2 differences found
|Kind: Change hidden: [],  , subelements: 0
|(Reference target in [] changed from org.eclipse.emf.ecore.impl.DynamicEObjectImpl@2ca9f04e (eProxyURI: file:/C:/Users/däumling/JamoppTranslator/CompareExample/src/src/MyArrayList.java#EMFTEXT_INTERNAL_URI_FRAGMENT_43_equals eClass: org.eclipse.emf.ecore.impl.EClassImpl@4a349f8a (name: AnnotationAttribute) (instanceClassName: null) (abstract: false, interface: false)) to org.eclipse.emf.ecore.impl.DynamicEObjectImpl@7a774652 (eProxyURI: file:/C:/Users/däumling/JamoppTranslator/src/src/MyArrayList.java#EMFTEXT_INTERNAL_URI_FRAGMENT_43_equals eClass: org.eclipse.emf.ecore.impl.EClassImpl@4a349f8a (name: AnnotationAttribute) (instanceClassName: null) (abstract: false, interface: false)))
|(class org.eclipse.emf.compare.diff.metamodel.impl.UpdateReferenceImpl)
|Kind: Deletion hidden: [],  , subelements: 0
|(org.eclipse.emf.ecore.impl.DynamicEObjectImpl@3f279472 (eClass: org.eclipse.emf.ecore.impl.EClassImpl@7029245b (name: Field) (instanceClassName: null) (abstract: false, interface: false)) has been removed)
|(class org.eclipse.emf.compare.diff.metamodel.impl.ModelElementChangeRightTargetImpl)


So my Question: Is there a way to get the Differences out of the Diffmodel? So the exact places in the model where something has been changed, and what has been changed? Is there some feature in the API so i can get this information and use it in my program?

Or is there some description of the Diffmodel or the Diff-Metamodel so i can extract the difference myself?

Thanks in advance!

[Updated on: Sat, 12 May 2012 13:46] by Moderator

Re: EMF Compare from API [message #871491 is a reply to message #871033] Mon, 14 May 2012 04:16 Go to previous message
Eclipse UserFriend
Stefan,

The metamodel can be found in the project org.eclipse.emf.compare.diff (you can import it through File > Import > plug-ins and fragments). It is located in the "model" folder (diff.ecore).

You can retrieve all differences through DiffModel.getDifferences().

You did not ask any particular question (you are already retrieving all diffs through diffModel.getDifferences() so I guess you already have the answer to that)? I assume your issue is that the UI shows you 1 single diff when your code gives you 2?

Since you use "MatchService.doContentMatch", you are not in the same use case as what is done from the UI.

First things first, your two models should be in separate resources sets (they could depend on other models that have the same URI but different content. Probably cannot happen from your code, but frequent with repository comparisons). Second, it is not an EObject you compare, but the resource sets themselves (or the resource containing those EObjects, this choice is the popup dialog "resource only" or "full resource set" from the UI). Your code will thus look like :

...

//register XMI resource factory for all other extensions
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
	Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
		 
ResourceSet resourceSet1 = new ResourceSetImpl();
ResourceSet resourceSet2 = new ResourceSetImpl();
		 
try {
	System.out.println("Loading resources.\n");

	EObject model1 = ModelUtils.load(new File(file1), resourceSet1);
	EObject model2 = ModelUtils.load(new File(file2), resourceSet2);

	System.out.println("Matching models.\n");
	// Trough their resource
	// MatchModel match = MatchService.doResourceMatch(model1.eResource(),
		model2.eResource(), Collections.<String, Object> emptyMap());
	// Through their resource sets
	MatchResourceSet matchRS = MatchService.doResourceSetMatch(resourceSet1,
		resourceSet2, Collections.<String, Object> emptyMap());
			
	System.out.println("Differencing models.\n"); 
	DiffResourceSet diffRS = DiffService.doDiff(matchRS);

...


Note that you will have to alter the rest of the code to take into account the fact that you now have a DiffResourceSet (Collection of DiffModels, one for each Resource) and not a single DiffModel.

Laurent Goubet
Obeo
Previous Topic:Plans for Eclipse 4.2
Next Topic:How Do I Get Selections From the CompareEditor?
Goto Forum:
  


Current Time: Fri Jul 04 15:03:48 EDT 2025

Powered by FUDForum. Page generated in 0.04329 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top