Class AbstractPythonProcessService

    • Field Detail

      • home

        private java.io.File home
      • pythonArgs

        private java.util.List<java.lang.String> pythonArgs
      • locationKey

        private java.lang.String locationKey
      • transportChannel

        private java.lang.String transportChannel
      • paramConfigurers

        private java.util.Map<java.lang.String,​ParameterConfigurer<?>> paramConfigurers
      • callbacks

        private java.util.Map<java.lang.String,​de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<?>> callbacks
      • averageResponseTime

        private java.lang.String averageResponseTime
    • Constructor Detail

      • AbstractPythonProcessService

        public AbstractPythonProcessService​(java.lang.String serviceId,
                                            java.io.InputStream ymlFile)
        Creates a service from a service id and a YAML artifact.
        Parameters:
        serviceId - the service id
        ymlFile - the YML file containing the YAML artifact with the service descriptor
      • AbstractPythonProcessService

        public AbstractPythonProcessService​(YamlService yaml)
        Creates a service from YAML information.
        Parameters:
        yaml - the service information as read from YAML. By default, the Python executable is "ServiceEnvironment.py", which can be overridden by YamlProcess.getExecutable(). YamlProcess.getHomePath() is set to the home path where the executable was extracted to. Further, YamlProcess.getCmdArg() are taken over if given.
    • Method Detail

      • initializeFrom

        protected void initializeFrom​(YamlService yaml)
        Does further setup of this instance from the given YAML information.
        Overrides:
        initializeFrom in class AbstractService
        Parameters:
        yaml - the service information as read from YAML
      • getHome

        protected java.io.File getHome()
        Returns the home directory.
        Returns:
        the home directory
      • getPythonModule

        private static java.lang.String getPythonModule​(java.lang.String module,
                                                        YamlService yaml,
                                                        java.io.File homePath)
        Returns the name of the Python module. The default (if not explicitly given) is "ServiceEnvironment.py".
        Parameters:
        module - the module name, may be empty or null
        yaml - the YAML service deployment information
        homePath - optional home path to check for the module first if not given
        Returns:
        the Python module name
      • startExecutableByName

        protected boolean startExecutableByName()
        Returns whether the Python executable shall be started by name or by full path.
        Returns:
        false for starting by full path returned by getPythonExecutable()
      • handleResult

        protected <O> void handleResult​(java.lang.Class<O> cls,
                                        java.lang.String data,
                                        java.lang.String typeName)
        Handles a received processing result and ingests it back asynchronously.
        Type Parameters:
        O - the data type
        Parameters:
        cls - the data type class
        data - the serialized data
        typeName - the data type name as specified in the configuration model
      • toJson

        protected java.lang.String toJson​(java.util.Map<java.lang.String,​java.lang.String> reconfValues)
        Turns a map of string values into JSON.
        Parameters:
        reconfValues - the values to turn into JSON
        Returns:
        the JSON representation
      • createAndCustomizeProcess

        protected java.lang.Process createAndCustomizeProcess​(java.lang.String data,
                                                              java.util.Map<java.lang.String,​java.lang.String> reconfValues)
                                                       throws java.util.concurrent.ExecutionException
        Creates and customizes the process.
        Parameters:
        data - the data to be directly handed to the process via command line
        reconfValues - values to (re)configure the service environment, may be null for none
        Returns:
        the process
        Throws:
        java.util.concurrent.ExecutionException - if the process cannot be created
      • getLocationKey

        protected java.lang.String getLocationKey()
        Returns the location key for access into InstalledDependenciesSetup.
        Returns:
        the key, may be null for fallback
      • getPythonExecutable

        protected java.io.File getPythonExecutable()
        Returns the Python executable, either via InstalledDependenciesSetup and getLocationKey() or as fallback via PythonUtils.getPythonExecutable().
        Returns:
        the Python executable
      • handleErrorStream

        protected void handleErrorStream​(java.io.InputStream err)
        Handles the error stream upon process creation.
        Parameters:
        err - the process error stream
      • getLogger

        protected static org.slf4j.Logger getLogger()
        Returns the logger.
        Returns:
        the logger
      • registerInputTypeTranslator

        public <I> void registerInputTypeTranslator​(java.lang.Class<I> inCls,
                                                    java.lang.String inTypeName,
                                                    de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I,​java.lang.String> inTrans)
        Adds an input type translator.
        Specified by:
        registerInputTypeTranslator in interface GenericMultiTypeService
        Type Parameters:
        I - the input data type
        Parameters:
        inCls - the class representing the input type
        inTypeName - symbolic name of inCls, e.g. from configuration model
        inTrans - the input data type translator
        See Also:
        registerOutputTypeTranslator(Class, String, TypeTranslator)
      • getInTypeInfo

        public AbstractPythonProcessService.InTypeInfo<?> getInTypeInfo​(java.lang.String inTypeName)
        Returns the input type information object for the given symbolic type name.
        Parameters:
        inTypeName - the symbolic type name
        Returns:
        the information object or null if none was registered
      • obtainInTypeInfo

        private <I> AbstractPythonProcessService.InTypeInfo<I> obtainInTypeInfo​(java.lang.Class<I> cls,
                                                                                java.lang.String typeName)
        Obtains an input type information object.
        Type Parameters:
        I - the input type
        Parameters:
        cls - the class representing the type
        typeName - the associated symbolic type name
        Returns:
        the input type information object, may be retrieved or new
      • getOutTypeInfo

        public AbstractPythonProcessService.OutTypeInfo<?> getOutTypeInfo​(java.lang.String outTypeName)
        Returns the output type information object for the given symbolic type name.
        Parameters:
        outTypeName - the symbolic type name
        Returns:
        the information object or null if none was registered
      • obtainOutTypeInfo

        private <O> AbstractPythonProcessService.OutTypeInfo<O> obtainOutTypeInfo​(java.lang.Class<O> cls,
                                                                                  java.lang.String typeName)
        Obtains an output type information object.
        Type Parameters:
        O - the output type
        Parameters:
        cls - the class representing the type
        typeName - the associated symbolic type name
        Returns:
        the output type information object, may be retrieved or new
      • registerOutputTypeTranslator

        public <O> void registerOutputTypeTranslator​(java.lang.Class<O> outCls,
                                                     java.lang.String outTypeName,
                                                     de.iip_ecosphere.platform.transport.serialization.TypeTranslator<java.lang.String,​O> outTrans)
        Adds an output type translator.
        Specified by:
        registerOutputTypeTranslator in interface GenericMultiTypeService
        Type Parameters:
        O - the output data type
        Parameters:
        outCls - the class representing the input type
        outTypeName - symbolic name of outCls, e.g. from configuration model
        outTrans - the output data type translator
        See Also:
        registerInputTypeTranslator(Class, String, TypeTranslator)
      • attachIngestor

        public <O> void attachIngestor​(java.lang.Class<O> outCls,
                                       java.lang.String outTypeName,
                                       DataIngestor<O> ingestor)
        Description copied from interface: GenericMultiTypeService
        Attaches an asynchronous result data ingestor.
        Specified by:
        attachIngestor in interface GenericMultiTypeService
        Type Parameters:
        O - the output data type
        Parameters:
        outCls - the class representing the type
        outTypeName - symbolic name of outCls, e.g. from configuration model
        ingestor - the ingestor instance
      • compose

        protected static java.lang.String compose​(java.lang.String typeName,
                                                  java.lang.String data)
        Composes a symbolic type name with the string-serialized data.
        Parameters:
        typeName - the type name
        data - the data
        Returns:
        the composed String
      • start

        protected ServiceState start()
                              throws java.util.concurrent.ExecutionException
        Description copied from class: AbstractService
        Starts the service and the background process.
        Overrides:
        start in class AbstractService
        Returns:
        the state to transition to, may be null for none
        Throws:
        java.util.concurrent.ExecutionException - if starting the process fails
      • establishServerListener

        private void establishServerListener​(java.lang.String typeName,
                                             java.lang.String serverChannel)
                                      throws java.io.IOException
        Establishes a server listener for internal server-client communication via Transport.
        Parameters:
        typeName - the name of the type for Java-Python communication
        serverChannel - the name of the server channel to send initial request to
        Throws:
        java.io.IOException - if sending/receiving messages fails
      • establishClientListener

        private void establishClientListener​(java.lang.String typeName,
                                             java.lang.String serverChannel)
                                      throws java.io.IOException
        Establishes a client listener for internal server-client communication via Transport.
        Parameters:
        typeName - the name of the type for Java-Python communication
        serverChannel - the name of the server channel to send initial request to
        Throws:
        java.io.IOException - if sending/receiving messages fails
      • getAvgResponseTime

        public long getAvgResponseTime()
        Returns the average response time for the execution in Python (without transport).
        Returns:
        the average response time