Ecore Metamodeling

Creating Your First Ecore Metamodel

Learn how to define a domain metamodel using Ecore by creating a simple company management system.

In this tutorial, you’ll create an Ecore metamodel that defines the structure for Person and Company entities, add attributes, and validate your metamodel using the swift-ecore command-line tool.

20 mins Estimated Time

Section 1

Understanding Ecore Metamodels

Ecore is a metamodeling framework that lets you define the structure of your domain models. A metamodel describes what kinds of objects exist in your domain and how they’re related.

Think of a metamodel as a schema or blueprint: just as a database schema defines tables and columns, an Ecore metamodel defines classes and attributes that your model instances will use.

Metamodel Hierarchy

Step 1

Create an empty Ecore package with namespace information.

Create a new file called Company.ecore in your working directory. The EPackage is the root container for your metamodel. The nsURI uniquely identifies your metamodel, whilst the nsPrefix is used in serialised models.

Company.ecore
1<?xml version="1.0" encoding="UTF-8"?>
2<ecore:EPackage xmi:version="2.0"
3 xmlns:xmi="http://www.omg.org/XMI"
4 xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
5 name="company"
6 nsURI="http://www.example.org/company"
7 nsPrefix="company">
8</ecore:EPackage>

Section 2

Defining Model Classes

Classes in Ecore (called EClass) represent the types of objects in your domain. Each class can have attributes and references to other classes.

We’ll start by creating a Person class, then add a Company class to build out our domain model.

Step 1

Add a Person class to your metamodel.

The EClass defines a new type in your metamodel. At this point, Person has no attributes—it’s just an empty class.

Company.ecore
1<?xml version="1.0" encoding="UTF-8"?>
2<ecore:EPackage xmi:version="2.0"
3 xmlns:xmi="http://www.omg.org/XMI"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
6 name="company"
7 nsURI="http://www.example.org/company"
8 nsPrefix="company">
9
10 <eClassifiers xsi:type="ecore:EClass" name="Person"/>
11
12</ecore:EPackage>

Step 2

Add attributes to the Person class.

Attributes (EAttribute) define the properties of a class. Here, Person has name and email attributes, both of type EString.

Company.ecore
1<?xml version="1.0" encoding="UTF-8"?>
2<ecore:EPackage xmi:version="2.0"
3 xmlns:xmi="http://www.omg.org/XMI"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
6 name="company"
7 nsURI="http://www.example.org/company"
8 nsPrefix="company">
9
10 <eClassifiers xsi:type="ecore:EClass" name="Person">
11 <eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
12 <eStructuralFeatures xsi:type="ecore:EAttribute" name="email" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
13 </eClassifiers>
14
15</ecore:EPackage>

Step 3

Add a Company class with its own attributes.

Your metamodel now defines two classes: Person and Company. Each has a name attribute, and Person also has an email.

Company.ecore
1<?xml version="1.0" encoding="UTF-8"?>
2<ecore:EPackage xmi:version="2.0"
3 xmlns:xmi="http://www.omg.org/XMI"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
6 name="company"
7 nsURI="http://www.example.org/company"
8 nsPrefix="company">
9
10 <eClassifiers xsi:type="ecore:EClass" name="Person">
11 <eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
12 <eStructuralFeatures xsi:type="ecore:EAttribute" name="email" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
13 </eClassifiers>
14
15 <eClassifiers xsi:type="ecore:EClass" name="Company">
16 <eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
17 </eClassifiers>
18
19</ecore:EPackage>

Section 3

Validating Your Metamodel

Once you’ve defined your metamodel, it’s important to validate it to ensure there are no structural errors.

The swift-ecore validate command checks that your metamodel is well-formed and follows Ecore’s rules.

Step 1

Validate your metamodel using the swift-ecore tool.

If validation succeeds, you’ll see a confirmation message. If there are errors, swift-ecore will report what needs to be fixed.

Terminal
1swift-ecore validate Company.ecore

Step 2

Inspect your metamodel to see its structure.

The inspect command shows detailed information about your metamodel, including all classes, attributes, and their types.

Terminal
1swift-ecore inspect Company.ecore --detail full

Check Your Understanding

Question 1 of 3

What is the purpose of an Ecore metamodel?

Question 2 of 3

What does the nsURI in an EPackage represent?

Question 3 of 3

What is an EAttribute used for?

Working with Model Instances

Learn how to create and manage XMI model instances that conform to your Ecore metamodel.