# Tutorial: Querying EMF Models with OCL

## Contents

- Overview
- References
- Querying a Model with an OCL Condition
- Using Context-Free OCL Conditions
- Advanced Querying with the EMF Query Framework
- Summary

## Overview

The EMF Query Framework provides support for specifying matching conditions using Object Constraint Language (OCL) version 2.0.

This tutorial will illustrate a variety of ways to query models using OCL.

[back to top]

## References

To see the complete source code for the examples shown in this tutorial, install the OCL Query Example plug-in into your workspace.

Other references:

- For an environment in which to test the queries that you will create in this tutorial, install the Library Metamodel example.
- EMF Query Framework tutorial.
- OCL 2.0 specification

[back to top]

## Querying a Model with an OCL Condition

The simplest sort of query is one that searches for a single kind of model element using a single OCL condition. This OCL condition may be arbitrarily complex, but is still a single expression. This case is handled using the `BooleanOCLCondition` API.

For example, to find writers that have written books in more than 2 categories:

    Resource myResource = ... // get the resource

    OCL ocl = org.eclipse.ocl.ecore.OCL.newInstance();
    Condition condition = new BooleanOCLCondition<EClassifier, EClass, EObject>(
        ocl.getEnvironment(),
        "self.books->collect(b : Book | b.category)->asSet()->size() > 2",
        EXTLibraryPackage.Literals.WRITER);

    SELECT statement = new SELECT(SELECT.UNBOUNDED, false,
    	new FROM(myResource.getContents()), new WHERE(condition),
    	new NullProgressMonitor());

    IQueryResult results = statement.execute();

    // do something with the results
    selectInEditor(results);

In the snippet above, the `Writer` metaclass is declared as the context type for the OCL condition; the context type is the type of the special `self` variable.

[back to top]

## Using Context-Free OCL Conditions

The previous section illustrated an query that specifies an OCL condition with a context type. This is somewhat restrictive, in that only elements of one type (and its sub-types) can be retrieved by such a query. But what if we want to find instances of various metaclasses that are not related to a single common ancestor metaclass but have similar features to query? An example is the `name` feature in the Library metamodel: both the `Library` and `Writer` metaclasses define a name, but they have no common ancestor with the same feature.

The solution is these cases is a context-free OCL condition. It allows us to specify a condition which may be applicable to one or more context types, and the query engine will take care of determining to which metaclasses it does apply. In effect, any metaclass on which the OCL expression will parse can be selected by the query.

The following query will retrieve any library or writer whose name is 'Bob'. This is probably unlikely to match a library, but we can try it anyway:

    Resource myResource = ... // get the resource

    OCL ocl = org.eclipse.ocl.ecore.OCL.newInstance();
    Condition condition = new BooleanOCLCondition<EClassifier, EClass, EObject>(
        ocl.getEnvironment(),
        "self.name = 'Bob'",
        null);  // this indicates a context-free condition

    SELECT statement = new SELECT(SELECT.UNBOUNDED, false,
    	new FROM(myResource.getContents()), new WHERE(condition),
    	new NullProgressMonitor());

    IQueryResult results = statement.execute();

    // do something with the results
    selectInEditor(results);

[back to top]

## Advanced Querying with the EMF Query Framework

Most real applications can expect to have more complex queries than we have seen so far. For example, how can we with one OCL condition expression find all of the writers and books that have a specific name/title? We could try a disjunction ("or") with type testing and casting:

    Condition condition = new BooleanOCLCondition<EClassifier, EClass, EObject>(
        ocl.getEnvironment(),
        "(self.oclIsKindOf(Writer) and self.oclAsType(Writer).name = 'Bob')" +
    	    "or (self.oclIsKindOf(Book) and self.oclAsType(Book).title = 'Bob')",
        null);  // this indicates a context-free condition

but we find that this does not actually work. We get an exception: `"Conformance Type Mismatch. No common supertype: (Writer), (Book)"` in trying to parse this expression against either the `Writer` or `Book` type, because OCL knows that neither of these types can be cast to the other.

The solution is to break the query into two distinct OCL condition expressions. That can be separately evaluated (if applicable) and combine them using the capabilities of the core query framework:

    Resource myResource = ... // get the resource

    // create the two OCL conditions
    Condition cond1 = new BooleanOCLCondition<EClassifier, EClass, EObject>(
        ocl.getEnvironment(),
        "self.name = 'Bob'",
        EXTLibraryPackage.Literals.WRITER);
    Condition cond2 = new BooleanOCLCondition<EClassifier, EClass, EObject>(
        ocl.getEnvironment(),
        "self.title = 'Bob'",
        EXTLibraryPackage.Literals.BOOK);

    // combine them into a single SELECT query statement
    SELECT statement = new SELECT(
        SELECT.UNBOUNDED, false,
        new FROM(myResource.getContents()),
        new WHERE(cond1.OR(cond2)),  // retrieve objects matching either OCL condition
        new NullProgressMonitor());

    // execute the query
    IQueryResult results = statement.execute();

    // do something with the results
    selectInEditor(results);

This gives us the result that we were looking for. Using the query framework, we can even mix OCL conditions with other kinds of conditions (implemented in Java code).

[back to top]

## Summary

To illustrate how to query EMF models using OCL, we

1. Performed OCL queries on specific context metaclasses.
2. Performed context-free OCL queries.
3. Executed complex queries with multiple distinct OCL condition expressions using the EMF Query Framework.

[back to top]

Copyright (c) 2000, 2007 IBM Corporation and others. All Rights Reserved.