Browse Source

metas: brain, brain parts attached to class instead instance

Bastien Sevajol 9 years ago
parent
commit
f34dc974dd

+ 0 - 4
intelligine/core/exceptions.py View File

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

+ 2 - 0
intelligine/cst.py View File

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

+ 5 - 3
intelligine/shorcut/brain.py View File

1
-from intelligine.cst import BRAIN, BRAIN_SCHEMA
1
+from intelligine.cst import BRAIN, BRAIN_SCHEMA, INSTANCE_CLASS
2
 
2
 
3
 
3
 
4
 def get_brain_class(context, object_id):
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
 def get_brain_part(context, object_id, brain_part_name):
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
     return object_brain_schema[brain_part_name]
12
     return object_brain_schema[brain_part_name]

+ 2 - 17
intelligine/simulation/object/brain/Brain.py View File

1
-from intelligine.core.exceptions import BrainPartAlreadyExist
2
 from intelligine.cst import BRAIN_SCHEMA
1
 from intelligine.cst import BRAIN_SCHEMA
3
 
2
 
4
 
3
 
9
     def __init__(self, context, host):
8
     def __init__(self, context, host):
10
         self._context = context
9
         self._context = context
11
         self._host = host
10
         self._host = host
12
-        self._schema = {}
13
         self._parts = {}
11
         self._parts = {}
14
         self._init_parts()
12
         self._init_parts()
15
 
13
 
16
     def _init_parts(self):
14
     def _init_parts(self):
17
         for brain_part_name in self._brain_parts:
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
     def get_part(self, name):
19
     def get_part(self, name):
27
         return self._parts[name]
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
     def get_context(self):
22
     def get_context(self):
38
         return self._context
23
         return self._context
39
 
24
 

+ 1 - 5
intelligine/synergy/event/Event.py View File

3
 
3
 
4
 
4
 
5
 class Event(BaseEvent):
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 View File

1
 from intelligine.core.exceptions import NearNothingFound
1
 from intelligine.core.exceptions import NearNothingFound
2
+from intelligine.shorcut.brain import get_brain_part
2
 from synergine.core.exceptions import NotConcernedEvent
3
 from synergine.core.exceptions import NotConcernedEvent
3
 from intelligine.synergy.event.src.NearEvent import NearEvent
4
 from intelligine.synergy.event.src.NearEvent import NearEvent
4
 from synergine_xyz.mechanism.AroundMechanism import AroundMechanism
5
 from synergine_xyz.mechanism.AroundMechanism import AroundMechanism
22
         except NearNothingFound:
23
         except NearNothingFound:
23
             raise NotConcernedEvent()
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
         if not brain_part.can_attack(context, object_id, parameters[self._near_name][0]):
27
         if not brain_part.can_attack(context, object_id, parameters[self._near_name][0]):
27
             raise NotConcernedEvent()
28
             raise NotConcernedEvent()
28
 
29
 

+ 2 - 2
intelligine/synergy/event/move/MoveEvent.py View File

1
 from intelligine.core.exceptions import UnableToFoundMovement
1
 from intelligine.core.exceptions import UnableToFoundMovement
2
 from intelligine.synergy.event.move.direction import get_position_with_direction_decal
2
 from intelligine.synergy.event.move.direction import get_position_with_direction_decal
3
+from intelligine.shorcut.brain import get_brain_part
3
 from synergine.core.exceptions import NotConcernedEvent
4
 from synergine.core.exceptions import NotConcernedEvent
4
 from intelligine.synergy.event.Event import Event
5
 from intelligine.synergy.event.Event import Event
5
 from synergine.core.simulation.mechanism.Mechanism import Mechanism
6
 from synergine.core.simulation.mechanism.Mechanism import Mechanism
31
         return parameters
32
         return parameters
32
 
33
 
33
     def _get_direction(self, object_id, context):
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
         return object_move_brain_part.get_direction(context, object_id)
36
         return object_move_brain_part.get_direction(context, object_id)
37
 
37
 
38
     @staticmethod
38
     @staticmethod

+ 3 - 2
intelligine/synergy/event/transport/PutableEvent.py View File

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

+ 3 - 2
intelligine/synergy/event/transport/TakeableEvent.py View File

1
 from intelligine.core.exceptions import NearNothingFound
1
 from intelligine.core.exceptions import NearNothingFound
2
+from intelligine.shorcut.brain import get_brain_part
2
 from synergine.core.exceptions import NotConcernedEvent
3
 from synergine.core.exceptions import NotConcernedEvent
3
 from intelligine.synergy.event.src.NearEvent import NearEvent
4
 from intelligine.synergy.event.src.NearEvent import NearEvent
4
 from synergine_xyz.mechanism.AroundMechanism import AroundMechanism
5
 from synergine_xyz.mechanism.AroundMechanism import AroundMechanism
34
 
35
 
35
     @classmethod
36
     @classmethod
36
     def _object_can_take(cls, object_id, context, object_to_take_id):
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 View File

20
         self._alive = True
20
         self._alive = True
21
         self._movements_count = -1
21
         self._movements_count = -1
22
         self._brain = self._brain_class(self._context, self)
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
         self._parts = {}
24
         self._parts = {}
25
         self._init_parts()
25
         self._init_parts()
26
 
26
 

+ 2 - 1
intelligine/synergy/object/SynergyObject.py View File

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