def scheduleNextArrival(self): shift = self.core.getCurrentShift() rand = Random() arrivalIncrement = rand.sourceIncrement(shift) arrivalEvent = Event( self, # eventCreator Constants.NEXT_ARRIVAL, # eventName self.core.currentTime, # eventSheduled self.core.currentTime + arrivalIncrement # eventTime ) return arrivalEvent
def scheduleEndService(self): operationType = self.hostedEntity.getOperationType() rand = Random() serviceIncrement = rand.processorIncrement(operationType) endServiceEvent = Event( self, # eventCreator Constants.END_SERVICE, # eventName self.core.currentTime, # eventScheduled self.core.currentTime + serviceIncrement # eventTime ) return endServiceEvent
def test_probability_distribution_source(self): processing_times = selection(self.cols['Event_Name'], lambda x: x == Constants.NEXT_ARRIVAL, self.big_matrix) processing_times = transformCols( [self.cols['Event_Scheduled'], self.cols['Event-Time']], self.cols['Current_Time'], lambda x, y: y - x, processing_times) processing_times = groupBy([self.cols['Shift']], [ self.cols['Current_Time'], self.cols['Current_Time'], self.cols['Current_Time'], self.cols['Current_Time'] ], [countA, sumA, minA, maxA], processing_times, True) processing_times = transformCols([1, 2], 1, lambda x, y: y / x, processing_times) # average processing_times = projection([0, 1, 3, 4], processing_times) self.assertEquals(len(processing_times), 1, "There should not be any shifts") shift3 = processing_times[0] self.assertGreater(shift3[2], 0, "The time should be greater or equal") random = Random() self.assertTrue( abs(shift3[2] - random.LAMBDA) < 50, "The lambda should be almost equal")
def __init__(self, parameters=None): if parameters is None: self.parameters = Parameters() else: self.parameters = parameters num_sources = Constants.DEFAULT_SOURCES num_processors = self.parameters.num_processors # Attributes initialization self.processors = [] self.sources = [] self.eventsList = PriorityQueue(0) # maxsize = 0 (infinite) self.previousTime = Constants.SIMULATION_INITIAL_TIME self.currentTime = Constants.SIMULATION_INITIAL_TIME self.idleProcessors = 0 self.serviceProcessors = 0 self.entitiesSystem = 0 self.service_per_shift = [] self.service_per_total = [] self.shift_durations = self.parameters.getParameters()[1] self.shift_next_time = self.shift_durations[0] self.shift_next_index = 1 # Instance creation self.queue = Queue(Constants.SLOTS_BUFFER) self.parking = Queue(Constants.SLOTS_QUEUE) self.random = Random() for _ in range(0, num_processors): self.processors.append(Processor(self)) for _ in range(0, num_sources): self.sources.append(Source(self)) # Dependency injection for source in self.sources: source.addOutput(self.queue) # source -> queue self.queue.addOutput(self.parking) # queue -> parking self.parking.addInput(self.queue) # parking <- queue for processor in self.processors: self.parking.addOutput(processor) # parking -> processor processor.addInput(self.parking) # processor <- parking self.output_file = None self.numberOfIdleProcessors = num_processors
def setUp(self): self.parametersObj = Parameters() self.parametersObj.WITH_SHIFTS = True rand = Random() rand.initialization()
def setWithShifts(self, value): param = Parameters() param.WITH_SHIFTS = value self.randObj = Random() self.randObj.initialization()
class TestRandom(unittest.TestCase): def setWithShifts(self, value): param = Parameters() param.WITH_SHIFTS = value self.randObj = Random() self.randObj.initialization() def setUp(self): self.setWithShifts(True) def tearDown(self): self.randObj = None def test_range_of_source_increment_ENTREGA(self): i = self.randObj.sourceIncrement(Constants.ENTREGA) self.assertGreater(i, 0, "The number returned must be greater than 0") def test_range_of_source_increment_RECOGIDA(self): i = self.randObj.sourceIncrement(Constants.RECOGIDA) self.assertGreater(i, 0, "The number returned must be greater than 0") def test_range_of_source_increment_DUALES(self): i = self.randObj.sourceIncrement(Constants.DUAL) self.assertGreater(i, 0, "The number returned must be greater than 0") def test_range_of_source_increment_NO_SHIFTS(self): self.setWithShifts(False) i = self.randObj.sourceIncrement(None) self.assertGreater(i, 0, "The number returned must be greater than 0") def test_range_of_processor_increment_ENTREGA(self): i = self.randObj.processorIncrement(Constants.ENTREGA) self.assertGreaterEqual( i, Constants.MINIMUM_TIME_ENTREGA, "The number returned must be greater than MINIMUM_TIME_ENTREGA") self.assertLessEqual( i, Constants.MAXIMUM_TIME_ENTREGA, "The number returned must be lower than MAXIMUM_TIME_ENTREGA") def test_range_of_processor_increment_RECOGIDA(self): i = self.randObj.processorIncrement(Constants.RECOGIDA) self.assertGreaterEqual( i, Constants.MINIMUM_TIME_RECOGIDA, "The number returned must be greater than MINIMUM_TIME_RECOGIDA") self.assertLessEqual( i, Constants.MAXIMUM_TIME_RECOGIDA, "The number returned must be lower than MAXIMUM_TIME_RECOGIDA") def test_range_of_processor_increment_DUALES(self): i = self.randObj.processorIncrement(Constants.DUAL) self.assertGreaterEqual( i, Constants.MINIMUM_TIME_DUAL, "The number returned must be greater than MINIMUM_TIME_DUAL") self.assertLessEqual( i, Constants.MAXIMUM_TIME_DUAL, "The number returned must be lower than MAXIMUM_TIME_DUAL") def test_range_of_processor_increment_NO_SHIFTS(self): self.setWithShifts(False) i = self.randObj.processorIncrement(None) self.assertGreaterEqual( i, Constants.MINIMUM_TIME, "The number returned must be greater than MINIMUM_TIME") self.assertLessEqual( i, Constants.MAXIMUM_TIME, "The number returned must be lower than MAXIMUM_TIME") def test_get_num_trucks_ENTREGA(self): i = self.randObj.getNumTrucks(Constants.ENTREGA) self.assertGreaterEqual( i, Constants.MINIMUM_TRUCKS_ENTREGA, "The number returned must be greater than MINIMUM_TRUCKS_ENTREGA") self.assertLessEqual( i, Constants.MAXIMUM_TRUCKS_ENTREGA, "The number returned must be lower than MAXIMUM_TRUCKS_ENTREGA") def test_get_num_trucks_RECOGIDA(self): i = self.randObj.getNumTrucks(Constants.RECOGIDA) self.assertGreaterEqual( i, Constants.MINIMUM_TRUCKS_RECOGIDA, "The number returned must be greater than MINIMUM_TRUCKS_RECOGIDA") self.assertLessEqual( i, Constants.MAXIMUM_TRUCKS_RECOGIDA, "The number returned must be lower than MAXIMUM_TRUCKS_RECOGIDA") def test_test_get_num_trucks_DUALES(self): i = self.randObj.getNumTrucks(Constants.DUAL) self.assertGreaterEqual( i, Constants.MINIMUM_TRUCKS_DUAL, "The number returned must be greater than MINIMUM_TRUCKS_DUAL") self.assertLessEqual( i, Constants.MAXIMUM_TRUCKS_DUAL, "The number returned must be lower than MAXIMUM_TRUCKS_DUAL") def test_test_get_num_trucks_NO_SHIFTS(self): self.setWithShifts(False) i = self.randObj.getNumTrucks(None) self.assertGreaterEqual( i, Constants.MINIMUM_TRUCKS, "The number returned must be greater than MINIMUM_TRUCKS") self.assertLessEqual( i, Constants.MAXIMUM_TRUCKS, "The number returned must be lower than MAXIMUM_TRUCKS") def test_ensure_there_s_one_instance(self): rand = Random() assert rand is self.randObj
def test_ensure_there_s_one_instance(self): rand = Random() assert rand is self.randObj
def setUp(self): param = Parameters() param.WITH_SHIFTS = True self.randObj = Random() self.randObj.initialization() self.coreObj = Core()
class TestIntegrationCoreOtherClasses(TestCase): def setUp(self): param = Parameters() param.WITH_SHIFTS = True self.randObj = Random() self.randObj.initialization() self.coreObj = Core() def tearDown(self): self.coreObj = None try: os.remove("./TEST.csv") except: pass try: os.remove("./TEST.stats.csv") except: pass try: os.remove("./TEST.txt") except: pass def test_startSimulation(self): source_obj = Source(self.coreObj) self.coreObj.output_file = open('./TEST.txt', "w+") self.coreObj.sources.append(source_obj) self.coreObj.startSimulation() self.coreObj.output_file.close() self.assertEquals( self.coreObj.eventsList.qsize(), 2, "The length should be two: start simulation event + first event from source" ) obj = self.coreObj.eventsList self.assertIsNotNone(obj, "The object is not none") with open('./TEST.txt', "r") as read: self.assertNotEquals(len(read.read()), 0, "The file should not be empty") def test_executeEvent_Start_Simulation(self): source_obj = Source(self.coreObj) self.coreObj.output_file = open('./TEST.txt', "w+") self.coreObj.sources.append(source_obj) self.coreObj.executeEvent(Event(eventName=Constants.START_SIMULATION)) self.coreObj.output_file.close() self.assertEquals( self.coreObj.eventsList.qsize(), 2, "The length should be two: start simulation event + first event from source" ) obj = self.coreObj.eventsList self.assertIsNotNone("The object is not none", obj) with open('./TEST.txt', "r") as read: self.assertNotEquals(len(read.read()), 0, "The file should not be empty") def test_executeEvent_OtherEvent(self): source_obj = Source(self.coreObj) self.coreObj.output_file = open('./TEST.txt', "w+") self.coreObj.sources.append(source_obj) self.coreObj.executeEvent(Event(eventName=Constants.END_SIMULATION)) self.coreObj.output_file.close() self.assertEquals( self.coreObj.eventsList.qsize(), 0, "The length should be 0 because the simulation did not start") obj = self.coreObj.eventsList self.assertIsNotNone("The object is not none", obj) with open('./TEST.txt', "r") as read: self.assertEquals(len(read.read()), 0, "The file should be empty") def test_endSimulation(self): self.coreObj.output_file = open('./TEST.txt', "w+") self.coreObj.endSimulation() self.coreObj.output_file.close() self.assertEquals( self.coreObj.eventsList.qsize(), 0, "The length should be 0 because the simulation ended") obj = self.coreObj.eventsList self.assertIsNotNone("The object is not none", obj) with open('./TEST.txt', "r") as read: self.assertNotEquals(len(read.read()), 0, "The file should not be empty") def test_run(self): param = Parameters() param.num_processors = 2 self.coreObj = Core(param) self.coreObj.output_file = open('./TEST.txt', "w+") self.coreObj.parameters.output_file = "TEST" self.coreObj.run() self.coreObj.output_file.close() self.assertEquals( self.coreObj.eventsList.qsize(), 0, "The length should be 0 because the simulation ended") obj = self.coreObj.eventsList self.assertIsNotNone("The object is not none", obj) with open('./TEST.stats.csv', "r") as read: self.assertNotEquals(len(read.read()), 0, "The file should not be empty") def test_getCurrentShift(self): source_obj = Source(self.coreObj) self.coreObj.sources.append(source_obj) self.coreObj.output_file = open('./TEST.txt', "w+") self.coreObj.executeEvent(Event(eventName=Constants.START_SIMULATION)) self.coreObj.output_file.close() self.assertEquals(self.coreObj.getCurrentShift(), Constants.ENTREGA, "The first shift should be of type ENTREGA")
def setWithShifts(cls, value): param = Parameters() param.WITH_SHIFTS = value cls.randObj = Random() cls.randObj.initialization()