EMF model comparison with EMF Compare

1.8k Views Asked by At

I have two versions of the EMF instances that are based on the same ecore model. I need to prepare a list of things that changed from v1 to v2 in the following format

For each object in the model Object Name: Modified Attributes: Added Attributes: Deleted Attributes:

Each of these emf instance files are actually a representation of the DB data. User doesn't directly change the DB but they change the emf instance file. The tool need to identify these changes and then need to generate the necessary DML statements. Appreciate if a pseudo code can be presented on how to achieve this or is there a better alternative. Below is the code I currently have

public Comparison compare()
{
    // Load the two input models
    ResourceSet resourceSet1 = new ResourceSetImpl();
    ResourceSet resourceSet2 = new ResourceSetImpl();
    String xmi1 = "src/test/java/com/equifax/ic/provisioning/service/v1.xmi";
    String xmi2 = "src/test/java/com/equifax/ic/provisioning/service/v2.xmi";
    load(xmi1, resourceSet1);
    load(xmi2, resourceSet2);

    // Configure EMF Compare
    EMFCompare comparator = EMFCompare.builder().build();

    // Compare the two models
    IComparisonScope scope = EMFCompare.createDefaultScope(resourceSet1, resourceSet2);
    return comparator.compare(scope);
}

@Test
public void testCompare()
{
    Comparison comparison = compare();
    List<Diff> differences = comparison.getDifferences();

    for(Diff d: differences)
    {
        System.err.println("d.getKind(): "+d.getKind());
        System.err.println("d.getMatch(): " + d.getMatch());
        System.err.println("State: " + d.getState());
    }

    assertSame(Integer.valueOf(12), Integer.valueOf(differences.size()));
}

Output

d.getKind(): ADD
d.getMatch(): MatchSpec{left=BillableSystemEvent@1b5340c Application Processed, right=BillableSystemEvent@16c163f Application Processed, origin=<null>, #differences=2, #submatches=2}
State: UNRESOLVED

d.getKind(): DELETE
d.getMatch(): MatchSpec{left=BillableSystemEvent@1b5340c Application Processed, right=BillableSystemEvent@16c163f Application Processed, origin=<null>, #differences=2, #submatches=2}
State: UNRESOLVED
1

There are 1 best solutions below

0
On

I cannot say I really understand everything you're trying to achieve, but as I understand it, you are not really interested in the format EMF Compare uses for its differences. Rather, you are trying to generate a differente kind of representation for the diffs.

You might be interested in just reimplementing an IDiffProcessor. Diff processors are notified every time we detect a change. By default we create our "Diff" instances... Nothing prevents you from generating DML statements instead. You can get a quick overview of the IDiffProcessor API here.