Ecore Metamodeling

Querying Models with AQL

Learn how to query and navigate models using AQL (ATLAS Query Language), including element selection, collection operations, and complex queries.

In this tutorial, you’ll explore AQL’s powerful query capabilities to extract information from models, filter collections, and navigate relationships programmatically.

40 mins Estimated Time

Section 1

Introduction to AQL

AQL (ATLAS Query Language) is a powerful query language for model-driven engineering. It lets you navigate models, select elements, and compute derived values without writing imperative code.

Use AQL for model validation, reporting, metrics collection, and transformation—any task that needs to extract or compute information from your models.

AQL Query Language

Step 1

Start with a Company model containing multiple employees.

We’ll query this model to extract information about employees, filter by attributes, and compute statistics.

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:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xmlns:company="http://www.example.org/company">
6
7 <company:Company xmi:id="company1" name="Tech Innovations Ltd">
8 <employees xsi:type="company:Employee" xmi:id="emp1" name="Alice Johnson" email="alice@techinnovations.com" status="FULL_TIME" employeeId="E001"/>
9 <employees xsi:type="company:Employee" xmi:id="emp2" name="Bob Smith" email="bob@techinnovations.com" status="PART_TIME" employeeId="E002"/>
10 <employees xsi:type="company:Manager" xmi:id="mgr1" name="Carol Williams" email="carol@techinnovations.com" status="FULL_TIME" department="Engineering"/>
11 </company:Company>
12
13</xmi:XMI>

Step 2

Write a simple AQL query to get all employees.

This query navigates from the Company to its employees collection. The self keyword refers to the current context element (the Company instance).

queries/all-employees.aql
1self.employees

Step 3

Execute the query against the model.

The query command evaluates AQL expressions against a model and displays results. Use --context to specify which element the query evaluates on.

Terminal
1swift-aql query company-model.xmi --query "self.employees" --context "company1"

Section 2

Filtering and Selection

AQL provides powerful collection operations for filtering, mapping, and selecting elements. Use select() to filter collections, collect() to transform elements, and size() to count.

These operations enable declarative queries that are concise and expressive, letting you focus on what you want to find rather than how to find it.

Step 1

Query for full-time employees only.

The select() operation filters the employees collection, keeping only elements where the lambda expression evaluates to true.

queries/fulltime-employees.aql
1self.employees->select(e | e.status = EmploymentStatus::FULL_TIME)

Step 2

Get just the names of all employees.

The collect() operation transforms each employee into their name, producing a collection of strings.

queries/employee-names.aql
1self.employees->collect(e | e.name)

Step 3

Count employees by status.

You can combine multiple queries to compute statistics. This example uses select() to filter and size() to count.

queries/count-by-status.aql
1let fullTime = self.employees->select(e | e.status = EmploymentStatus::FULL_TIME)->size() in
2let partTime = self.employees->select(e | e.status = EmploymentStatus::PART_TIME)->size() in
3let contractors = self.employees->select(e | e.status = EmploymentStatus::CONTRACTOR)->size() in
4Tuple{fullTime=fullTime, partTime=partTime, contractors=contractors}

Check Your Understanding

Question 1 of 5

What is AQL used for?

Question 2 of 5

What does the select() operation do?

Question 3 of 5

What does self refer to in an AQL query?

Question 4 of 5

How do you navigate a reference in AQL?

Question 5 of 5

What is the difference between collect() and select()?

Cross-Resource References

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