Ecore Metamodeling

Cross-Resource References

Learn how to split models across multiple files and manage cross-resource references in Swift-Ecore.

In this tutorial, you’ll explore how to break down large models into smaller, linked resources. You’ll understand how Ecore handles references between files using proxies and URIs, and how to work with ResourceSets to load dependent models.

45 mins Estimated Time

Section 1

Understanding Resources and Proxies

In EMF and Swift-Ecore, a Resource represents a single file containing model data. A ResourceSet is a collection of Resources that are loaded and linked together.

When a model element in one resource refers to an element in another resource, it uses a proxy. A proxy is a placeholder that identifies the target element via a URI. When you access the reference, the engine “resolves” the proxy by loading the target resource.

Step 1

Create a metamodel that supports cross-referencing.

This metamodel defines Department and Employee classes. A Department contains Employees, but can also refer to related Departments (potentially in other files).

Organisation.ecore
1<?xml version="1.0" encoding="UTF-8"?>
2<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="Organisation" nsURI="http://www.example.org/organisation" nsPrefix="org">
4 <eClassifiers xsi:type="ecore:EClass" name="Department">
5 <eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
6 <eStructuralFeatures xsi:type="ecore:EReference" name="employees" upperBound="-1" eType="#//Employee" containment="true"/>
7 <eStructuralFeatures xsi:type="ecore:EReference" name="relatedDepartments" upperBound="-1" eType="#//Department"/>
8 </eClassifiers>
9 <eClassifiers xsi:type="ecore:EClass" name="Employee">
10 <eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
11 </eClassifiers>
12</ecore:EPackage>

Step 2

Understand the href syntax used in XMI for cross-resource references: href="targetFile.xmi#//TargetElementID" where the part before # is the target resource URI and the part after is the fragment identifier.

Section 2

Creating Linked Models

We will create two separate model files: a “Library” of shared definitions, and a “Project” that references them.

Step 1

Create the referenced resource (The Library).

This file contains common departments (e.g., “HR”, “IT”) that other projects might refer to. It acts as a shared library.

SharedDeps.xmi
1<?xml version="1.0" encoding="UTF-8"?>
2<org:Department xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:org="http://www.example.org/organisation" name="Shared Services">
3 <relatedDepartments name="IT"/>
4 <relatedDepartments name="HR"/>
5 <relatedDepartments name="Finance"/>
6</org:Department>

Step 2

Create the referencing resource (The Project).

This file defines a specific project. It refers to the “IT” department defined in SharedDeps.xmi. Note the href pointing to the other file.

ProjectA.xmi
1<?xml version="1.0" encoding="UTF-8"?>
2<org:Department xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:org="http://www.example.org/organisation" name="Project Alpha">
3 <!-- Reference to the IT department in the shared library -->
4 <relatedDepartments href="SharedDeps.xmi#//@relatedDepartments.0"/>
5 <employees name="Alice"/>
6 <employees name="Bob"/>
7</org:Department>

Section 3

Resolving References

When you load ProjectA.xmi, Swift-Ecore initially sees a proxy for the IT department. If you navigate to it or validate the model, the engine automatically loads SharedDeps.xmi to resolve the reference.

Step 1

Validate the linked models.

Run the validation command on ProjectA.xmi. The tool will follow the reference to SharedDeps.xmi to ensure the target element exists and matches the type.

Terminal
1swift run swift-ecore validate ProjectA.xmi

Step 2

Inspect the resolved model.

Use the query command to verify that the reference is correctly resolved. You can navigate from ProjectA to the properties of the shared IT department.

Terminal
1swift run swift-ecore query ProjectA.xmi --query "relatedDepartments"

Check Your Understanding

Question 1 of 2

What is a “proxy” in Ecore?

Question 2 of 2

What syntax is used in XMI for cross-resource references?

Creating Your First ATL Transformation

Learn how to transform models with the Atlas Transformation Language (ATL) using the Families to Persons example.