annotate bGrease/component/base.py @ 56:3f6299f975fe

Added BaseBehaviour. Moved methods from MovingAgentBehaviour to BaseBehaviour.
author KarstenBock@gmx.net
date Fri, 09 Sep 2011 15:18:17 +0200
parents ff3e395abf91
children a6bbb732b27b
rev   line source
5
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
1 class ComponentBase(object):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
2 """Component abstract base class
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
3
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
4 Strictly speaking you do not need to derive from this class to create your
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
5 own components, but it does serve to document the full interface that a
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
6 component implements and it provides some basic implementations for
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
7 certain methods
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
8 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
9
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
10 ## Optional attributes and methods ##
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
11
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
12 def set_manager(self, manager):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
13 """Set the manager of this component. If this method exists it will be
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
14 automatically called when the component is added to a manager.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
15
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
16 This method stores the manager and allows the component to be added
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
17 only once to a single manager.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
18 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
19 assert getattr(self, 'manager', None) is None, 'Component cannot be added to multiple managers'
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
20 self.manager = manager
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
21
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
22 def __del__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
23 """Break circrefs to allow faster collection"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
24 if hasattr(self, 'manager'):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
25 del self.manager
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
26
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
27 ## Mandatory methods ##
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
28
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
29 def add(self, entity_id, data=None, **data_kw):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
30 """Add a data entry in the component for the given entity. Additional
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
31 data (if any) for the entry can be provided in the data argument or as
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
32 keyword arguments. Additional data is optional and if omitted then
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
33 suitable defaults will be used. Return an entity data object
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
34 for the new entity entry.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
35
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
36 The semantics of the data arguments is up to the component.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
37
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
38 An entity_id is a unique key, thus multiple separate data entries for
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
39 a given entity are not allowed. Components can indivdually decide
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
40 what to do if an entity_id is added multiple times to the component.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
41 Potential options include, raising an exception, replacing the
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
42 existing data or coalescing it somehow.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
43 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
44
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
45 def remove(self, entity_id):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
46 """Remove the entity data entry from the component. If the
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
47 entity is not in the component, raise KeyError
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
48 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
49
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
50 def __delitem_(self, entity_id):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
51 """Same as remove()"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
52
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
53 def __len__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
54 """Return the number of entities in the component"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
55 raise NotImplementedError()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
56
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
57 def __iter__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
58 """Return an iterator of entity data objects in this component
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
59
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
60 No order is defined for these data objects
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
61 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
62 raise NotImplementedError()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
63
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
64 def __contains__(self, entity_id):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
65 """Return True if the entity is contained in the component"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
66 raise NotImplementedError()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
67
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
68 def __getitem__(self, entity_id):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
69 """Return the entity data object for the given entity.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
70 The entity data object returned may be mutable, immutable or a
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
71 mutable copy of the data at the discretion of the component
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
72 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
73 raise NotImplementedError()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
74