Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Create merged model using emf.compare ?
Create merged model using emf.compare ? [message #666068] Mon, 18 April 2011 21:07
Anton is currently offline AntonFriend
Messages: 23
Registered: July 2009
Junior Member
I am trying to understand how to use emf compare from code. Basically I
would like to compare two models A and B and create third model C which is
the result of merging A and B. Currently I assume that B and only contain
addition data so C should always be:

C = A+B

Currently I have the following test:

private void test() {
ResourceSet resourceSet = new ResourceSetImpl();
Map extensionMap = (Map) resourceSet.getResourceFactoryRegistry()
.getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());
try {

Region region01 = model1(resourceSet);
printModel(region01);

Region region02 = model2(resourceSet);
printModel(region02);

final MatchModel match = MatchService.doMatch(region01, region02,
Collections.<String, Object> emptyMap());
addResourceToModel(resourceSet, match, "match.xmi");

final DiffModel diff = DiffService.doDiff(match, false);
addResourceToModel(resourceSet, diff, "diff.xmi");

// Prints the results
System.out.println("MATCH:");
if (match.eResource() != null)
System.out.println(ModelUtils.serialize(match));


System.out.println("DIFF:");
if (diff.eResource() != null)
System.out.println(ModelUtils.serialize(diff));


EList<DiffElement> differences = diff.getDifferences();
for (DiffElement diffElement : differences) {
MergeService.merge(diffElement, true);
}

System.out.println("DIFF AFTER MERGE:");
if (diff.eResource() != null)
System.out.println(ModelUtils.serialize(diff));

// EObject load = ModelUtils.load(new File("diff.xmi"), resourceSet);

} catch (final InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

private Region model1(ResourceSet resourceSet) {
Region region01 = StatemachineFactoryImpl.eINSTANCE.createRegion();
addResourceToModel(resourceSet, region01, "st1.xmi");
State state01 = StatemachineFactoryImpl.eINSTANCE.createState();
state01.setName("aaaa");
region01.getState().add(state01);
if (state01.eResource() == null) {
System.out.println("state01 NOT contained in resource!");
return null;
}
return region01;
}

private Region model2(ResourceSet resourceSet) {
Region region02 = StatemachineFactoryImpl.eINSTANCE.createRegion();
addResourceToModel(resourceSet, region02, "st2.xmi");

State state02 = StatemachineFactoryImpl.eINSTANCE.createState();
state02.setName("bbbb");

State state03 = StatemachineFactoryImpl.eINSTANCE.createState();
state03.setName("cccc");

State state04 = StatemachineFactoryImpl.eINSTANCE.createState();
state04.setName("dddd");

region02.getState().add(state02);
region02.getState().add(state03);
region02.getState().add(state04);
if (state02.eResource() == null || state03.eResource() == null) {
System.out.println("state02/03 NOT contained in resource!");
return null;
}
return region02;
}

private void addResourceToModel(ResourceSet resourceSet, EObject obj,
String path) {
Resource res = resourceSet.createResource(URI.createURI(path));
res.getContents().add(obj);
}

private void printModel(EObject model1) {
String serialize = null;
try {
serialize = ModelUtils.serialize(model1);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(serialize);
}



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

The above gives/prints the following result:

<?xml version="1.0" encoding="Cp1252"?>
<statemachine:Region xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
xmlns:statemachine="http://statemachine/r1">
<state name="aaaa"/>
</statemachine:Region>

<?xml version="1.0" encoding="Cp1252"?>
<statemachine:Region xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
xmlns:statemachine="http://statemachine/r1">
<state name="bbbb"/>
<state name="cccc"/>
<state name="dddd"/>
</statemachine:Region>

MATCH:
<?xml version="1.0" encoding="Cp1252"?>
<match:MatchModel xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:match="http://www.eclipse.org/emf/compare/match/1.1">
<matchedElements xsi:type="match:Match2Elements" similarity="0.9">
<leftElement href="st1.xmi#/"/>
<rightElement href="st2.xmi#/"/>
</matchedElements>
<unmatchedElements>
<element href="st1.xmi#//@state.0"/>
</unmatchedElements>
<unmatchedElements side="Right">
<element href="st2.xmi#//@state.0"/>
</unmatchedElements>
<unmatchedElements side="Right">
<element href="st2.xmi#//@state.1"/>
</unmatchedElements>
<unmatchedElements side="Right">
<element href="st2.xmi#//@state.2"/>
</unmatchedElements>
<leftRoots href="st1.xmi#/"/>
<rightRoots href="st2.xmi#/"/>
</match:MatchModel>

DIFF:
<?xml version="1.0" encoding="Cp1252"?>
<diff:DiffModel xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:diff="http://www.eclipse.org/emf/compare/diff/1.1">
<ownedElements xsi:type="diff:DiffGroup">
<subDiffElements xsi:type="diff:DiffGroup">
<subDiffElements xsi:type="diff:ModelElementChangeLeftTarget">
<rightParent href="st2.xmi#/"/>
<leftElement href="st1.xmi#//@state.0"/>
</subDiffElements>
<subDiffElements xsi:type="diff:ModelElementChangeRightTarget">
<leftParent href="st1.xmi#/"/>
<rightElement href="st2.xmi#//@state.0"/>
</subDiffElements>
<subDiffElements xsi:type="diff:ModelElementChangeRightTarget">
<leftParent href="st1.xmi#/"/>
<rightElement href="st2.xmi#//@state.1"/>
</subDiffElements>
<subDiffElements xsi:type="diff:ModelElementChangeRightTarget">
<leftParent href="st1.xmi#/"/>
<rightElement href="st2.xmi#//@state.2"/>
</subDiffElements>
<rightParent href="st2.xmi#/"/>
</subDiffElements>
</ownedElements>
<leftRoots href="st1.xmi#/"/>
<rightRoots href="st2.xmi#/"/>
</diff:DiffModel>

DIFF AFTER MERGE:
<?xml version="1.0" encoding="Cp1252"?>
<diff:DiffModel xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:diff="http://www.eclipse.org/emf/compare/diff/1.1">
<ownedElements xsi:type="diff:DiffGroup"/>
<leftRoots href="st1.xmi#/"/>
<rightRoots href="st2.xmi#/"/>
</diff:DiffModel>







But how do I apply the Diff/Match/Merge models/services to create the final
merged model which should look like this:

<?xml version="1.0" encoding="Cp1252"?>
<statemachine:Region xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
xmlns:statemachine="http://statemachine/r1">
<state name="aaaa"/>
<state name="bbbb"/>
<state name="cccc"/>
<state name="dddd"/>
</statemachine:Region>
Previous Topic:Unserialize
Next Topic:Bidirectional dependency between 2 different meta-model entities
Goto Forum:
  


Current Time: Sat Apr 20 04:13:05 GMT 2024

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

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

Back to the top