コード例 #1
0
ファイル: looping.py プロジェクト: licode/VisTrails
    def compute(self):
        name_output = self.get_input('OutputPort') # or 'self'
        name_input = self.force_get_input('InputPort') # or None
        lower_bound = self.get_input('LowerBound') # or 0
        higher_bound = self.get_input('HigherBound') # required

        connectors = self.inputPorts.get('FunctionPort')
        if len(connectors) != 1:
            raise ModuleError(self,
                              "Multiple modules connected on FunctionPort")

        outputs = []
        suspended = []
        loop = self.logging.begin_loop_execution(self,
                                                 higher_bound - lower_bound)
        for i in xrange(lower_bound, higher_bound):
            module = copy.copy(connectors[0].obj)

            if not self.upToDate:
                module.upToDate = False
                module.computed = False

                # Pass iteration number on input port
                if name_input is not None:
                    if name_input in module.inputPorts:
                        del module.inputPorts[name_input]
                    new_connector = ModuleConnector(create_constant(i),
                                                    'value')
                    module.set_input_port(name_input, new_connector)
                    # Affix a fake signature on the module
                    inputPort_hash = sha1_hash()
                    inputPort_hash.update(name_input)
                    module.signature = b16encode(xor(
                            b16decode(self.signature.upper()),
                            long2bytes(i, 20),
                            inputPort_hash.digest()))

            loop.begin_iteration(module, i)

            try:
                module.update()
            except ModuleSuspended, e:
                suspended.append(e)
                loop.end_iteration(module)
                continue

            loop.end_iteration(module)

            if name_output not in module.outputPorts:
                raise ModuleError(module,
                                  "Invalid output port: %s" % name_output)
            outputs.append(module.get_output(name_output))
コード例 #2
0
    def make_connection(self, conn, src, dst):
        """make_connection(self, conn, src, dst)
        Builds a execution-time connection between modules.

        """
        iport = conn.destination.name
        oport = conn.source.name
        src.enable_output_port(oport)
        src.load_type_check_descs()
        if isinstance(src, src.InputPort_desc.module):
            typecheck = [False]
        else:
            typecheck = src.get_type_checks(conn.source.spec)
        dst.set_input_port(
            iport, ModuleConnector(src, oport, conn.source.spec, typecheck))
コード例 #3
0
ファイル: looping.py プロジェクト: licode/VisTrails
    def compute(self):
        name_output = self.get_input('OutputPort')
        name_condition = self.force_get_input('ConditionPort')
        name_state_input = self.force_get_input('StateInputPorts')
        name_state_output = self.force_get_input('StateOutputPorts')
        max_iterations = self.get_input('MaxIterations')
        delay = self.force_get_input('Delay')

        if (name_condition is None and
                not self.has_input('MaxIterations')):
            raise ModuleError(self,
                              "Please set MaxIterations or use ConditionPort")

        if name_state_input or name_state_output:
            if not name_state_input or not name_state_output:
                raise ModuleError(self,
                                  "Passing state between iterations requires "
                                  "BOTH StateInputPorts and StateOutputPorts "
                                  "to be set")
            if len(name_state_input) != len(name_state_output):
                raise ModuleError(self,
                                  "StateInputPorts and StateOutputPorts need "
                                  "to have the same number of ports "
                                  "(got %d and %d)" % (len(name_state_input),
                                                       len(name_state_output)))

        connectors = self.inputPorts.get('FunctionPort')
        if len(connectors) != 1:
            raise ModuleError(self,
                              "Multiple modules connected on FunctionPort")
        module = copy.copy(connectors[0].obj)

        state = None

        loop = self.logging.begin_loop_execution(self, max_iterations)
        for i in xrange(max_iterations):
            if not self.upToDate:
                module.upToDate = False
                module.computed = False

                # Set state on input ports
                if i > 0 and name_state_input:
                    for value, input_port, output_port \
                    in izip(state, name_state_input, name_state_output):
                        if input_port in module.inputPorts:
                            del module.inputPorts[input_port]
                        new_connector = ModuleConnector(
                                           create_constant(value), 'value',
                                           module.output_specs.get(output_port, None))
                        module.set_input_port(input_port, new_connector)
                        # Affix a fake signature on the module
                        inputPort_hash = sha1_hash()
                        inputPort_hash.update(input_port)
                        module.signature = b16encode(xor(
                                b16decode(self.signature.upper()),
                                inputPort_hash.digest()))

            loop.begin_iteration(module, i)

            module.update() # might raise ModuleError, ModuleSuspended,
                            # ModuleHadError, ModuleWasSuspended

            loop.end_iteration(module)

            if name_condition is not None:
                if name_condition not in module.outputPorts:
                    raise ModuleError(
                            module,
                            "Invalid output port: %s" % name_condition)
                if not module.get_output(name_condition):
                    break

            if delay and i+1 != max_iterations:
                time.sleep(delay)

            # Get state on output ports
            if name_state_output:
                state = [module.get_output(port) for port in name_state_output]

            self.logging.update_progress(self, i * 1.0 / max_iterations)

        loop.end_loop_execution()

        if name_output not in module.outputPorts:
            raise ModuleError(module,
                              "Invalid output port: %s" % name_output)
        result = module.get_output(name_output)
        self.set_output('Result', result)
コード例 #4
0
ファイル: sub_module.py プロジェクト: hjanime/VisTrails
    def compute(self):
        # Check required attributes
        if not hasattr(self, 'pipeline') or self.pipeline is None:
            raise VistrailsInternalError(
                "%s cannot execute -- pipeline doesn't exist" %
                self.__class__.__name__)
        elif (not hasattr(self, 'output_remap') or self.output_remap is None
              or not hasattr(self, 'input_remap') or self.input_remap is None):
            raise VistrailsInternalError(
                "%s cannot execute -- remap dictionaries don't exist" %
                self.__class__.__name__)

        # Setup pipeline for execution
        res = self.interpreter.setup_pipeline(self.pipeline)
        self.persistent_modules = res[0].values()
        if len(res[5]) > 0:
            raise ModuleError(
                self, "Error(s) inside group:\n" +
                "\n".join(me.msg for me in res[5].itervalues()))
        tmp_id_to_module_map = res[0]

        # Connect Group's external input ports to internal InputPort modules
        for iport_name, conn in self.inputPorts.iteritems():
            from vistrails.core.modules.basic_modules import create_constant
            # The type information is lost when passing as Variant,
            # so we need to use the the final normalized value
            value = self.get_input(iport_name)
            temp_conn = ModuleConnector(create_constant(value), 'value',
                                        conn[0].spec)
            iport_module = self.input_remap[iport_name]
            iport_obj = tmp_id_to_module_map[iport_module.id]
            iport_obj.set_input_port('ExternalPipe', temp_conn)

        # Execute pipeline
        kwargs = {
            'logger': self.logging.log.recursing(self),
            'clean_pipeline': True,
            'current_version': self.moduleInfo['version']
        }
        module_info_args = set(
            ['locator', 'reason', 'extra_info', 'actions', 'job_monitor'])
        for arg in module_info_args:
            if arg in self.moduleInfo:
                kwargs[arg] = self.moduleInfo[arg]

        res = self.interpreter.execute_pipeline(self.pipeline, *res[:2],
                                                **kwargs)

        # Check and propagate errors
        if len(res[2]) > 0:
            raise ModuleError(
                self, "Error(s) inside group:\n" +
                "\n".join("%s: %s" % (me.module.__class__.__name__, me.msg)
                          for me in res[2].itervalues()))

        # Check and propagate ModuleSuspended exceptions
        if res[4]:
            message = "\n".join([ms.msg for ms in res[4].itervalues()])
            children = list(res[4].values())
            raise ModuleSuspended(self, message, children=children)

        # Connect internal OutputPort modules to Group's external output ports
        for oport_name, oport_module in self.output_remap.iteritems():
            if oport_name is not 'self':
                oport_obj = tmp_id_to_module_map[oport_module.id]
                self.set_output(oport_name,
                                oport_obj.get_output('ExternalPipe'))

        self.interpreter.finalize_pipeline(self.pipeline,
                                           *res[:-1],
                                           reset_computed=False)
コード例 #5
0
    def setup_pipeline(self, pipeline, **kwargs):
        """setup_pipeline(controller, pipeline, locator, currentVersion,
                          view, aliases, **kwargs)
        Matches a pipeline with the persistent pipeline and creates
        instances of modules that aren't in the cache.
        """
        def fetch(name, default):
            return kwargs.pop(name, default)

        controller = fetch('controller', None)
        locator = fetch('locator', None)
        current_version = fetch('current_version', None)
        view = fetch('view', DummyView())
        vistrail_variables = fetch('vistrail_variables', None)
        aliases = fetch('aliases', None)
        params = fetch('params', None)
        extra_info = fetch('extra_info', None)
        logger = fetch('logger', DummyLogController)
        sinks = fetch('sinks', None)
        reason = fetch('reason', None)
        actions = fetch('actions', None)
        done_summon_hooks = fetch('done_summon_hooks', [])
        module_executed_hook = fetch('module_executed_hook', [])
        stop_on_error = fetch('stop_on_error', True)
        parent_exec = fetch('parent_exec', None)
        job_monitor = fetch('job_monitor', None)

        reg = get_module_registry()

        if len(kwargs) > 0:
            raise VistrailsInternalError('Wrong parameters passed '
                                         'to setup_pipeline: %s' % kwargs)

        def create_null():
            """Creates a Null value"""
            getter = reg.get_descriptor_by_name
            descriptor = getter(basic_pkg, 'Null')
            return descriptor.module()

        def create_constant(param, module):
            """Creates a Constant from a parameter spec"""
            getter = reg.get_descriptor_by_name
            desc = getter(param.identifier, param.type, param.namespace)
            constant = desc.module()
            constant.id = module.id
            #             if param.evaluatedStrValue:
            #                 constant.setValue(param.evaluatedStrValue)
            if param.strValue != '':
                constant.setValue(param.strValue)
            else:
                constant.setValue(
                    constant.translate_to_string(constant.default_value))
            return constant

        ### BEGIN METHOD ###

#         if self.debugger:
#             self.debugger.update()
        to_delete = []
        errors = {}

        if controller is not None:
            # Controller is none for sub_modules
            controller.validate(pipeline)
        else:
            pipeline.validate()

        self.resolve_aliases(pipeline, aliases)
        if vistrail_variables:
            self.resolve_variables(vistrail_variables, pipeline)

        self.update_params(pipeline, params)

        (tmp_to_persistent_module_map, conn_map, module_added_set,
         conn_added_set) = self.add_to_persistent_pipeline(pipeline)

        # Create the new objects
        for i in module_added_set:
            persistent_id = tmp_to_persistent_module_map[i]
            module = self._persistent_pipeline.modules[persistent_id]
            obj = self._objects[persistent_id] = module.summon()
            obj.interpreter = self
            obj.id = persistent_id
            obj.signature = module._signature

            # Checking if output should be stored
            if module.has_annotation_with_key('annotate_output'):
                annotate_output = module.get_annotation_by_key(
                    'annotate_output')
                #print annotate_output
                if annotate_output:
                    obj.annotate_output = True

            for f in module.functions:
                connector = None
                if len(f.params) == 0:
                    connector = ModuleConnector(create_null(), 'value',
                                                f.get_spec('output'))
                elif len(f.params) == 1:
                    p = f.params[0]
                    try:
                        constant = create_constant(p, module)
                        connector = ModuleConnector(constant, 'value',
                                                    f.get_spec('output'))
                    except Exception, e:
                        debug.unexpected_exception(e)
                        err = ModuleError(
                            module,
                            "Uncaught exception creating Constant from "
                            "%r: %s" % (p.strValue, debug.format_exception(e)))
                        errors[i] = err
                        to_delete.append(obj.id)
                else:
                    tupleModule = vistrails.core.interpreter.base.InternalTuple(
                    )
                    tupleModule.length = len(f.params)
                    for (j, p) in enumerate(f.params):
                        try:
                            constant = create_constant(p, module)
                            constant.update()
                            connector = ModuleConnector(
                                constant, 'value', f.get_spec('output'))
                            tupleModule.set_input_port(j, connector)
                        except Exception, e:
                            debug.unexpected_exception(e)
                            err = ModuleError(
                                module, "Uncaught exception creating Constant "
                                "from %r: %s" %
                                (p.strValue, debug.format_exception(e)))
                            errors[i] = err
                            to_delete.append(obj.id)
                    connector = ModuleConnector(tupleModule, 'value',
                                                f.get_spec('output'))
                if connector:
                    obj.set_input_port(f.name, connector, is_method=True)