Kaynağa Gözat

permit shared index with subject ids

Bastien Sevajol 6 yıl önce
ebeveyn
işleme
a9014cf1f8
2 değiştirilmiş dosya ile 32 ekleme ve 8 silme
  1. 30 6
      synergine2/share.py
  2. 2 2
      tests/test_share.py

+ 30 - 6
synergine2/share.py Dosyayı Görüntüle

@@ -8,6 +8,9 @@ from synergine2.base import IdentifiedObject
8 8
 from synergine2.exceptions import SynergineException
9 9
 from synergine2.exceptions import UnknownSharedData
10 10
 
11
+if typing.TYPE_CHECKING:
12
+    from synergine2.simulation import Subject
13
+
11 14
 
12 15
 class NoSharedDataInstance(SynergineException):
13 16
     pass
@@ -22,12 +25,15 @@ class SharedDataIndex(object):
22 25
         self.shared_data_manager = shared_data_manager
23 26
         self.key = key
24 27
 
25
-    def add(self, value: typing.Any) -> None:
28
+    def add(self, subject: 'Subject', value: typing.Any) -> None:
26 29
         raise NotImplementedError()
27 30
 
28
-    def remove(self, value: typing.Any) -> None:
31
+    def remove(self, subject: 'Subject', value: typing.Any) -> None:
29 32
         raise NotImplementedError()
30 33
 
34
+    def get_final_key(self, subject: 'Subject', value: typing.Any) -> str:
35
+        return self.key.format(shared_key=value, subject_id=subject.id)
36
+
31 37
 
32 38
 class SharedData(object):
33 39
     def __init__(
@@ -226,7 +232,7 @@ class SharedDataManager(object):
226 232
             try:
227 233
                 previous_value = self.get(final_key)
228 234
                 for index in indexes:
229
-                    index.remove(previous_value)
235
+                    index.remove(instance, previous_value)
230 236
             except UnknownSharedData:
231 237
                 pass  # If no shared data, no previous value to remove
232 238
 
@@ -236,7 +242,7 @@ class SharedDataManager(object):
236 242
                 self.set(final_key, shared_data.type(value_))
237 243
 
238 244
             for index in indexes:
239
-                index.add(value_)
245
+                index.add(instance, value_)
240 246
 
241 247
         def fdel(self_):
242 248
             raise SynergineException('You cannot delete a shared data: not implemented yet')
@@ -258,8 +264,26 @@ class SharedDataManager(object):
258 264
 shared = SharedDataManager()
259 265
 
260 266
 
267
+class SubjectListIndex(SharedDataIndex):
268
+    def add(self, subject: 'Subject', value):
269
+        final_key = self.get_final_key(subject, value)
270
+        try:
271
+            values = self.shared_data_manager.get(final_key)
272
+        except UnknownSharedData:
273
+            values = []
274
+
275
+        values.append(subject.id)
276
+        self.shared_data_manager.set(final_key, values)
277
+
278
+    def remove(self, subject: 'Subject', value):
279
+        final_key = self.get_final_key(subject, value)
280
+        values = self.shared_data_manager.get(final_key)
281
+        values.remove(subject.id)
282
+        self.shared_data_manager.set(final_key, values)
283
+
284
+
261 285
 class ListIndex(SharedDataIndex):
262
-    def add(self, value):
286
+    def add(self, subject: 'Subject', value):
263 287
         try:
264 288
             values = self.shared_data_manager.get(self.key)
265 289
         except UnknownSharedData:
@@ -268,7 +292,7 @@ class ListIndex(SharedDataIndex):
268 292
         values.append(value)
269 293
         self.shared_data_manager.set(self.key, values)
270 294
 
271
-    def remove(self, value):
295
+    def remove(self, subject: 'Subject', value):
272 296
         values = self.shared_data_manager.get(self.key)
273 297
         values.remove(value)
274 298
         self.shared_data_manager.set(self.key, values)

+ 2 - 2
tests/test_share.py Dosyayı Görüntüle

@@ -71,7 +71,7 @@ class TestShare(BaseTest):
71 71
 
72 72
     def test_position_index(self):
73 73
         class ListIndex(share.SharedDataIndex):
74
-            def add(self, value):
74
+            def add(self, subject, value):
75 75
                 try:
76 76
                     values = self.shared_data_manager.get(self.key)
77 77
                 except UnknownSharedData:
@@ -80,7 +80,7 @@ class TestShare(BaseTest):
80 80
                 values.append(value)
81 81
                 self.shared_data_manager.set(self.key, values)
82 82
 
83
-            def remove(self, value):
83
+            def remove(self, subject, value):
84 84
                 values = self.shared_data_manager.get(self.key)
85 85
                 values.remove(value)
86 86
                 self.shared_data_manager.set(self.key, values)