Example #1
0
    def test_subject_behaviour_cycle_frequency(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()

        class MySubject(Subject):
            behaviours_classes = [MyCycledSubjectBehaviour]

        simulation = Simulation(config)
        my_subject = MySubject(config, simulation)
        subjects = Subjects(simulation=simulation)
        subjects.append(my_subject)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )

        # Cycle 0: behaviour IS executed
        cycle_manager.current_cycle = 0
        results_by_subjects = cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert results_by_subjects

        # Cycle 1: behaviour IS NOT executed
        cycle_manager.current_cycle = 1
        results_by_subjects = cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert not results_by_subjects
Example #2
0
    def test_subject_behaviour_produce_data(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()

        class MySubject(Subject):
            behaviours_classes = [MySubjectBehaviour]

        simulation = Simulation(config)
        my_subject = MySubject(config, simulation)
        subjects = Subjects(simulation=simulation)
        subjects.append(my_subject)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )
        results_by_subjects = cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert results_by_subjects
        assert id(my_subject) in results_by_subjects
        assert MySubjectBehaviour in results_by_subjects[id(my_subject)]
        assert 'bar' in results_by_subjects[id(my_subject)][MySubjectBehaviour]
        assert 142 == results_by_subjects[id(my_subject)][MySubjectBehaviour]['bar']
Example #3
0
    def test_mechanism_not_called_if_no_subject_behavior(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()
        global called
        called = 0

        class MySubjectMechanism(SubjectMechanism):
            def run(self):
                global called
                called += 1
                return {'foo': 42}

        class MySubject(Subject):
            behaviours_classes = []

        simulation = Simulation(config)
        my_subject = MySubject(config, simulation)
        subjects = Subjects(simulation=simulation)
        subjects.append(my_subject)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )
        cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert called == 0
Example #4
0
    def test_mechanism_not_called_if_subject_behavior_timebase_not_active_yet(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()
        global called
        called = 0

        class MySubjectMechanism(SubjectMechanism):
            def run(self):
                global called
                called += 1
                return {'foo': 42}

        class MySubjectBehaviour1(SubjectBehaviour):
            use = [MySubjectMechanism]

            @property
            def seconds_frequency(self):
                return 1.0

            def run(self, data):
                self.last_execution_time = time.time()
                return {'bar': data[MySubjectMechanism]['foo'] + 100}

        class MySubject(Subject):
            behaviours_classes = [MySubjectBehaviour1]

        simulation = Simulation(config)
        my_subject = MySubject(config, simulation)
        subjects = Subjects(simulation=simulation)
        subjects.append(my_subject)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )

        with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 0)):
            cycle_manager._job_subjects(worker_id=0, process_count=1)
            assert called == 1

        with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 0, 500000)):
            cycle_manager._job_subjects(worker_id=0, process_count=1)
            assert called == 1

        with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 0, 700000)):
            cycle_manager._job_subjects(worker_id=0, process_count=1)
            assert called == 1

        with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 1, 500000)):
            cycle_manager._job_subjects(worker_id=0, process_count=1)
            assert called == 2
Example #5
0
    def test_mechanism_not_called_if_subject_behavior_cycled_not_active_yet(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()
        global called
        called = 0

        class MySubjectMechanism(SubjectMechanism):
            def run(self):
                global called
                called += 1
                return {'foo': 42}

        class MySubjectBehaviour1(SubjectBehaviour):
            use = [MySubjectMechanism]

            @property
            def cycle_frequency(self):
                return 2

            def run(self, data):
                return {'bar': data[MySubjectMechanism]['foo'] + 100}

        class MySubject(Subject):
            behaviours_classes = [MySubjectBehaviour1]

        simulation = Simulation(config)
        my_subject = MySubject(config, simulation)
        subjects = Subjects(simulation=simulation)
        subjects.append(my_subject)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )

        cycle_manager.current_cycle = 0
        cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert called == 1

        cycle_manager.current_cycle = 1
        cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert called == 1

        cycle_manager.current_cycle = 2
        cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert called == 2

        cycle_manager.current_cycle = 3
        cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert called == 2
Example #6
0
    def test_mechanism_not_called_if_no_simulation_behavior(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()
        global called
        called = 0

        class MySimulationMechanism(SimulationMechanism):
            def run(self, process_number: int = None, process_count: int = None):
                global called
                called += 1
                return {'foo': 42}

        class MySimulation(Simulation):
            pass

        simulation = MySimulation(config)
        subjects = Subjects(simulation=simulation)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )
        cycle_manager._job_simulation(worker_id=0, process_count=1)
        assert called == 0
Example #7
0
    def test_simulation_behaviour_cycle_frequency(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()

        class MyCycledSimulation(Simulation):
            behaviours_classes = [MyCycledSimulationBehaviour]

        simulation = MyCycledSimulation(config)
        subjects = Subjects(simulation=simulation)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )

        # Cycle 0: behaviour IS executed
        cycle_manager.current_cycle = 0
        data = cycle_manager._job_simulation(worker_id=0, process_count=1)
        assert data

        # Cycle 1: behaviour IS NOT executed
        cycle_manager.current_cycle = 1
        data = cycle_manager._job_simulation(worker_id=0, process_count=1)
        assert not data
Example #8
0
    def test_mechanism_called_once_for_multiple_subject_behaviors(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()
        global called
        called = 0

        class MySubjectMechanism(SubjectMechanism):
            def run(self):
                global called
                called += 1
                return {'foo': 42}

        class MySubjectBehaviour1(SubjectBehaviour):
            use = [MySubjectMechanism]

            def run(self, data):
                return {'bar': data[MySubjectMechanism]['foo'] + 100}

        class MySubjectBehaviour2(SubjectBehaviour):
            use = [MySubjectMechanism]

            def run(self, data):
                return {'bar': data[MySubjectMechanism]['foo'] + 100}

        class MySubject(Subject):
            behaviours_classes = [MySubjectBehaviour1, MySubjectBehaviour2]

        simulation = Simulation(config)
        my_subject = MySubject(config, simulation)
        subjects = Subjects(simulation=simulation)
        subjects.append(my_subject)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )
        cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert called == 1
Example #9
0
    def test_subject_behaviour_seconds_frequency(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()

        class MySubject(Subject):
            behaviours_classes = [MyTimedSubjectBehaviour]

        simulation = Simulation(config)
        my_subject = MySubject(config, simulation)
        subjects = Subjects(simulation=simulation)
        subjects.append(my_subject)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )

        # Thirst time, behaviour IS executed
        with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 0)):
            data = cycle_manager._job_subjects(worker_id=0, process_count=1)
            assert data
            assert id(my_subject) in data
            assert data[id(my_subject)]

        # Less second after: NOT executed
        with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 0, 500000)):
            data = cycle_manager._job_subjects(worker_id=0, process_count=1)
            assert not data

        # Less second after: NOT executed
        with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 0, 700000)):
            data = cycle_manager._job_subjects(worker_id=0, process_count=1)
            assert not data

        # Less second after: IS executed
        with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 1, 500000)):
            data = cycle_manager._job_subjects(worker_id=0, process_count=1)
            assert data
Example #10
0
    def test_subject_behavior_not_called_if_no_more_subjects(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()

        class MySubject(Subject):
            behaviours_classes = [MySubjectBehaviour]

        simulation = Simulation(config)
        my_subject = MySubject(config, simulation)
        subjects = Subjects(simulation=simulation)
        subjects.append(my_subject)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )
        results_by_subjects = cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert results_by_subjects
        assert id(my_subject) in results_by_subjects
        assert results_by_subjects[id(my_subject)]

        # If we remove subject, no more data generated
        subjects.remove(my_subject)
        results_by_subjects = cycle_manager._job_subjects(worker_id=0, process_count=1)
        assert not results_by_subjects
Example #11
0
    def test_terminal_as_main_process(self):
        shared.reset()
        config = Config()
        simulation = Simulation(config)
        simulation.subjects = Subjects(simulation=simulation)
        cycle_manager = CycleManager(
            config=config,
            simulation=simulation,
        )

        global terminal_pid
        global core_pid
        terminal_pid = 0
        core_pid = 0

        class MyMainTerminal(Terminal):
            main_process = True

            def run(self):
                global terminal_pid
                terminal_pid = os.getpid()

        terminal = MyMainTerminal(config)

        class MyCore(Core):
            def _end_cycle(self):
                self._continue = False
                global core_pid
                core_pid = os.getpid()

        core = MyCore(
            config=config,
            simulation=simulation,
            cycle_manager=cycle_manager,
            terminal_manager=TerminalManager(
                config=config,
                terminals=[terminal],
            ),
        )
        core.run()
        core.main_process_terminal.core_process.terminate()
        cycle_manager.stop()

        assert terminal_pid == os.getpid()
        assert core_pid == 0  # because changed in other process
Example #12
0
    def test_simulation_behaviour_produce_data(
        self,
        do_nothing_process_manager: ProcessManager,
    ):
        shared.reset()
        simulation = MySimulation(config)
        subjects = Subjects(simulation=simulation)
        simulation.subjects = subjects

        cycle_manager = CycleManager(
            config,
            simulation=simulation,
            process_manager=do_nothing_process_manager,
        )
        data = cycle_manager._job_simulation(worker_id=0, process_count=1)
        assert data
        assert MySimulationBehaviour in data
        assert 'bar' in data[MySimulationBehaviour]
        assert 142 == data[MySimulationBehaviour]['bar']