123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- # coding: utf-8
- # -*- coding: utf-8 -*-
- from synergine2.config import Config
- from synergine2.share import shared
- from synergine2.simulation import Subject
- from synergine2_xyz.mechanism import ProximitySubjectMechanism
- from synergine2_xyz.simulation import XYZSimulation
- from synergine2_xyz.xyz import XYZSubjectMixin
- from synergine2_xyz.subjects import XYZSubjects
- from synergine2_xyz.utils import get_positions_from_str_representation
- from synergine2_xyz.utils import get_str_representation_from_positions
- from tests import BaseTest
- from tests import str_kwargs
-
-
- class MySubject(XYZSubjectMixin, Subject):
- pass
-
-
- class MyProximityMechanism(ProximitySubjectMechanism):
- distance = 10
-
-
- class TestXYZ(BaseTest):
- def test_proximity_mechanism_with_one(self):
- shared.reset()
- simulation = XYZSimulation(Config())
- simulation.add_to_index(
- MyProximityMechanism,
- MySubject,
- )
-
- 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()
-
- def test_proximity_mechanism_excluding(self):
- shared.reset()
- simulation = XYZSimulation(Config())
- simulation.add_to_index(
- MyProximityMechanism,
- MySubject,
- )
-
- 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()
-
- def test_proximity_mechanism_with_multiple(self):
- shared.reset()
- simulation = XYZSimulation(Config())
- simulation.add_to_index(
- MyProximityMechanism,
- MySubject,
- )
-
- 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
-
- def test_str_representation_from_str(self):
- str_ = """
- 0 0 1 0 0
- 0 1 1 1 0
- 0 0 1 0 0
- """
- items_positions = {
- '0': [
- (-2, -1, 0),
- (-1, -1, 0),
- (1, -1, 0),
- (2, -1, 0),
- (-2, 0, 0),
- (2, 0, 0),
- (-2, 1, 0),
- (-1, 1, 0),
- (1, 1, 0),
- (2, 1, 0),
- ],
- '1': [
- (0, -1, 0),
- (-1, 0, 0),
- (0, 0, 0),
- (1, 0, 0),
- (0, 1, 0),
- ],
- }
- assert items_positions == get_positions_from_str_representation(str_)
-
- def test_str_representation_to_str(self):
- expected = """
- 0 0 1 0 0
- 0 1 1 1 0
- 0 0 1 0 0
- """
- items_positions = {
- '0': [
- (-2, -1, 0),
- (-1, -1, 0),
- (1, -1, 0),
- (2, -1, 0),
- (-2, 0, 0),
- (2, 0, 0),
- (-2, 1, 0),
- (-1, 1, 0),
- (1, 1, 0),
- (2, 1, 0),
- ],
- '1': [
- (0, -1, 0),
- (-1, 0, 0),
- (0, 0, 0),
- (1, 0, 0),
- (0, 1, 0),
- ],
- }
-
- assert expected == \
- get_str_representation_from_positions(
- items_positions,
- **str_kwargs
- )
-
- # def test_str_representation_to_str_multi_levels(self):
- # expected = """
- # 0 0 1 0 0
- # 0 1 1 1 0
- # 0 0 1 0 0
- # ----
- # 0 0 0 0 0
- # 0 0 1 0 0
- # 0 0 0 0 0
- # """
- # items_positions = {
- # '0': [
- # (-2, -1, 0),
- # (-1, -1, 0),
- # (1, -1, 0),
- # (2, -1, 0),
- # (-2, 0, 0),
- # (2, 0, 0),
- # (-2, 1, 0),
- # (-1, 1, 0),
- # (1, 1, 0),
- # (2, 1, 0),
- # (-2, -1, 1),
- # (-1, -1, 1),
- # (1, -1, 1),
- # (2, -1, 1),
- # (-1, 0, 1),
- # (-2, 0, 1),
- # (2, 0, 1),
- # (-2, 1, 1),
- # (-1, 1, 1),
- # (1, 1, 1),
- # (2, 1, 1),
- # (2, -1, 1),
- # (1, 0, 1),
- # (2, 1, 1),
- # ],
- # '1': [
- # (0, -1, 0),
- # (-1, 0, 0),
- # (0, 0, 0),
- # (1, 0, 0),
- # (0, 1, 0),
- # (0, 0, 1),
- # ],
- # }
- #
- # assert expected == \
- # get_str_representation_from_positions(
- # items_positions,
- # **str_kwargs
- # )
|