Is there a way to combine multiple Ecore models (2 or more) in a single Ecore model programmatically in Java? With all models conform to the same metamodel.
In:
Model1 conforming to metamodelX
Model2 conforming to metamodelX
model3 conforming to metamodelX
model4 conforming to metamodelX
model5 conforming to metamodelX
Out:
modelOut conforming to metamodelX and merge of Model1, Model2, model3, model4, model5 ...
There is Eclipse project for handling EMF comparing and Merging, called EMF Compare.
Here is example provided by them:
// Loading models
EObject 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);
This really provides very good ways to handle model merging and other compare stuffs. You can also manually go through the changes.
Here is full example provided by them: Here
You will need to define what 'merge' means to you. You can easily attach all EMF models to the same resource and serialize them.
You will probably want to establish equivalencies between model1 and model2. Find some objects which are equal between model1 and model2. After this, you can find the differences.
As an example:
Model1 is a FARM with serial number 33829. Children: 2 CHICKENS, 3 EGGS and 1 PIG
Model2 is a FARM with serial number 33829. Children: 4 CHICKENS, 3 EGGS and 1 PIG
The matching step establishes the following equivalencies:
Model1->FARM = Model2->FARM because serial number is equal
all other entities have not been matched
After this step comes the differences step:
REMOVED: 2 CHICKENS, 3 EGGS, 1 PIG
ADDED: 4 CHICKENS, 3 EGGS, 1 PIG
Using those differences, you can apply them to your model. Applying only the 'ADDED' difference gives you the following model:
Model1+2 is a FARM with serial number 33829. Children: 2 CHICKENS, 3 EGGS, 1 PIG, 4 CHICKENS, 3 EGGS, 1 PIG
It's up to you to determine the business rules of 'merging'. You will first have to determine when two entities are the same (matching). This can be based on a unique key, on their place in the tree, or based on a lot of other things, depending on your metamodel.
As a result, you will have a list of 'differences'. It's up to you to define which differences to apply.
If you see 'merge' as an SVN Merge (i.e. Model1 = Model0 + changes, Model2 = Model0 + other changes), then the MergeService
already contains all business rules to do this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With