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))
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)