Eclipse Model Operations (EMO)

»code usage«

Usage via code

Comparing 2 models

If you have two EMF models contained in two EObjects you can compare them easily by feeding the Comparator with them as shown below.

/*
 * This method compares two emf models given as EObjects.
 * It also maps both models and computes the differences between them.
 */
public void compare(EObject model1, EObject model2) {
	//this Hashtable contains the weighting-scheme for the comparators used
	//for detailed information please refer to the documentation.
	Hashtable<String, Double> weightings = null;
	//indicates whether to use a full cross product between both models for comparison
	boolean crossProd = false;
	//indicates whether to use the position of compared elements for critical
	//mapping-decisions
	boolean useTreePos = false;
	
	//now initialize the ModelComparator with the weighting-scheme, and the flags for
	//cross product and tree position usage
	ModelComparator comparator = new ModelComparator(weightings, crossProd, treePos);
	//after that you have to initialize both models
	//in this step the comparator builds up the models for comparison
	comparator.initModels(model1, model2);
	//calling the method 'compareTo()' will execute the matching of both models and will
	//return a Matching containing pairs of elements with the associated matching-values
	Matching matching = 
		comparator.compareTo(comparator.getLeftModel(), comparator.getRightModel());
	
	//after that you can retrieve the mapping and the difference-model
	//therefore you just have to initialize the ModelMatcher with the matching
	//and the comparator from the previous step.
	ModelMatcher matcher = new ModelMatcher(matching, comparator);
	//then you can easily get the mapping and diff by calling the appropriate methods
	//both results are given as EMF models for ensuring the ease of your further work
	MapModel mapping = matcher.getMapping();
	DiffModel diffModel = matcher.getDiff(mapping);
}					

The next snippet shows you how to compare two models which are serialized to files.

/*
 * This method compares two emf models which are serialized to the given files.
 * It also maps both models and computes the differences between them.
 */
public void compare(String model1Path, String model2Path, String metaModelPath) {
	//this Hashtable contains the weighting-scheme for the comparators used
	//for detailed information please refer to the documentation.
	Hashtable<String, Double> weightings = null;
	//indicates whether to use a full cross product between both models for comparison
	boolean crossProd = false;
	//indicates whether to use the position of compared elements for critical
	//mapping-decisions
	boolean useTreePos = false;
	
	//now initialize the ModelComparator with the weighting-scheme, and the flags for
	//cross product and tree position usage
	ModelComparator comparator = new ModelComparator(weightings, crossProd, treePos);
	/* 
	 * after that you have to initialize both models
	 * in this step the comparator builds up the models for comparison
	 * just hand over the paths of both models (the path for the metamodel can be ""
	 * because the mm is currently not used for comparison
	 */
	comparator.initModels(model1Path, model2Path, metaModelPath);
	//calling the method 'compareTo()' will execute the matching of both models and will
	//return a Matching containing pairs of elements with the associated matching-values
	Matching matching = 
		comparator.compareTo(comparator.getLeftModel(), comparator.getRightModel());
	
	/* 
	 * after that you can retrieve the mapping and the difference-model
	 * therefore you just have to initialize the ModelMatcher with the matching
	 * and the comparator from the previous step.
	 */
	ModelMatcher matcher = new ModelMatcher(matching, comparator);
	//then you can easily get the mapping and diff by calling the appropriate methods
	//both results are given as EMF models for ensuring the ease of your further work
	MapModel mapping = matcher.getMapping();
	DiffModel diffModel = matcher.getDiff(mapping);
}