Ecore Metamodeling

Working with Model Instances

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

In this tutorial, you’ll create model instances using the Company metamodel from Tutorial 01, populate them with data, and validate that they conform to the metamodel structure.

25 mins Estimated Time

Section 1

Understanding Model Instances

A metamodel defines the structure (classes and attributes), whilst model instances contain the actual data. Think of it like the relationship between a database schema and database records.

Model instances in Ecore are typically stored in XMI (XML Metadata Interchange) format, which is a standard way of serialising models.

Metamodel vs Model Instance

Step 1

Use the Company metamodel from Tutorial 01 as the schema.

This metamodel defines two classes: Person (with name and email) and Company (with name). We’ll create instances that conform to this structure.

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>

Step 2

Create an empty XMI model file.

The XMI file starts with XML and XMI declarations, plus the namespace for the company metamodel. The xmlns:company must match the nsURI from your metamodel.

company-model.xmi
1<?xml version="1.0" encoding="UTF-8"?>
2<xmi:XMI xmi:version="2.0"
3 xmlns:xmi="http://www.omg.org/XMI"
4 xmlns:company="http://www.example.org/company">
5</xmi:XMI>

Section 2

Creating Model Instances

Model instances are created by adding elements that correspond to classes defined in your metamodel.

Each instance must have an xmi:id for unique identification and can have attribute values matching those defined in the metamodel.

Step 1

Add a Person instance to your model.

The <company:Person> element creates an instance of the Person class. The name and email attributes match those defined in the metamodel.

company-model.xmi
1<?xml version="1.0" encoding="UTF-8"?>
2<xmi:XMI xmi:version="2.0"
3 xmlns:xmi="http://www.omg.org/XMI"
4 xmlns:company="http://www.example.org/company">
5
6 <company:Person xmi:id="person1" name="Alice Johnson" email="alice@example.com"/>
7
8</xmi:XMI>

Step 2

Add multiple instances including a Company and several Persons.

You can create multiple instances of the same class. Each must have a unique xmi:id. Here we have one Company and three Person instances.

company-model.xmi
1<?xml version="1.0" encoding="UTF-8"?>
2<xmi:XMI xmi:version="2.0"
3 xmlns:xmi="http://www.omg.org/XMI"
4 xmlns:company="http://www.example.org/company">
5
6 <company:Company xmi:id="company1" name="Tech Innovations Ltd"/>
7
8 <company:Person xmi:id="person1" name="Alice Johnson" email="alice@techinnovations.com"/>
9 <company:Person xmi:id="person2" name="Bob Smith" email="bob@techinnovations.com"/>
10 <company:Person xmi:id="person3" name="Carol Williams" email="carol@techinnovations.com"/>
11
12</xmi:XMI>

Section 3

Validating Model Instances

After creating model instances, you should validate them against the metamodel to ensure they conform to the defined structure.

The swift-ecore validation tool checks that:

  • All element types exist in the metamodel

  • All attributes are defined in the metamodel

  • Attribute types match the metamodel definitions

Step 1

Validate your model instance against the metamodel.

The validate command checks that company-model.xmi conforms to Company.ecore. If validation succeeds, the model is structurally correct.

Terminal
1swift-ecore validate company-model.xmi --metamodel Company.ecore

Step 2

View the model structure using the query command.

The query command lets you inspect the contents of your model, listing all instances organised by class type.

Terminal
1swift-ecore query company-model.xmi \
2 --metamodel Company.ecore \
3 --query "list-classes"

Check Your Understanding

Question 1 of 3

What is the relationship between a metamodel and a model instance?

Question 2 of 3

Why does each model element need an xmi:id attribute?

Question 3 of 3

What does the xmlns:company declaration specify?

Metamodel Relationships

Learn how to create relationships between classes using EReference, including containment, cardinality, and bidirectional references.