Class EMFQueryRuntimeContext
- java.lang.Object
-
- org.eclipse.viatra.query.runtime.matchers.context.AbstractQueryRuntimeContext
-
- org.eclipse.viatra.query.runtime.emf.EMFQueryRuntimeContext
-
- All Implemented Interfaces:
IQueryRuntimeContext
- Direct Known Subclasses:
DynamicEMFQueryRuntimeContext
public class EMFQueryRuntimeContext extends AbstractQueryRuntimeContext
The EMF-based runtime query context, backed by an IQBase NavigationHelper.- Since:
- 1.4
-
-
Field Summary
Fields Modifier and Type Field Description protected NavigationHelper
baseIndex
protected java.util.Map<org.eclipse.emf.ecore.EClass,java.util.EnumSet<IndexingService>>
indexedClasses
protected java.util.Map<org.eclipse.emf.ecore.EDataType,java.util.EnumSet<IndexingService>>
indexedDataTypes
protected java.util.Map<org.eclipse.emf.ecore.EStructuralFeature,java.util.EnumSet<IndexingService>>
indexedFeatures
protected org.apache.log4j.Logger
logger
protected EMFQueryMetaContext
metaContext
-
Constructor Summary
Constructors Constructor Description EMFQueryRuntimeContext(NavigationHelper baseIndex, org.apache.log4j.Logger logger, EMFScope emfScope)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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, ITuple seed)
Simpler form of#enumerateTuples(IInputKey, TupleMask, Tuple)
in the case where all values of the tuples are bound by the seed.int
countTuples(IInputKey key, TupleMask seedMask, ITuple seed)
Returns the number of tuples in the extensional relation identified by the input key seeded with the given mask and tuple.void
dispose()
void
ensureEnumerableKey(IInputKey key)
void
ensureIndexed(org.eclipse.emf.ecore.EClass eClass, IndexingService service)
void
ensureIndexed(org.eclipse.emf.ecore.EDataType eDataType, IndexingService service)
void
ensureIndexed(org.eclipse.emf.ecore.EStructuralFeature feature, IndexingService service)
void
ensureIndexed(IInputKey key, IndexingService service)
If the given (enumerable) input key is not yet indexed, the model will be traversed (after the end of the outermost coalescing block, seeIQueryRuntimeContext.coalesceTraversals(Callable)
) so that the index can be built.void
ensureValidKey(IInputKey key)
void
ensureWildcardIndexing(IndexingService service)
Starts wildcard indexing for the given service.java.lang.Iterable<Tuple>
enumerateTuples(IInputKey key, TupleMask seedMask, ITuple 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, TupleMask seedMask, ITuple seed)
Simpler form of#enumerateTuples(IInputKey, TupleMask, Tuple)
in the case where all values of the tuples are bound by the seed except for one.java.util.Optional<java.lang.Double>
estimateAverageBucketSize(IInputKey key, TupleMask groupMask, Accuracy requiredAccuracy)
Gives an estimate of the average size of different groups the tuples of the given relation are projected into by the given mask (e.g.java.util.Optional<java.lang.Long>
estimateCardinality(IInputKey key, TupleMask groupMask, Accuracy requiredAccuracy)
Gives an estimate of the number of different groups the tuples of the given relation are projected into by the given mask (e.g.void
executeAfterTraversal(java.lang.Runnable runnable)
Execute the given runnable after traversal.protected java.util.EnumSet<IndexingService>
getCurrentIndexingServiceFor(IInputKey key)
Retrieve the current registered indexing services for the given key.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, IndexingService service)
Returns true if index is available for the given key providing the given service.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 formTuple
unwrapTuple(Tuple internalElements)
Unwraps the tuple of internal representations of elements into their original formsjava.lang.Object
wrapElement(java.lang.Object externalElement)
Wraps the external element into the internal representation that is to be used by the query backendTuple
wrapTuple(Tuple externalElements)
Unwraps the tuple of elements into the internal representation that is to be used by the query backend
-
-
-
Field Detail
-
baseIndex
protected final NavigationHelper baseIndex
-
indexedClasses
protected final java.util.Map<org.eclipse.emf.ecore.EClass,java.util.EnumSet<IndexingService>> indexedClasses
-
indexedDataTypes
protected final java.util.Map<org.eclipse.emf.ecore.EDataType,java.util.EnumSet<IndexingService>> indexedDataTypes
-
indexedFeatures
protected final java.util.Map<org.eclipse.emf.ecore.EStructuralFeature,java.util.EnumSet<IndexingService>> indexedFeatures
-
metaContext
protected final EMFQueryMetaContext metaContext
-
logger
protected org.apache.log4j.Logger logger
-
-
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 usingIQueryRuntimeContext.isCoalescing()
).- Throws:
java.lang.reflect.InvocationTargetException
-
isCoalescing
public boolean isCoalescing()
- 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.
-
ensureIndexed
public void ensureIndexed(IInputKey key, IndexingService service)
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, seeIQueryRuntimeContext.coalesceTraversals(Callable)
) so that the index can be built. It is possible that the base indexer will select a higher indexing level merging multiple indexing requests to an appropriate level.Postcondition: After invoking this method,
#getIndexed(IInputKey, IndexingService)
for the same key and service will be guaranteed to return the requested or a highing indexing level as soon asIQueryRuntimeContext.isCoalescing()
first returns false.Precondition: the given key is enumerable, see
IQueryMetaContext.isEnumerable(IInputKey)
.
-
getCurrentIndexingServiceFor
protected java.util.EnumSet<IndexingService> getCurrentIndexingServiceFor(IInputKey key)
Retrieve the current registered indexing services for the given key. May not return null, returns an empty set if no indexing is registered.- Since:
- 1.4
-
isIndexed
public boolean isIndexed(IInputKey key, IndexingService service)
Description copied from interface:IQueryRuntimeContext
Returns true if index is available for the given key providing the given service.
-
containsTuple
public boolean containsTuple(IInputKey key, ITuple seed)
Description copied from interface:IQueryRuntimeContext
Simpler form of#enumerateTuples(IInputKey, TupleMask, 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.
- Parameters:
key
- an input keyseed
- the tuple of fixed values restricting the match set to be considered, in the same order as given in parameterSeedMask, so that for each considered match tuple, projectedParameterSeed.equals(parameterSeedMask.transform(match)) should hold. Must not be null.- Returns:
- true iff there is at least a single tuple contained in the relation that corresponds to the seed tuple
-
enumerateTuples
public java.lang.Iterable<Tuple> enumerateTuples(IInputKey key, TupleMask seedMask, ITuple seed)
Description copied from interface:IQueryRuntimeContext
Returns the tuples in the extensional relation identified by the input key, optionally seeded with the given tuple.- Parameters:
key
- an input keyseedMask
- a mask that extracts those parameters of the input key (from the entire parameter list) that should be bound to a fixed value; must not be null. Note: any given index must occur at most once in seedMask.seed
- the tuple of fixed values restricting the match set to be considered, in the same order as given in parameterSeedMask, so that for each considered match tuple, projectedParameterSeed.equals(parameterSeedMask.transform(match)) should hold. Must not be 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, TupleMask seedMask, ITuple seed)
Description copied from interface:IQueryRuntimeContext
Simpler form of#enumerateTuples(IInputKey, TupleMask, 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 is not bound by the ssed mask.
- Parameters:
key
- an input keyseedMask
- a mask that extracts those parameters of the input key (from the entire parameter list) that should be bound to a fixed value; must not be null. Note: any given index must occur at most once in seedMask, and seedMask must include all parameters in any arbitrary order except one.seed
- the tuple of fixed values restricting the match set to be considered, in the same order as given in parameterSeedMask, so that for each considered match tuple, projectedParameterSeed.equals(parameterSeedMask.transform(match)) should hold. Must not be null.- 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, TupleMask seedMask, ITuple seed)
Description copied from interface:IQueryRuntimeContext
Returns the number of tuples in the extensional relation identified by the input key seeded with the given mask and tuple.- Parameters:
key
- an input keyseedMask
- a mask that extracts those parameters of the input key (from the entire parameter list) that should be bound to a fixed value; must not be null. Note: any given index must occur at most once in seedMask.seed
- the tuple of fixed values restricting the match set to be considered, in the same order as given in parameterSeedMask, so that for each considered match tuple, projectedParameterSeed.equals(parameterSeedMask.transform(match)) should hold. Must not be 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)
.
-
estimateCardinality
public java.util.Optional<java.lang.Long> estimateCardinality(IInputKey key, TupleMask groupMask, Accuracy requiredAccuracy)
Description copied from interface:IQueryRuntimeContext
Gives an estimate of the number of different groups the tuples of the given relation are projected into by the given mask (e.g. for an identity mask, this means the full relation size). The estimate must meet the required accuracy.Must accept any input key, even non-enumerables or those not recognized by this runtime context. If there is insufficient information to provide an answer up to the required precision,
Optional.empty()
is returned.PRE:
TupleMask.isNonrepeating()
must hold for the group mask.- Returns:
- if available, an estimate of the cardinality of the projection of the given extensional relation, with the desired accuracy.
- Since:
- 2.1
-
estimateAverageBucketSize
public java.util.Optional<java.lang.Double> estimateAverageBucketSize(IInputKey key, TupleMask groupMask, Accuracy requiredAccuracy)
Description copied from interface:IQueryRuntimeContext
Gives an estimate of the average size of different groups the tuples of the given relation are projected into by the given mask (e.g. for an identity mask, this means 1, while for an empty mask, the result is the full relation size). The estimate must meet the required accuracy.Must accept any input key, even non-enumerables or those not recognized by this runtime context. If there is insufficient information to provide an answer up to the required precision,
Optional.empty()
may be returned.For an empty relation, zero is acceptable as an exact answer.
PRE:
TupleMask.isNonrepeating()
must hold for the group mask.- Returns:
- if available, an estimate of the average size of each projection group of the given extensional relation, with the desired accuracy.
- Since:
- 2.1
-
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, IndexingService service)
- Since:
- 1.4
-
ensureIndexed
public void ensureIndexed(org.eclipse.emf.ecore.EDataType eDataType, IndexingService service)
- Since:
- 1.4
-
ensureIndexed
public void ensureIndexed(org.eclipse.emf.ecore.EStructuralFeature feature, IndexingService service)
- Since:
- 1.4
-
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
- Parameters:
key
- an input keyseed
- 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 changesPrecondition: 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.- Parameters:
key
- an input keyseed
- 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 changesPrecondition: 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 forminternal object -> model element
null must be mapped to null.
-
unwrapTuple
public Tuple unwrapTuple(Tuple internalElements)
Description copied from interface:IQueryRuntimeContext
Unwraps the tuple of internal representations of elements into their original formsinternal objects -> model elements
null must be mapped to null.
-
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 backendmodel element -> internal object.
null must be mapped to null.
-
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 backendmodel elements -> internal objects
null must be mapped to null.
-
ensureWildcardIndexing
public void ensureWildcardIndexing(IndexingService service)
Description copied from interface:IQueryRuntimeContext
Starts wildcard indexing for the given service. After this call, no registration is required for thisIndexingService
. a previously set wildcard level cannot be lowered, only extended.- Since:
- 1.4
-
executeAfterTraversal
public void executeAfterTraversal(java.lang.Runnable runnable) throws java.lang.reflect.InvocationTargetException
Description copied from interface:IQueryRuntimeContext
Execute the given runnable after traversal. It is guaranteed that the runnable is executed as soon as the indexing is finished. The callback is executed only once, then is removed from the callback queue.- Throws:
java.lang.reflect.InvocationTargetException
- Since:
- 1.4
-
-