Introduction: Based on the EMF (Eclipse modeling Framework) model reflection mechanism, this paper implements a method of comparing EMF model objects and shows how to use the algorithm to get the matching degree of the object. First, you set the object's list of fields to compare. For each of these fields, gets and compares the field values of the objects. In the process of comparison, the algorithm decomposes the comparison of composite data types, such as custom classes and lists, into comparisons of their child data types. The result of a model comparison is a list of variance items that, as a basis for subsequent applications, can be used in scenarios such as version control, model import/export, and so on.
Introduction to EMF and Ecore
The Eclipse Modeling Framework (EMF) is an open source model-driven application development framework. It can create Java code based on XML Schema, UML, or Java interface with model feature annotations, which enables graphical data editing, manipulation, reading, and serialization. EMF is the foundation of many tools in IBM WebSphere Studio and Eclipse projects.
The Ecore meta model is the core of the EMF framework that describes the EMF model and provides run-time support for the model, including: Model modification notifications, the default XMI serialization to provide EMF persistence support, and the efficient reflection API that is common to manipulating EMF objects. In this paper, the EMF reflection API is used to read the value of the EMF object, on the basis of which the EMF objects are compared.
Figure 1. Ecore type Tree
Figure 1 is the Ecore type tree. The gray fill background in the figure is expressed in the EMF framework, where the implementation class of the interface is abstract, and the interface with the yellow fill background has a Non-abstract implementation class. The types associated with this article in the diagram are described below:
Eattribute: Used to describe a property that has a name and a type. Eattribute describes simple data, which is specified by a edatatype.
Eclass: Is the meta type of the EMF object, used to describe the modeling model. It describes the fields (field) of the modeling class with attributes (Eattribute) and references (ereference). Java-like Object.getclass () gets the Class that invokes the Eobject object's Eclass () method to get Eclass.
Edatatype: Used to describe the type of an attribute that must be a simple data type, including basic (primitive type) data types such as int, a Java type such as String, or an array.
Efactory: An abstract factory that contains methods for creating modeling objects.
Eobject: Visible from Figure 1, Eobject is the base type (or super type) of all EMF modeling objects, similar to Java.lang.Object within the EMF framework. To distinguish the method names in user modeling, the method names defined in the Eobject interface begin with "E". such as the Eclass () method returns the Metamodel (Eclass) of an EMF object.
Epackage: In Ecore, Epackage contains information about Model classes (ECLASS) and data types (edatatype), how to get epackage instances and information about the model class is described in detail later.
Ereference: Used to describe the association relationship between classes, ereference have names, a Boolean flag bit that describes the containing relationship (whether or not the relationship between the two types is aggregated (Aggregation) or combination (compostition)); The reference (target) type, which specifies the type of relationship, because the association relationship is a relationship between two types, so ereference always points to the combined data type.
Estructurefeature: Ereference and Eattribute are the common super classes. You can use it as a field in understanding. Because the implementation of Estructuralfeature is an abstract class, the resulting Estructuralfeature object must be an instance of Eattribute or ereference type.