Class JythonFactory

java.lang.Object
nz.org.riskscape.jython.JythonFactory
All Implemented Interfaces:
Closeable, AutoCloseable, JavaJythonHelpers

public class JythonFactory extends Object implements Closeable, AutoCloseable, JavaJythonHelpers

Creates Riskscape objects from python scripts

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final nz.org.riskscape.engine.function.IdentifiedFunction.Category
    IdentifiedFunction.Category that will apply to jython functions that have not specifically set a category.
  • Constructor Summary

    Constructors
    Constructor
    Description
    JythonFactory(nz.org.riskscape.engine.Project project)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    nz.org.riskscape.problem.ResultOrProblems<nz.org.riskscape.engine.function.IdentifiedFunction>
    buildFunction(String functionId, nz.org.riskscape.engine.resource.Resource resource)
    Builds a Python function from the given Resource.
    nz.org.riskscape.problem.ResultOrProblems<nz.org.riskscape.engine.function.IdentifiedFunction>
    buildFunction(nz.org.riskscape.engine.resource.Resource resource, nz.org.riskscape.engine.function.FunctionMetadata metadata)
    Builds a Python function from the given Resource with pre-defined function metadata.
    void
     
    nz.org.riskscape.engine.types.Type
    createType(nz.org.riskscape.engine.resource.Resource resource)
    Create an asset type from jython.
    protected nz.org.riskscape.engine.typeset.IdentifiedType
    extractType(String functionId, org.python.core.PyObject globals, String prefix, String scriptSource)
    Looks for either prefixTYPE or prefixTYPE_ID from globals and attempts to convert either in to an IdentifiedType.
    protected <T extends org.python.core.PyObject>
    Optional<T>
    findItem(org.python.core.PyObject source, String key, Class<T> clazz, Object typeOfThing, String scriptSource)
     
    protected <T extends org.python.core.PyObject>
    T
    findRequiredItem(org.python.core.PyObject source, String key, Class<T> clazz, Object typeOfThing, String scriptSource)
     
    protected String
    getFunctionID(nz.org.riskscape.engine.resource.Resource resource)
    Searches through the given resource for an ID declaration - allows an ID to be extracted without having to parse and (and potentially execute parts of) the script.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface nz.org.riskscape.jython.JavaJythonHelpers

    convertFromPython, toPyCallable
  • Field Details

    • DEFAULT_CATEGORY

      public static final nz.org.riskscape.engine.function.IdentifiedFunction.Category DEFAULT_CATEGORY

      IdentifiedFunction.Category that will apply to jython functions that have not specifically set a category.

  • Constructor Details

    • JythonFactory

      public JythonFactory(nz.org.riskscape.engine.Project project)
  • Method Details

    • buildFunction

      public nz.org.riskscape.problem.ResultOrProblems<nz.org.riskscape.engine.function.IdentifiedFunction> buildFunction(String functionId, nz.org.riskscape.engine.resource.Resource resource)

      Builds a Python function from the given Resource. The function's metadata (argument-types, etc) are read from the Python code itself by looking for specially named global variables

    • buildFunction

      public nz.org.riskscape.problem.ResultOrProblems<nz.org.riskscape.engine.function.IdentifiedFunction> buildFunction(nz.org.riskscape.engine.resource.Resource resource, nz.org.riskscape.engine.function.FunctionMetadata metadata)

      Builds a Python function from the given Resource with pre-defined function metadata.

    • getFunctionID

      protected String getFunctionID(nz.org.riskscape.engine.resource.Resource resource)

      Searches through the given resource for an ID declaration - allows an ID to be extracted without having to parse and (and potentially execute parts of) the script. Falls back to parsing the script if the ID can't be found

    • createType

      public nz.org.riskscape.engine.types.Type createType(nz.org.riskscape.engine.resource.Resource resource)

      Create an asset type from jython.

      Parameters:
      resource - containing the jython script
      Returns:
      asset type
    • extractType

      protected nz.org.riskscape.engine.typeset.IdentifiedType extractType(String functionId, org.python.core.PyObject globals, String prefix, String scriptSource)

      Looks for either prefixTYPE or prefixTYPE_ID from globals and attempts to convert either in to an IdentifiedType. Ids return an UnresolvedType whereas a TYPE returns a ResolvedType with a bogus id based on the functions id TODO consider making that id actually resolvable If either both or none are defined, an exception is raised

    • findItem

      protected <T extends org.python.core.PyObject> Optional<T> findItem(org.python.core.PyObject source, String key, Class<T> clazz, Object typeOfThing, String scriptSource)
    • findRequiredItem

      protected <T extends org.python.core.PyObject> T findRequiredItem(org.python.core.PyObject source, String key, Class<T> clazz, Object typeOfThing, String scriptSource)
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException