# Defining an EMF metamodel

To illustrate the metamodel, before we deal with the intricacies of EMF, here is the metamodel in UML:

Figure 1. Sample metamodel

### Creating an EMF project

Create an EMF project as depicted below:

Figure 2. Create EMF project

It is important that you create an EMF project, not just a simple or a Java project. Name it `xpand.demo.emf.datamodel`.

### Defining the (meta)model

Create a new source folder `metamodel` in that project. Then, create a new Ecore model in that source folder named `data.ecore`. Use EPackage as the model object.

Figure 3. Create new Ecore model

This opens the Ecore Editor. You will see a root package with name `null`. Open the Properties View (context menu). Set the following properties for the package:

- Name: data

- Ns prefix: data

- Ns URI: http://www.xpand.org/xpand.demo.emf.datamodel

Figure 4. Adjust namespace settings

Create the following Ecore model.[1]Make sure you set the following properties exactly as described next:

Within the data package, create these `EClass` elements with their attributes:[2]

EClass name | EAttribute name | EAttribute EType
--- | --- | ---
DataModel |  |
| name | EString
Entity |  |
| name | EString
Attribute |  |
| name | EString
| type | EString
EntityReference |  |
| name | EString
| toMany | EBoolean

Now, it is time to create references between the model elements. Add children of type `EReferences` as follows:[3]

EClass | EReference name | EReference attribute name | EReference attribute value
--- | --- | --- | ---
DataModel |  |  |
| entity |  |
|  | EType | Entity
|  | containment | true
|  | Lowerbound | 0
|  | Upperbound | -1
Entity |  |  |
| attribute |  |
|  | EType | Attribute
|  | containment | true
|  | Lowerbound | 1
|  | Upperbound | -1
Entity |  |  |
| reference |  |
|  | EType | EntityReference
|  | containment | true
|  | Lowerbound | 0
|  | Upperbound | -1
EntityReference |  |  |
| target |  |
|  | EType | Entity
|  | containment | false
|  | Lowerbound | 1
|  | Upperbound | 1

Figure 5. Metamodel structure

EMF saves the model we created above in its own dialect of XMI. To avoid any ambiguities, here is the complete XMI source for the metamodel. It goes into the file `data.ecore`:

    <?xml version="1.0" encoding="UTF-8"?>
    <ecore:EPackage xmi:version="2.0"
        xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="data"
        nsURI="http://www.xpand.org/xpand.demo.emf.datamodel" nsPrefix="data">
      <eClassifiers xsi:type="ecore:EClass" name="DataModel">
        <eStructuralFeatures xsi:type="ecore:EAttribute" name="name"
          eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
        <eStructuralFeatures xsi:type="ecore:EReference" name="entity" upperBound="-1"
            eType="#//Entity" containment="true"/>
      </eClassifiers>
      <eClassifiers xsi:type="ecore:EClass" name="Entity">
        <eStructuralFeatures xsi:type="ecore:EAttribute" name="name"
          eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
        <eStructuralFeatures xsi:type="ecore:EReference" name="attribute" lowerBound="1"
            upperBound="-1" eType="#//Attribute" containment="true"/>
        <eStructuralFeatures xsi:type="ecore:EReference" name="reference" upperBound="-1"
            eType="#//EntityReference" containment="true"/>
      </eClassifiers>
      <eClassifiers xsi:type="ecore:EClass" name="Attribute">
        <eStructuralFeatures xsi:type="ecore:EAttribute" name="name"
          eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
        <eStructuralFeatures xsi:type="ecore:EAttribute" name="type"
          eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
      </eClassifiers>
      <eClassifiers xsi:type="ecore:EClass" name="EntityReference">
        <eStructuralFeatures xsi:type="ecore:EAttribute" name="name"
          eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
        <eStructuralFeatures xsi:type="ecore:EAttribute" name="toMany"
          eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EBoolean"/>
        <eStructuralFeatures xsi:type="ecore:EReference" name="target" lowerBound="1"
            eType="#//Entity"/>
      </eClassifiers>
    </ecore:EPackage>

[1] To add children, right-click on the element to which you want to add these children and select the type of the child from the list. To configure the properties, open the properties dialog by selecting Show Properties View at the bottom of any of the context menus. Note that this is not an EMF tutorial. For more details on how to build EMF (meta-)models, please refer to the EMF documentation.

[2] Attributes are children of type `EAttribute`. Please fill in the Name and the EType properties.

[3] Note: there are a couple of -1's ... don't miss the minus! Also, the containment flag is essential. If containment is `true` you will be able to create children of the referenced type, otherwise you can only reference them.