Browse Source

Fix tests

Bastien Sevajol 7 years ago
parent
commit
630e1f06b6
4 changed files with 62 additions and 25 deletions
  1. 1 1
      synergine2/share.py
  2. 19 8
      synergine2/simulation.py
  3. 16 12
      synergine2_xyz/xyz.py
  4. 26 4
      tests/test_xyz.py

+ 1 - 1
synergine2/share.py View File

176
             key_formatter = get_key
176
             key_formatter = get_key
177
 
177
 
178
         def fget(self_):
178
         def fget(self_):
179
-            return self.get(key)
179
+            return self.get(key_formatter(self_))
180
 
180
 
181
         def fset(self_, value_):
181
         def fset(self_, value_):
182
             try:
182
             try:

+ 19 - 8
synergine2/simulation.py View File

52
         else:
52
         else:
53
             self.intentions = IntentionManager()
53
             self.intentions = IntentionManager()
54
 
54
 
55
-        # TODO: Revoir le mechanisme de collection: utilité, usage avec les process, etc
56
-        # for collection in self.collections:
57
-        #     self.simulation.collections[collection].append(self)
58
-
59
     @property
55
     @property
60
     def id(self) -> int:
56
     def id(self) -> int:
61
-        return self._id
57
+        try:
58
+            return self._id
59
+        except AttributeError:
60
+            pass
62
 
61
 
63
     def change_id(self, id_: int) -> None:
62
     def change_id(self, id_: int) -> None:
64
         self._id = id_
63
         self._id = id_
75
 
74
 
76
         subject_classes[self._id] = id(type(self))
75
         subject_classes[self._id] = id(type(self))
77
 
76
 
77
+        for collection in self.collections:
78
+            self.simulation.collections.setdefault(collection, []).append(self.id)
79
+
78
     def __str__(self):
80
     def __str__(self):
79
         return self.__repr__()
81
         return self.__repr__()
80
 
82
 
100
         self._auto_expose = True
102
         self._auto_expose = True
101
         super().__init__(*args, **kwargs)
103
         super().__init__(*args, **kwargs)
102
 
104
 
105
+        if self.auto_expose:
106
+            for subject in self:
107
+                subject.expose()
108
+
103
     @property
109
     @property
104
     def auto_expose(self) -> bool:
110
     def auto_expose(self) -> bool:
105
         return self._auto_expose
111
         return self._auto_expose
135
         if self.auto_expose:
141
         if self.auto_expose:
136
             p_object.expose()
142
             p_object.expose()
137
 
143
 
144
+    def extend(self, iterable):
145
+        for item in iterable:
146
+            self.append(item)
147
+
138
 
148
 
139
 class Simulation(BaseObject):
149
 class Simulation(BaseObject):
140
     accepted_subject_class = Subjects
150
     accepted_subject_class = Subjects
143
     subject_behaviours_index = shared.create('subject_behaviours_index', {})
153
     subject_behaviours_index = shared.create('subject_behaviours_index', {})
144
     subject_mechanisms_index = shared.create('subject_mechanisms_index', {})
154
     subject_mechanisms_index = shared.create('subject_mechanisms_index', {})
145
     subject_classes = shared.create('subject_classes', {})
155
     subject_classes = shared.create('subject_classes', {})
156
+    collections = shared.create('collections', {})
146
 
157
 
147
     def __init__(
158
     def __init__(
148
         self,
159
         self,
149
         config: Config,
160
         config: Config,
150
     ):
161
     ):
151
         self.config = config
162
         self.config = config
152
-        self.collections = collections.defaultdict(list)
153
         self._subjects = None  # type: Subjects
163
         self._subjects = None  # type: Subjects
154
 
164
 
155
         # Should contain all usable class of Behaviors, Mechanisms, SubjectBehaviourSelectors,
165
         # Should contain all usable class of Behaviors, Mechanisms, SubjectBehaviourSelectors,
172
                 simulation=self,
182
                 simulation=self,
173
             )
183
             )
174
 
184
 
175
-    def add_to_index(self, class_: type) -> None:
185
+    def add_to_index(self, *classes: type) -> None:
176
         assert not self._index_locked
186
         assert not self._index_locked
177
-        self._index[id(class_)] = class_
187
+        for class_ in classes:
188
+            self._index[id(class_)] = class_
178
 
189
 
179
     @property
190
     @property
180
     def index(self) -> typing.Dict[int, type]:
191
     def index(self) -> typing.Dict[int, type]:

+ 16 - 12
synergine2_xyz/xyz.py View File

5
 from math import sqrt
5
 from math import sqrt
6
 
6
 
7
 from synergine2.exceptions import SynergineException
7
 from synergine2.exceptions import SynergineException
8
+from synergine2.share import shared
8
 from synergine2.simulation import Subject
9
 from synergine2.simulation import Subject
9
 
10
 
10
 """
11
 """
115
 
116
 
116
 
117
 
117
 class XYZSubjectMixin(object, metaclass=XYZSubjectMixinMetaClass):
118
 class XYZSubjectMixin(object, metaclass=XYZSubjectMixinMetaClass):
119
+    position = shared.create(['{id}', 'counter'], (0, 0, 0))
120
+
118
     def __init__(self, *args, **kwargs):
121
     def __init__(self, *args, **kwargs):
119
         """
122
         """
120
         :param position: tuple with (x, y, z)
123
         :param position: tuple with (x, y, z)
121
         """
124
         """
122
-        self._position = kwargs.pop('position')
123
-        self.previous_direction = None
124
-        super().__init__(*args, **kwargs)
125
+        position = None
126
+        try:
127
+            position = kwargs.pop('position')
128
+        except KeyError:
129
+            pass
125
 
130
 
126
-    @property
127
-    def position(self):
128
-        return self._position
131
+        self.previous_direction = None  # TODO: shared
132
+        super().__init__(*args, **kwargs)
129
 
133
 
130
-    @position.setter
131
-    def position(self, value):
132
-        self._position = value
134
+        if position:
135
+            self.position = position
133
 
136
 
134
 
137
 
135
 class ProximityMixin(object):
138
 class ProximityMixin(object):
152
             # TODO: Optimiser en calculant directement les positions alentours et
155
             # TODO: Optimiser en calculant directement les positions alentours et
153
             # en regardant si elles sont occupés dans subjects.xyz par un subject
156
             # en regardant si elles sont occupés dans subjects.xyz par un subject
154
             # etant dans fell_collection
157
             # etant dans fell_collection
155
-            for subject in simulation.collections.get(feel_collection, []):
156
-                if subject == exclude_subject:
158
+            for subject_id in simulation.collections.get(feel_collection, []):
159
+                subject = simulation.get_or_create_subject(subject_id)
160
+                if subject.id == exclude_subject.id:
157
                     continue
161
                     continue
158
 
162
 
159
                 if self.have_to_check_position_is_possible() and not simulation.is_possible_position(subject.position):
163
                 if self.have_to_check_position_is_possible() and not simulation.is_possible_position(subject.position):
175
                         self.direction_round_decimals,
179
                         self.direction_round_decimals,
176
                     )
180
                     )
177
                     subjects.append({
181
                     subjects.append({
178
-                        'subject': subject,
182
+                        'subject_id': subject.id,
179
                         'direction': direction,
183
                         'direction': direction,
180
                         'distance': distance,
184
                         'distance': distance,
181
                     })
185
                     })

+ 26 - 4
tests/test_xyz.py View File

1
 # coding: utf-8
1
 # coding: utf-8
2
 # -*- coding: utf-8 -*-
2
 # -*- coding: utf-8 -*-
3
 from synergine2.config import Config
3
 from synergine2.config import Config
4
+from synergine2.share import shared
4
 from synergine2.simulation import Subject
5
 from synergine2.simulation import Subject
5
 from synergine2_xyz.mechanism import ProximitySubjectMechanism
6
 from synergine2_xyz.mechanism import ProximitySubjectMechanism
6
 from synergine2_xyz.simulation import XYZSimulation
7
 from synergine2_xyz.simulation import XYZSimulation
22
 
23
 
23
 class TestXYZ(BaseTest):
24
 class TestXYZ(BaseTest):
24
     def test_proximity_mechanism_with_one(self):
25
     def test_proximity_mechanism_with_one(self):
26
+        shared.reset()
25
         simulation = XYZSimulation(Config())
27
         simulation = XYZSimulation(Config())
28
+        simulation.add_to_index(
29
+            MyProximityMechanism,
30
+            MySubject,
31
+        )
32
+
26
         subject = MySubject(Config(), simulation, position=(0, 0, 0))
33
         subject = MySubject(Config(), simulation, position=(0, 0, 0))
27
         other_subject = MySubject(Config(), simulation, position=(5, 0, 0))
34
         other_subject = MySubject(Config(), simulation, position=(5, 0, 0))
28
 
35
 
30
             [subject, other_subject],
37
             [subject, other_subject],
31
             simulation=simulation,
38
             simulation=simulation,
32
         )
39
         )
40
+        simulation.subjects.auto_expose = False
33
 
41
 
34
         proximity_mechanism = MyProximityMechanism(
42
         proximity_mechanism = MyProximityMechanism(
35
             config=Config(),
43
             config=Config(),
42
             subject=other_subject,
50
             subject=other_subject,
43
         )
51
         )
44
         assert [{
52
         assert [{
45
-            'subject': other_subject,
53
+            'subject_id': other_subject.id,
46
             'direction': 90.0,
54
             'direction': 90.0,
47
             'distance': 5.0,
55
             'distance': 5.0,
48
         }] == proximity_mechanism.run()
56
         }] == proximity_mechanism.run()
49
 
57
 
50
     def test_proximity_mechanism_excluding(self):
58
     def test_proximity_mechanism_excluding(self):
59
+        shared.reset()
51
         simulation = XYZSimulation(Config())
60
         simulation = XYZSimulation(Config())
61
+        simulation.add_to_index(
62
+            MyProximityMechanism,
63
+            MySubject,
64
+        )
65
+
52
         subject = MySubject(Config(), simulation, position=(0, 0, 0))
66
         subject = MySubject(Config(), simulation, position=(0, 0, 0))
53
         other_subject = MySubject(Config(), simulation, position=(11, 0, 0))
67
         other_subject = MySubject(Config(), simulation, position=(11, 0, 0))
54
 
68
 
56
             [subject, other_subject],
70
             [subject, other_subject],
57
             simulation=simulation,
71
             simulation=simulation,
58
         )
72
         )
73
+        simulation.subjects.auto_expose = False
59
 
74
 
60
         proximity_mechanism = MyProximityMechanism(
75
         proximity_mechanism = MyProximityMechanism(
61
             config=Config(),
76
             config=Config(),
71
         assert [] == proximity_mechanism.run()
86
         assert [] == proximity_mechanism.run()
72
 
87
 
73
     def test_proximity_mechanism_with_multiple(self):
88
     def test_proximity_mechanism_with_multiple(self):
89
+        shared.reset()
74
         simulation = XYZSimulation(Config())
90
         simulation = XYZSimulation(Config())
91
+        simulation.add_to_index(
92
+            MyProximityMechanism,
93
+            MySubject,
94
+        )
95
+
75
         subject = MySubject(Config(), simulation, position=(0, 0, 0))
96
         subject = MySubject(Config(), simulation, position=(0, 0, 0))
76
         other_subjects = []
97
         other_subjects = []
77
 
98
 
80
 
101
 
81
         simulation.subjects = XYZSubjects([subject], simulation=simulation)
102
         simulation.subjects = XYZSubjects([subject], simulation=simulation)
82
         simulation.subjects.extend(other_subjects)
103
         simulation.subjects.extend(other_subjects)
104
+        simulation.subjects.auto_expose = False
83
 
105
 
84
         proximity_mechanism = MyProximityMechanism(
106
         proximity_mechanism = MyProximityMechanism(
85
             config=Config(),
107
             config=Config(),
91
         assert [
113
         assert [
92
             {
114
             {
93
                 'direction': 0,
115
                 'direction': 0,
94
-                'subject': other_subjects[0],
116
+                'subject_id': other_subjects[0].id,
95
                 'distance': 0.0,
117
                 'distance': 0.0,
96
             },
118
             },
97
             {
119
             {
98
                 'direction': 135.0,
120
                 'direction': 135.0,
99
-                'subject': other_subjects[1],
121
+                'subject_id': other_subjects[1].id,
100
                 'distance': 1.41
122
                 'distance': 1.41
101
             },
123
             },
102
             {
124
             {
103
                 'direction': 135.0,
125
                 'direction': 135.0,
104
-                'subject': other_subjects[2],
126
+                'subject_id': other_subjects[2].id,
105
                 'distance': 2.83
127
                 'distance': 2.83
106
             },
128
             },
107
         ] == data
129
         ] == data