Bläddra i källkod

metas: brain, brain parts attached to class instead instance

Bastien Sevajol 9 år sedan
förälder
incheckning
f34dc974dd

+ 0 - 4
intelligine/core/exceptions.py Visa fil

@@ -50,10 +50,6 @@ class BrainException(Exception):
50 50
     pass
51 51
 
52 52
 
53
-class BrainPartAlreadyExist(BrainException):
54
-    pass
55
-
56
-
57 53
 class BodyException(Exception):
58 54
     pass
59 55
 

+ 2 - 0
intelligine/cst.py Visa fil

@@ -57,6 +57,8 @@ COL_TRANSPORTER_NOT_CARRYING = IncrementedNamedInt.get('intelligine.col.transpor
57 57
 COL_EATABLE = IncrementedNamedInt.get('intelligine.col.eatable')
58 58
 COL_SMELL = IncrementedNamedInt.get('intelligine.col.smell')
59 59
 
60
+INSTANCE_CLASS = IncrementedNamedInt.get('intelligine.instance_class')
61
+
60 62
 BRAIN = IncrementedNamedInt.get('intelligine.brain')
61 63
 BRAIN_SCHEMA = IncrementedNamedInt.get('intelligine.brain_schema')
62 64
 BRAIN_PART_MOVE = IncrementedNamedInt.get('intelligine.brain.part.move')

+ 5 - 3
intelligine/shorcut/brain.py Visa fil

@@ -1,10 +1,12 @@
1
-from intelligine.cst import BRAIN, BRAIN_SCHEMA
1
+from intelligine.cst import BRAIN, BRAIN_SCHEMA, INSTANCE_CLASS
2 2
 
3 3
 
4 4
 def get_brain_class(context, object_id):
5
-    return context.metas.value.get(BRAIN, object_id)
5
+    object_class = context.metas.value.get(INSTANCE_CLASS, object_id)
6
+    return context.metas.value.get(BRAIN, object_class)
6 7
 
7 8
 
8 9
 def get_brain_part(context, object_id, brain_part_name):
9
-    object_brain_schema = context.metas.value.get(BRAIN_SCHEMA, object_id)
10
+    object_class = context.metas.value.get(INSTANCE_CLASS, object_id)
11
+    object_brain_schema = context.metas.value.get(BRAIN_SCHEMA, object_class)
10 12
     return object_brain_schema[brain_part_name]

+ 2 - 17
intelligine/simulation/object/brain/Brain.py Visa fil

@@ -1,4 +1,3 @@
1
-from intelligine.core.exceptions import BrainPartAlreadyExist
2 1
 from intelligine.cst import BRAIN_SCHEMA
3 2
 
4 3
 
@@ -9,31 +8,17 @@ class Brain():
9 8
     def __init__(self, context, host):
10 9
         self._context = context
11 10
         self._host = host
12
-        self._schema = {}
13 11
         self._parts = {}
14 12
         self._init_parts()
15 13
 
16 14
     def _init_parts(self):
17 15
         for brain_part_name in self._brain_parts:
18
-            self._set_brain_part(brain_part_name, self._brain_parts[brain_part_name](self))
19
-
20
-    def _set_brain_part(self, name, brain_part, replace=False):
21
-        if name in self._parts and not replace:
22
-            raise BrainPartAlreadyExist()
23
-        self._parts[name] = brain_part
24
-        self._update_schema()
16
+            self._parts[brain_part_name] = self._brain_parts[brain_part_name](self)
17
+        self._context.metas.value.set(BRAIN_SCHEMA, self._host.__class__, self._brain_parts)
25 18
 
26 19
     def get_part(self, name):
27 20
         return self._parts[name]
28 21
 
29
-    def _update_schema(self):
30
-        self._schema = {}
31
-        for part_name in self._parts:
32
-            self._schema[part_name] = self._parts[part_name].__class__
33
-        # TODO: N'est-ce pas un schema appartenant a la classe ? Ne suffirai t-il pas de stocker ce schema par classe
34
-        # plutôt que par objet ?
35
-        self._context.metas.value.set(BRAIN_SCHEMA, self._host.get_id(), self._schema)
36
-
37 22
     def get_context(self):
38 23
         return self._context
39 24
 

+ 1 - 5
intelligine/synergy/event/Event.py Visa fil

@@ -3,8 +3,4 @@ from intelligine.cst import BRAIN_SCHEMA
3 3
 
4 4
 
5 5
 class Event(BaseEvent):
6
-
7
-    @classmethod
8
-    def _get_brain_part(cls, context, object_id, brain_part_name):
9
-        object_brain_schema = context.metas.value.get(BRAIN_SCHEMA, object_id)
10
-        return object_brain_schema[brain_part_name]
6
+    pass

+ 2 - 1
intelligine/synergy/event/attack/NearAttackableEvent.py Visa fil

@@ -1,4 +1,5 @@
1 1
 from intelligine.core.exceptions import NearNothingFound
2
+from intelligine.shorcut.brain import get_brain_part
2 3
 from synergine.core.exceptions import NotConcernedEvent
3 4
 from intelligine.synergy.event.src.NearEvent import NearEvent
4 5
 from synergine_xyz.mechanism.AroundMechanism import AroundMechanism
@@ -22,7 +23,7 @@ class NearAttackableEvent(NearEvent):
22 23
         except NearNothingFound:
23 24
             raise NotConcernedEvent()
24 25
 
25
-        brain_part = self._get_brain_part(context, object_id, BRAIN_PART_ATTACK)
26
+        brain_part = get_brain_part(context, object_id, BRAIN_PART_ATTACK)
26 27
         if not brain_part.can_attack(context, object_id, parameters[self._near_name][0]):
27 28
             raise NotConcernedEvent()
28 29
 

+ 2 - 2
intelligine/synergy/event/move/MoveEvent.py Visa fil

@@ -1,5 +1,6 @@
1 1
 from intelligine.core.exceptions import UnableToFoundMovement
2 2
 from intelligine.synergy.event.move.direction import get_position_with_direction_decal
3
+from intelligine.shorcut.brain import get_brain_part
3 4
 from synergine.core.exceptions import NotConcernedEvent
4 5
 from intelligine.synergy.event.Event import Event
5 6
 from synergine.core.simulation.mechanism.Mechanism import Mechanism
@@ -31,8 +32,7 @@ class MoveEvent(Event):
31 32
         return parameters
32 33
 
33 34
     def _get_direction(self, object_id, context):
34
-        object_brain_schema = context.metas.value.get(BRAIN_SCHEMA, object_id)
35
-        object_move_brain_part = object_brain_schema[BRAIN_PART_MOVE]
35
+        object_move_brain_part = get_brain_part(context, object_id, BRAIN_PART_MOVE)
36 36
         return object_move_brain_part.get_direction(context, object_id)
37 37
 
38 38
     @staticmethod

+ 3 - 2
intelligine/synergy/event/transport/PutableEvent.py Visa fil

@@ -1,4 +1,5 @@
1 1
 from intelligine.core.exceptions import NearNothingFound, CantFindWhereToPut
2
+from intelligine.shorcut.brain import get_brain_part
2 3
 from intelligine.synergy.event.src.NearEvent import NearEvent
3 4
 from synergine.core.exceptions import NotConcernedEvent
4 5
 from intelligine.cst import CANT_PUT_STILL, COL_TRANSPORTER_CARRYING, TRANSPORTABLE, BRAIN_SCHEMA, BRAIN_PART_PUT
@@ -27,7 +28,7 @@ class PutableEvent(NearEvent):
27 28
             raise NotConcernedEvent()
28 29
 
29 30
         object_near_id = parameters[self._near_name][0]
30
-        brain_part = self._get_brain_part(context, object_id, BRAIN_PART_PUT)
31
+        brain_part = get_brain_part(context, object_id, BRAIN_PART_PUT)
31 32
 
32 33
         if not brain_part.can_put(context, object_id, object_near_id):
33 34
             raise NotConcernedEvent()
@@ -47,5 +48,5 @@ class PutableEvent(NearEvent):
47 48
 
48 49
     @classmethod
49 50
     def _object_can_put(cls, object_id, context, object_to_put_id):
50
-        object_take_brain_part = cls._get_brain_part(context, object_id, BRAIN_PART_PUT)
51
+        object_take_brain_part = get_brain_part(context, object_id, BRAIN_PART_PUT)
51 52
         return object_take_brain_part.can_put(context, object_id, object_to_put_id)

+ 3 - 2
intelligine/synergy/event/transport/TakeableEvent.py Visa fil

@@ -1,4 +1,5 @@
1 1
 from intelligine.core.exceptions import NearNothingFound
2
+from intelligine.shorcut.brain import get_brain_part
2 3
 from synergine.core.exceptions import NotConcernedEvent
3 4
 from intelligine.synergy.event.src.NearEvent import NearEvent
4 5
 from synergine_xyz.mechanism.AroundMechanism import AroundMechanism
@@ -34,5 +35,5 @@ class TakeableEvent(NearEvent):
34 35
 
35 36
     @classmethod
36 37
     def _object_can_take(cls, object_id, context, object_to_take_id):
37
-        object_take_brain_part = cls._get_brain_part(context, object_id, BRAIN_PART_TAKE)
38
-        return object_take_brain_part.can_take(context, object_id, object_to_take_id)
38
+        object_take_brain_part = get_brain_part(context, object_id, BRAIN_PART_TAKE)
39
+        return object_take_brain_part.can_take(context, object_id, object_to_take_id)

+ 1 - 1
intelligine/synergy/object/BaseBug.py Visa fil

@@ -20,7 +20,7 @@ class BaseBug(Transportable):
20 20
         self._alive = True
21 21
         self._movements_count = -1
22 22
         self._brain = self._brain_class(self._context, self)
23
-        self._context.metas.value.set(BRAIN, self.get_id(), self._brain_class)
23
+        self._context.metas.value.set(BRAIN, self.__class__, self._brain_class)
24 24
         self._parts = {}
25 25
         self._init_parts()
26 26
 

+ 2 - 1
intelligine/synergy/object/SynergyObject.py Visa fil

@@ -1,4 +1,4 @@
1
-from intelligine.cst import OBJ_SMELL
1
+from intelligine.cst import OBJ_SMELL, INSTANCE_CLASS
2 2
 from synergine_xyz.SynergyObject import SynergyObject as XyzSynergyObject
3 3
 
4 4
 
@@ -7,6 +7,7 @@ class SynergyObject(XyzSynergyObject):
7 7
     def __init__(self, collection, context):
8 8
         super().__init__(collection, context)
9 9
         self._smell = None
10
+        context.metas.value.set(INSTANCE_CLASS, self.get_id(), self.__class__)
10 11
 
11 12
     def _set_smell(self, smell_type):
12 13
         self._smell = smell_type