def setUp(self): self.queue = EventQueue() self.event = prototype.JobEvent(timestamp=0, job=None) self.events = [ prototype.JobEvent(timestamp=i, job=None) for i in xrange(10) ] self.handler = _create_handler()
class ControllerBase(object): def __init__(self, object_manager, keep_running): self.object_manager = object_manager self.keep_running = keep_running # Keep track of input ports self.input_ports = [] self.input_queue = EventQueue(); # Keep track of output ports self.output_ports = [] self.output_listeners = [] # Let statechart run one last time before stopping self.done = False def addInputPort(self, port_name): self.input_ports.append(port_name) def addOutputPort(self, port_name): self.output_ports.append(port_name) def broadcast(self, new_event): self.object_manager.broadcast(new_event) def start(self): self.object_manager.start() def stop(self): pass def addInput(self, input_event, time_offset = 0.0): if input_event.getName() == "" : raise InputException("Input event can't have an empty name.") if input_event.getPort() not in self.input_ports : raise InputException("Input port mismatch.") self.input_queue.add(input_event, time_offset) def outputEvent(self, event): for listener in self.output_listeners : listener.add(event) def addOutputListener(self, ports): listener = OutputListener(ports) self.output_listeners.append(listener) return listener def addEventList(self, event_list): for (event, time_offset) in event_list : self.addInput(event, time_offset) def getObjectManager(self): return self.object_manager;
def __init__(self, controller): self.active = False self.is_stable = True self.events = EventQueue() self.controller = controller self.timers = None self.inports = {} self.semantics = StatechartSemantics()
def __init__(self, jobs, num_processors, scheduler): self.event_queue = EventQueue() self.machine = ValidatingMachine(num_processors=num_processors, event_queue=self.event_queue) self.scheduler = scheduler self.event_queue.add_handler(JobSubmissionEvent, self.handle_submission_event) self.event_queue.add_handler(JobTerminationEvent, self.handle_termination_event) for job in jobs: self.event_queue.add_event( JobSubmissionEvent(timestamp=job.submit_time, job=job))
def __init__(self, object_manager): self.object_manager = object_manager self.private_port_counter = 0 # Keep track of input ports self.input_ports = {} self.input_queue = EventQueue() # Keep track of output ports self.output_ports = [] self.output_listeners = [] # Let statechart run one last time before stopping self.done = False
def get_game_variables(constants): # Open the event queue, and load it up. event_queue = EventQueue() # Create entities lists. entities = [] # Create player. location = (int(constants['screen_width'] / 2), int(constants['screen_height'] / 2)) player = entity_factory(EntityType.PLAYER, location, entities) # Create NPC. location = (int(constants['screen_width'] / 2) - 12, int(constants['screen_height'] / 2)) entity_factory(EntityType.NPC, location, entities) # Create cursor. cursor_component = Cursor() location_component = Location() render_component = Render('X', libtcod.red) cursor = Entity('cursor', 0, cursor=cursor_component, location=location_component, render=render_component) cursor.render_order=RenderOrder.CURSOR # Create game_map. game_map = GameMap(constants['map_width'], constants['map_height']) # Create message_log. message_log = MessageLog(constants['message_x'], constants['message_width'], constants['message_height']) # Set game_state. game_state = GameStates.PLAYER_TURN # Set turn_state. turn_state = TurnStates.UPKEEP_PHASE return player, cursor, entities, game_map, message_log, game_state, turn_state, event_queue
def __init__(self, jobs, num_processors, scheduler): self.event_queue = EventQueue() self.machine = ValidatingMachine(num_processors=num_processors, event_queue=self.event_queue) self.scheduler = scheduler self.event_queue.add_handler(JobSubmissionEvent, self.handle_submission_event) self.event_queue.add_handler(JobTerminationEvent, self.handle_termination_event) for job in jobs: self.event_queue.add_event(JobSubmissionEvent(timestamp=job.submit_time, job=job))
class RuntimeClassBase(object): __metaclass__ = abc.ABCMeta def __init__(self): self.active = False self.state_changed = False self.events = EventQueue(); self.timers = None; def addEvent(self, event, time_offset = 0.0): self.events.add(event, time_offset); def getEarliestEventTime(self) : if self.timers : return min(self.events.getEarliestTime(), min(self.timers.itervalues())) return self.events.getEarliestTime(); def step(self, delta): if not self.active : return self.events.decreaseTime(delta); if self.timers : next_timers = {} for (key,value) in self.timers.iteritems() : time = value - delta if time <= 0.0 : self.addEvent( Event("_" + str(key) + "after"), time); else : next_timers[key] = time self.timers = next_timers; self.microstep() while (self.state_changed) : self.microstep() def microstep(self): due = self.events.popDueEvents() if (len(due) == 0) : self.transition() else : for event in due : self.transition(event); @abc.abstractmethod def transition(self, event = None): pass def start(self): self.active = True def stop(self): self.active = False
def __init__(self, links, flows, hosts, routers, verbose, fast_insteadof_reno): self.links = links self.flows = flows self.hosts = hosts self.routers = routers # Set up clocks self.clock = Clock() for item in flows.values(): item.controller.clock = self.clock for item in hosts.values(): item.clock = self.clock # Set up event schedulers self.event_queue = EventQueue(self.clock) for flow in flows.values() + links.values() + hosts.values(): flow.event_scheduler = self.event_queue for flow in flows.values(): flow.controller.event_scheduler = self.event_queue # Set up initial events for flow in flows.values(): self.event_queue.delay_event(flow.start_time, FlowWakeEvent(flow)) for host in hosts.values(): self.event_queue.delay_event(0, RoutingUpdateEvent(host)) # Set up logging self.logger = Logger(self.clock, verbose, fast_insteadof_reno) for item in flows.values() + links.values() + hosts.values( ) + routers.values(): item.set_logger(self.logger) # Set up PrintElapsedSimulationTimeEvents if not verbose: # combining this with verbose mode would be chaos. self.event_queue.delay_event( 0, PrintElapsedSimulationTimeEvent(self.clock.current_time, self.event_queue)) print "Simulation started..."
def __init__(self): jsonObject = self.readData() # initialize the hosts,flows,routers,links self.queue = EventQueue(0) self.routers = {} for r in jsonObject['routers']: self.routers[r['id']] = Router(r['id']) self.hosts = {} for h in jsonObject['hosts']: self.hosts[h['id']] = Host(h['id']) self.links = {} for l in jsonObject['links']: self.links[l['id']] = Link(l) self.links[l['id']].scheduler = self.queue if (l['endpoints'][0][0] == 'H' or l['endpoints'][0][0] == 'S' or l['endpoints'][0][0] == 'T'): self.links[l['id']].pointA = self.hosts[l['endpoints'][0]] self.hosts[l['endpoints'][0]].link = self.links[l['id']] else: self.links[l['id']].pointA = self.routers[l['endpoints'][0]] self.routers[l['endpoints'][0]].links.append( self.links[l['id']]) if (l['endpoints'][1][0] == 'H' or l['endpoints'][1][0] == 'S' or l['endpoints'][1][0] == 'T'): self.links[l['id']].pointB = self.hosts[l['endpoints'][1]] self.hosts[l['endpoints'][1]].link = self.links[l['id']] else: self.links[l['id']].pointB = self.routers[l['endpoints'][1]] self.routers[l['endpoints'][1]].links.append( self.links[l['id']]) self.flows = {} for f in jsonObject['flows']: self.flows[f['id']] = Flow(f) self.flows[f['id']].event_queue = self.queue self.flows[f['id']].controller.event_scheduler = self.queue self.flows[f['id']].source = self.hosts[f['source']] self.hosts[f['source']].flow[f['id']] = self.flows[f['id']] self.flows[f['id']].destination = self.hosts[f['destination']] self.hosts[f['destination']].flow[f['id']] = self.flows[f['id']] self.queue.blind(self.routers)
def __init__(self, object_manager, keep_running): self.object_manager = object_manager self.keep_running = keep_running # Keep track of input ports self.input_ports = [] self.input_queue = EventQueue(); # Keep track of output ports self.output_ports = [] self.output_listeners = [] # Let statechart run one last time before stopping self.done = False
def __init__(self): self.behaviors = [ 'Hover', 'MoveLeft', 'MoveForward', 'MoveBackwards', 'MoveUp', 'MoveDown', 'MoveRight', 'RotateLeft', 'RotateRight', 'TakeOff', 'Land', 'UTurn', 'AlignCorridor', 'CenterCorridor' ] self.commands = { 'Stop': [(0, 'Hover')], 'Dance': [(0, 'MoveLeft'), (0, 'MoveUp'), (2.5, 'MoveRight'), (3.2, 'Hover')], 'TakeOff': [(0, 'TakeOff'), (1, 'Hover')], 'Land': [(0, 'Land')], 'Hover': [(0, 'Hover')], 'MoveForward': [(0, 'MoveForward')], 'MoveBackwards': [(0, 'MoveBackwards')], 'MoveLeft': [(0, 'MoveLeft')], 'MoveRight': [(0, 'MoveRight')], 'MoveDown': [(0, 'MoveDown')], 'MoveUp': [(0, 'MoveUp')], 'RotateLeft': [(0, 'RotateLeft')], 'RotateRight': [(0, 'RotateRight')], 'UTurn': [(0, 'UTurn')], 'AlignCorridor': [(0, 'AlignCorridor')], 'CenterCorridor': [(0, 'CenterCorridor')] } self.command = rospy.Subscriber("/command", String, self.command_callback, queue_size=1) self.behavior = rospy.Publisher("/behavior", BehaviorStatus, queue_size=1) self.queue_lock = Lock() self.event_queue = EventQueue()
def main(): """Main function of the game. Creates all the classes that GameLoop-class needs to run the game, and then starts the game by calling gameloop.start(). """ display = pygame.display.set_mode((1024, 640)) pygame.display.set_caption("Clicker") pygame.font.init() colors = Colors() event_queue = EventQueue() startview = StartView(display, colors) menu = Menu(display, colors) logic = GameLogic() shop = Shop(display, colors, logic) save = SaveGame(logic) game_loop = GameLoop(logic, display, colors, startview, event_queue, menu, shop, save) pygame.init() game_loop.start()
def __init__(self): """Class constructor. Loads everything into memory """ self.display = pg.display.set_mode((640, 360)) pg.display.set_caption("Crawler") pg.init() self.level_list = None self.gui = Gui((640, 360)) self.level_id = 0 self.level = None self.event_queue = EventQueue() self.clock = Clock() self.renderer = Renderer(self.display, self.level, self.gui) self.mixer = Mixer() self.mixer.load_track(0) self.mixer.set_volume(0.3) self.game_loop = None self.menu_loop = MenuLoop(self.renderer, self.event_queue, self.clock) self.start_menu()
def __init__(self, links, flows, hosts, routers, verbose, fast_insteadof_reno): self.links = links self.flows = flows self.hosts = hosts self.routers = routers # Set up clocks self.clock = Clock() for item in flows.values(): item.controller.clock = self.clock for item in hosts.values(): item.clock = self.clock # Set up event schedulers self.event_queue = EventQueue(self.clock) for flow in flows.values() + links.values() + hosts.values(): flow.event_scheduler = self.event_queue for flow in flows.values(): flow.controller.event_scheduler = self.event_queue # Set up initial events for flow in flows.values(): self.event_queue.delay_event(flow.start_time, FlowWakeEvent(flow)) for host in hosts.values(): self.event_queue.delay_event(0, RoutingUpdateEvent(host)) # Set up logging self.logger = Logger(self.clock, verbose, fast_insteadof_reno) for item in flows.values() + links.values() + hosts.values() + routers.values(): item.set_logger(self.logger) # Set up PrintElapsedSimulationTimeEvents if not verbose: # combining this with verbose mode would be chaos. self.event_queue.delay_event(0, PrintElapsedSimulationTimeEvent(self.clock.current_time, self.event_queue)) print "Simulation started..."
class ObjectManagerBase(object): __metaclass__ = abc.ABCMeta def __init__(self, controller): self.controller = controller self.events = EventQueue() self.instances_map = {} #a dictionary that maps RuntimeClassBase to InstanceWrapper def addEvent(self, event, time_offset = 0.0): self.events.add(event, time_offset) # Broadcast an event to all instances def broadcast(self, new_event): for i in self.instances_map: i.addEvent(new_event) def getWaitTime(self): #first get waiting time of the object manager's events smallest_time = self.events.getEarliestTime(); #check all the instances for instance in self.instances_map.iterkeys() : smallest_time = min(smallest_time, instance.getEarliestEventTime()) return smallest_time; def stepAll(self, delta): self.step(delta) for i in self.instances_map.iterkeys(): i.step(delta) def step(self, delta): self.events.decreaseTime(delta); for event in self.events.popDueEvents() : self.handleEvent(event) def start(self): for i in self.instances_map: i.start() def handleEvent(self, e): if e.getName() == "narrow_cast" : self.handleNarrowCastEvent(e.getParameters()) elif e.getName() == "broad_cast" : self.handleBroadCastEvent(e.getParameters()) elif e.getName() == "create_instance" : self.handleCreateEvent(e.getParameters()) elif e.getName() == "associate_instance" : self.handleAssociateEvent(e.getParameters()) elif e.getName() == "start_instance" : self.handleStartInstanceEvent(e.getParameters()) elif e.getName() == "delete_instance" : self.handleDeleteInstanceEvent(e.getParameters()) def processAssociationReference(self, input_string): if len(input_string) == 0 : raise AssociationReferenceException("Empty association reference.") regex_pattern = re.compile("^([a-zA-Z_]\w*)(?:\[(\d+)\])?$"); path_string = input_string.split("/") result = [] for piece in path_string : match = regex_pattern.match(piece) if match : name = match.group(1) index = match.group(2) if index is None : index = -1 result.append((name,int(index))) else : raise AssociationReferenceException("Invalid entry in association reference.") return result def handleStartInstanceEvent(self, parameters): if len(parameters) != 2 : raise ParameterException ("The start instance event needs 2 parameters.") else : source = parameters[0] traversal_list = self.processAssociationReference(parameters[1]) for i in self.getInstances(source, traversal_list) : i.instance.start() source.addEvent(Event("instance_started", parameters = [parameters[1]])) def handleBroadCastEvent(self, parameters): if len(parameters) != 1 : raise ParameterException ("The broadcast event needs 1 parameter.") self.broadcast(parameters[0]) def handleCreateEvent(self, parameters): if len(parameters) < 2 : raise ParameterException ("The create event needs at least 2 parameters.") else : source = parameters[0] association_name = parameters[1] association = self.instances_map[source].getAssociation(association_name) if association.allowedToAdd() : ''' allow subclasses to be instantiated ''' class_name = association.class_name if len(parameters) == 2 else parameters[2] new_instance_wrapper = self.createInstance(class_name, parameters[3:]) idx = association.addInstance(new_instance_wrapper) try: new_instance_wrapper.getAssociation('parent').addInstance(self.instances_map[source]) except AssociationReferenceException: pass source.addEvent(Event("instance_created", parameters = ['%s[%i]' % (association_name, idx)])) else : source.addEvent(Event("instance_creation_error", parameters = [association_name])) def handleDeleteInstanceEvent(self, parameters): if len(parameters) < 2 : raise ParameterException ("The delete event needs at least 2 parameters.") else : source = parameters[0] association_name = parameters[1] traversal_list = self.processAssociationReference(association_name) instances = self.getInstances(source, traversal_list) association = self.instances_map[source].getAssociation(traversal_list[0][0]) for i in instances: association.removeInstance(i) i.getInstance().stop() if hasattr(i.instance, '__del__'): i.instance.__del__() source.addEvent(Event("instance_deleted", parameters = [parameters[1]])) def handleAssociateEvent(self, parameters): if len(parameters) != 3 : raise ParameterException ("The associate_instance event needs 3 parameters."); else : source = parameters[0] to_copy_list = self.getInstances(source,self.processAssociationReference(parameters[1])) if len(to_copy_list) != 1 : raise AssociationReferenceException ("Invalid source association reference.") wrapped_to_copy_instance = to_copy_list[0] dest_list = self.processAssociationReference(parameters[2]) if len(dest_list) == 0 : raise AssociationReferenceException ("Invalid destination association reference.") last = dest_list.pop() if last[1] != -1 : raise AssociationReferenceException ("Last association name in association reference should not be accompanied by an index.") for i in self.getInstances(source, dest_list) : i.getAssociation(last[0]).addInstance(wrapped_to_copy_instance) def handleNarrowCastEvent(self, parameters): if len(parameters) != 3 : raise ParameterException ("The associate_instance event needs 3 parameters.") source = parameters[0] traversal_list = self.processAssociationReference(parameters[1]) cast_event = parameters[2] for i in self.getInstances(source, traversal_list) : i.instance.addEvent(cast_event) def getInstances(self, source, traversal_list): currents = [self.instances_map[source]] for (name, index) in traversal_list : nexts = [] for current in currents : association = current.getAssociation(name) if (index >= 0 ) : nexts.append ( association.getInstance(index) ); elif (index == -1) : nexts.extend ( association.instances.values() ); else : raise AssociationReferenceException("Incorrect index in association reference.") currents = nexts return currents @abc.abstractmethod def instantiate(self, class_name, construct_params): pass def createInstance(self, class_name, construct_params = []): instance_wrapper = self.instantiate(class_name, construct_params) if instance_wrapper: self.instances_map[instance_wrapper.getInstance()] = instance_wrapper return instance_wrapper
def __init__(self, controller): self.controller = controller self.events = EventQueue() self.instances = [ ] #a dictionary that maps RuntimeClassBase to InstanceWrapper
def __init__(self): self.active = False self.state_changed = False self.events = EventQueue(); self.timers = None;
class ControllerBase(object): def __init__(self, object_manager): self.object_manager = object_manager self.private_port_counter = 0 # Keep track of input ports self.input_ports = {} self.input_queue = EventQueue() # Keep track of output ports self.output_ports = [] self.output_listeners = [] # Let statechart run one last time before stopping self.done = False def addInputPort(self, virtual_name, instance = None): if instance == None : port_name = virtual_name else: port_name = "private_" + str(self.private_port_counter) + "_" + virtual_name self.private_port_counter += 1 self.input_ports[port_name] = InputPortEntry(virtual_name, instance) return port_name def addOutputPort(self, port_name): self.output_ports.append(port_name) def broadcast(self, new_event): self.object_manager.broadcast(new_event) def start(self): self.object_manager.start() def stop(self): pass def addInput(self, input_event_list, time_offset = 0.0): if not isinstance(input_event_list, list): input_event_list = [input_event_list] for e in input_event_list: if e.getName() == "" : raise InputException("Input event can't have an empty name.") if e.getPort() not in self.input_ports : raise InputException("Input port mismatch, no such port: " + e.getPort() + ".") self.input_queue.add(input_event_list, time_offset) def getWaitTime(self): return min(self.object_manager.getWaitTime(), self.input_queue.getEarliestTime()) def handleInput(self, delta): self.input_queue.decreaseTime(delta) for events in self.input_queue.popDueEvents(): for e in events: input_port = self.input_ports[e.getPort()] e.port = input_port.virtual_name target_instance = input_port.instance if target_instance == None: self.broadcast(e) else: target_instance.addEvent(e) def outputEvent(self, event): for listener in self.output_listeners : listener.add(event) def addOutputListener(self, ports): listener = OutputListener(ports) self.output_listeners.append(listener) return listener def addMyOwnOutputListener(self, listener): self.output_listeners.append(listener) # deprecated, to add multiple events, use addInput instead def addEventList(self, event_list): for (event, time_offset) in event_list : self.addInput(event, time_offset) def getObjectManager(self): return self.object_manager
class ControllerBase(object): def __init__(self, object_manager): self.object_manager = object_manager self.private_port_counter = 0 # Keep track of input ports self.input_ports = {} self.input_queue = EventQueue() # Keep track of output ports self.output_ports = [] self.output_listeners = [] # Let statechart run one last time before stopping self.done = False def addInputPort(self, virtual_name, instance=None): if instance == None: port_name = virtual_name else: port_name = "private_" + str( self.private_port_counter) + "_" + virtual_name self.private_port_counter += 1 self.input_ports[port_name] = InputPortEntry(virtual_name, instance) return port_name def addOutputPort(self, port_name): self.output_ports.append(port_name) def broadcast(self, new_event): self.object_manager.broadcast(new_event) def start(self): self.object_manager.start() def stop(self): pass def addInput(self, input_event_list, time_offset=0.0): if not isinstance(input_event_list, list): input_event_list = [input_event_list] for e in input_event_list: if e.getName() == "": raise InputException("Input event can't have an empty name.") if e.getPort() not in self.input_ports: raise InputException("Input port mismatch, no such port: " + e.getPort() + ".") self.input_queue.add(input_event_list, time_offset) def getWaitTime(self): return min(self.object_manager.getWaitTime(), self.input_queue.getEarliestTime()) def handleInput(self, delta): self.input_queue.decreaseTime(delta) for events in self.input_queue.popDueEvents(): for e in events: input_port = self.input_ports[e.getPort()] e.port = input_port.virtual_name target_instance = input_port.instance if target_instance == None: self.broadcast(e) else: target_instance.addEvent(e) def outputEvent(self, event): for listener in self.output_listeners: listener.add(event) def addOutputListener(self, ports): listener = OutputListener(ports) self.output_listeners.append(listener) return listener def addMyOwnOutputListener(self, listener): self.output_listeners.append(listener) # deprecated, to add multiple events, use addInput instead def addEventList(self, event_list): for (event, time_offset) in event_list: self.addInput(event, time_offset) def getObjectManager(self): return self.object_manager
tdf = read_trades_csv(trades_csv_path) qdf = read_quotes_csv(quotes_csv_path) sym = "XBTUSD" all_data = {sym: {}} all_data[sym]["TRADES"] = tdf all_data[sym]["QUOTES"] = qdf init_cap = 100000 start_time = tdf.index[1500] # start time end_time = tdf.index[-1] qdf = qdf.loc[:end_time] dh = DataHandler(start_time, all_data) eq = EventQueue(start_time) strat = DollarWeightedMACD(eq, dh) data0 = pd.Series([1000, -200000], index=["size", "price"], name=tdf.index[1501]) data1 = pd.Series([1000, 2000000], index=["size", "price"], name=tdf.index[1501]) event_time = tdf.index[1501] print(strat.previously_increasing) de = DataEvent(event_time, sym, "TRADES", data0) strat.update_lookbacks(de) print(strat.calculate_signal(de))
class Simulator(object): def __init__(self, jobs, num_processors, scheduler): self.event_queue = EventQueue() self.machine = ValidatingMachine(num_processors=num_processors, event_queue=self.event_queue) self.scheduler = scheduler self.event_queue.add_handler(JobSubmissionEvent, self.handle_submission_event) self.event_queue.add_handler(JobTerminationEvent, self.handle_termination_event) for job in jobs: self.event_queue.add_event( JobSubmissionEvent(timestamp=job.submit_time, job=job)) def run(self): while not self.event_queue.is_empty: self.event_queue.advance() def handle_submission_event(self, event): assert isinstance(event, JobSubmissionEvent) newEvents = self.scheduler.handleSubmissionOfJobEvent( event.job, event.timestamp) for event in newEvents: self.event_queue.add_event(event) def handle_termination_event(self, event): assert isinstance(event, JobTerminationEvent) newEvents = self.scheduler.handleTerminationOfJobEvent( event.job, event.timestamp) for event in newEvents: self.event_queue.add_event(event) def handle_prediction_is_over_event(self, event): assert isinstance(event, JobPredictionIsOverEvent) newEvents = self.scheduler.handlePredictionIsOverEvent( event.job, event.timestamp) for event in newEvents: self.event_queue.add_event(event)
# print(callback.__name__) callback(key.fileobj, mask) except EventLoopAppException: pass if __name__ == "__main__": env = 'development' cache_policy = LRU if len(sys.argv) > 1: env = sys.argv[1] if len(sys.argv) == 3: cache_policy = sys.argv[2] print('Event loop app is starting in ' + env + ' environment with ' + cache_policy + ' cache policy.') event_queue = EventQueue() app = EventLoopApp(env, event_queue) event_loop = EventLoop(event_queue, cache_policy) try: conn_process = Thread(name='conn_process', target=app.start) event_loop_process = Thread(name='event_loop_process', target=event_loop.start) conn_process.start() event_loop_process.start() except KeyboardInterrupt: app.SERVER_SOCKET.close() print('You pressed CTRL + C') print('Server terminated gracefully')
class Simulation: """An instance of this class contains the data necessary for an entire simulation. Attributes: event_queue: the priority queue that stores events clock: the clock that must be updated whenever an event is dequeued links: dictionary of links (key is the ID, value is the Link object) flows: dictionary of flows (key is the ID, value is the Flow object) hosts: dictionary of hosts (key is the ID, value is the Host object) routers: dictionary of routers (key is the ID, value is the Router object) """ def __init__(self, links, flows, hosts, routers, verbose, fast_insteadof_reno): self.links = links self.flows = flows self.hosts = hosts self.routers = routers # Set up clocks self.clock = Clock() for item in flows.values(): item.controller.clock = self.clock for item in hosts.values(): item.clock = self.clock # Set up event schedulers self.event_queue = EventQueue(self.clock) for flow in flows.values() + links.values() + hosts.values(): flow.event_scheduler = self.event_queue for flow in flows.values(): flow.controller.event_scheduler = self.event_queue # Set up initial events for flow in flows.values(): self.event_queue.delay_event(flow.start_time, FlowWakeEvent(flow)) for host in hosts.values(): self.event_queue.delay_event(0, RoutingUpdateEvent(host)) # Set up logging self.logger = Logger(self.clock, verbose, fast_insteadof_reno) for item in flows.values() + links.values() + hosts.values() + routers.values(): item.set_logger(self.logger) # Set up PrintElapsedSimulationTimeEvents if not verbose: # combining this with verbose mode would be chaos. self.event_queue.delay_event(0, PrintElapsedSimulationTimeEvent(self.clock.current_time, self.event_queue)) print "Simulation started..." """Performs a single event from the event queue.""" def step(self): try: event = self.event_queue.dequeue_next_event() event.perform() return True except Queue.Empty: return False """Determines whether all flows have been completed.""" def all_flows_finished(self): for flow in self.flows.values(): if not flow.completed(): return False return True """Repeatedly performs events from the queue until all flows have been completed.""" def run(self): while not self.all_flows_finished(): if not self.step(): sys.exit("TCP deadlock: event queue empty but flows not complete") print "All flows finished transmitting!" print "Elapsed time in simulation world: " + str(self.clock) def __str__(self): return ("----LINKS----\n" + "\n".join(map(str, self.links.values())) + "\n" "----FLOWS----\n" + "\n".join(map(str, self.flows.values())) + "\n" "----HOSTS----\n" + "\n".join(map(str, self.hosts.values())) + "\n" "----ROUTERS----\n" + "\n".join(map(str, self.routers.values())))
class RuntimeClassBase(object): __metaclass__ = abc.ABCMeta def __init__(self, controller): self.active = False self.is_stable = True self.events = EventQueue() self.controller = controller self.timers = None self.inports = {} self.semantics = StatechartSemantics() def start(self): self.current_state = {} self.history_state = {} self.timers = {} self.big_step = BigStepState() self.combo_step = ComboStepState() self.small_step = SmallStepState() self.active = True self.is_stable = False self.initializeStatechart() self.processBigStepOutput() def stop(self): self.active = False def addEvent(self, event_list, time_offset=0.0): if not isinstance(event_list, list): event_list = [event_list] self.events.add(event_list, time_offset) def getEarliestEventTime(self): if not self.active: return INFINITY if not self.is_stable: return 0.0 if self.timers: return min(self.events.getEarliestTime(), min(self.timers.itervalues())) return self.events.getEarliestTime() def processBigStepOutput(self): for e in self.big_step.getOutputEvents(): self.controller.outputEvent(e) for e in self.big_step.getOutputEventsOM(): self.controller.object_manager.addEvent(e) def step(self, delta): if not self.active: return # decrease event queue time self.events.decreaseTime(delta) # decrease timers time next_timers = {} for (key, value) in self.timers.iteritems(): time = value - delta if time <= 0.0: self.addEvent(Event("_" + str(key) + "after"), time) else: next_timers[key] = time self.timers = next_timers # execute big step(s) due = self.events.popDueEvents() if not due and not self.is_stable: due = [[]] for input_events in due: # perform 1 big step per slot in 'due' self.is_stable = not self.bigStep(input_events) self.processBigStepOutput() def inState(self, nodes): for c in self.current_state.itervalues(): new_nodes = [] for n in nodes: if not (n in c): new_nodes.append(n) nodes = new_nodes if len(nodes) == 0: return True return False def bigStep(self, input_events): #print "new big step" self.big_step.next(input_events) self.small_step.reset() self.combo_step.reset() while self.comboStep(): self.big_step.setStepped() if self.semantics.big_step_maximality == StatechartSemantics.TakeOne: break # Take One -> only one combo step allowed return self.big_step.hasStepped() def comboStep(self): #print "new combo step" self.combo_step.next() while self.smallStep(): self.combo_step.setStepped() return self.combo_step.hasStepped() def smallStep(self): if self.small_step.hasStepped(): self.small_step.next() self.generateCandidates() if self.small_step.hasCandidates(): #print "new small step, have " + str(len(self.small_step.getCandidates())) + " candidates" if self.semantics.concurrency == StatechartSemantics.Single: transition, parameters = self.small_step.getCandidates()[ 0] # execute first of candidates transition(parameters) elif self.semantics.concurrency == StatechartSemantics.Many: pass # TODO: implement self.small_step.setStepped() return self.small_step.hasStepped() def getEnabledEvents(self): result = self.small_step.getCurrentEvents( ) + self.combo_step.getCurrentEvents() if self.semantics.input_event_lifeline == StatechartSemantics.Whole or ( not self.big_step.hasStepped() and (self.semantics.input_event_lifeline == StatechartSemantics.FirstComboStep or (not self.combo_step.hasStepped() and self.semantics.input_event_lifeline == StatechartSemantics.FirstSmallStep))): result += self.big_step.getInputEvents() return result def raiseInternalEvent(self, event): if self.semantics.internal_event_lifeline == StatechartSemantics.NextSmallStep: self.small_step.addNextEvent(event) elif self.semantics.internal_event_lifeline == StatechartSemantics.NextComboStep: self.combo_step.addNextEvent(event) elif self.semantics.internal_event_lifeline == StatechartSemantics.Queue: self.events.add([event], 0.0) @abc.abstractmethod def initializeStatechart(self): pass @abc.abstractmethod def generateCandidates(self): pass
class test_EventQueue(TestCase): def setUp(self): self.queue = EventQueue() self.event = prototype.JobEvent(timestamp=0, job=None) self.events = [ prototype.JobEvent(timestamp=i, job=None) for i in xrange(10) ] self.handler = _create_handler() def tearDown(self): del self.queue, self.event, self.events, self.handler def test_len_empty(self): self.assertEqual( 0, len(self.queue) ) def test_len_nonempty(self): for event in self.events: self.queue.add_event(event) self.assertEqual( len(self.events), len(self.queue) ) def test_add_event_sanity(self): self.queue.add_event( self.event ) def test_add_event_single_event(self): self.queue.add_event(self.event) self.assertEqual( [self.event], self.queue.sorted_events ) def test_add_same_event_fails(self): self.queue.add_event(self.event) self.assertRaises(Exception, self.queue.add_event, self.event) def test_add_event_simple(self): for event in self.events: self.queue.add_event(event) self.assertEqual( self.events, list(self.queue.sorted_events) ) def test_add_event_sorting(self): random_events = _gen_random_timestamp_events() for event in random_events: self.queue.add_event(event) self.assertEqual( sorted(random_events), self.queue.sorted_events ) def test_remove_event_fails_on_empty(self): self.assertRaises(Exception, self.queue.remove_event, self.event) def test_remove_event_fails_on_missing_event(self): event1 = prototype.JobEvent(0, 0) event2 = prototype.JobEvent(0, 1) assert event1 != event2 # different events self.queue.add_event(event1) self.assertRaises(Exception, self.queue.remove_event, event2) def test_remove_event_succeeds(self): self.queue.add_event(self.event) self.queue.remove_event(self.event) self.failUnless( self.queue.is_empty ) def test_pop_one_job(self): self.queue.add_event( self.event ) assert self.queue.pop() is self.event def test_pop_many_jobs(self): for event in self.events: self.queue.add_event(event) for event in self.events: assert self.queue.pop() is event def test_pop_empty(self): self.assertRaises(AssertionError, self.queue.pop) def test_empty_true(self): self.failUnless( self.queue.is_empty ) def test_empty_false(self): self.queue.add_event( self.event ) self.failIf( self.queue.is_empty ) def test_add_handler_sanity(self): self.queue.add_handler(prototype.JobEvent, self.handler) self.queue.add_event(self.event) self.failIf( self.handler.called ) def test_get_event_handlers_empty(self): self.assertEqual( 0, len(self.queue._get_event_handlers( prototype.JobEvent )) ) def test_get_event_handlers_nonempty(self): self.queue.add_handler(prototype.JobEvent, self.handler) self.assertEqual( 1, len(self.queue._get_event_handlers( prototype.JobEvent )) ) def test_advance_empty_queue(self): self.assertRaises(AssertionError, self.queue.advance) def test_advance_eats_event(self): self._add_event_and_advance(self.event) self.failUnless(self.queue.is_empty) def test_add_event_earlier_event_after_later_advance(self): # after handling an event with a later timestamp, adding an event with # an older timestamp should fail self._add_event_and_advance(prototype.JobEvent(timestamp=2, job="x")) self.assertRaises(Exception, self.queue.add_event, prototype.JobEvent(timestamp=1, job="x")) def test_add_event_same_timestamp_after_advance(self): # same timestamp should succeed even after an event has been handled self._add_event_and_advance(prototype.JobEvent(timestamp=2, job="x")) self.queue.add_event(prototype.JobEvent(timestamp=2, job="y")) def test_advance_one_handler_handles(self): self.queue.add_handler(prototype.JobEvent, self.handler) self._add_event_and_advance(self.event) self.failUnless( self.handler.called ) def test_advance_one_handler_doesnt_handle(self): self.queue.add_handler(prototype.JobStartEvent, self.handler) self._add_event_and_advance(self.event) # JobEvent, different type self.failIf( self.handler.called ) def test_advance_many_handlers(self): matching_handlers = [ _create_handler() for i in xrange(5) ] nonmatching_handlers = [ _create_handler() for i in xrange(5) ] # register handlers that should run for handler in matching_handlers: self.queue.add_handler(prototype.JobEvent, handler) # register handlers that shouldn't run with a different event type for handler in nonmatching_handlers: self.queue.add_handler(prototype.JobStartEvent, handler) self._add_event_and_advance(self.event) for handler in matching_handlers: self.failUnless( handler.called ) for handler in nonmatching_handlers: self.failIf( handler.called ) def test_sometimes_relevant_handler(self): self.queue.add_handler(prototype.JobEvent, self.handler) self._add_event_and_advance(prototype.JobEvent(timestamp=0, job="x")) self.failUnless(self.handler.called) self.handler.called = False self._add_event_and_advance(prototype.JobStartEvent(timestamp=1, job="x")) self.failIf(self.handler.called) self._add_event_and_advance(prototype.JobEvent(timestamp=2, job="x")) self.failUnless(self.handler.called) def _add_event_and_advance(self, event): self.queue.add_event(event) self.queue.advance()
df = pd.read_csv( quotes_csv_path, usecols=["bidSize", "bidPrice", "askPrice", "askSize", "recorded"], parse_dates=["recorded"], index_col="recorded", ) df.index.name = "received" return df trades_csv_path = "play_data/XBTUSD_trades_191214_0434.csv" quotes_csv_path = "play_data/XBTUSD_quotes_191214_0434.csv" tdf = read_trades_csv(trades_csv_path) qdf = read_quotes_csv(quotes_csv_path) sym = "XBTUSD" all_data = {sym: {}} all_data[sym]["TRADES"] = tdf all_data[sym]["QUOTES"] = qdf init_cap = 100000 start_time = tdf.index[1500] # start time dh = DataHandler(start_time, all_data) eq = EventQueue(start_time) eh = BacktestExecutionHandler(eq, dh) order = Order(sym, "BitMEX", "MKT", "BUY", 10, 0) eh.send_order(order, start_time) print(eq.get())
def setUp(self): self.event_queue = EventQueue() self.machine = prototype.ValidatingMachine(50, self.event_queue) self.unique_numbers = unique_numbers()
class test_ValidatingMachine(TestCase): def setUp(self): self.event_queue = EventQueue() self.machine = prototype.ValidatingMachine(50, self.event_queue) self.unique_numbers = unique_numbers() def tearDown(self): del self.event_queue, self.machine, self.unique_numbers def _unique_job(self, user_estimated_run_time=100, actual_run_time=60, num_required_processors=20): return prototype.Job(id=self.unique_numbers.next(), user_estimated_run_time=user_estimated_run_time, actual_run_time=actual_run_time, num_required_processors=num_required_processors) def test_no_jobs_on_init(self): self.assertEqual(0, len(self.machine.jobs)) def test_add_job(self): job = self._unique_job() self.machine._add_job(job, current_timestamp=0) assert job in self.machine.jobs def test_add_several_jobs_success(self): for i in xrange(5): self.machine._add_job(self._unique_job(num_required_processors=5), current_timestamp=0) def test_add_job_too_big(self): self.assertRaises(Exception, self.machine._add_job, self._unique_job(num_required_processors=100), current_timestamp=0) def test_add_second_job_too_big(self): self.machine._add_job(self._unique_job(num_required_processors=40), current_timestamp=0) self.assertRaises(Exception, self.machine._add_job, self._unique_job(num_required_processors=40), current_timestamp=0) def test_free_processors_empty(self): self.assertEqual(50, self.machine.free_processors) def test_free_processors_nonempty(self): for i in xrange(10): self.machine._add_job(self._unique_job(num_required_processors=3), current_timestamp=0) self.assertEqual(20, self.machine.free_processors) def test_free_processors_full(self): self.machine._add_job(self._unique_job(num_required_processors=50), current_timestamp=0) self.assertEqual(0, self.machine.free_processors) def test_busy_processors_empty(self): self.assertEqual(0, self.machine.busy_processors) def test_busy_processors_nonempty(self): for i in xrange(10): self.machine._add_job(self._unique_job(num_required_processors=3), current_timestamp=0) self.assertEqual(30, self.machine.busy_processors) def test_busy_processors_full(self): self.machine._add_job(self._unique_job(num_required_processors=50), current_timestamp=0) self.assertEqual(50, self.machine.busy_processors) def test_add_job_adds_job_end_event(self): self.machine._add_job(self._unique_job(), current_timestamp=0) self.failIf(self.event_queue.is_empty) def test_job_done_removed(self): self.machine._add_job(self._unique_job(), current_timestamp=0) self.event_queue.advance() self.assertEqual(0, self.machine.busy_processors) def test_add_job_different_current_timestamp(self): self.machine._add_job(current_timestamp=100, job=self._unique_job(actual_run_time=20, num_required_processors=10)) self.machine._add_job(current_timestamp=120, job=self._unique_job(actual_run_time=40, num_required_processors=5)) self.event_queue.advance() self.assertEqual(5, self.machine.busy_processors) def test_add_job_different_current_timestamp2(self): self.machine._add_job(current_timestamp=110, job=self._unique_job(actual_run_time=20, num_required_processors=10)) self.machine._add_job(current_timestamp=100, job=self._unique_job(actual_run_time=40, num_required_processors=5)) self.event_queue.advance() self.assertEqual(5, self.machine.busy_processors) def test_start_job_handler(self): job = self._unique_job() self.event_queue.add_event( prototype.JobStartEvent(timestamp=0, job=job)) self.event_queue.advance() assert job in self.machine.jobs
def __init__(self, controller): self.controller = controller self.events = EventQueue() self.instances_map = {} #a dictionary that maps RuntimeClassBase to InstanceWrapper
class test_ValidatingMachine(TestCase): def setUp(self): self.event_queue = EventQueue() self.machine = prototype.ValidatingMachine(50, self.event_queue) self.unique_numbers = unique_numbers() def tearDown(self): del self.event_queue, self.machine, self.unique_numbers def _unique_job(self, user_estimated_run_time=100, actual_run_time=60, num_required_processors=20): return prototype.Job( id = self.unique_numbers.next(), user_estimated_run_time = user_estimated_run_time, actual_run_time = actual_run_time, num_required_processors = num_required_processors ) def test_no_jobs_on_init(self): self.assertEqual(0, len(self.machine.jobs)) def test_add_job(self): job = self._unique_job() self.machine._add_job(job, current_timestamp=0) assert job in self.machine.jobs def test_add_several_jobs_success(self): for i in xrange(5): self.machine._add_job( self._unique_job(num_required_processors=5), current_timestamp=0 ) def test_add_job_too_big(self): self.assertRaises(Exception, self.machine._add_job, self._unique_job(num_required_processors=100), current_timestamp=0) def test_add_second_job_too_big(self): self.machine._add_job( self._unique_job(num_required_processors=40), current_timestamp=0 ) self.assertRaises(Exception, self.machine._add_job, self._unique_job(num_required_processors=40), current_timestamp=0 ) def test_free_processors_empty(self): self.assertEqual(50, self.machine.free_processors) def test_free_processors_nonempty(self): for i in xrange(10): self.machine._add_job(self._unique_job(num_required_processors=3), current_timestamp=0) self.assertEqual(20, self.machine.free_processors) def test_free_processors_full(self): self.machine._add_job(self._unique_job(num_required_processors=50), current_timestamp=0) self.assertEqual(0, self.machine.free_processors) def test_busy_processors_empty(self): self.assertEqual(0, self.machine.busy_processors) def test_busy_processors_nonempty(self): for i in xrange(10): self.machine._add_job(self._unique_job(num_required_processors=3), current_timestamp=0) self.assertEqual(30, self.machine.busy_processors) def test_busy_processors_full(self): self.machine._add_job(self._unique_job(num_required_processors=50), current_timestamp=0) self.assertEqual(50, self.machine.busy_processors) def test_add_job_adds_job_end_event(self): self.machine._add_job(self._unique_job(), current_timestamp=0) self.failIf(self.event_queue.is_empty) def test_job_done_removed(self): self.machine._add_job(self._unique_job(), current_timestamp=0) self.event_queue.advance() self.assertEqual(0, self.machine.busy_processors) def test_add_job_different_current_timestamp(self): self.machine._add_job(current_timestamp=100, job=self._unique_job(actual_run_time=20, num_required_processors=10)) self.machine._add_job(current_timestamp=120, job=self._unique_job(actual_run_time=40, num_required_processors=5)) self.event_queue.advance() self.assertEqual(5, self.machine.busy_processors) def test_add_job_different_current_timestamp2(self): self.machine._add_job(current_timestamp=110, job=self._unique_job(actual_run_time=20, num_required_processors=10)) self.machine._add_job(current_timestamp=100, job=self._unique_job(actual_run_time=40, num_required_processors=5)) self.event_queue.advance() self.assertEqual(5, self.machine.busy_processors) def test_start_job_handler(self): job = self._unique_job() self.event_queue.add_event( prototype.JobStartEvent( timestamp=0, job=job ) ) self.event_queue.advance() assert job in self.machine.jobs
class ObjectManagerBase(object): __metaclass__ = abc.ABCMeta def __init__(self, controller): self.controller = controller self.events = EventQueue() self.instances = [ ] #a dictionary that maps RuntimeClassBase to InstanceWrapper def addEvent(self, event, time_offset=0.0): self.events.add(event, time_offset) # Broadcast an event to all instances def broadcast(self, new_event): for i in self.instances: i.addEvent(new_event) def getWaitTime(self): #first get waiting time of the object manager's events smallest_time = self.events.getEarliestTime() #check all the instances for instance in self.instances: smallest_time = min(smallest_time, instance.getEarliestEventTime()) return smallest_time def stepAll(self, delta): self.step(delta) for i in self.instances: i.step(delta) def step(self, delta): self.events.decreaseTime(delta) for event in self.events.popDueEvents(): self.handleEvent(event) def start(self): for i in self.instances: i.start() def handleEvent(self, e): if e.getName() == "narrow_cast": self.handleNarrowCastEvent(e.getParameters()) elif e.getName() == "broad_cast": self.handleBroadCastEvent(e.getParameters()) elif e.getName() == "create_instance": self.handleCreateEvent(e.getParameters()) elif e.getName() == "associate_instance": self.handleAssociateEvent(e.getParameters()) elif e.getName() == "start_instance": self.handleStartInstanceEvent(e.getParameters()) elif e.getName() == "delete_instance": self.handleDeleteInstanceEvent(e.getParameters()) def processAssociationReference(self, input_string): if len(input_string) == 0: raise AssociationReferenceException("Empty association reference.") regex_pattern = re.compile("^([a-zA-Z_]\w*)(?:\[(\d+)\])?$") path_string = input_string.split("/") result = [] for piece in path_string: match = regex_pattern.match(piece) if match: name = match.group(1) index = match.group(2) if index is None: index = -1 result.append((name, int(index))) else: raise AssociationReferenceException( "Invalid entry in association reference. Input string: " + input_string) return result def handleStartInstanceEvent(self, parameters): if len(parameters) != 2: raise ParameterException( "The start instance event needs 2 parameters.") else: source = parameters[0] traversal_list = self.processAssociationReference(parameters[1]) for i in self.getInstances(source, traversal_list): i["instance"].start() source.addEvent( Event("instance_started", parameters=[parameters[1]])) def handleBroadCastEvent(self, parameters): if len(parameters) != 1: raise ParameterException("The broadcast event needs 1 parameter.") self.broadcast(parameters[0]) def handleCreateEvent(self, parameters): if len(parameters) < 2: raise ParameterException( "The create event needs at least 2 parameters.") source = parameters[0] association_name = parameters[1] association = source.associations[association_name] #association = self.instances_map[source].getAssociation(association_name) if association.allowedToAdd(): ''' allow subclasses to be instantiated ''' class_name = association.to_class if len( parameters) == 2 else parameters[2] new_instance = self.createInstance(class_name, parameters[3:]) if not new_instance: raise ParameterException("Creating instance: no such class: " + class_name) #index = association.addInstance(new_instance) try: index = association.addInstance(new_instance) except AssociationException as exception: raise RuntimeException( "Error adding instance to association '" + association_name + "': " + str(exception)) p = new_instance.associations.get("parent") if p: p.addInstance(source) source.addEvent( Event("instance_created", None, [association_name + "[" + str(index) + "]"])) else: source.addEvent( Event("instance_creation_error", None, [association_name])) def handleDeleteInstanceEvent(self, parameters): if len(parameters) < 2: raise ParameterException( "The delete event needs at least 2 parameters.") else: source = parameters[0] association_name = parameters[1] traversal_list = self.processAssociationReference(association_name) instances = self.getInstances(source, traversal_list) #association = self.instances_map[source].getAssociation(traversal_list[0][0]) association = source.associations[traversal_list[0][0]] for i in instances: try: association.removeInstance(i["instance"]) except AssociationException as exception: raise RuntimeException( "Error removing instance from association '" + association_name + "': " + str(exception)) i["instance"].stop() #if hasattr(i.instance, 'user_defined_destructor'): i["instance"].user_defined_destructor() source.addEvent( Event("instance_deleted", parameters=[parameters[1]])) def handleAssociateEvent(self, parameters): if len(parameters) != 3: raise ParameterException( "The associate_instance event needs 3 parameters.") else: source = parameters[0] to_copy_list = self.getInstances( source, self.processAssociationReference(parameters[1])) if len(to_copy_list) != 1: raise AssociationReferenceException( "Invalid source association reference.") wrapped_to_copy_instance = to_copy_list[0]["instance"] dest_list = self.processAssociationReference(parameters[2]) if len(dest_list) == 0: raise AssociationReferenceException( "Invalid destination association reference.") last = dest_list.pop() if last[1] != -1: raise AssociationReferenceException( "Last association name in association reference should not be accompanied by an index." ) for i in self.getInstances(source, dest_list): i["instance"].associations[last[0]].addInstance( wrapped_to_copy_instance) def handleNarrowCastEvent(self, parameters): if len(parameters) != 3: raise ParameterException( "The associate_instance event needs 3 parameters.") source = parameters[0] traversal_list = self.processAssociationReference(parameters[1]) cast_event = parameters[2] for i in self.getInstances(source, traversal_list): i["instance"].addEvent(cast_event) def getInstances(self, source, traversal_list): currents = [{ "instance": source, "ref": None, "assoc_name": None, "assoc_index": None }] #currents = [source] for (name, index) in traversal_list: nexts = [] for current in currents: association = current["instance"].associations[name] if (index >= 0): nexts.append({ "instance": association.instances[index], "ref": current["instance"], "assoc_name": name, "assoc_index": index }) elif (index == -1): for i in association.instances: nexts.append({ "instance": association.instances[i], "ref": current["instance"], "assoc_name": name, "assoc_index": index }) #nexts.extend( association.instances.values() ) else: raise AssociationReferenceException( "Incorrect index in association reference.") currents = nexts return currents @abc.abstractmethod def instantiate(self, class_name, construct_params): pass def createInstance(self, to_class, construct_params=[]): instance = self.instantiate(to_class, construct_params) self.instances.append(instance) return instance
class RuntimeClassBase(object): __metaclass__ = abc.ABCMeta def __init__(self, controller): self.active = False self.is_stable = True self.events = EventQueue() self.controller = controller self.timers = None self.inports = {} self.semantics = StatechartSemantics() def start(self): self.current_state = {} self.history_state = {} self.timers = {} self.big_step = BigStepState() self.combo_step = ComboStepState() self.small_step = SmallStepState() self.active = True self.is_stable = False self.initializeStatechart() self.processBigStepOutput() def stop(self): self.active = False def addEvent(self, event_list, time_offset = 0.0): if not isinstance(event_list, list): event_list = [event_list] self.events.add(event_list, time_offset) def getEarliestEventTime(self) : if not self.active: return INFINITY if not self.is_stable: return 0.0 if self.timers: return min(self.events.getEarliestTime(), min(self.timers.itervalues())) return self.events.getEarliestTime() def processBigStepOutput(self): for e in self.big_step.getOutputEvents(): self.controller.outputEvent(e) for e in self.big_step.getOutputEventsOM(): self.controller.object_manager.addEvent(e) def step(self, delta): if not self.active : return # decrease event queue time self.events.decreaseTime(delta) # decrease timers time next_timers = {} for (key,value) in self.timers.iteritems() : time = value - delta if time <= 0.0 : self.addEvent( Event("_" + str(key) + "after"), time) else : next_timers[key] = time self.timers = next_timers # execute big step(s) due = self.events.popDueEvents() if not due and not self.is_stable: due = [[]] for input_events in due: # perform 1 big step per slot in 'due' self.is_stable = not self.bigStep(input_events) self.processBigStepOutput() def inState(self, nodes): for c in self.current_state.itervalues(): new_nodes = [] for n in nodes: if not (n in c): new_nodes.append(n) nodes = new_nodes if len(nodes) == 0: return True return False def bigStep(self, input_events): #print "new big step" self.big_step.next(input_events) self.small_step.reset() self.combo_step.reset() while self.comboStep(): self.big_step.setStepped() if self.semantics.big_step_maximality == StatechartSemantics.TakeOne: break # Take One -> only one combo step allowed return self.big_step.hasStepped() def comboStep(self): #print "new combo step" self.combo_step.next() while self.smallStep(): self.combo_step.setStepped() return self.combo_step.hasStepped() def smallStep(self): if self.small_step.hasStepped(): self.small_step.next() self.generateCandidates() if self.small_step.hasCandidates(): #print "new small step, have " + str(len(self.small_step.getCandidates())) + " candidates" if self.semantics.concurrency == StatechartSemantics.Single: transition, parameters = self.small_step.getCandidates()[0] # execute first of candidates transition(parameters) elif self.semantics.concurrency == StatechartSemantics.Many: pass # TODO: implement self.small_step.setStepped() return self.small_step.hasStepped() def getEnabledEvents(self): result = self.small_step.getCurrentEvents() + self.combo_step.getCurrentEvents() if self.semantics.input_event_lifeline == StatechartSemantics.Whole or ( not self.big_step.hasStepped() and (self.semantics.input_event_lifeline == StatechartSemantics.FirstComboStep or ( not self.combo_step.hasStepped() and self.semantics.input_event_lifeline == StatechartSemantics.FirstSmallStep))): result += self.big_step.getInputEvents() return result def raiseInternalEvent(self, event): if self.semantics.internal_event_lifeline == StatechartSemantics.NextSmallStep: self.small_step.addNextEvent(event) elif self.semantics.internal_event_lifeline == StatechartSemantics.NextComboStep: self.combo_step.addNextEvent(event) elif self.semantics.internal_event_lifeline == StatechartSemantics.Queue: self.events.add([event], 0.0) @abc.abstractmethod def initializeStatechart(self): pass @abc.abstractmethod def generateCandidates(self): pass
start_time = tdf.index[1500] symbol = "XBTUSD" all_data = {} all_data[symbol] = {} all_data[symbol]["TRADES"] = tdf all_data[symbol]["QUOTES"] = qdf timer.start("initializing data_handler") data_handler = DataHandler(start_time, all_data) timer.stop() timer.start("initializing event_queue") event_queue = EventQueue(start_time) timer.stop() timer.start("initializing execution_handler") execution_handler = BacktestExecutionHandler(event_queue, data_handler) timer.stop() timer.start("initializing portfolio") portfolio = BacktestPortfolio(event_queue, data_handler, execution_handler) timer.stop() timer.start("initializing strat") tdf = data_handler.read_table(symbol, "TRADES") strat = DollarWeightedMACD(tdf) timer.stop()
class Command: def __init__(self): self.behaviors = [ 'Hover', 'MoveLeft', 'MoveForward', 'MoveBackwards', 'MoveUp', 'MoveDown', 'MoveRight', 'RotateLeft', 'RotateRight', 'TakeOff', 'Land', 'UTurn', 'AlignCorridor', 'CenterCorridor' ] self.commands = { 'Stop': [(0, 'Hover')], 'Dance': [(0, 'MoveLeft'), (0, 'MoveUp'), (2.5, 'MoveRight'), (3.2, 'Hover')], 'TakeOff': [(0, 'TakeOff'), (1, 'Hover')], 'Land': [(0, 'Land')], 'Hover': [(0, 'Hover')], 'MoveForward': [(0, 'MoveForward')], 'MoveBackwards': [(0, 'MoveBackwards')], 'MoveLeft': [(0, 'MoveLeft')], 'MoveRight': [(0, 'MoveRight')], 'MoveDown': [(0, 'MoveDown')], 'MoveUp': [(0, 'MoveUp')], 'RotateLeft': [(0, 'RotateLeft')], 'RotateRight': [(0, 'RotateRight')], 'UTurn': [(0, 'UTurn')], 'AlignCorridor': [(0, 'AlignCorridor')], 'CenterCorridor': [(0, 'CenterCorridor')] } self.command = rospy.Subscriber("/command", String, self.command_callback, queue_size=1) self.behavior = rospy.Publisher("/behavior", BehaviorStatus, queue_size=1) self.queue_lock = Lock() self.event_queue = EventQueue() def command_callback(self, msg): print("Entered callback") for behavior in self.behaviors: message = BehaviorStatus() message.behavior_name = behavior message.active = False # print(message) self.behavior.publish(message) with self.queue_lock: self.event_queue.setEvents(self.commands[msg.data]) def contact_behavior(self, behavior_name): message = BehaviorStatus() message.behavior_name = behavior_name message.active = True self.behavior.publish(message) def loop(self): while not rospy.is_shutdown(): with self.queue_lock: validEvents = self.event_queue.returnValidEvents() for behavior in validEvents: self.contact_behavior(behavior[1]) rospy.sleep(0.1)
class ObjectManagerBase(object): __metaclass__ = abc.ABCMeta def __init__(self, controller): self.controller = controller self.events = EventQueue() self.instances = [] #a dictionary that maps RuntimeClassBase to InstanceWrapper def addEvent(self, event, time_offset = 0.0): self.events.add(event, time_offset) # Broadcast an event to all instances def broadcast(self, new_event): for i in self.instances: i.addEvent(new_event) def getWaitTime(self): #first get waiting time of the object manager's events smallest_time = self.events.getEarliestTime() #check all the instances for instance in self.instances: smallest_time = min(smallest_time, instance.getEarliestEventTime()) return smallest_time def stepAll(self, delta): self.step(delta) for i in self.instances: i.step(delta) def step(self, delta): self.events.decreaseTime(delta) for event in self.events.popDueEvents() : self.handleEvent(event) def start(self): for i in self.instances: i.start() def handleEvent(self, e): if e.getName() == "narrow_cast" : self.handleNarrowCastEvent(e.getParameters()) elif e.getName() == "broad_cast" : self.handleBroadCastEvent(e.getParameters()) elif e.getName() == "create_instance" : self.handleCreateEvent(e.getParameters()) elif e.getName() == "associate_instance" : self.handleAssociateEvent(e.getParameters()) elif e.getName() == "start_instance" : self.handleStartInstanceEvent(e.getParameters()) elif e.getName() == "delete_instance" : self.handleDeleteInstanceEvent(e.getParameters()) def processAssociationReference(self, input_string): if len(input_string) == 0 : raise AssociationReferenceException("Empty association reference.") regex_pattern = re.compile("^([a-zA-Z_]\w*)(?:\[(\d+)\])?$") path_string = input_string.split("/") result = [] for piece in path_string : match = regex_pattern.match(piece) if match : name = match.group(1) index = match.group(2) if index is None : index = -1 result.append((name,int(index))) else : raise AssociationReferenceException("Invalid entry in association reference. Input string: " + input_string) return result def handleStartInstanceEvent(self, parameters): if len(parameters) != 2 : raise ParameterException ("The start instance event needs 2 parameters.") else : source = parameters[0] traversal_list = self.processAssociationReference(parameters[1]) for i in self.getInstances(source, traversal_list) : i["instance"].start() source.addEvent(Event("instance_started", parameters = [parameters[1]])) def handleBroadCastEvent(self, parameters): if len(parameters) != 1 : raise ParameterException ("The broadcast event needs 1 parameter.") self.broadcast(parameters[0]) def handleCreateEvent(self, parameters): if len(parameters) < 2 : raise ParameterException ("The create event needs at least 2 parameters.") source = parameters[0] association_name = parameters[1] association = source.associations[association_name] #association = self.instances_map[source].getAssociation(association_name) if association.allowedToAdd() : ''' allow subclasses to be instantiated ''' class_name = association.to_class if len(parameters) == 2 else parameters[2] new_instance = self.createInstance(class_name, parameters[3:]) if not new_instance: raise ParameterException("Creating instance: no such class: " + class_name) #index = association.addInstance(new_instance) try: index = association.addInstance(new_instance) except AssociationException as exception: raise RuntimeException("Error adding instance to association '" + association_name + "': " + str(exception)) p = new_instance.associations.get("parent") if p: p.addInstance(source) source.addEvent(Event("instance_created", None, [association_name+"["+str(index)+"]"])) else : source.addEvent(Event("instance_creation_error", None, [association_name])) def handleDeleteInstanceEvent(self, parameters): if len(parameters) < 2 : raise ParameterException ("The delete event needs at least 2 parameters.") else : source = parameters[0] association_name = parameters[1] traversal_list = self.processAssociationReference(association_name) instances = self.getInstances(source, traversal_list) #association = self.instances_map[source].getAssociation(traversal_list[0][0]) association = source.associations[traversal_list[0][0]] for i in instances: try: association.removeInstance(i["instance"]) except AssociationException as exception: raise RuntimeException("Error removing instance from association '" + association_name + "': " + str(exception)) i["instance"].stop() #if hasattr(i.instance, 'user_defined_destructor'): i["instance"].user_defined_destructor() source.addEvent(Event("instance_deleted", parameters = [parameters[1]])) def handleAssociateEvent(self, parameters): if len(parameters) != 3 : raise ParameterException ("The associate_instance event needs 3 parameters.") else : source = parameters[0] to_copy_list = self.getInstances(source,self.processAssociationReference(parameters[1])) if len(to_copy_list) != 1 : raise AssociationReferenceException ("Invalid source association reference.") wrapped_to_copy_instance = to_copy_list[0]["instance"] dest_list = self.processAssociationReference(parameters[2]) if len(dest_list) == 0 : raise AssociationReferenceException ("Invalid destination association reference.") last = dest_list.pop() if last[1] != -1 : raise AssociationReferenceException ("Last association name in association reference should not be accompanied by an index.") for i in self.getInstances(source, dest_list) : i["instance"].associations[last[0]].addInstance(wrapped_to_copy_instance) def handleNarrowCastEvent(self, parameters): if len(parameters) != 3 : raise ParameterException ("The associate_instance event needs 3 parameters.") source = parameters[0] traversal_list = self.processAssociationReference(parameters[1]) cast_event = parameters[2] for i in self.getInstances(source, traversal_list) : i["instance"].addEvent(cast_event) def getInstances(self, source, traversal_list): currents = [{ "instance" : source, "ref" : None, "assoc_name" : None, "assoc_index" : None }] #currents = [source] for (name, index) in traversal_list : nexts = [] for current in currents : association = current["instance"].associations[name] if (index >= 0 ) : nexts.append({ "instance" : association.instances[index], "ref" : current["instance"], "assoc_name" : name, "assoc_index" : index }) elif (index == -1) : for i in association.instances: nexts.append({ "instance" : association.instances[i], "ref" : current["instance"], "assoc_name" : name, "assoc_index" : index }) #nexts.extend( association.instances.values() ) else : raise AssociationReferenceException("Incorrect index in association reference.") currents = nexts return currents @abc.abstractmethod def instantiate(self, class_name, construct_params): pass def createInstance(self, to_class, construct_params = []): instance = self.instantiate(to_class, construct_params) self.instances.append(instance) return instance
def param(one, two="something else"): print "*** param(" + str(one) + ") executed" return "param(" + str(one) + ") result" def raiseexceptionnoparams(): print "*** raiseexceptionnoparams() executed" raise ValueError("raiseexceptionnoparams() exception") def raiseexceptionparam(one): print "*** raiseexceptionparam(" + str(one) + ") executed" raise ValueError("raiseexceptionparam(" + str(one) + ") exception") eq = EventQueue() results = [ eq.enqueue(param, [1, 2]), eq.enqueue(param, kwargs={ 'one': 2, 'two': 2 }), eq.enqueue(noparams, highPriority=True), eq.enqueue(raiseexceptionnoparams), eq.enqueue(raiseexceptionparam, [-1]), eq.stop(), eq.enqueue(param, ["Over 9000"]) ] for func in results:
def run(self): # initialize event queue with creation initialEvents = self.workload.getInitialEvents() queue = EventQueue(initialEvents) # number of alive tasks nTasks = queue.getSize() # there are no tasks if nTasks == 0: return # first timer event timerEvent = TimerEvent(0) queue.addEvent(timerEvent) self.scheduler.start() # Main Loop while not queue.isEmpty(): event = queue.extractMin() # time can be an arbitrary real number self.processor.setTime(event.getTime()) self.processor.updateRunningTask() if isinstance(event, TimerEvent): print "Timer Event [Tick=%d]" % event.getTime() # tick is only integer self.processor.setTicks(event.getTime()) # inform scheduler of timer interrupt self.scheduler.timerIntr(self.processor.getTicks()) timerEvent = TimerEvent(event.getTime() + 1) queue.addEvent(timerEvent) elif isinstance(event, TaskCreationEvent): task = event.getTask() print "Creating Task: ", task.getName() # inform scheduler of task creation self.scheduler.createTask(task) elif isinstance(event, TaskFinishEvent): task = event.getTask() self.processor.premptRunningTask() print "Finishing Task: ", task.getName() print "Used CPU Time: ", task.getUsedCpuTime() print "Total CPU Time: ", task.getTotalCpuTime() # inform scheduler that task finished self.scheduler.finishTask(task) # TODO: fix finishing bug if len(self.scheduler.getAllTaks()) == 0: return elif isinstance(event, IOStartEvent): task = event.getTask() runningTask = self.processor.getRunningTask() assert task is runningTask self.processor.premptRunningTask() print "IO Start: ", task.getName() print "IO Durration: ", event.getDuration() print "Used CPU time: ", task.getUsedCpuTime() task.setInIO(True) # inform scheduler that task is blocked self.scheduler.block(task) duration = event.getDuration() time = self.processor.getTime() + duration ioCompleteEvent = IOCompleteEvent(time, task) queue.addEvent(ioCompleteEvent) elif isinstance(event, IOCompleteEvent): task = event.getTask() print "IO Complete: ", task.getName() # inform scheduler that task is ready self.scheduler.unblock(task) else: print "Unkown event" sys.exit(1) self.genNonTriggeredEvents(queue) print "Running Task", self.processor.getRunningTask().getName()
class test_EventQueue(TestCase): def setUp(self): self.queue = EventQueue() self.event = prototype.JobEvent(timestamp=0, job=None) self.events = [ prototype.JobEvent(timestamp=i, job=None) for i in xrange(10) ] self.handler = _create_handler() def tearDown(self): del self.queue, self.event, self.events, self.handler def test_len_empty(self): self.assertEqual(0, len(self.queue)) def test_len_nonempty(self): for event in self.events: self.queue.add_event(event) self.assertEqual(len(self.events), len(self.queue)) def test_add_event_sanity(self): self.queue.add_event(self.event) def test_add_event_single_event(self): self.queue.add_event(self.event) self.assertEqual([self.event], self.queue.sorted_events) def test_add_same_event_fails(self): self.queue.add_event(self.event) self.assertRaises(Exception, self.queue.add_event, self.event) def test_add_event_simple(self): for event in self.events: self.queue.add_event(event) self.assertEqual(self.events, list(self.queue.sorted_events)) def test_add_event_sorting(self): random_events = _gen_random_timestamp_events() for event in random_events: self.queue.add_event(event) self.assertEqual(sorted(random_events), self.queue.sorted_events) def test_remove_event_fails_on_empty(self): self.assertRaises(Exception, self.queue.remove_event, self.event) def test_remove_event_fails_on_missing_event(self): event1 = prototype.JobEvent(0, 0) event2 = prototype.JobEvent(0, 1) assert event1 != event2 # different events self.queue.add_event(event1) self.assertRaises(Exception, self.queue.remove_event, event2) def test_remove_event_succeeds(self): self.queue.add_event(self.event) self.queue.remove_event(self.event) self.failUnless(self.queue.is_empty) def test_pop_one_job(self): self.queue.add_event(self.event) assert self.queue.pop() is self.event def test_pop_many_jobs(self): for event in self.events: self.queue.add_event(event) for event in self.events: assert self.queue.pop() is event def test_pop_empty(self): self.assertRaises(AssertionError, self.queue.pop) def test_empty_true(self): self.failUnless(self.queue.is_empty) def test_empty_false(self): self.queue.add_event(self.event) self.failIf(self.queue.is_empty) def test_add_handler_sanity(self): self.queue.add_handler(prototype.JobEvent, self.handler) self.queue.add_event(self.event) self.failIf(self.handler.called) def test_get_event_handlers_empty(self): self.assertEqual( 0, len(self.queue._get_event_handlers(prototype.JobEvent))) def test_get_event_handlers_nonempty(self): self.queue.add_handler(prototype.JobEvent, self.handler) self.assertEqual( 1, len(self.queue._get_event_handlers(prototype.JobEvent))) def test_advance_empty_queue(self): self.assertRaises(AssertionError, self.queue.advance) def test_advance_eats_event(self): self._add_event_and_advance(self.event) self.failUnless(self.queue.is_empty) def test_add_event_earlier_event_after_later_advance(self): # after handling an event with a later timestamp, adding an event with # an older timestamp should fail self._add_event_and_advance(prototype.JobEvent(timestamp=2, job="x")) self.assertRaises(Exception, self.queue.add_event, prototype.JobEvent(timestamp=1, job="x")) def test_add_event_same_timestamp_after_advance(self): # same timestamp should succeed even after an event has been handled self._add_event_and_advance(prototype.JobEvent(timestamp=2, job="x")) self.queue.add_event(prototype.JobEvent(timestamp=2, job="y")) def test_advance_one_handler_handles(self): self.queue.add_handler(prototype.JobEvent, self.handler) self._add_event_and_advance(self.event) self.failUnless(self.handler.called) def test_advance_one_handler_doesnt_handle(self): self.queue.add_handler(prototype.JobStartEvent, self.handler) self._add_event_and_advance(self.event) # JobEvent, different type self.failIf(self.handler.called) def test_advance_many_handlers(self): matching_handlers = [_create_handler() for i in xrange(5)] nonmatching_handlers = [_create_handler() for i in xrange(5)] # register handlers that should run for handler in matching_handlers: self.queue.add_handler(prototype.JobEvent, handler) # register handlers that shouldn't run with a different event type for handler in nonmatching_handlers: self.queue.add_handler(prototype.JobStartEvent, handler) self._add_event_and_advance(self.event) for handler in matching_handlers: self.failUnless(handler.called) for handler in nonmatching_handlers: self.failIf(handler.called) def test_sometimes_relevant_handler(self): self.queue.add_handler(prototype.JobEvent, self.handler) self._add_event_and_advance(prototype.JobEvent(timestamp=0, job="x")) self.failUnless(self.handler.called) self.handler.called = False self._add_event_and_advance( prototype.JobStartEvent(timestamp=1, job="x")) self.failIf(self.handler.called) self._add_event_and_advance(prototype.JobEvent(timestamp=2, job="x")) self.failUnless(self.handler.called) def _add_event_and_advance(self, event): self.queue.add_event(event) self.queue.advance()
BALL_SPAWN_POINT = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2) BALL_INIT_VEL = (-50, 0) BINDINGS = {'w': 'PADDLE_UP', 's': 'PADDLE_DOWN', 'escape': 'QUIT'} BINDINGS2 = { 'up': 'PADDLE_UP', 'down': 'PADDLE_DOWN', } pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) game_font = pygame.font.SysFont('Comic Sans MS', 30) clock = pygame.time.Clock() event_queue = EventQueue() class InputMapperProcessor(esper.Processor): def process(self): global event_queue for event in pygame.event.get(): for ent, input in world.get_component(Input): if event.type == pygame.KEYDOWN: action = self.lookup_binding(input.bindings, event.key) if action is not None: input.actions.append(action) if event.type == pygame.KEYUP: action = self.lookup_binding(input.bindings, event.key) if action is not None and action in input.actions: input.actions.remove(action)
class Simulation: """An instance of this class contains the data necessary for an entire simulation. Attributes: event_queue: the priority queue that stores events clock: the clock that must be updated whenever an event is dequeued links: dictionary of links (key is the ID, value is the Link object) flows: dictionary of flows (key is the ID, value is the Flow object) hosts: dictionary of hosts (key is the ID, value is the Host object) routers: dictionary of routers (key is the ID, value is the Router object) """ def __init__(self, links, flows, hosts, routers, verbose, fast_insteadof_reno): self.links = links self.flows = flows self.hosts = hosts self.routers = routers # Set up clocks self.clock = Clock() for item in flows.values(): item.controller.clock = self.clock for item in hosts.values(): item.clock = self.clock # Set up event schedulers self.event_queue = EventQueue(self.clock) for flow in flows.values() + links.values() + hosts.values(): flow.event_scheduler = self.event_queue for flow in flows.values(): flow.controller.event_scheduler = self.event_queue # Set up initial events for flow in flows.values(): self.event_queue.delay_event(flow.start_time, FlowWakeEvent(flow)) for host in hosts.values(): self.event_queue.delay_event(0, RoutingUpdateEvent(host)) # Set up logging self.logger = Logger(self.clock, verbose, fast_insteadof_reno) for item in flows.values() + links.values() + hosts.values( ) + routers.values(): item.set_logger(self.logger) # Set up PrintElapsedSimulationTimeEvents if not verbose: # combining this with verbose mode would be chaos. self.event_queue.delay_event( 0, PrintElapsedSimulationTimeEvent(self.clock.current_time, self.event_queue)) print "Simulation started..." """Performs a single event from the event queue.""" def step(self): try: event = self.event_queue.dequeue_next_event() event.perform() return True except Queue.Empty: return False """Determines whether all flows have been completed.""" def all_flows_finished(self): for flow in self.flows.values(): if not flow.completed(): return False return True """Repeatedly performs events from the queue until all flows have been completed.""" def run(self): while not self.all_flows_finished(): if not self.step(): sys.exit( "TCP deadlock: event queue empty but flows not complete") print "All flows finished transmitting!" print "Elapsed time in simulation world: " + str(self.clock) def __str__(self): return ( "----LINKS----\n" + "\n".join(map(str, self.links.values())) + "\n" "----FLOWS----\n" + "\n".join(map(str, self.flows.values())) + "\n" "----HOSTS----\n" + "\n".join(map(str, self.hosts.values())) + "\n" "----ROUTERS----\n" + "\n".join(map(str, self.routers.values())))
def precompute_kmppti_norslp(dataset): algorithm = 'kmmpti-norslp' logger = Logger(algorithm, 'precomputing') logger.set_time(0) logger.set_data_info(dataset[0].split('_')) print ('Precompute started') event_queue = EventQueue() data = {} data = input_csv(dataset, event_queue) event_queue.sort_queue() metadata = get_metadata(event_queue, dataset, data) data['product']['active'] = [] data['customer']['active'] = [] pandora_box = PandoraBox(metadata[4] + 1, metadata[0] + 1, data['product']['data']) dsl = DynamicSkyline(data['product'], data['customer'], metadata[3]) while not event_queue.is_empty(): event = event_queue.dequeue() if event[1] == 0: if event[3] == 0: data['product']['active'].append(event[2]) threads = [] for cust_id in data['customer']['active']: t = threading.Thread(target=compute_dsl, args=(cust_id, event[0], event[3], event[2], dsl)) threads.append(t) for t in threads: t.start() for t in threads: t.join() threads = [] for cust_id in data['customer']['active']: if 'dsl' in data['customer']['data'][cust_id].keys(): t = threading.Thread(target=update_pbox, args=(cust_id, event[0], dsl, pandora_box, data)) threads.append(t) for t in threads: t.start() for t in threads: t.join() elif event[3] == 1: threads = [] for cust_id in data['customer']['active']: if 'dsl' in data['customer']['data'][cust_id].keys(): t = threading.Thread(target=update_pbox, args=(cust_id, event[0], dsl, pandora_box, data)) threads.append(t) for t in threads: t.start() for t in threads: t.join() threads = [] for cust_id in data['customer']['active']: t = threading.Thread(target=compute_dsl, args=(cust_id, event[0], event[3], event[2], dsl)) threads.append(t) for t in threads: t.start() for t in threads: t.join() data['product']['active'].remove(event[2]) elif event[1] == 1: if event[3] == 0: data['customer']['active'].append(event[2]) dsl.customer_in(event[2], event[0]) if 'dsl' in data['customer']['data'][event[2]].keys(): pandora_box.update(data['customer']['data'][event[2]]['dsl']) dsl.update_history(event[2]) elif event[3] == 1: dsl.update(event[2], event[0]) if 'dsl' in data['customer']['data'][event[2]].keys(): pandora_box.update(data['customer']['data'][event[2]]['dsl']) dsl.update_history(event[2]) data['customer']['active'].remove(event[2]) session_name, session_file = make_session(dataset[0].split('_'), algorithm, metadata) pandora_path = session_file + '/' + session_name os.mkdir(pandora_path) pandora_box.export_csv(pandora_path) logger.set_time(1) logger.set_runtime() logger.set_mem_usage() logger.export_log() return session_name
class Simulator(object): def __init__(self, jobs, num_processors, scheduler): self.event_queue = EventQueue() self.machine = ValidatingMachine(num_processors=num_processors, event_queue=self.event_queue) self.scheduler = scheduler self.event_queue.add_handler(JobSubmissionEvent, self.handle_submission_event) self.event_queue.add_handler(JobTerminationEvent, self.handle_termination_event) for job in jobs: self.event_queue.add_event( JobSubmissionEvent(timestamp = job.submit_time, job = job) ) def run(self): while not self.event_queue.is_empty: self.event_queue.advance() def handle_submission_event(self, event): assert isinstance(event, JobSubmissionEvent) newEvents = self.scheduler.handleSubmissionOfJobEvent(event.job, event.timestamp) for event in newEvents: self.event_queue.add_event(event) def handle_termination_event(self, event): assert isinstance(event, JobTerminationEvent) newEvents = self.scheduler.handleTerminationOfJobEvent(event.job, event.timestamp) for event in newEvents: self.event_queue.add_event(event) def handle_prediction_is_over_event(self, event): assert isinstance(event, JobPredictionIsOverEvent) newEvents = self.scheduler.handlePredictionIsOverEvent(event.job, event.timestamp) for event in newEvents: self.event_queue.add_event(event)
class Simulator: def __init__(self): jsonObject = self.readData() # initialize the hosts,flows,routers,links self.queue = EventQueue(0) self.routers = {} for r in jsonObject['routers']: self.routers[r['id']] = Router(r['id']) self.hosts = {} for h in jsonObject['hosts']: self.hosts[h['id']] = Host(h['id']) self.links = {} for l in jsonObject['links']: self.links[l['id']] = Link(l) self.links[l['id']].scheduler = self.queue if (l['endpoints'][0][0] == 'H' or l['endpoints'][0][0] == 'S' or l['endpoints'][0][0] == 'T'): self.links[l['id']].pointA = self.hosts[l['endpoints'][0]] self.hosts[l['endpoints'][0]].link = self.links[l['id']] else: self.links[l['id']].pointA = self.routers[l['endpoints'][0]] self.routers[l['endpoints'][0]].links.append( self.links[l['id']]) if (l['endpoints'][1][0] == 'H' or l['endpoints'][1][0] == 'S' or l['endpoints'][1][0] == 'T'): self.links[l['id']].pointB = self.hosts[l['endpoints'][1]] self.hosts[l['endpoints'][1]].link = self.links[l['id']] else: self.links[l['id']].pointB = self.routers[l['endpoints'][1]] self.routers[l['endpoints'][1]].links.append( self.links[l['id']]) self.flows = {} for f in jsonObject['flows']: self.flows[f['id']] = Flow(f) self.flows[f['id']].event_queue = self.queue self.flows[f['id']].controller.event_scheduler = self.queue self.flows[f['id']].source = self.hosts[f['source']] self.hosts[f['source']].flow[f['id']] = self.flows[f['id']] self.flows[f['id']].destination = self.hosts[f['destination']] self.hosts[f['destination']].flow[f['id']] = self.flows[f['id']] self.queue.blind(self.routers) def readData(self): if case == 1: return json.load(file('testcase1.json')) if case == 2: return json.load(file('testcase2.json')) def run(self): self.queue.current_time = 0 for id in self.flows: self.queue.put_event(self.flows[id].start_time, FlowWakeEvent(self.flows[id])) for id in self.hosts: self.queue.put_event(5, RoutingUpdateEvent(self.hosts[id])) while (not self.queue.isEmpty()): e = self.queue.get_event() if (e.canceled == False): #print e #print clock.clk.get_time() #a=input() e.perform() def init_router_table(self): for id in self.links: self.links[id].init_router() while (not self.queue.isEmpty()): e = self.queue.get_event() if (e.canceled == False): #print e e.perform()