def merge(self): """ Attempts to merge the packets into a single one, only if all threads had succeeded. """ self.exception = None self.is_success = False def failure(): self.is_success = False self.exception = TransformationException() self.exception.packet = NIL_PACKET return NIL_PACKET if len(self.success) == self.threads: packet = self._custom_merge() if packet is not None: self.is_success = True self.reset() return packet else: packet = self._default_merge() if packet is not None: self.is_success = True self.reset() return packet else: return failure() elif len(self.success) + len(self.fail) == self.threads: self.is_success = False return Random.choice(self.fail) else: return failure()
def select(self): ''' Selects a packet randomly from the success list. If the success list is empty, then from the fail list. ''' self.exception = None self.is_success = False if len(self.success) > 0: self.is_success = True packet = Random.choice(self.success) self.exclusions.append(packet.current) return packet elif len(self.fail) > 0: self.is_success = False return Random.choice(self.fail) else: self.is_success = False #TODO: This should be a TransformationLanguageSpecificException self.exception = TransformationException('No packet was received') self.exception.packet = NIL_PACKET return NIL_PACKET
def packet_in(self, packet): self.exception = None self.is_success = False remaining_branches = range(len(self.branches)) original = packet.clone() # Success on the first branch that is in success while True: if len(remaining_branches) == 0: # They all failed return packet branch_no = Random.choice(remaining_branches) branch = self.branches[branch_no] packet = branch.packet_in(packet) if not branch.is_success: if branch.exception is not None: self.exception = branch.exception return packet else: # Ignore this branch for next try remaining_branches.remove(branch_no) packet = original else: self.is_success = True return packet