Interface RealizationContext


public interface RealizationContext

Access to contextual bits and pieces in which realization is going to happen - this metadata will also be relevant for the eventual execution of an Expression and can be passed through to any RealizedExpressions and their dependents that result from realization.

At the moment this is quite light in contents, but I expect it will eventually be the place where resource management, lifecycle stuff and other supporting parts might go.

  • Method Details

    • getExpressionRealizer

      ExpressionRealizer getExpressionRealizer()
      Returns:
      an ExpressionRealizer for creating RealizedExpression from a parsed Expression
    • realizeConstant

      default ResultOrProblems<RealizedExpression> realizeConstant(String constantExpression)

      Convenience function for going straight from an expression string to a realized expression

    • getProject

      Project getProject()
      Returns:
      the Project that owns the elements that are being Realized
    • getFunctionResolver

      FunctionResolver getFunctionResolver()
      Returns:
      a FunctionResolver that can be used for producing RiskscapeFunctions that work for a particular set of input types.
    • normalizeStruct

      Struct normalizeStruct(Struct struct)

      Returns an equal struct to the given one, such that a subsequent call would return the exact same one. This method exists to handle situations where a struct is implicitly defined by an Expression to ensure the subsequent implicitly defined ones use the exact same struct to avoid an IllegalArgumentException when using Tuple.fetch(nz.org.riskscape.engine.types.Struct.StructMember)

      Without this, expressions like [{foo: 1}, {foo: 2}, {foo: 3}] would define three equal but different struct objects.

      This method lives on the context, rather than, say, a project, so that we can rely on the lifecycle of the realization context as a proxy for the life of the set of normalized structs we memoize. Alternatively, we'd need to have some way of managing the number of structs in this set via something like a WeakReference.

      In "the future", a project is likely to be a fairly long lived object whereas a context is going to be used during some short to mid term set

      Parameters:
      struct - a struct to normalize
      Returns:
      either the exact same object if no struct like this already exists in this RealizationContext, or a previously seen Struct that is equal
    • getProblemSink

      ProblemSink getProblemSink()
      Returns:
      a place for problems to be output, typically used outside of realization where some user feedback needs to be given that isn't going to cause execution to stop.
    • getEngine

      default Engine getEngine()
    • getOrComputeFromCache

      <T> T getOrComputeFromCache(Object cacheKey, Class<T> expectedType, Function<Object,T> compute)

      Lookup and possibly populate an item in this context's cache, in a thread-safe way, so that there's only ever one of each object in the cache.

      Note that objects may be removed from the cache at any time. The most likely reason for this would to to free memory. For this reason compute functions may be applied many times.

      Parameters:
      cacheKey - object whose equals and hashcode methods serve as a unique key for the object.
      expectedType - type safety measure to ensure the built and constructed object is of the correct type
      compute - a function that constructs the object if it doesn't exist in the context. May be called more than once if the cache has cleared it's value.
      Returns:
      An object that resulted from compute
    • getOrComputeFromCache

      default <T> T getOrComputeFromCache(Class<?> prefix, Object key, Class<T> expectedType, Function<Object,T> compute)

      Convenience version of getOrComputeFromCache(Object, Class, Function) that namespaces keys with a class

    • getTypeSet

      default TypeSet getTypeSet()

      Access to the TypeSet for this context (via the project)