Package teamwork :: Package math :: Module ThespianKeys
[hide private]
[frames] | no frames]

Source Code for Module teamwork.math.ThespianKeys

  1  from Keys import * 
  2   
3 -class MessageKey(Key):
4 keyType = 'message' 5 slots = {'sact_type':Key.ACTION, 6 'sender':Key.ENTITY, 7 'addressee':Key.ENTITIES, 8 'lhs':Key.STATE, 9 } 10 decayRate = 0.5 11
12 - def instantiate(self,table):
13 """Utility method that takes a mapping from entity references (e.g., 'self', 'actor') to actual entity names (e.g., 'Bill', 'agent0'). It returns a new key string with the substitution applied. It also reduces any 'identity' keys to be either constant or null depending on whether the key matches the identity of the entity represented in the dictionary provided. Subclasses should override this method for more instantiation of more specialized fields. 14 @param table: A dictionary of label-value pairs. Any slot in the key whose filler matches a label in the given table will be replaced by the associated value. 15 @type table: C{dict} 16 @return: A new L{Key} instance with the appropriate label substitutions 17 @rtype: L{Key} 18 """ 19 keys=[] 20 if not self['sact_type'] or self['sact_type'] == 'wait': 21 22 for addressee in table['addressee']: 23 feature='obs-'+table['actor']+'-'+table['sact_type']+'-to-'+addressee 24 25 if table['sact_type'] in ['enquiry','inform','accept','reject','inform_info']: 26 feature += '-about-'+table['factors'][0]['lhs'][1]+ '-'+table['factors'][0]['lhs'][3] 27 28 key = StateKey({'entity':table['self'].name, 29 'feature':feature}) 30 ## print key 31 keys.append(key) 32 else: 33 34 for addressee in table['addressee']: 35 feature='obs-'+table['actor']+'-'+self['sact_type']+'-to-'+addressee 36 37 if table['sact_type'] in ['enquiry','inform','accept','reject','inform_info']: 38 feature += '-about-'+table['factors'][0]['lhs'][1]+ '-'+table['factors'][0]['lhs'][3] 39 40 key = StateKey({'entity':table['self'].name, 41 'feature':feature}) 42 ## print key 43 keys.append(key) 44 if len(keys) > 0: 45 return keys 46 else: 47 return keyDelete
48
49 - def simpleText(self):
50 content = self['sact_type'] 51 if self['addressee']: 52 for add in self['addressee']: 53 content += add+' ,' 54 if self['sender']: 55 content += ' by %s' % (self['sender']) 56 if self['lhs']: 57 content += ' about %s' % (self['lhs']) 58 return content
59
60 -class GroupStateKey(Key):
61 keyType = 'groupstate' 62 slots = {'entity':Key.ENTITY, 63 'feature':Key.STATE} 64
65 - def simpleText(self):
66 return '%s\'s %s' % (self['entity'],self['feature'])
67
68 -class MetaStateKey(Key):
69 keyType = 'meta' 70 slots = {'entity':Key.ENTITY, 71 'metafeature':Key.STATE} 72
73 - def simpleText(self):
74 return '%s\'s %s' % (self['entity'],self['metafeature'])
75 76
77 -class MessageEqualKey(Key):
78 keyType = 'equal' 79 slots = {'messageAttr':Key.STATE, 80 'value':Key.VALUE} 81
82 - def instantiate(self,table):
83 """Utility method that takes a mapping from entity references (e.g., 'self', 'actor') to actual entity names (e.g., 'Bill', 'agent0'). It returns a new key string with the substitution applied. It also reduces any 'identity' keys to be either constant or null depending on whether the key matches the identity of the entity represented in the dictionary provided. Subclasses should override this method for more instantiation of more specialized fields. 84 @param table: A dictionary of label-value pairs. Any slot in the key whose filler matches a label in the given table will be replaced by the associated value. 85 @type table: C{dict} 86 @return: A new L{Key} instance with the appropriate label substitutions 87 @rtype: L{Key} 88 """ 89 if table[self['messageAttr']] == table[self['value']]: 90 return keyConstant 91 else: 92 return keyDelete
93
94 - def simpleText(self):
95 return 'action['+self['messageAttr']+'] equals to '+self['value']
96
97 -class ObligKey(Key):
98 keyType = 'checkApply' 99 slots = {'obligation':Key.STATE, 100 'role':Key.VALUE} 101
102 - def instantiate(self,table):
103 """Utility method that takes a mapping from entity references (e.g., 'self', 'actor') to actual entity names (e.g., 'Bill', 'agent0'). It returns a new key string with the substitution applied. It also reduces any 'identity' keys to be either constant or null depending on whether the key matches the identity of the entity represented in the dictionary provided. Subclasses should override this method for more instantiation of more specialized fields. 104 @param table: A dictionary of label-value pairs. Any slot in the key whose filler matches a label in the given table will be replaced by the associated value. 105 @type table: C{dict} 106 @return: A new L{Key} instance with the appropriate label substitutions 107 @rtype: L{Key} 108 """ 109 ## obli-inform-to-Hamed-about-student-feature_nationality 110 ## obli-bye_resp-to-Xaled 111 ## print self['obligation'] 112 113 try: 114 dummy1,sact_type,dummy2,toobject,dummy3,entity,feature = string.split(self['obligation'],'-') 115 except ValueError: 116 try: 117 dummy1,sact_type,dummy2,toobject = string.split(self['obligation'],'-') 118 except: 119 print self 120 print table 121 raise 'Illegal obligation name: ',self['obligation'] 122 123 try: 124 actor = table['actor'].name 125 except AttributeError: 126 actor = table['actor'] 127 128 assert(isinstance(actor,str)) 129 130 if actor == toobject: 131 if table['sact_type']=='greet_init' and sact_type == 'greet_resp': 132 return keyConstant 133 elif table['sact_type']=='greet2_init' and sact_type == 'greet2_resp': 134 return keyConstant 135 elif table['sact_type']=='bye_init' and sact_type == 'bye_resp': 136 return keyConstant 137 elif table['sact_type']=='thank' and sact_type == 'urwelcome': 138 return keyConstant 139 elif table['sact_type']=='inform_info' and sact_type == 'OK': 140 return keyConstant 141 elif table['sact_type']=='request' and sact_type == 'accept': 142 return keyConstant 143 ## topic = table['factors'][0]['lhs'][1]+ '-'+table['factors'][0]['lhs'][3] 144 ## try: 145 ## entity = entity.name 146 ## except AttributeError: 147 ## pass 148 ## if topic == entity+'-'+feature: 149 ## loState[key] = hiState[key] = 1. 150 ## else: 151 ## pass 152 ## print topic, entity+'-'+feature 153 elif table['sact_type']=='enquiry' and sact_type == 'inform': 154 topic = table['factors'][0]['lhs'][1]+ '-'+table['factors'][0]['lhs'][3] 155 try: 156 entity = entity.name 157 except AttributeError: 158 pass 159 if topic == entity+'-'+feature: 160 return keyConstant 161 else: 162 pass 163 ## print topic, entity+'-'+feature 164 return keyDelete
165
166 - def simpleText(self):
167 return 'I have the obligation '+self['obligation']+' as an '+self['role']
168
169 -class ExistObligKey(Key):
170 keyType = 'checkExistOblig' 171 slots = {'entity':Key.ENTITY,} 172
173 - def instantiate(self,table):
174 """Utility method that takes a mapping from entity references (e.g., 'self', 'actor') to actual entity names (e.g., 'Bill', 'agent0'). It returns a new key string with the substitution applied. It also reduces any 'identity' keys to be either constant or null depending on whether the key matches the identity of the entity represented in the dictionary provided. Subclasses should override this method for more instantiation of more specialized fields. 175 @param table: A dictionary of label-value pairs. Any slot in the key whose filler matches a label in the given table will be replaced by the associated value. 176 @type table: C{dict} 177 @return: A new L{Key} instance with the appropriate label substitutions 178 @rtype: L{StateKey} 179 """ 180 keys = [] 181 for addressee in table['addressee']: 182 feature = 'obli-'+table['sact_type']+'-to-'+addressee 183 if table['sact_type'] in ['inform','accept','reject']: 184 feature += '-about-'+table['factors'][0]['lhs'][1]+ '-'+table['factors'][0]['lhs'][3] 185 ##print feature 186 keys.append(StateKey({'entity':table['self'].name, 187 'feature':feature})) 188 189 return keys
190
191 - def simpleText(self):
192 return 'I have the obligation to make this response'
193
194 -class GroupExistObligKey(Key):
195 keyType = 'checkGroupExistOblig' 196 slots = {'entity':Key.ENTITY,} 197
198 - def instantiate(self,table):
199 """Utility method that takes a mapping from entity references (e.g., 'self', 'actor') to actual entity names (e.g., 'Bill', 'agent0'). It returns a new key string with the substitution applied. It also reduces any 'identity' keys to be either constant or null depending on whether the key matches the identity of the entity represented in the dictionary provided. Subclasses should override this method for more instantiation of more specialized fields. 200 @param table: A dictionary of label-value pairs. Any slot in the key whose filler matches a label in the given table will be replaced by the associated value. 201 @type table: C{dict} 202 @return: A new L{Key} instance with the appropriate label substitutions 203 @rtype: L{StateKey} 204 """ 205 keys = [] 206 for addressee in table['addressee']: 207 feature = 'obli-'+table['sact_type']+'-to-'+addressee 208 209 if table['sact_type'] in ['inform','accept','reject']: 210 feature += '-about-'+table['factors'][0]['lhs'][1]+ '-'+table['factors'][0]['lhs'][3] 211 212 for entity1 in table['self'].getEntityBeliefs(): 213 if entity1.getState('group') == table['self'].getState('group'): 214 keys.append(StateKey({'entity':entity1.name, 215 'feature':feature})) 216 if len(keys) > 0: 217 return keys 218 else: 219 return keyDelete
220
221 - def simpleText(self):
222 return 'Somebody in the group have the obligation to make this response'
223
224 -class ExistUnFinOligKey(Key):
225 keyType = 'checkExistUnFinOblig' 226 slots = {'entity':Key.ENTITY,} 227
228 - def instantiate(self,table):
229 """Utility method that takes a mapping from entity references (e.g., 'self', 'actor') to actual entity names (e.g., 'Bill', 'agent0'). It returns a new key string with the substitution applied. It also reduces any 'identity' keys to be either constant or null depending on whether the key matches the identity of the entity represented in the dictionary provided. Subclasses should override this method for more instantiation of more specialized fields. 230 @param table: A dictionary of label-value pairs. Any slot in the key whose filler matches a label in the given table will be replaced by the associated value. 231 @type table: C{dict} 232 @return: A new L{Key} instance with the appropriate label substitutions 233 @rtype: L{Key} 234 """ 235 keys = [] 236 for feature in table['self'].getStateFeatures(): 237 if string.find(feature,'obli')==0 and feature != 'obligNorm': 238 keys.append(StateKey({'entity':table['self'].name, 239 'feature':feature})) 240 if len(keys) > 0: 241 return keys 242 else: 243 return keyDelete
244
245 - def simpleText(self):
246 return 'I have unfinished obligation'
247
248 -class GroupExistUnFinOligKey(Key):
249 keyType = 'checkGroupExistUnFinOblig' 250 slots = {'entity':Key.ENTITY,} 251
252 - def instantiate(self,table):
253 """Utility method that takes a mapping from entity references (e.g., 'self', 'actor') to actual entity names (e.g., 'Bill', 'agent0'). It returns a new key string with the substitution applied. It also reduces any 'identity' keys to be either constant or null depending on whether the key matches the identity of the entity represented in the dictionary provided. Subclasses should override this method for more instantiation of more specialized fields. 254 @param table: A dictionary of label-value pairs. Any slot in the key whose filler matches a label in the given table will be replaced by the associated value. 255 @type table: C{dict} 256 @return: A new L{Key} instance with the appropriate label substitutions 257 @rtype: L{Key} 258 """ 259 keys = [] 260 for entity1 in table['self'].getEntityBeliefs(): 261 for feature in entity1.getStateFeatures(): 262 if string.find(feature,'obli')==0 and not feature == 'obligNorm': 263 key = StateKey({'entity':entity1.name, 264 'feature':feature}) 265 keys.append(key) 266 if len(keys) > 0: 267 return keys 268 else: 269 return keyDelete
270
271 - def simpleText(self):
272 return 'Somebody in the group still have unfinished obligation'
273
274 -def makeGroupStateKey(entity,feature):
275 """Helper function for creating GroupStateKey objects 276 @param entity: The group of entity to be pointed to by this key 277 @type entity: string 278 @param feature: The state feature to be pointed to by this key 279 @type feature: string 280 @return: the corresponding GroupStateKey object""" 281 return GroupStateKey({'entity':entity,'feature':feature})
282 283
284 -def makeMessageKey(action):
285 """Returns a key correspond to the value of the given action""" 286 try: 287 return MessageKey({'sact_type':action['sact_type'], 288 'sender':action['sender'], 289 'addressee':action['addressee'], 290 'lhs':action['factors'][0]['lhs'],}) 291 except: 292 return MessageKey({'sact_type':action['sact_type'], 293 'sender':action['sender'], 294 'addressee':action['addressee'], 295 'lhs':'dummy',})
296
297 -def makeBelongKey(entity):
298 return IdentityKey({'relationship':'in', 299 'entity':entity})
300
301 -def makeMessageEqualKey(attr,value):
302 return EqualKey({'messageAttr':'attr', 303 'value':value})
304 305 ## if this state feature is the obligation related to the current action
306 -def makeObligKey(feature,role):
307 return ObligKey({'obligation':feature, 308 'role':role})
309
310 -def makeMetaStateKey(entity,feature):
311 """Returns a key correspond to the value of the given feature of the 312 given entity""" 313 return MetaStateKey({'entity':entity,'metafeature':feature})
314 315 ## if the current action is satisfying an existing obligation of the entity
316 -def makeExistObligKey(entity):
317 return ExistObligKey({'entity':entity})
318 319 ## if the current action is satisfying an existing obligation of somebody in the group
320 -def makeGroupExistObligKey(entity):
321 return GroupExistObligKey({'entity':entity})
322 323 ## if the agent has unsatisfied obligations
324 -def makeExistUnFinOligKey(entity):
325 return ExistUnFinOligKey({'entity':entity})
326 327 ## if any agent in the conversation has unsatisfied obligations
328 -def makeGroupExistUnFinOligKey(entity):
329 return GroupExistUnFinOligKey({'entity':entity})
330