Class Rewriting.Context

java.lang.Object
eu.bandm.tools.graph.Rewriting.Context
Enclosing class:
Rewriting<O,A,E>

public class Rewriting.Context extends Object
Context-sensitive callback to the rewriting process.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Enforces that the current original object is replaced during rewriting, regardless of whether its neighborhood has changed.
    Returns the static environment of this rewriting process.
    long
    Returns the number of steps performed so far by this rewriting process.
    boolean
    Checks whether this rewriting process is currently inclined to create a new result object rather than share an original.
    popCycleEntry(O original, @Opt A argument)
    Forgets an object as the entry point of a cycle.
    void
    Completes the local rewriting process for the neighborhood of an object, and discards the result.
    Completes the local rewriting process for the neighborhood of an object, and returns the result.
    void
    pushCycleEntry(O original, @Opt A argument, O result)
    Records an object as the entry point of a cycle.
    void
    pushNeighborhood(O original)
    Starts the local rewriting process for the neighborhood of an object.
    rewrite(O original, @Opt A argument)
    Rewrites an object and its neighbors recursively.
    rewriteOrNull(@Opt O original, @Opt A argument)
    Rewrites an object and its neighbors recursively, or leaves a null reference unchanged.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getEnvironment

      public @Opt E getEnvironment()
      Returns the static environment of this rewriting process.

      This is the data that is available for all steps of the process.

      Returns:
      the static environment of this rewriting process, or null if not applicable
    • getStepCounter

      public long getStepCounter()
      Returns the number of steps performed so far by this rewriting process.
      Returns:
      the number of steps performed so far by this rewriting process
    • isChanged

      public boolean isChanged()
      Checks whether this rewriting process is currently inclined to create a new result object rather than share an original.
      Returns:
      true if either shareOriginals is set to false, subobjects in the current neighborhood have been replaced, or forceReplacement() has been called in the current neighborhood; false otherwise
    • pushCycleEntry

      public void pushCycleEntry(O original, @Opt A argument, O result)
      Records an object as the entry point of a cycle.

      This method should be invoked at the beginning of a rewriting step for a potential cycle entry point. It requires a new, preliminary result object to have been created already. Initialization of its neighborhood references can be deferred.

      An invocation of this method must be balanced with a later invocation of popCycleEntry(O, A). Otherwise, behavior is unspecified.

      An object may not be simultaneously recorded as the entry point of several equivalent (same-argument) cycles.

      Parameters:
      original - the original object to rewrite
      argument - the dynamic argument to the rewriting, or null if not applicable
      result - the new preliminary result, with some neighbor references still uninitialized
      Throws:
      IllegalStateException - if an equivalent cycle is already recorded
      See Also:
    • popCycleEntry

      public O popCycleEntry(O original, @Opt A argument)
      Forgets an object as the entry point of a cycle.

      This method should be invoked at the end of a rewriting step for a potential cycle entry point, after the preliminary result has been initialized completely by recursive rewriting.

      An invocation of this method must be balanced with an earlier invocation of pushCycleEntry(O, A, O). Otherwise, behavior is unspecified.

      Parameters:
      original - the original object to rewrite
      argument - the dynamic argument to the rewriting, or null if not applicable
      Returns:
      the recorded result
      Throws:
      IllegalStateException - if no such cycle is recorded
      See Also:
    • rewrite

      public O rewrite(O original, @Opt A argument)
      Rewrites an object and its neighbors recursively.

      1. If the original object is the recorded entry point of a cycle, a reference to the associated preliminary result is returned.
      2. Otherwise, if internal sharing is enabled, and the original object has been rewritten with the same argument before, a reference to the cached result is returned.
      3. Otherwise, the step function of this rewriting process is invoked to compute a result.

      If the sharing of original objects in the result is enabled, and this method returns an object that is distinct from the original, it is recorded that the current neighborhood has changed, and hence that the parent original may not be shared.

      Parameters:
      original - the original object to rewrite
      argument - the dynamic argument to the rewriting, or null if not applicable
      Returns:
      the rewritten result; either a back-reference to a cycle entry, or a shared reference to the original, or a newly computed object
    • rewriteOrNull

      public @Opt O rewriteOrNull(@Opt O original, @Opt A argument)
      Rewrites an object and its neighbors recursively, or leaves a null reference unchanged.
      Parameters:
      original - the original object to rewrite, or null
      argument - the dynamic argument to the rewriting, or null if not applicable
      Returns:
      the result of rewrite(original, argument), or null, respectively
    • pushNeighborhood

      public void pushNeighborhood(O original)
      Starts the local rewriting process for the neighborhood of an object.

      An invocation of this method must be balanced with a later invocation of either popNeighborhood(Supplier) or popNeighborhood(). Otherwise, behavior is unspecified.

      Parameters:
      original - the original object to rewrite
    • popNeighborhood

      public O popNeighborhood(Supplier<O> newResult)
      Completes the local rewriting process for the neighborhood of an object, and returns the result.

      An invocation of this method must be balanced with an earlier invocation of pushNeighborhood(O). Otherwise, behavior is unspecified.

      This method either returns the original object recorded with the corresponding invocation of pushNeighborhood(O), or uses the given supplier for a new result:

      • If some recursive rewrite has returned a result that is different from the respective original, or forceReplacement() has been called, then the supplier is used.
      • Otherwise, the original is returned.

      Note that this change tracking is local: recursive sub-neighborhoods, with their own pair of push--pop operations, do not interfere.

      Parameters:
      newResult - a supplier to use if the result has not been pre-recorded
      Returns:
      the rewriting result; either the original object if no changes have been detected, or otherwise supplied by newResult
      Throws:
      NullPointerException - if newResult is null
    • popNeighborhood

      public void popNeighborhood()
      Completes the local rewriting process for the neighborhood of an object, and discards the result.

      An invocation of this method must be balanced with an earlier invocation of pushNeighborhood(O). Otherwise, behavior is unspecified.

    • forceReplacement

      public void forceReplacement()
      Enforces that the current original object is replaced during rewriting, regardless of whether its neighborhood has changed.

      This method should be invoked between balanced invocations of pushNeighborhood(O) and popNeighborhood(Supplier) to enforce the creation of a new, unshared result.