Overview of Query2

Query2 provides easy mechanism to query emf metadata with minimal loading of resources. Query2 has an in-built indexing mechanism which indexes types, resources and references. This data is used during query execution and hence avoiding loading of resources wherever needed. Hence, if you want to use query2, you are mandatorily required to setup indexing. After that you can use it execute queries. We will see how to set it up in later sections. Query2 also provides you different ways to specify queries. We will see them in coming sections.

Similarly you can IndexQueryFactory .createResourceQuery() to create queries on Resources on the common index.

Step 3 Using QueryIndexBuilder: In eclipse IDE, we work on eclipse resources. Query2 indexing component provides an utility to index all the resources in your project using a custom builder. To enable it, you have to execute following steps:

1. Add the project nature: “org.eclipse.emf.query2.index.ui.queryIndexNature” Open the .project file for your project and add this nature.

2. Add Builder: “org.eclipse.emf.query2.index.ui.queryIndexBuilder” Open the .project file and add this builder.

3. The Builder will index all the files with extensions registered in EMF extensionToFactoryMap. You can add your file extension to factory using plugin.xml e.g as in following snippet:

Samples Queries

Note: The model used in the queries is mentioned in Appendix: Sample data 1. Select all books borrowed by an author:

from Book as b
select b
where b.borrowedBy in (
from Manuscript as m, Person as p select p where m.author = p
)

2. Select pages with title “Linux made easy”

from Manuscript as m select m.pages where m.title = 'Linux Made Easy'

3. Select all Manuscripts only In Berlin.xml or Hamburg.xmi

from Library as lib
in resources {"platform:/resource/org.eclipse.emf.query2.librarytest/data/library/Hamburg.xmi","platform:/resource/org.eclipse.emf.query2.librarytest/data/library/Berlin.xmi"},
Book as b,
Manuscript as m
select m
where lib.books = b
and b.instanceOf = m

For accessing sample data, refer to Appendix: Sample data

Writing Queries in Object format

The main class to create Query object is: org.eclipse.emf.query2.Query. You can use the constructors in Query class to create instance of Query. A Query requires mandatorily define following:

For specifying select clause: One or more SelectAttrs or SelectAlias

For specifying From clause: One or more FromType\FromFixedSet

You can also specify where clauses to specify conditions by specifying following:

For normal conditions: Use following:

LocalWhereEntry: A local where-entry allows the definition of a boolean expression, which compares the primitive typed attributes of one alias.

For Join conditions: Use any of following:

a. WhereComparisonAliases: A where-comparison aliases compares two aliases to each other for equality, where both aliases have to be of the same type.

b. WhereComparisonAttrs: A where-comparison attributes compares two attributes of two aliases to each other for equality, where both attributes have to be of the same primitive type.

c. WhereNestedReference: A nested where-entry defines a where-entry which constrains an alias by connecting it via an association to the results of a nested query. Nested where-entries can be negated.

d. WhereRelationReference: A where-relation defines a where-entry which constrains two aliases to be involved in an association relation

Sample Queries:

Note: The model used in the queries is mentioned in Appendix: Sample data

Sample Data

The samples data exists in eclipse CVS at following location: “/cvsroot/modeling/org.eclipse.emf/org.eclipse.emf.query/org.eclipse.emf.query2/tests/org.eclipse.emf.query2.librarytest” For accessing eclipse CVS you can refer to following page: CVS_HowTo.

Use “:pserver:anonymous@dev.eclipse.org:/cvsroot/modeling” to access projects related to Modeling. The model used in the samples data is following:

Date and Big Integer Support

Query 2 supports datatype like Big Integer and Date.

Now if any attribute in the model is expressed as a type of BigInteger.The query core now understands the BigInteger data type
and evaluate the queries on the basis of that.

Resource Index basically lists all the indexed resources. The indexed resources are represented by the Resource URIs in the Index View. Each Resource URI node consists of three child nodes:- EObject Tables, Incoming Link Tables and Outgoing Links Table. EObject Tables consists of a list of EObject URIs which represent the EObjects inside that particular resource. The Incoming and and Outgoing Link Tables are group of Reference URIs which represent incoming links to the resource and outgoing links from the resource respectively.

In the Sample Data given, there are two resources, resERefQuery_1.xmi and resERefQuery_2.xmi.

Resource resERefQuery_2.xmi contains two local EObjects EClass2_2 and EClass2. Whereas resERefQuery_1.xmi has two local EObjects EClass1 which inherits from EClass2 in resERefQuery_2.xmi and EClass1_2.

Each resource is represented by their resource uri, for Eg. platform:/resource/TestData/data/resourcesample/resERefQuery_1.xmi for resERefQuery_1.xmi.

The EObject tables represent all the EObjects present inside the resource, hence, in the given sample data, resERefQuery_2.xmi will contain EClass2_2 and EClass2 whereas, resERefQuery_1.xmi will contain EClass1, EClass1_2 as well as another entry which represents a pointer to the super type.

In the Incoming Link Table, we can see the links coming to the given resource. For Eg in the case of resERefQuery_2.xmi there are incoming references towards the resource, as it is the container resource of EClass2 which is the superclass of EClass1. Since it has no references from the resource, Outgoing links table is empty.

Type Index lists the indexed resources in a grouped format based on the type of the resources. For Eg. EPackage, EClass etc. The type is represented by the URI of the type.

Other than just displaying the Indices in the Index View, you can also rebuild the Indices using Re-Create Indices toolbar item.

You can also open the editor for the indexed resource using Open in Editor Context menu item. If a resource URI is selected, editor for that resource is opened. But if an EObject URI is selected, editor for the resource containing the EObject is opened whereas for Reference URIs, source and target resources are opened in the editor. Index View also enables the user to copy the qualified name of any physical node using the Copy Qualified Name context menu item.

Index View is also facilitated with Filters, so that the users can filter out Local tables(EObject table, Incoming Links Table, Outgoing Links Table) if not required.

Index View also has Legends for the users to understand the representation of each icon in the Index View.