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_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_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_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_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_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_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'])
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_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_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_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()
import random from chronon import ProcessManager, EventManager, Process # RANDOM_SEED = 42 NEW_CUSTOMERS = 7 # Total number of customers NUMBER_OF_COUNTERS = 1 # Total number of counters INTERVAL_CUSTOMERS = 3.0 # Generate new customers roughly every x seconds MIN_PATIENCE = 1 # Min. customer patience MAX_PATIENCE = 5 # Max. customer patience # create process manager pm = ProcessManager() # create resource pm.create_resource('counters', capacity=NUMBER_OF_COUNTERS) # process definition class CustomerWaitCounter(Process): def definition(self, user): # time when customer arrives arrive = self.env.now user.set_checkpoint('Arrived') # amount of patience a customer can have patience = random.uniform(MIN_PATIENCE, MAX_PATIENCE) # waiting... yield user.waits('counters', patience=patience) # calculate time waited since customer arrived wait = self.env.now - arrive if wait < patience: # clients go to the counter
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_create_resource(): pm = ProcessManager() dummy_resource = ['test1', 'test2'] pm.create_resource(dummy_resource) assert len(pm.rm._store) == 2
import numpy as np import pandas as pd import random from chronon import ProcessManager, EventManager, Process, Resource STATIONS = ['Station A', 'Station B', 'Station C'] TARGET_OCCUPATION = 0.7 N_CYCLISTS = 7 pm = ProcessManager() # Defining distances between stations station_x = np.array([round(random.uniform(0, 30), 2) for s in STATIONS]) distances = abs(station_x.reshape(-1, 1) - station_x) # Bike process class IncludeBike(Process): def definition(self, bike): yield bike.waits(bike.initial_dock) # Cyclist process class Cycle(Process): def definition(self, cyclist): # Request a bike-as-resource cyclist.set_checkpoint(f'Requested a bike at {cyclist.from_station}') response = yield cyclist.waits( self.get_resources(by_properties={'type': 'bike'}), which='any', having={'station': cyclist.from_station} )
import random from datetime import timedelta, datetime from chronon import ProcessManager, EventManager, Process RANDOM_SEED = 42 NUM_MACHINES = 2 # Number of machines in the carwash WASHTIME = 5 # Minutes it takes to clean a car T_INTER = 3 # Create a car every ~T_INTER minutes SIM_TIME = 25 # Simulation time in minutes N_INITIAL = 1 # Number of cars in the car wash at the start # Instantiate Process Manager random.seed(RANDOM_SEED) pm = ProcessManager() # 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.')