Exemplo n.º 1
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
Exemplo n.º 2
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
Exemplo n.º 3
0
    def test_new_subject(self):
        shared.reset()
        config = Config({'core': {'use_x_cores': 1}})

        simulation = Simulation(config)
        subjects = MySubjects(simulation=simulation)
        simulation.subjects = subjects

        for i in range(3):
            subjects.append(MySubject(config, simulation=simulation))

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

        events = cycle_manager.next()

        assert 3 == len(events)
        event_values = [e.value for e in events]
        assert all([s.id * 2 in event_values for s in subjects])

        subjects.append(MySubject(config, simulation=simulation))
        events = cycle_manager.next()
        cycle_manager.stop()

        assert 4 == len(events)
        event_values = [e.value for e in events]
        assert all([s.id * 2 in event_values for s in subjects])
Exemplo n.º 4
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
Exemplo n.º 5
0
    def test_proximity_mechanism_with_one(self):
        shared.reset()
        simulation = XYZSimulation(Config())
        subject = MySubject(Config(), simulation, position=(0, 0, 0))
        other_subject = MySubject(Config(), simulation, position=(5, 0, 0))

        simulation.subjects = XYZSubjects(
            [subject, other_subject],
            simulation=simulation,
        )
        simulation.subjects.auto_expose = False

        proximity_mechanism = MyProximityMechanism(
            config=Config(),
            simulation=simulation,
            subject=subject,
        )

        assert 5 == proximity_mechanism.get_distance_of(
            position=subject.position,
            subject=other_subject,
        )
        assert [{
            'subject_id': other_subject.id,
            'direction': 90.0,
            'distance': 5.0,
        }] == proximity_mechanism.run()
Exemplo n.º 6
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
Exemplo n.º 7
0
    def test_proximity_mechanism_excluding(self):
        shared.reset()
        simulation = XYZSimulation(Config())

        subject = MySubject(Config(), simulation, position=(0, 0, 0))
        other_subject = MySubject(Config(), simulation, position=(11, 0, 0))

        simulation.subjects = XYZSubjects(
            [subject, other_subject],
            simulation=simulation,
        )
        simulation.subjects.auto_expose = False

        proximity_mechanism = MyProximityMechanism(
            config=Config(),
            simulation=simulation,
            subject=subject,
        )

        assert 11 == proximity_mechanism.get_distance_of(
            position=subject.position,
            subject=other_subject,
        )
        # other_subject is to far away
        assert [] == proximity_mechanism.run()
Exemplo n.º 8
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']
Exemplo n.º 9
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
Exemplo n.º 10
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
Exemplo n.º 11
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
Exemplo n.º 12
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
Exemplo n.º 13
0
    def test_simulation_events(self):
        shared.reset()
        config = Config({'core': {'use_x_cores': 2}})

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

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

        events = cycle_manager.next()
        cycle_manager.stop()

        assert 1 == len(events)
        assert events[0].value == 4002
Exemplo n.º 14
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']
Exemplo n.º 15
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
Exemplo n.º 16
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
Exemplo n.º 17
0
    def test_proximity_mechanism_with_multiple(self):
        shared.reset()
        simulation = XYZSimulation(Config())

        subject = MySubject(Config(), simulation, position=(0, 0, 0))
        other_subjects = []

        for i in range(3):
            other_subjects.append(
                MySubject(Config(), simulation, position=(i, i, 0)))

        simulation.subjects = XYZSubjects([subject], simulation=simulation)
        simulation.subjects.extend(other_subjects)
        simulation.subjects.auto_expose = False

        proximity_mechanism = MyProximityMechanism(
            config=Config(),
            simulation=simulation,
            subject=subject,
        )

        data = proximity_mechanism.run()
        assert [
            {
                'direction': 0,
                'subject_id': other_subjects[0].id,
                'distance': 0.0,
            },
            {
                'direction': 135.0,
                'subject_id': other_subjects[1].id,
                'distance': 1.41
            },
            {
                'direction': 135.0,
                'subject_id': other_subjects[2].id,
                'distance': 2.83
            },
        ] == data