def test_attach_process(): pm = ProcessManager() class TestProcess(Process): def definition(self, user, **kwargs): bene = True pm.attach_process(TestProcess) assert len(pm._store) == 1
def test_create_user(): pm = ProcessManager() class TestProcess1(Process): def definition(self, user, **kwargs): bene = True pm.attach_process(TestProcess1) em = EventManager(pm) em.create_user(f'user_test', instant=0) assert len(em.um._store) == 1
def test_get_user(): pm = ProcessManager() class TestProcess1(Process): def definition(self, user, **kwargs): bene = True pm.attach_process(TestProcess1) em = EventManager(pm) em.create_user(f'user_test', instant=0) assert isinstance(em.um.get_user('user_test'), User)
def test_run(): pm = ProcessManager() class TestProcess1(Process): def definition(self, user, **kwargs): yield user.waits(5) print('va bene') pm.attach_process(TestProcess1) em = EventManager(pm) em.create_user(f'user_test') em.run()
def test_set_flow_only_one_initial(): pm = ProcessManager() class TestProcess1(Process): def definition(self, user, **kwargs): va = True pm.attach_process(TestProcess1) em = EventManager(pm) assert (pm.flow['from'] == 'initial').sum() == 1
def test_set_flow_initial_final(): pm = ProcessManager() class TestProcess1(Process): def definition(self, user, **kwargs): va = True pm.attach_process(TestProcess1) em = EventManager(pm) assert pm.flow['from'][0] == 'initial' and pm.flow['to'][1] == 'final'
def test_request(): pm = ProcessManager() dummy_resource = ['test1'] pm.create_resource(dummy_resource) class TestProcess1(Process): def definition(self, user, **kwargs): assert user.requests('test1')[0].triggered == True yield user.waits(1) pm.attach_process(TestProcess1) em = EventManager(pm) em.create_user('user_test') em.run()
def test_release(): pm = ProcessManager() dummy_resource = ['test1'] pm.create_resource(dummy_resource) class TestProcess1(Process): def definition(self, user, **kwargs): user.requests('test1') yield user.waits(1.2) user.releases('test1') pm.attach_process(TestProcess1) em = EventManager(pm) em.create_user('user_test') em.run()
def test_set_flow_need_sequence(): pm = ProcessManager() class TestProcess1(Process): def definition(self, user, **kwargs): va = True pm.attach_process(TestProcess1) class TestProcess2(Process): def definition(self, user, **kwargs): bene = True pm.attach_process(TestProcess2) with pytest.raises(ValueError): em = EventManager(pm)
def test_unblock_flow(): pm = ProcessManager() class TestProcess1(Process): def definition(self, user, **kwargs): va = True pm.attach_process(TestProcess1) class TestProcess2(Process): def definition(self, user, **kwargs): bene = True pm.attach_process(TestProcess2) pm.block_flow() pm.unblock_flow() pm.set_flow(sequence=('TestProcess1', 'TestProcess2'))
def test_set_flow_order_processes(): pm = ProcessManager() class TestProcess1(Process): def definition(self, user, **kwargs): va = True pm.attach_process(TestProcess1) class TestProcess2(Process): def definition(self, user, **kwargs): bene = True pm.attach_process(TestProcess2) pm.set_flow(sequence=('TestProcess1', 'TestProcess2')) assert pm.flow['from'][1] == 'TestProcess1' and pm.flow['to'][ 1] == 'TestProcess2'
def test_waits_unlimited_patience(): pm = ProcessManager() dummy_resource = ['test1', 'test2'] pm.create_resource(dummy_resource) class TestProcess1(Process): def definition(self, user, **kwargs): require = 'any' arrived = self.env.now yield user.waits(dummy_resource, require=require) now = self.env.now assert now - arrived == 0 pm.attach_process(TestProcess1) em = EventManager(pm) em.create_user('user_test') em.run()
def test_wait_for_time(): pm = ProcessManager() df = pd.DataFrame({'integer_test': [1], 'float_test': [1.1]}) class TestProcess(Process): def definition(self, user): yield user.waits(1) yield user.waits(1.1) yield user.waits(df['integer_test'].values[0]) yield user.waits(df['float_test'].values[0]) yield user.waits(datetime(2021, 1, 1)) yield user.waits(timedelta(minutes=1)) user.set_checkpoint('Finished') pm.attach_process(TestProcess) em = EventManager(pm) em.create_user('user_test') em.run() assert em.checkpoints['instant'][0] == 1609459264.2
def test_block_flow(): pm = ProcessManager() class TestProcess1(Process): def definition(self, user, **kwargs): va = True pm.attach_process(TestProcess1) class TestProcess2(Process): def definition(self, user, **kwargs): bene = True pm.attach_process(TestProcess2) pm.block_flow() with pytest.raises(ValueError): assert pm.set_flow(sequence=('TestProcess1', 'TestProcess2'))
def test_reset_flow(): pm = ProcessManager() class TestProcess1(Process): def definition(self, user, **kwargs): va = True pm.attach_process(TestProcess1) class TestProcess2(Process): def definition(self, user, **kwargs): bene = True pm.attach_process(TestProcess2) pm.set_flow(sequence=('TestProcess1', 'TestProcess2')) pm.reset_flow() assert pm.flow.empty == True # assert if dataframe is empty or not
def test_set_flow_cant_change_flow(): pm = ProcessManager() class TestProcess1(Process): def definition(self, user, **kwargs): va = True pm.attach_process(TestProcess1) class TestProcess2(Process): def definition(self, user, **kwargs): bene = True pm.attach_process(TestProcess2) pm.set_flow(sequence=['TestProcess1', 'TestProcess2']) em = EventManager(pm) with pytest.raises(ValueError): pm.set_flow(sequence=['TestProcess2', 'TestProcess1'])
time_discussing = 12.0 td = random.expovariate(1.0 / time_discussing) # yield as long as the customer is discussing at the counter yield user.waits(td) # release the resource user.releases('counters') user.set_checkpoint('Finished') else: # release the resource user.releases('counters') # customer reneged user.set_checkpoint('Reneged') # store process into process Manager pm.attach_process(CustomerWaitCounter) # create event manager em = EventManager(pm) for i in range(NEW_CUSTOMERS): # create new customer t = random.expovariate(1.0 / INTERVAL_CUSTOMERS) em.create_user(f'Customer {i}', instant=t) # random.seed(RANDOM_SEED) if __name__ == '__main__': # execute the program em.run() print(em.checkpoints) print(pm.get_resource('counters').usage)
# Create car wash with NUM_MACHINE machines and attach car wash process pm.create_resource('machines', capacity=NUM_MACHINES) class CarWash(Process): def definition(self, user): # Car arrives and requests the use of a machine yield user.waits('machines') # Car enters the car wash and spends WASHTIME minutes inside yield user.waits(timedelta(minutes=WASHTIME)) # Car leaves the car wash and releases the machine for the next car to use user.set_checkpoint(f'Left the carwash. {random.randint(50, 99)}% of dirt removed.') user.releases('machines') pm.attach_process(CarWash) # Create event stream em = EventManager(pm) now = datetime.now() times = np.concatenate([np.zeros(3), np.random.randint(T_INTER-2, T_INTER+2, int(SIM_TIME/(T_INTER-2)))]).cumsum() for i, t in enumerate(times): em.create_user(f'Car {i}', instant=now+timedelta(minutes=t)) if __name__ == '__main__': # Execute! em.run(until=now+timedelta(minutes=SIM_TIME)) print(em.checkpoints) print(pm.get_resource('machines').usage)
self.get_resources(by_properties={ 'type': 'dock', 'station': cyclist.to_station }), which='any', ) dest_dock = response.events[0].resource bike.set_checkpoint(f'Docked on {dest_dock.name} at {dest_dock.station}') # Finish cycle cyclist.releases(bike_resource) cyclist.set_checkpoint( f'Left {bike_resource.name} at {dest_dock.station}' ) pm.attach_process(IncludeBike) pm.attach_process(Cycle) pm.set_flow(initial_process='IncludeBike') pm.set_flow(final_process='IncludeBike') pm.set_flow(initial_process='Cycle') pm.set_flow(final_process='Cycle') em = EventManager(pm) # Custom Resource class for bikes class BikeResource(Resource): @property def station(self): # Bike-user and bike-resource have the same name dock = self.rm.get_resources(by_user=self.name) if len(dock) == 0: