class Processor(Atomic): def __init__(self, name, proc_time): super().__init__(name) self.i_in = Port(Job, "i_in") self.o_out = Port(Job, "o_out") self.add_in_port(self.i_in) self.add_out_port(self.o_out) self.current_job = None self.proc_time = proc_time def initialize(self): self.passivate() def exit(self): pass def deltint(self): self.passivate() def deltext(self, e): if self.phase == PHASE_PASSIVE: self.current_job = self.i_in.get_single_value() self.hold_in(PHASE_ACTIVE, self.proc_time) def lambdaf(self): self.o_out.add_value(self.current_job)
class Player(Atomic): def __init__(self, name, proc_time): super().__init__(name) self.i_input = Port(Ball, 'i_input') self.o_output = Port(Ball, 'o_output') self.add_in_port(self.i_input) self.add_out_port(self.o_output) self.current_job = None self.proc_time = proc_time self.clock = 0 def initialize(self): self.hold_in(PHASE_WAIT, INFINITY) def deltint(self): self.clock += self.sigma if self.phase == PHASE_SEND: logging.info("%s launched ball number %d @ t = %.1f" % (self.name, self.current_job.count, self.clock)) self.current_job = None self.hold_in(PHASE_WAIT, INFINITY) def deltext(self, e): self.clock += e if self.phase == PHASE_WAIT: if self.i_input: job = self.i_input.get_single_value() logging.info("%s received ball number %d @ t = %.1f" % (self.name, job.count, self.clock)) self.current_job = job self.hold_in(PHASE_SEND, self.proc_time) def lambdaf(self): if self.phase == PHASE_SEND: self.current_job.count += 1 self.o_output.add_value(self.current_job) def exit(self): pass
class Transducer(Atomic): def __init__(self, name, obs_time): super().__init__(name) self.i_arrived = Port(Job, "i_arrived") self.i_solved = Port(Job, "i_solved") self.o_out = Port(Job, "o_out") self.add_in_port(self.i_arrived) self.add_in_port(self.i_solved) self.add_out_port(self.o_out) self.jobs_arrived = [] self.jobs_solved = [] self.total_ta = 0 self.clock = 0 self.obs_time = obs_time def initialize(self): self.hold_in(PHASE_ACTIVE, self.obs_time) def exit(self): pass def deltint(self): self.clock += self.sigma throughput = 0 avg_ta = 0 if self.phase == PHASE_ACTIVE: if self.jobs_solved: avg_ta = self.total_ta / len( self.jobs_solved ) # TODO total_ta is not used -> avg_ta is always 0 throughput = len( self.jobs_solved) / self.clock if self.clock > 0 else 0 else: avg_ta = 0 throughput = 0 logging.info("End time: %d" % self.clock) logging.info("Jobs arrived: %d" % len(self.jobs_arrived)) logging.info("Jobs solved: %d" % len(self.jobs_solved)) logging.info("Average TA: %3f" % avg_ta) logging.info("Throughput: %.3f\n" % throughput) self.hold_in(PHASE_DONE, 0) else: self.passivate() def deltext(self, e): self.clock += e if self.phase == PHASE_ACTIVE: job = None if self.i_arrived: job = self.i_arrived.get_single_value() logging.info("Starting job %s @ t = %d" % (job.name, self.clock)) job.time = self.clock self.jobs_arrived.append(job) if self.i_solved: job = self.i_solved.get_single_value() logging.info("Job %s finished @ t = %d" % (job.name, self.clock)) job.time = self.clock self.jobs_solved.append(job) def lambdaf(self): if self.phase == PHASE_DONE: self.o_out.add_value(Job("null"))