Package teamwork :: Package examples :: Package Thespian :: Module ThespianGenericSociety
[hide private]
[frames] | no frames]

Source Code for Module teamwork.examples.Thespian.ThespianGenericSociety

 1  __author__ = 'David V. Pynadath <pynadath@isi.edu>' 
 2  from xml.dom.minidom import * 
 3  
 
 4  from teamwork.agent.Generic import GenericModel 
 5  from teamwork.agent.Entities import * 
 6  from teamwork.multiagent.Multiagent import * 
 7  from teamwork.multiagent.GenericSociety import * 
 8  
 
 9  from ThespianGeneric import * 
10  from ThespianAgents import * 
11  
 
12 -class ThespianGenericSociety(GenericSociety):
13
14 - def __copyGeneric(self,generic):
15 """Copies top-level generic values onto nested belief models""" 16 for other in generic.getEntities(): 17 entity = generic.getEntity(other) 18 if self.has_key(other): 19 # Belief about another class in this society 20 toExplore = [other] 21 while len(toExplore) > 0: 22 cls = toExplore.pop() 23 if len(self[cls].models) > 0: 24 entity.models = self[cls].models 25 break 26 toExplore += self[cls].getParents() 27 entity.dynamics = self[other].dynamics 28 # Anything else to copy? 29 # Descend recursively 30 self.__copyGeneric(entity)
31
32 - def importDict(self,hierarchy):
33 """Updates society from dictionary-style hierarchy""" 34 # Read the separate models in 35 for name,generic in hierarchy.items(): 36 if not name: 37 # Ignore "None" entry? 38 continue 39 model = ThespianGenericModel(name) 40 41 self[name] = model 42 ## model.hierarchy = self 43 model.importDict(generic) 44 # Tie up some connections among models 45 for name,generic in self.items(): 46 self._copyGeneric(generic)
47 48 49
50 - def instantiate(self,className,instanceName,objClass=ThespianAgent):
51 """Returns a new instantiated agent model 52 53 className: The string name of the relevant generic model 54 instanceName: The string name for the new instance 55 objClass: (Optional) Class to create agent object from""" 56 entity = objClass(instanceName) 57 entity.applyDefaults(className,self) 58 return entity
59