org.eclipse.incquery.runtime.emf

## Class EMFQueryRuntimeContext

- java.lang.Object
- - org.eclipse.incquery.runtime.emf.EMFQueryRuntimeContext

- All Implemented Interfaces:
    IQueryRuntimeContext
  Direct Known Subclasses:
    DynamicEMFQueryRuntimeContext

      public class EMFQueryRuntimeContext
      extends java.lang.Object
      implements IQueryRuntimeContext

  The EMF-based runtime query context, backed by an IQBase NavigationHelper.
  Author:
    Bergmann Gabor

    TODO: `containsTuple(IInputKey, Tuple)` and `countTuples(IInputKey, Tuple)` are inefficient as they first enumerate the collections.

    TODO: `ensureIndexed(EClass)` may be inefficient if supertype already cached.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `EMFQueryRuntimeContext(NavigationHelper baseIndex, org.apache.log4j.Logger logger, EMFScope emfScope)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addUpdateListener(IInputKey key, Tuple seed, IQueryRuntimeContextListener listener)` Subscribes for updates in the extensional relation identified by the input key, optionally seeded with the given tuple.
    `<V> V` | `coalesceTraversals(java.util.concurrent.Callable<V> callable)` The given callable will be executed, and all model traversals will be delayed until the execution is done.
    `boolean` | `containsTuple(IInputKey key, Tuple seed)` Simpler form of `IQueryRuntimeContext.enumerateTuples(IInputKey, Tuple)` in the case where all values of the tuples are bound by the seed.
    `int` | `countTuples(IInputKey key, Tuple seed)` Returns the number of tuples in the extensional relation identified by the input key, optionally seeded with the given tuple.
    `void` | `dispose()`
    `void` | `ensureEnumerableKey(IInputKey key)`
    `void` | `ensureIndexed(org.eclipse.emf.ecore.EClass eClass)`
    `void` | `ensureIndexed(org.eclipse.emf.ecore.EDataType eDataType)`
    `void` | `ensureIndexed(org.eclipse.emf.ecore.EStructuralFeature feature)`
    `void` | `ensureIndexed(IInputKey key)` If the given (enumerable) input key is not yet indexed, the model will be traversed (after the end of the outermost coalescing block, see `IQueryRuntimeContext.coalesceTraversals(Callable)`) so that the index can be built.
    `void` | `ensureValidKey(IInputKey key)`
    `java.lang.Iterable<Tuple>` | `enumerateTuples(IInputKey key, Tuple seed)` Returns the tuples in the extensional relation identified by the input key, optionally seeded with the given tuple.
    `java.lang.Iterable<? extends java.lang.Object>` | `enumerateValues(IInputKey key, Tuple seed)` Simpler form of `IQueryRuntimeContext.enumerateTuples(IInputKey, Tuple)` in the case where all values of the tuples are bound by the seed except for one.
    `EMFScope` | `getEmfScope()`
    `IQueryMetaContext` | `getMetaContext()` Provides metamodel-specific info independent of the runtime instance model.
    `void` | `illegalEnumerateValues(Tuple seed)`
    `void` | `illegalInputKey(IInputKey key)`
    `boolean` | `isCoalescing()`
    `boolean` | `isIndexed(IInputKey key)`
    `void` | `removeUpdateListener(IInputKey key, Tuple seed, IQueryRuntimeContextListener listener)` Unsubscribes from updates in the extensional relation identified by the input key, optionally seeded with the given tuple.
    `java.lang.Object` | `unwrapElement(java.lang.Object internalElement)` Unwraps the internal representation of the element into its original form
    `Tuple` | `unwrapTuple(Tuple internalElements)` Unwraps the tuple of internal representations of elements into their original forms
    `java.lang.Object` | `wrapElement(java.lang.Object externalElement)` Wraps the external element into the internal representation that is to be used by the query backend
    `Tuple` | `wrapTuple(Tuple externalElements)` Unwraps the tuple of elements into the internal representation that is to be used by the query backend

    - ### Methods inherited from class java.lang.Object

      `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### EMFQueryRuntimeContext

          public EMFQueryRuntimeContext(NavigationHelper baseIndex,
                                        org.apache.log4j.Logger logger,
                                        EMFScope emfScope)

  - ### Method Detail

    - #### getEmfScope

          public EMFScope getEmfScope()

    - #### dispose

          public void dispose()

    - #### coalesceTraversals

          public <V> V coalesceTraversals(java.util.concurrent.Callable<V> callable)
                                   throws java.lang.reflect.InvocationTargetException

      Description copied from interface: `IQueryRuntimeContext`
      The given callable will be executed, and all model traversals will be delayed until the execution is done. If there are any outstanding information to be read from the model, a single coalesced model traversal will initialize the caches and deliver the notifications.

      Calls may be nested. A single coalesced traversal will happen at the end of the outermost call.

      Caution: results returned by the runtime context may be incomplete during the coalescing period, to be corrected by notifications sent during the final coalesced traversal. For example, if a certain input key is not cached yet, an empty relation may be reported during `callable.call()`; the cache will be constructed after the call terminates and notifications will deliver the entire content of the relation. Non-incremental query backends should therefore never enumerate input keys while coalesced (verify using `IQueryRuntimeContext.isCoalescing()`).

      Specified by:
        `coalesceTraversals` in interface `IQueryRuntimeContext`
      Throws:
        `java.lang.reflect.InvocationTargetException`

    - #### isCoalescing

          public boolean isCoalescing()

      Specified by:
        `isCoalescing` in interface `IQueryRuntimeContext`
      Returns:
        true iff currently within a coalescing section (i.e. within the callable of a call to `IQueryRuntimeContext.coalesceTraversals(Callable)`).

    - #### getMetaContext

          public IQueryMetaContext getMetaContext()

      Description copied from interface: `IQueryRuntimeContext`
      Provides metamodel-specific info independent of the runtime instance model.
      Specified by:
        `getMetaContext` in interface `IQueryRuntimeContext`

    - #### ensureIndexed

          public void ensureIndexed(IInputKey key)

      Description copied from interface: `IQueryRuntimeContext`
      If the given (enumerable) input key is not yet indexed, the model will be traversed (after the end of the outermost coalescing block, see `IQueryRuntimeContext.coalesceTraversals(Callable)`) so that the index can be built.

      Postcondition: After invoking this method, `IQueryRuntimeContext.isIndexed(IInputKey)` for the same key will be guaranteed to return true as soon as `IQueryRuntimeContext.isCoalescing()` first returns false.

      Precondition: the given key is enumerable, see `IQueryMetaContext.isEnumerable(IInputKey)`.

      Specified by:
        `ensureIndexed` in interface `IQueryRuntimeContext`

    - #### isIndexed

          public boolean isIndexed(IInputKey key)

      Specified by:
        `isIndexed` in interface `IQueryRuntimeContext`
      Returns:
        true iff the given input key is already indexed, and contents are available without costly model traversal.

    - #### containsTuple

          public boolean containsTuple(IInputKey key,
                                       Tuple seed)

      Description copied from interface: `IQueryRuntimeContext`
      Simpler form of `IQueryRuntimeContext.enumerateTuples(IInputKey, Tuple)` in the case where all values of the tuples are bound by the seed.

      Returns whether the given tuple is in the extensional relation identified by the input key.

      Note: this call works for non-enumerable input keys as well.

      Specified by:
        `containsTuple` in interface `IQueryRuntimeContext`
      Parameters:
        `key` - an input key
        `seed` - a tuple with matching arity, consisting of non-null elements (null can be used in the 0-ary case).
      Returns:
        true iff the seed tuple is contained in the relation

    - #### enumerateTuples

          public java.lang.Iterable<Tuple> enumerateTuples(IInputKey key,
                                                           Tuple seed)

      Description copied from interface: `IQueryRuntimeContext`
      Returns the tuples in the extensional relation identified by the input key, optionally seeded with the given tuple.
      Specified by:
        `enumerateTuples` in interface `IQueryRuntimeContext`
      Parameters:
        `key` - an input key
        `seed` - can be null or a tuple with matching arity; if non-null, only those tuples in the model are enumerated that match the seed at positions where the seed is non-null.
      Returns:
        the tuples in the model for the given key and seed

        Precondition: the given key is enumerable, see `IQueryMetaContext.isEnumerable(IInputKey)`.

    - #### enumerateValues

          public java.lang.Iterable<? extends java.lang.Object> enumerateValues(IInputKey key,
                                                                                Tuple seed)

      Description copied from interface: `IQueryRuntimeContext`
      Simpler form of `IQueryRuntimeContext.enumerateTuples(IInputKey, Tuple)` in the case where all values of the tuples are bound by the seed except for one.

      Selects the tuples in the extensional relation identified by the input key, optionally seeded with the given tuple, and then returns the single value from each tuple which corresponds to the only null value in the seed.

      Specified by:
        `enumerateValues` in interface `IQueryRuntimeContext`
      Parameters:
        `key` - an input key
        `seed` - can be null (if key is unary) or a tuple with matching arity; if non-null, only those tuples in the model are enumerated that match the seed at positions where the seed is non-null; the seed is null at exactly one position.
      Returns:
        the objects in the model for the given key and seed

        Precondition: the given key is enumerable, see `IQueryMetaContext.isEnumerable(IInputKey)`.

    - #### countTuples

          public int countTuples(IInputKey key,
                                 Tuple seed)

      Description copied from interface: `IQueryRuntimeContext`
      Returns the number of tuples in the extensional relation identified by the input key, optionally seeded with the given tuple.
      Specified by:
        `countTuples` in interface `IQueryRuntimeContext`
      Parameters:
        `key` - an input key
        `seed` - can be null or a tuple with matching arity; if non-null, only those tuples in the model are counted that match the seed at positions where the seed is non-null.
      Returns:
        the number of tuples in the model for the given key and seed

        Precondition: the given key is enumerable, see `IQueryMetaContext.isEnumerable(IInputKey)`.

    - #### ensureEnumerableKey

          public void ensureEnumerableKey(IInputKey key)

    - #### ensureValidKey

          public void ensureValidKey(IInputKey key)

    - #### illegalInputKey

          public void illegalInputKey(IInputKey key)

    - #### illegalEnumerateValues

          public void illegalEnumerateValues(Tuple seed)

    - #### ensureIndexed

          public void ensureIndexed(org.eclipse.emf.ecore.EClass eClass)

    - #### ensureIndexed

          public void ensureIndexed(org.eclipse.emf.ecore.EDataType eDataType)

    - #### ensureIndexed

          public void ensureIndexed(org.eclipse.emf.ecore.EStructuralFeature feature)

    - #### addUpdateListener

          public void addUpdateListener(IInputKey key,
                                        Tuple seed,
                                        IQueryRuntimeContextListener listener)

      Description copied from interface: `IQueryRuntimeContext`
      Subscribes for updates in the extensional relation identified by the input key, optionally seeded with the given tuple.

      This should be called after invoking

      Specified by:
        `addUpdateListener` in interface `IQueryRuntimeContext`
      Parameters:
        `key` - an input key
        `seed` - can be null or a tuple with matching arity; if non-null, only those updates in the model are notified about that match the seed at positions where the seed is non-null.
        `listener` - will be notified of future changes

        Precondition: the given key is enumerable, see `IQueryMetaContext.isEnumerable(IInputKey)`.

    - #### removeUpdateListener

          public void removeUpdateListener(IInputKey key,
                                           Tuple seed,
                                           IQueryRuntimeContextListener listener)

      Description copied from interface: `IQueryRuntimeContext`
      Unsubscribes from updates in the extensional relation identified by the input key, optionally seeded with the given tuple.
      Specified by:
        `removeUpdateListener` in interface `IQueryRuntimeContext`
      Parameters:
        `key` - an input key
        `seed` - can be null or a tuple with matching arity; if non-null, only those updates in the model are notified about that match the seed at positions where the seed is non-null.
        `listener` - will no longer be notified of future changes

        Precondition: the given key is enumerable, see `IQueryMetaContext.isEnumerable(IInputKey)`.

    - #### unwrapElement

          public java.lang.Object unwrapElement(java.lang.Object internalElement)

      Description copied from interface: `IQueryRuntimeContext`
      Unwraps the internal representation of the element into its original form

      internal object -> model element

      null must be mapped to null.

      Specified by:
        `unwrapElement` in interface `IQueryRuntimeContext`

    - #### unwrapTuple

          public Tuple unwrapTuple(Tuple internalElements)

      Description copied from interface: `IQueryRuntimeContext`
      Unwraps the tuple of internal representations of elements into their original forms

      internal objects -> model elements

      null must be mapped to null.

      Specified by:
        `unwrapTuple` in interface `IQueryRuntimeContext`

    - #### wrapElement

          public java.lang.Object wrapElement(java.lang.Object externalElement)

      Description copied from interface: `IQueryRuntimeContext`
      Wraps the external element into the internal representation that is to be used by the query backend

      model element -> internal object.

      null must be mapped to null.

      Specified by:
        `wrapElement` in interface `IQueryRuntimeContext`

    - #### wrapTuple

          public Tuple wrapTuple(Tuple externalElements)

      Description copied from interface: `IQueryRuntimeContext`
      Unwraps the tuple of elements into the internal representation that is to be used by the query backend

      model elements -> internal objects

      null must be mapped to null.

      Specified by:
        `wrapTuple` in interface `IQueryRuntimeContext`