class Machine(Process): IDLE = "idle" BUSY = "busy" current = None state = TSeries() def status(self): return "(%s) %s" % (self.state.last_value, self.current) def reset(self): self.state = TSeries(numeric=False, storing=True, time_fnc=self.sim.clock.get) self.current = None @Chain def initialize(self): while True: self.state.collect(Machine.IDLE) self.current = None self.current = (yield self.parent.queue.get()).result #yield self.prepare() yield self.process() self.current.action.succeed() @Chain def process(self): with self.current.history.add("process", machine=self.path): self.state.collect(Machine.BUSY) yield self.current.quantity * self.current.product.complexity[self.parent.name] def finalize(self): self.state.repeat() print self.path, self.state.report()
class VirusSim(Simulator): def reset(self): self.clock.precision = 0.5 self.members.clear() Virus.autoname_reset() for _ in xrange(Virus.initial_population): self.members.add(Virus()) self.count = TSeries(storing=True, time_fnc=self.clock.get) self.count.collect(len(self.members)) @Chain def initialize(self): # Print time if trace is disabled. if not self.stack.trace: while True: print "%10.6f: %d viruses" % (self.time, len(self.members)) yield 10 def finalize(self): axes = self.count.run_chart(label="Registered growth", title="Virus population", legend=True) self.plotter = axes.figure.plotter print "\n" + self.count.report()