Class AdvancedInterpreterEngine

java.lang.Object
tools.refinery.interpreter.api.InterpreterEngine
tools.refinery.interpreter.api.AdvancedInterpreterEngine
Direct Known Subclasses:
InterpreterEngineImpl

public abstract class AdvancedInterpreterEngine extends InterpreterEngine
Advanced interface to a Refinery Interpreter incremental evaluation engine.

You can create a new, private, unmanaged AdvancedInterpreterEngine instance using createUnmanagedEngine(QueryScope). Additionally, you can access the advanced interface on any InterpreterEngine by from(InterpreterEngine).

While the default interface InterpreterEngine, is suitable for most users, this advanced interface provides more control over the engine. The most important added functionality is the following:

  • You can have tighter control over the lifecycle of the engine, if you create a private, unmanaged engine instance. For instance, a (non-managed) engine can be disposed in order to detach from the EMF model and stop listening on update notifications. The indexes built previously in the engine can then be garbage collected, even if the model itself is retained. Total lifecycle control is only available for private, unmanaged engines (created using createUnmanagedEngine(QueryScope)); a managed engine (obtained via
    invalid reference
    InterpreterEngine#on(QueryScope)
    ) is shared among clients and can not be disposed or wiped.
  • You can add and remove listeners to receive notification when the model or the match sets change.
  • You can add and remove listeners to receive notification on engine lifecycle events, such as creation of new matchers. For instance, if you explicitly share a private, unmanaged engine between multiple sites, you should register a callback using addLifecycleListener(InterpreterEngineLifecycleListener) to learn when another client has called the destructive methods dispose() or wipe().
  • Constructor Details

    • AdvancedInterpreterEngine

      public AdvancedInterpreterEngine()
  • Method Details

    • createUnmanagedEngine

      public static AdvancedInterpreterEngine createUnmanagedEngine(QueryScope scope)
      Creates a new unmanaged Refinery Interpreter engine to evaluate queries over a given scope specified by an QueryScope.

      Repeated invocations will return different instances, so other clients are unable to independently access and influence the returned engine. Note that unmanaged engines do not benefit from some performance improvements that stem from sharing incrementally maintained indices and caches between multiple clients using the same managed engine instance.

      Client is responsible for the lifecycle of the returned engine, hence the usage of the advanced interface AdvancedInterpreterEngine.

      The match set of any patterns will be incrementally refreshed upon updates from this scope.

      Parameters:
      scope - the scope of query evaluation; the definition of the set of model elements that this engine is operates on. Provide e.g. a
      invalid reference
      EMFScope
      for evaluating queries on an EMF model.
      Returns:
      the advanced interface to a newly created unmanaged engine
      Since:
      0.9
    • createUnmanagedEngine

      public static AdvancedInterpreterEngine createUnmanagedEngine(QueryScope scope, InterpreterEngineOptions options)
      Creates a new unmanaged Refinery Intepreter engine to evaluate queries over a given scope specified by an QueryScope.

      Repeated invocations will return different instances, so other clients are unable to independently access and influence the returned engine. Note that unmanaged engines do not benefit from some performance improvements that stem from sharing incrementally maintained indices and caches between multiple clients using the same managed engine instance.

      Client is responsible for the lifecycle of the returned engine, hence the usage of the advanced interface AdvancedInterpreterEngine.

      The match set of any patterns will be incrementally refreshed upon updates from this scope.

      Parameters:
      scope - the scope of query evaluation; the definition of the set of model elements that this engine is operates on. Provide e.g. a
      invalid reference
      EMFScope
      for evaluating queries on an EMF model.
      Returns:
      the advanced interface to a newly created unmanaged engine
      Since:
      1.4
    • from

      public static AdvancedInterpreterEngine from(InterpreterEngine engine)
      Provides access to a given existing engine through the advanced interface.
      Parameters:
      engine - the engine to access using the advanced interface
      Returns:
      a reference to the same engine conforming to the advanced interface
    • addLifecycleListener

      public abstract void addLifecycleListener(InterpreterEngineLifecycleListener listener)
      Add an engine lifecycle listener to this engine instance.
      Parameters:
      listener - the InterpreterEngineLifecycleListener that should listen to lifecycle events from this engine
    • removeLifecycleListener

      public abstract void removeLifecycleListener(InterpreterEngineLifecycleListener listener)
      Remove an existing lifecycle listener from this engine instance.
      Parameters:
      listener - the InterpreterEngineLifecycleListener that should not listen to lifecycle events from this engine anymore
    • addModelUpdateListener

      public abstract void addModelUpdateListener(InterpreterModelUpdateListener listener)
      Add an model update event listener to this engine instance (that fires its callbacks according to its notification level).
      Parameters:
      listener - the InterpreterModelUpdateListener that should listen to model update events from this engine.
    • removeModelUpdateListener

      public abstract void removeModelUpdateListener(InterpreterModelUpdateListener listener)
      Remove an existing model update event listener to this engine instance.
      Parameters:
      listener - the InterpreterModelUpdateListener that should not listen to model update events from this engine anymore
    • addMatchUpdateListener

      public abstract <Match extends IPatternMatch> void addMatchUpdateListener(InterpreterMatcher<Match> matcher, IMatchUpdateListener<? super Match> listener, boolean fireNow)
      Registers low-level callbacks for match appearance and disappearance on this pattern matcher.

      Caution: This is a low-level callback that is invoked when the pattern matcher is not necessarily in a consistent state yet. Importantly, no model modification permitted during the callback. Most users should use the databinding support (

      invalid reference
      ViatraObservables
      ) or the event-driven API (
      invalid reference
      EventDrivenVM
      ) instead.

      Performance note: expected to be much more efficient than polling at

      invalid reference
      #addCallbackAfterUpdates(Runnable)
      , but prone to "signal hazards", e.g. spurious match appearances that will disappear immediately afterwards.

      The callback can be unregistered via

      invalid reference
      #removeCallbackOnMatchUpdate(IMatchUpdateListener)
      .
      Parameters:
      matcher - the InterpreterMatcher for which this listener should be active
      listener - the listener that will be notified of each new match that appears or disappears, starting from now.
      fireNow - if true, appearCallback will be immediately invoked on all current matches as a one-time effect. See also InterpreterMatcher.forEachMatch(IMatchProcessor).
    • removeMatchUpdateListener

      public abstract <Match extends IPatternMatch> void removeMatchUpdateListener(InterpreterMatcher<Match> matcher, IMatchUpdateListener<? super Match> listener)
      Remove an existing match update event listener to this engine instance.
      Parameters:
      matcher - the InterpreterMatcher for which this listener should not be active anymore
      listener - the IMatchUpdateListener that should not receive the callbacks anymore
    • getMatcher

      public abstract <Matcher extends InterpreterMatcher<? extends IPatternMatch>> Matcher getMatcher(IQuerySpecification<Matcher> querySpecification, QueryEvaluationHint optionalEvaluationHints)
      Access a pattern matcher based on a IQuerySpecification, overriding some of the default query evaluation hints. Multiple calls may return the same matcher depending on the actual evaluation hints.

      It is guaranteed that this method will always return a matcher instance which is functionally compatible with the requested functionality (see IMatcherCapability). Otherwise, the query evaluator is free to ignore any hints.

      For stateful query backends (Rete), hints may be effective only the first time a matcher is created.

      Parameters:
      querySpecification - a IQuerySpecification that describes a Refinery Interpreter query
      optionalEvaluationHints - additional / overriding options on query evaluation; passing null means default options associated with the query
      Returns:
      a pattern matcher corresponding to the specification
      Throws:
      InterpreterRuntimeException - if the matcher could not be initialized
      Since:
      0.9
    • prepareGroup

      public abstract void prepareGroup(IQueryGroup queryGroup, QueryEvaluationHint optionalEvaluationHints)
      Initializes matchers for a group of patterns as one step (optionally overriding some of the default query evaluation hints). If some of the pattern matchers are already constructed in the engine, no task is performed for them.

      This preparation step has the advantage that it prepares pattern matchers for an arbitrary number of patterns in a single-pass traversal of the model. This is typically more efficient than traversing the model each time an individual pattern matcher is initialized on demand. The performance benefit only manifests itself if the engine is not in wildcard mode.

      Parameters:
      queryGroup - a IQueryGroup identifying a set of Refinery interpreter queries
      optionalEvaluationHints - additional / overriding options on query evaluation; passing null means default options associated with each query
      Throws:
      InterpreterRuntimeException - if there was an error in preparing the engine
      Since:
      0.9
    • isTainted

      public abstract boolean isTainted()
      Indicates whether the engine is in a tainted, inconsistent state due to some internal errors. If true, results are no longer reliable; engine should be disposed.

      The engine is in a tainted state if any of its internal processes report back a fatal error. The InterpreterEngineLifecycleListener interface provides a callback method for entering the tainted state.

      Returns:
      the tainted state
    • wipe

      public abstract void wipe()
      Discards any pattern matcher caches and forgets known patterns. The base index built directly on the underlying EMF model, however, is kept in memory to allow reuse when new pattern matchers are built. Use this method if you have e.g. new versions of the same patterns, to be matched on the same model.

      Matcher objects will continue to return stale results. If no references are retained to the matchers, they can eventually be GC'ed.

      Disallowed if the engine is managed (see

      invalid reference
      #isManaged()
      ), as there may be other clients using it.

      If you explicitly share a private, unmanaged engine between multiple sites, register a callback using addLifecycleListener(InterpreterEngineLifecycleListener) to learn when another client has called this destructive method.

      Throws:
      UnsupportedOperationException - if engine is managed
    • dispose

      public abstract void dispose()
      Completely disconnects and dismantles the engine. Cannot be reversed.

      Matcher objects will continue to return stale results. If no references are retained to the matchers or the engine, they can eventually be GC'ed, and they won't block the EMF model from being GC'ed anymore.

      The base indexer (see InterpreterEngine.getBaseIndex()) built on the model will be disposed alongside the engine, unless the user has manually added listeners on the base index that were not removed yet.

      Disallowed if the engine is managed (see

      invalid reference
      #isManaged()
      ), as there may be other clients using it.

      If you explicitly share a private, unmanaged engine between multiple sites, register a callback using addLifecycleListener(InterpreterEngineLifecycleListener) to learn when another client has called this destructive method.

      Throws:
      UnsupportedOperationException - if engine is managed
    • getQueryBackend

      public abstract IQueryBackend getQueryBackend(IQueryBackendFactory iQueryBackendFactory)
      Provides access to the selected query backend component of the Refinery Interpreter engine.
      Throws:
      InterpreterRuntimeException
    • getExistingMatcher

      public abstract <Matcher extends InterpreterMatcher<? extends IPatternMatch>> Matcher getExistingMatcher(IQuerySpecification<Matcher> querySpecification, QueryEvaluationHint optionalOverrideHints)
      Access an existing pattern matcher based on a IQuerySpecification, and optional hints override.
      Parameters:
      querySpecification - a IQuerySpecification that describes a Refinery Interpreter query specification
      optionalOverrideHints - a QueryEvaluationHint that may override the pattern hints (can be null)
      Returns:
      a pattern matcher corresponding to the specification, null if a matcher does not exist yet.
      Since:
      1.4
    • getEngineOptions

      public abstract InterpreterEngineOptions getEngineOptions()
      Returns the immutable InterpreterEngineOptions of the engine.
      Returns:
      the engine options
      Since:
      1.4
    • getResultProviderOfMatcher

      public abstract IQueryResultProvider getResultProviderOfMatcher(InterpreterMatcher<? extends IPatternMatch> matcher)
      Return the underlying result provider for the given matcher.
      Since:
      1.4
    • delayUpdatePropagation

      public abstract <V> V delayUpdatePropagation(Callable<V> callable) throws InvocationTargetException
      The given callable will be executed, and all update propagation in stateful query backends will be delayed until the execution is done. Within the callback, these backends will provide stale results.

      It is optional for a IQueryBackend to support the delaying of update propagation; stateless backends will display up-to-date results. In this case, the given callable shall be executed, and the update propagation shall happen just like in non-delayed execution.

      Example: in the Rete network, no messages will be propagated until the given callable is executed. After the execution of the callable, all accumulated messages will be delivered.

      The purpose of this method is that stateful query backends may save work when multiple model modifications are performed within the callback that partially cancel each other out.

      Parameters:
      callable - the callable to be executed
      Returns:
      the result of the callable
      Throws:
      InvocationTargetException
      Since:
      1.6
    • isUpdatePropagationDelayed

      public abstract boolean isUpdatePropagationDelayed()
      Returns true if the update propagation in this engine is currently delayed, false otherwise.
      Since:
      1.6
    • isDisposed

      public abstract boolean isDisposed()
      Returns true if the dispose() method was called on this engine previously.
      Since:
      2.0