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

Source Code for Module teamwork.examples.Thespian.ThespianActions

  1  import copy 
  2  import string 
  3  from types import * 
  4   
  5  from teamwork.agent.Agent import Agent 
  6  from teamwork.action.PsychActions import * 
  7   
8 -class ThespianAction(Action):
9 nop = 'wait' 10 fields = { 11 # Redundant fields between actions and messages 12 'actor':{},'sender':{}, 13 'object':{},'receiver':{}, 14 # Same as in actions 15 'type':{},'command':{}, 16 # Unique to messages 17 'factors':{},'performative':{},'force':{}, 18 '_observed':{},'_unobserved':{}, 19 20 'sact_type':{}, 21 'attitude':{'pos_face':0.0,'neg_face':0.0}, 22 'addressee':{}, 23 } 24 ## format = ['actor','type','object','command','item','property','sact_type','attitude'] 25
26 - def __init__(self,arg={}):
27 """Translates an action in string form into a structured 28 representation of that same action (e.g., 29 'CLF-violence-to-UrbanPoor' becomes a PsychAction object 30 {'actor':CLF,'type':'violence','object':UrbanPoor}""" 31 ## self.command = None 32 if len(arg) == 0: 33 dict.__init__(self,{'type': self.nop}) 34 if type(arg) is StringType: 35 components = string.split(arg,'-') 36 ## print '[PS] ', components 37 if len(components) < 2: 38 # <type> 39 ## self.type = components[0] 40 dict.__init__(self,{'type': components[0]}) 41 elif len(components) < 3: 42 # <actor>-<type> 43 ## self.actor = components[0] 44 ## self.type = components[1] 45 dict.__init__(self,{'actor': components[0],'type': components[1]}) 46 elif len(components) < 4: 47 if components[1]=='to': 48 # <type>-to-<object> 49 ## self.type = components[0] 50 ## self.object = components[2] 51 dict.__init__(self,{'type': components[0],'object': components[2]}) 52 elif components[1]=='about': 53 # <type>-about-<item> 54 ## self.type = components[0] 55 ## self.item = components[2] 56 dict.__init__(self,{'type': components[0],'item': components[2]}) 57 elif len(components) < 5: 58 # <actor>-<type>-to-<object> 59 if components[2]=='to': 60 ## self.object = components[3] 61 ## self.actor = components[0] 62 ## self.type = components[1] 63 dict.__init__(self,{'actor': components[0],'type': components[1],'object': components[3]}) 64 else: 65 #mei 03/16/04 66 #<type>-<object>-<item>-<property> 67 ## self.type = components[0] 68 ## self.object = components[1] 69 ## self.item = components[2] 70 ## self.property = components[3] 71 dict.__init__(self,{'type': components[0],'object': components[1],'item': components[2],'property': components[3]}) 72 ## elif components[2] == 'to': 73 ## # <actor>-<command>-to-<object>-<action> 74 ## self.object = components[3] 75 ## substr = string.join(components[4:],'-') 76 ## self.command = PsychAction(substr) 77 ## self.actor = components[0] 78 ## self.type = components[1] 79 ## elif: 80 ## # <command>-to-<object>-<action> 81 ## self.object = components[2] 82 ## substr = string.join(components[3:],'-') 83 ## self.command = PsychAction(substr) 84 ## self.type = components[0] 85 elif len(components) < 6: 86 # <actor>-<type>-<object>-<item>-<property> 87 # mei added 03/16/04 88 #print 'coming here' 89 if components[1] == 'to': 90 #<type>-to-<object>-about-<item> 91 ## self.type = components[0] 92 ## self.object = components[2] 93 ## self.item = components[4] 94 dict.__init__(self,{'type': components[0],'object': components[2],'item': components[4]}) 95 else: 96 ## self.actor = components[0] 97 ## self.type = components[1] 98 ## self.object = components[2] 99 ## self.item = components[3] 100 ## self.property = components[4] 101 dict.__init__(self,{'actor': components[0],'type': components[1],'object': components[2],'item': components[3],'property': components[4]}) 102 elif len(components) < 7: 103 #<actor>-<type>-to-<object>-about-<item> 104 if components[2] == 'to': 105 ## self.actor = components[0] 106 ## self.type = components[1] 107 ## self.object = components[3] 108 ## self.item = components[5] 109 dict.__init__(self,{'actor': components[0],'type': components[1],'object': components[3],'item': components[5]}) 110 elif len(components) < 8: 111 #<type>-to-<object>-about-<property>-of-<item> 112 ## self.type = components[0] 113 ## self.object = components[2] 114 ## self.item = components[6] 115 ## self.property = components[4] 116 dict.__init__(self,{'type': components[0],'object': components[2],'item': components[6],'property': components[4]}) 117 #student-request-to-oldman-about-name-of-town 118 elif len(components) < 9: 119 ## self.actor = components[0] 120 ## self.type = components[1] 121 ## self.object = components[3] 122 ## self.item = components[7] 123 ## self.property = components[5] 124 dict.__init__(self,{'actor': components[0],'type': components[1],'object': components[3],'item': components[7],'property': components[5]}) 125 ## print 'psychaction: ',self 126 127 if (not self.has_key('addressee')) and self.has_key('object'): 128 self['addressee'] = [self['object']] 129 self['object'] = None 130 131 elif type(arg) is ListType: 132 dict.__init__(self,arg[0]) 133 else: 134 ## for key in self.fields: 135 ## try: 136 ## self[key] = arg[key] 137 ## except KeyError: 138 ## self[key] = None 139 dict.__init__(self,arg) 140 ## if self.command: 141 ## self.command = PsychAction(self.command) 142 143 self.name = '' 144 145 if not self.has_key('sact_type'): 146 self['sact_type'] = self['type'] 147 148 if not self.has_key('addressee'): 149 self['addressee'] = []
150 151 152 ## quick to make it compatibale with current code 153 ## self.type = self['type'] 154 ## self.actor = self['actor'] 155 ## self.object = self['object'] 156 ## self.item = self['item'] 157 ## self.property = self['property'] 158 159 160 ## for key in self.fields: 161 ## if not self.__dict__.has_key(key): 162 ## self.__dict__[key] = None 163 164 165 ## def instantiate(self,entities=[]): 166 ## if type(entities) is DictType: 167 ## for key in self.fields: 168 ## if entities.has_key(key): 169 ## self.__dict__[key] = entities[key] 170 ## elif type(entities) is ListType: 171 ## for key in self.fields: 172 ## for entity in entities: 173 ## try: 174 ## if entity.name == self.__dict__[key]: 175 ## self.__dict__[key] = entity 176 ## break 177 ## except AttributeError: 178 ## pass 179 ## else: 180 ## raise TypeError,`type(entities)`+' not supported by '+\ 181 ## 'PsychAgents instantiate method' 182 ## if self.command: 183 ## self.command.instantiate(entities) 184 ### Mei 185 ## def toString (self): 186 ## res=self.type 187 ## if self.actor: 188 ## tmp=self.actor 189 ## if not type(tmp) is StringType: 190 ## tmp = tmp.name 191 ## res=tmp+'-'+res 192 ## 193 ## if self.object: 194 ## tmp=self.object 195 ## if not type(tmp) is StringType: 196 ## tmp = tmp.name 197 ## res=res+'-'+tmp 198 ## if self.item: 199 ## res=res+'-'+self.item 200 ## if self.property: 201 ## res=res+'-'+self.property 202 ## 203 ## 204 ## return res 205 ## 206 ## def __getitem__(self,index): 207 ## if index in self.fields: 208 ## return self.__dict__[index] 209 ## else: 210 ## return KeyError,index 211 ## 212 ## def __setitem__(self,index,value): 213 ## if index in self.fields: 214 ## self.__dict__[index] = value 215 ## else: 216 ## return KeyError,index 217 ## 218 ## def __delitem__(self,index): 219 ## if index in self.fields: 220 ## self.__dict__[index] = None 221 ## else: 222 ## return KeyError,index 223 224 ## def keys(self): 225 ## list = [] 226 ## for key in self.fields: 227 ## if self.__dict__[key]: 228 ## list.append(key) 229 ## return list 230
231 - def __repr__(self):
232 """Translates a structured dictionary representation of an action 233 into a standardized string format (e.g., 234 {'actor':CLF,'type':'violence','object':UrbanPoor} becomes 235 'CLF-violence-to-UrbanPoor'""" 236 str = '' 237 try: 238 str = self['type'] 239 if self['object']: 240 obj = self['object'] 241 if not type(obj) is StringType: 242 obj = obj.name 243 str = str+'-to-'+obj 244 if self['actor']: 245 actor = self['actor'] 246 if not type(actor) is StringType: 247 actor = actor.name 248 str = actor+'-'+str 249 ## if self['command']: 250 ## str = str + '-' + `self.command` 251 252 except AttributeError: 253 pass 254 ## print obj 255 256 self.name = str 257 return self.name
258 259 260 if __name__ == '__main__': 261 ## from teamwork.examples.PsychSim.EntityFactory import * 262 263 #act = PsychAction('CortinaGov-wait') 264 #print act.keys() 265 #<type>-to-<object>-about-<item>-of-<property> 266 act = ThespianAction('request-to-oldman-about-name-of-town') 267 act['actor']='stu' 268 print act 269 270 ## actor = createEntity('ProGovPsyops','Coalition') 271 ## for actType in actor.getDefault('actions'): 272 ## act = PsychAction({'actor':actor.name, 273 ## 'type':actType, 274 ## 'object':'Poor'}) 275 ## print act 276 ## act = PsychAction('CortinaGov-command-to-UrbanPoor-violence-to-CLF') 277 ## copy = PsychAction(act) 278 ## if act == copy: 279 ## print 'yay!' 280