Class AbstractScriptSession<S extends AbstractScriptSession.Snapshot>

All Implemented Interfaces:
LogOutputAppendable, LivenessManager, LivenessNode, LivenessReferent, ScriptSession, Serializable
Direct Known Subclasses:
GroovyDeephavenSession, NoLanguageDeephavenSession, PythonDeephavenSession

public abstract class AbstractScriptSession<S extends AbstractScriptSession.Snapshot> extends LivenessArtifact implements ScriptSession
This class exists to make all script sessions to be liveness artifacts, and provide a default implementation for evaluateScript which handles liveness and diffs in a consistent way.
See Also:
  • Field Details

    • classCacheDirectory

      protected final File classCacheDirectory
    • executionContext

      protected final ExecutionContext executionContext
  • Constructor Details

  • Method Details

    • newClassCacheLocation

      protected static Path newClassCacheLocation()
    • createScriptCache

      public static void createScriptCache()
    • getExecutionContext

      public ExecutionContext getExecutionContext()
      Description copied from interface: ScriptSession
      Obtain an ExecutionContext instance for the current script session. This is the execution context that is used when executing scripts.
      Specified by:
      getExecutionContext in interface ScriptSession
    • publishInitial

      protected void publishInitial()
    • observeScopeChanges

      public void observeScopeChanges()
      Description copied from interface: ScriptSession
      Observe (and report via onScopeChanges) any changes to this ScriptSession's QueryScope that may have been made externally, rather than during script evaluation.
      Specified by:
      observeScopeChanges in interface ScriptSession
    • emptySnapshot

      protected abstract S emptySnapshot()
    • takeSnapshot

      protected abstract S takeSnapshot()
    • createDiff

      protected abstract ScriptSession.Changes createDiff(S from, S to, RuntimeException e)
    • evaluateScript

      public final ScriptSession.Changes evaluateScript(String script, @Nullable @Nullable String scriptName)
      Description copied from interface: ScriptSession
      Evaluates the script and manages liveness of objects that are exported to the user. This method should be called from the serial executor as it manipulates static state.
      Specified by:
      evaluateScript in interface ScriptSession
      Parameters:
      script - the code to execute
      scriptName - an optional script name, which may be ignored by the implementation, or used improve error messages or for other internal purposes
      Returns:
      the changes made to the exportable objects
    • applyVariableChangeToDiff

      protected void applyVariableChangeToDiff(ScriptSession.Changes diff, String name, @Nullable @Nullable Object fromValue, @Nullable @Nullable Object toValue)
    • evaluateScript

      public ScriptSession.Changes evaluateScript(Path scriptPath)
      Description copied from interface: ScriptSession
      Evaluates the script and manages liveness of objects that are exported to the user. This method should be called from the serial executor as it manipulates static state.
      Specified by:
      evaluateScript in interface ScriptSession
      Parameters:
      scriptPath - the path to the script to execute
      Returns:
      the changes made to the exportable objects
    • destroy

      protected void destroy()
      Description copied from class: ReferenceCountedLivenessReferent
      Attempt to release (destructively when necessary) resources held by this object. This may render the object unusable for subsequent operations. Implementations should be sure to call super.destroy().

      This is intended to only ever be used as a side effect of decreasing the reference count to 0.

      Overrides:
      destroy in class ReferenceCountedLivenessReferent
    • evaluate

      protected abstract void evaluate(String command, @Nullable @Nullable String scriptName)
      Evaluates command in the context of the current ScriptSession.
      Parameters:
      command - the command to evaluate
      scriptName - an optional script name, which may be ignored by the implementation, or used improve error messages or for other internal purposes
    • getQueryScope

      public QueryScope getQueryScope()
      Description copied from interface: ScriptSession
      Provides access to the query scope defined by the state in this script session.
      Specified by:
      getQueryScope in interface ScriptSession
      Returns:
      an implementation defined QueryScope, allowing access to state in the script session
    • getVariable

      protected abstract <T> T getVariable(String name) throws QueryScope.MissingVariableException
      Retrieve a variable from the script session's bindings. Values may need to be unwrapped.
      Parameters:
      name - the name of the variable to retrieve
      Returns:
      the variable value
      Throws:
      QueryScope.MissingVariableException - if the variable does not exist
    • getVariableNames

      protected abstract Set<String> getVariableNames()
      Retrieves all variable names present in the session's scope.
      Returns:
      a caller-owned mutable set of variable names
    • hasVariable

      protected abstract boolean hasVariable(String name)
      Check if the scope has the given variable name.
      Parameters:
      name - the variable name
      Returns:
      True iff the scope has the given variable name
    • setVariable

      protected abstract Object setVariable(String name, @Nullable @Nullable Object value)
      Inserts a value into the script's scope.
      Parameters:
      name - the variable name to set
      value - the new value of the variable
      Returns:
      the old value for this name, if any. As with getVariable(String), may need to be unwrapped.
    • getAllValues

      protected abstract <T> Map<String,T> getAllValues(@Nullable @Nullable Function<Object,T> valueMapper, @NotNull QueryScope.ParamFilter<T> filter)
      Returns a mutable map with all known variables and their values.

      Callers may want to pass in a valueMapper of ScriptSession.unwrapObject(Object) which would unwrap values before filtering. The returned map is owned by the caller.

      Type Parameters:
      T - the type of the mapped values
      Parameters:
      valueMapper - a function to map the values
      filter - a predicate to filter the map entries
      Returns:
      a caller-owned mutable map with all known variables and their mapped values. As with getVariable(String), values may need to be unwrapped.