AgE 2.7 : Queries implementation

Requirements

Queries in jAgE must fullfil following requirements:

  • Simplicity of both defining of a query and its execution.
  • Reusability of a single query against many targets.
  • Good performance of execution.
  • Extensibility — to allow creation of problem-centric queries.

On this page:

Implementation

The queries mechanism is centered around the IQuery interface. It represents a query and declares only one operation: R execute(Q target) where:

  • R is a type of the result of the query.
  • T is a type of a queried object.

The platform offers a mechanism to build queries using a semi-fluent interface. This mechanism is implemented in the GenericQuery class and its subclasses. Three operations for declaring a query are defined:

  1. Initial selection, represented by IInitialSelector: performs preselection of the values to query.
  2. Filtering by value, represented by IValueFilter: similar to WHERE clause in SQL.
  3. Selection of values (e.g. fields from objects): similar to SELECT x, y from SQL.
  4. Functions, represented by IQueryFunction: operations working on an entire result set, can remove, add or modify elements.

The execute() method of all subcless of MultiElementQuery works in the following sequence:

  1. Execution of initial selectors in the order they were added to the query. This means that all selectors must match for an element to be included in following actions.
  2. Matching of values with value filters. Consecutively added value filters are joined with the AND operator.
  3. Selection of the apropriate values. This step is omitted if no IValueSelector was added. In this case full objects are used.
  4. Execution of functions in the order they were added.

In the case of GenericQuery the first phase is omitted.

Following specialised query classes are provided out-of-the-box with the platform:

  • MapQuery for querying all implementations of the java.util.Map interface. Results are always a map.
  • CollectionQuery for querying all implementations of the java.util.Collection interface. Results are always a collection.
  • PropertyContainerCollectionQuery that is a CollectionQuery with some additional methods to match contents of property containers.
  • AgentEnvironmentQuery that allows transparent querying of agent environments.

Cache

In the version 2.5 only a simple cache based on the workplace step is provided. To work it needs to have an access to the workplace with the step property. It is not a general solution and it has a reduced application.

For the example usage of the cache see Queries.

When implementing a new cache you should consider following aspects:

  1. A cache should work as a proxy for a query (if it is a cache for results). It allows to transparently replace queries with cache in already existing classes.
  2. Provide a way to invalidate cached results.
  3. Use already existing envelope for cached results: CachedQueryResult.

Attachments:

new-query.png (image/png)