[Compare] How to perform one-way-merge [message #783222] |
Wed, 25 January 2012 10:04 |
Holger S Messages: 4 Registered: January 2012 |
Junior Member |
|
|
Hi,
I have a small question about a problem I initially thought to be trivial....
I modeled a metamodel mainly consisting of a root having several child classes where each of the child class has some properties or containments.
This metamodel is instantiated from two different places of my application where each place is responsible for a specific part of the metamodel. So, each place initiates a root object and adds its child classes to it.
Finally I would like to merge these two "submodels" together to an overall model containing all information.
Unfortunatly, using the "standard EMF compare code snippet" which can be found all over the forums doesn't lead to my expected solution as all the information from the second model is removed and the whole instances of the first model are copied to the second one. As a result, I receive two identical models.... Here is the snippet I am using:
Object model1 = ModelUtils.load(model1, resourceSet);
EObject model2 = ModelUtils.load(model2, resourceSet);
// Matching model elements
MatchModel match = MatchService.doMatch(model1, model2, Collections.<String, Object> emptyMap());
// Computing differences
DiffModel diff = DiffService.doDiff(match, false);
// Merges all differences from model1 to model2
List<DiffElement> differences = new ArrayList<DiffElement>(diff.getOwnedElements());
MergeService.merge(differences, true);
However, I am able to reach my goal if I use the Eclipse EMF Compare Plugin by comparing my submodels, selecting each instance of the left side and clicking on "Copy current change from left to right". In that way, I get a model on the right side with all instances included!
So, this leads to my final question: how can I reach that goal with code?
Greetings and thanks in advance
Holger
|
|
|
Re: [Compare] How to perform one-way-merge [message #788022 is a reply to message #783222] |
Wed, 01 February 2012 08:54 |
|
Hi Holger,
The difference between your two approaches is that from your code, you merge "all" difference (thus making the two models identical), while from the UI you select a specific set of differences to merge. You need to change this part of your code :
List<DiffElement> differences = new ArrayList<DiffElement>(diff.getOwnedElements());
MergeService.merge(differences, true);
in order to filter out of the "differences" list those diffs you do not want to merge. The semantics of "what you want to merge", however, are up to you. For example, if you wish to merge all model element that have been "added" to the left model (considering that the "right" one is the oldest), then you need to merge all differences that match
diff instanceof ModelElementChangeLeftTarget && !diff.isRemote()
Note that if you only consider two-way comparisons, you do not need to check for the "remote" boolean.
Laurent Goubet
Obeo
|
|
|
|
Powered by
FUDForum. Page generated in 0.02025 seconds