Ejemplo n.º 1
0
class ChiefCoordinator:
    def __init__(self, teams_path, table_paths, history_path,
                 additional_config):
        self.teams_path = teams_path
        self.table_paths = table_paths
        self.history_path = history_path

        with open(teams_path, newline='') as teams_file:
            lines = teams_file.readlines()
            assert (len(lines) >= 1)
            self.teams = [team.strip() for team in lines[0].split(',')]
        self.history_manager = HistoryManager(history_path)

        self.tables = OrderedDict()
        for name in table_paths:
            self.tables[name] = Table(table_paths[name], self.history_manager,
                                      additional_config)
            assert (name == self.tables[name].name)
        self.skipped_positions = additional_config['SKIPPED_POSITIONS']
        self.start_time = additional_config['START_TIME']
        self.maximum_time = additional_config['MAXIMUM_TIME']
        self.break_times = additional_config['BREAK_TIMES']

    # Saves the current states of tables and history to the given files.
    # The default files are the ones passed to the constructor.
    def dump_to_file(self, table_paths=None, history_path=None):
        if table_paths is None: table_paths = self.table_paths
        if history_path is None: history_path = self.history_path
        self.history_manager.dump_to_file(history_path)
        for name in table_paths:
            self.tables[name].dump_to_file(table_paths[name])
Ejemplo n.º 2
0
    def test_table(self):
        history = HistoryManager(cohmo.app.config['HISTORY_FILE_PATH'])
        table = Table(cohmo.app.config['TABLE_FILE_PATHS']['T2'], history,
                      app.config)
        self.assertEqual(table.queue, ['ITA', 'ENG', 'IND'])
        self.assertEqual(table.status, TableStatus.IDLE)
        self.assertTrue(table.switch_to_calling())
        self.assertEqual(table.status, TableStatus.CALLING)
        self.assertTrue(table.switch_to_idle())
        self.assertFalse(table.switch_to_idle())
        self.assertEqual(table.status, TableStatus.IDLE)
        self.assertTrue(table.start_coordination('IND'))
        self.assertEqual(table.status, TableStatus.CORRECTING)
        self.assertEqual(table.current_coordination_team, 'IND')
        self.assertGreater(table.current_coordination_start_time, 100)
        self.assertTrue(table.remove_from_queue('ENG'))
        self.assertFalse(table.remove_from_queue('KOR'))
        self.assertEqual(table.queue, ['ITA', 'IND'])

        # Constructing Table from the file written by dump_to_file.
        table = Table(cohmo.app.config['TABLE_FILE_PATHS']['T2'], history,
                      app.config)
        self.assertEqual(table.queue, ['ITA', 'IND'])
        self.assertEqual(table.status, TableStatus.CORRECTING)
        self.assertEqual(table.current_coordination_team, 'IND')
        self.assertFalse(table.switch_to_calling())
        self.assertFalse(table.switch_to_idle())
        self.assertFalse(table.start_coordination('ITA'))
        self.assertEqual(
            len(history.get_corrections({
                'table': 'T2',
                'team': 'IND'
            })), 0)
        self.assertTrue(table.finish_coordination())
        self.assertEqual(table.status, TableStatus.IDLE)
        self.assertEqual(
            len(history.get_corrections({
                'table': 'T2',
                'team': 'IND'
            })), 1)

        # Testing the queue modifying APIs.
        self.assertTrue(table.add_to_queue('ENG'))
        self.assertFalse(table.add_to_queue('ITA'))
        self.assertTrue(table.add_to_queue('KOR', 0))
        self.assertTrue(table.add_to_queue('CHN', 2))
        self.assertEqual(table.queue, ['KOR', 'ITA', 'CHN', 'IND', 'ENG'])
        self.assertFalse(table.remove_from_queue('FRA'))
        self.assertTrue(table.remove_from_queue('ITA'))
        self.assertFalse(table.remove_from_queue('ITA'))
        self.assertFalse(table.swap_teams_in_queue('CHN', 'CHN'))
        self.assertFalse(table.swap_teams_in_queue('FRA', 'KOR'))
        self.assertTrue(table.swap_teams_in_queue('KOR', 'IND'))
        self.assertEqual(table.queue, ['IND', 'CHN', 'KOR', 'ENG'])
Ejemplo n.º 3
0
class ChiefCoordinator:
    def __init__(self, teams_path, table_paths, history_path,
                 additional_config):
        self.teams_path = teams_path
        self.table_paths = table_paths
        self.history_path = history_path

        with open(teams_path, newline='') as teams_file:
            lines = teams_file.readlines()
            assert (len(lines) >= 1)
            self.teams = [team.strip() for team in lines[0].split(',')]
        self.history_manager = HistoryManager(history_path)

        self.tables = OrderedDict()
        for name in table_paths:
            self.tables[name] = Table(table_paths[name], self.history_manager,
                                      additional_config)
            assert (name == self.tables[name].name)
        self.skipped_positions = additional_config['SKIPPED_POSITIONS']
        self.start_time = additional_config['START_TIME']
        self.maximum_time = additional_config['MAXIMUM_TIME']
        self.break_times = additional_config['BREAK_TIMES']

    # Saves the current states of tables and history to the given files.
    # The default files are the ones passed to the constructor.
    def dump_to_file(self, table_paths=None, history_path=None):
        if table_paths is None: table_paths = self.table_paths
        if history_path is None: history_path = self.history_path
        self.history_manager.dump_to_file(history_path)
        for name in table_paths:
            self.tables[name].dump_to_file(table_paths[name])

    # Returns a list of all teams that are currently in a coordination session are being called.
    # They are unavailable for being called by other teams.
    def get_unavailable_teams(self):
        current_teams = [
            table.current_coordination_team for table in self.tables.values()
            if table.status == 1
        ]
        calling_teams = [
            table.queue[0] for table in self.tables.values()
            if table.status == 0
        ]
        return current_teams + calling_teams
Ejemplo n.º 4
0
 def test_operations_num(self):
     history = HistoryManager(cohmo.app.config['HISTORY_FILE_PATH'])
     table = Table(cohmo.app.config['TABLE_FILE_PATHS']['T2'], history,
                   app.config)
     ops = history.operations_num
     self.assertTrue(table.start_coordination('ITA'))
     self.assertAlmostEqual(history.operations_num, ops + 1)
     self.assertTrue(table.finish_coordination())
     self.assertAlmostEqual(history.operations_num, ops + 2)
     self.assertTrue(table.add_to_queue('CHN'))
     self.assertAlmostEqual(history.operations_num, ops + 3)
Ejemplo n.º 5
0
    def __init__(self, teams_path, table_paths, history_path,
                 additional_config):
        self.teams_path = teams_path
        self.table_paths = table_paths
        self.history_path = history_path

        with open(teams_path, newline='') as teams_file:
            lines = teams_file.readlines()
            assert (len(lines) >= 1)
            self.teams = [team.strip() for team in lines[0].split(',')]
        self.history_manager = HistoryManager(history_path)

        self.tables = OrderedDict()
        for name in table_paths:
            self.tables[name] = Table(table_paths[name], self.history_manager,
                                      additional_config)
            assert (name == self.tables[name].name)
        self.skipped_positions = additional_config['SKIPPED_POSITIONS']
        self.start_time = additional_config['START_TIME']
        self.maximum_time = additional_config['MAXIMUM_TIME']
        self.break_times = additional_config['BREAK_TIMES']
Ejemplo n.º 6
0
    def test_history(self):
        history = HistoryManager(cohmo.app.config['HISTORY_FILE_PATH'])
        self.assertTrue(history.add('ITA', 'T2', 10, 20))
        self.assertTrue(history.add('FRA', 'T8', 20, 30))
        self.assertTrue(history.add('KOR', 'T5', 15, 30))
        self.assertFalse(history.delete('ID_NOT_EXISTENT'))
        self.assertEqual(len(history.get_corrections({'identifier': 'ID2'})),
                         1)
        self.assertTrue(history.delete('ID2'))
        self.assertEqual(history.get_corrections({'identifier': 'ID2'}), [])
        self.assertEqual(len(history.corrections), 5)

        # Constructing HistoryManager from the file written by dump_to_file.
        history = HistoryManager(cohmo.app.config['HISTORY_FILE_PATH'])
        self.assertEqual(len(history.corrections), 5)
        self.assertEqual(history.corrections[2].table, 'T2')
        self.assertEqual(history.corrections[2].team, 'ITA')
        self.assertTrue(history.add('ITA', 'T5', 20, 30))

        # Testing various calls to get_corrections.
        history = HistoryManager(cohmo.app.config['HISTORY_FILE_PATH'])
        self.assertEqual(history.get_corrections({'table': 'NOWAY'}), [])
        self.assertEqual(len(history.get_corrections({'table': 'T5'})), 3)
        self.assertEqual(history.get_corrections({'identifier': 'ID2'}), [])
        self.assertEqual(len(history.get_corrections({'table': 'T2'})), 2)
        self.assertEqual(len(history.get_corrections({'table': 'T8'})), 1)
        self.assertEqual(
            len(history.get_corrections({
                'table': 'T5',
                'team': 'KOR'
            })), 1)
        self.assertEqual(
            history.get_corrections({
                'table': 'T5',
                'team': 'ROK'
            }), [])
        self.assertEqual(
            len(history.get_corrections({'start_time': (-100, 100)})), 6)
        self.assertEqual(len(history.get_corrections({'end_time': (15, 25)})),
                         2)
Ejemplo n.º 7
0
    def test_get_expected_duration(self):
        cohmo.app.config['NUM_SIGN_CORR'] = 2
        cohmo.app.config['APRIORI_DURATION'] = 3
        cohmo.app.config['MINIMUM_DURATION'] = 2
        cohmo.app.config['MAXIMUM_DURATION'] = 8
        cohmo.app.config['START_TIME'] = 0
        tmp_maximum_time = cohmo.app.config['MAXIMUM_TIME']
        cohmo.app.config['MAXIMUM_TIME'] = 25
        cohmo.app.config['BREAK_TIMES'] = [[14, 16]]
        history = HistoryManager(cohmo.app.config['HISTORY_FILE_PATH'])
        table = Table(cohmo.app.config['TABLE_FILE_PATHS']['T2'], history,
                      app.config)

        # Testing the basic behaviour.
        self.assertEqual(history.corrections[0].duration(), 5)
        self.assertEqual(len(history.get_corrections({'table': 'T2'})), 1)
        self.assertAlmostEqual(table.get_expected_duration(), 4)  # 10
        self.assertTrue(table.start_coordination('ITA'))  # 3, 10
        self.assertAlmostEqual(table.get_expected_duration(), 4)
        self.assertTrue(table.finish_coordination())  # 4, 10
        self.assertAlmostEqual(table.get_expected_duration(), 3)
        self.assertEqual(len(history.get_corrections({'table': 'T2'})), 2)

        # Testing the imposition on the maximum_time.
        self.assertTrue(table.start_coordination('ITA'))  # 2, 10
        self.assertTrue(table.finish_coordination())  # 11, 10
        self.assertAlmostEqual(table.get_expected_duration(), 13 / 3)

        # Testing the case when the history is empty.
        self.assertEqual(len(history.get_corrections({'table': 'T2'})), 3)
        corrections = history.get_corrections({'table': 'T2'})
        self.assertTrue(history.delete(corrections[0].id))
        self.assertTrue(history.delete(corrections[1].id))
        self.assertTrue(history.delete(corrections[2].id))
        self.assertEqual(len(history.get_corrections({'table': 'T2'})), 0)

        # Recomputing the expected_duration and deleting almost all the queue.
        table.compute_expected_duration()  # 10
        self.assertAlmostEqual(table.get_expected_duration(), 3)
        self.assertTrue(table.remove_from_queue('ENG'))  # 10
        self.assertTrue(table.remove_from_queue('IND'))  # 10
        self.assertAlmostEqual(table.get_expected_duration(), 3)

        # Testing the maximum_duration.
        self.assertTrue(table.start_coordination('ITA'))  # 2, 10
        self.assertTrue(table.finish_coordination())  # 11, 10
        self.assertAlmostEqual(table.get_expected_duration(), 6)

        self.assertTrue(table.start_coordination('ITA'))  # 2, 10
        self.assertTrue(table.finish_coordination())  # 11, 10
        self.assertAlmostEqual(table.get_expected_duration(), 8)

        # Clearing the history again.
        self.assertEqual(len(history.get_corrections({'table': 'T2'})), 2)
        corrections = history.get_corrections({'table': 'T2'})
        self.assertTrue(history.delete(corrections[0].id))
        self.assertTrue(history.delete(corrections[1].id))

        # Testing the minimum_duration.
        self.assertTrue(table.start_coordination('ITA'))  # 2, 10
        self.assertTrue(table.finish_coordination())  # 3, 10
        self.assertAlmostEqual(table.get_expected_duration(), 2)

        self.assertTrue(table.start_coordination('ITA'))  # 2, 10
        self.assertTrue(table.finish_coordination())  # 3, 10
        self.assertAlmostEqual(table.get_expected_duration(), 2)

        cohmo.app.config['MAXIMUM_TIME'] = tmp_maximum_time