Package teamwork :: Package agent :: Module AgentClasses
[hide private]
[frames] | no frames]

Source Code for Module teamwork.agent.AgentClasses

  1   
  2  classHierarchy = {} 
  3   
  4  classHierarchy['Entity'] = { 
  5      # Default goal weights 
  6      'goals': [], 
  7      # Default horizon on lookahead 
  8      'horizon': 1, 
  9      # Default depth on belief recursion 
 10      'depth': 2, 
 11      # Default set of possible models 
 12      'models': {}, 
 13      # Current model used as basis for this entity 
 14      'model': None, 
 15      # Default relationships 
 16      'relationships': {}, 
 17      # Default initially public persona 
 18      'persona': {}, 
 19      # Default set of actions 
 20      'actions': {'type':None}, 
 21      # For UI purposes... 
 22      'full-name': "Generic Entity", 
 23      'subtype-name': None, 
 24      # this class should be considered abstract 
 25      'abstract': 1, 
 26      # default count for scenerio creation 
 27      'defaultCount': 0 
 28      } 
 29   
30 -def getClassAttrib( classId, attribId ):
31 classObj = classHierarchy[ classId ] 32 if( classObj.has_key( attribId ) ): 33 return classObj[ attribId ] 34 # if( classId == None ): 35 # throw new KeyError #or however Python does it 36 else: 37 for parent in classObj['parent']: 38 try: 39 return getClassAttrib(parent, attribId ) 40 except KeyError: 41 pass 42 else: 43 raise KeyError,'%s has no feature %s' % (classId,attribId)
44 45
46 -def isSubclassOf( class1, class2 ):
47 #print "isSubclassOf( ", class1, ", ", class2, " )" 48 assert classHierarchy.has_key( class1 ) 49 assert classHierarchy.has_key( class2 ) 50 51 # dp: Modified to handle new multiple-inheritance taxonomy 52 classList = [class1] 53 while len(classList) > 0: 54 curClass = classList.pop() 55 if class2 == curClass: 56 return 1 57 else: 58 try: 59 classList += classHierarchy[curClass]['parent'] 60 except KeyError: 61 pass 62 return None
63 ## if( class1 == class2 ): 64 ## return 1 65 ## curClass = class1 66 ## while( curClass != None ): 67 ## curClass = classHierarchy[curClass]['parent'] 68 ## if( curClass == class2 ): 69 ## return 1 70 ## return None 71
72 -def createEntityClass( className, parentClass ):
73 try: 74 assert classHierarchy.has_key( parentClass ) 75 if not classHierarchy.has_key( className ): 76 77 newClass = { 'parent': parentClass } 78 classHierarchy[ className ] = newClass 79 except AssertionError: 80 return 81 82 return classHierarchy[ className ]
83 84 85 86 # Return an alphabetical list of Entity class name
87 -def getEntityClassList( classHierarchy ):
88 # The class names 89 names = classHierarchy.keys() 90 names.remove(None) 91 names.sort() 92 93 return names
94 95 # Returns a new dict: class Id -> dict sub-class Id, etc...
96 -def buildHierarchicalEntityDict( classHierarchy ):
97 names = getEntityClassList( classHierarchy ) 98 99 #remove None (the hierarchy root) 100 names.pop(0) 101 # init dicts 102 root = {} 103 dicts = { None: root } 104 105 for name in names: 106 if( dicts.has_key( name ) ): 107 dict = dicts[name] 108 else: 109 dict = {} 110 dicts[name] = dict 111 112 # dp: rewritten to handle multiple parents...not sure if it's 113 # doing the intended thing anymore 114 classList = classHierarchy[name]['parent'] 115 if len(classList) == 0: 116 classList = [None] 117 for parent in classList: 118 if( dicts.has_key( parent ) ): 119 pDict = dicts[parent] 120 else: 121 pDict = {} 122 dicts[parent] = pDict 123 124 pDict[name] = dict 125 return dicts # The generic entity and it's sub-classes
126 127 # Returns a new dict: class Id -> dict sub-class Id, etc... 128 ##def buildUserHierarchicalEntityDict( classHierarchy ): 129 ## names = getEntityClassList() 130 131 ## #remove None (the hierarchy root) 132 ## names.pop(0) 133 ## # init dicts 134 ## root = {} 135 ## dicts = { None: root } 136 137 ## for name in names: 138 ## dict = {} 139 ## if( dicts.has_key( name ) ): 140 ## dict = dicts[name] 141 ## else: 142 ## dicts[name] = dict 143 144 ## parent = classHierarchy[name]['parent'] 145 ## pDict = {} 146 ## if( dicts.has_key( parent ) ): 147 ## pDict = dicts[parent] 148 ## else: 149 ## dicts[parent] = pDict 150 151 ## pDict[name] = dict 152 153 ## return { None: root } # The generic entity and it's sub-classes 154 155 156 157 #def printEntityHierarchy( classhierarchy=defaultClassHierarchy ):
158 -def printEntityHierarchy():
159 printEntityHierarchyImpl( buildHierarchicalEntityDict(classHierarchy), '' )
160
161 -def printEntityHierarchyImpl( dict, indent ):
162 children = dict.keys() 163 children.sort() 164 for childId in children: 165 child = classHierarchy[childId] 166 try: 167 name = child['full-name'] 168 if( child.has_key( 'subtype-name' ) and 169 child['subtype-name'] != None ): 170 name = child['subtype-name'] 171 print( indent+name+" ("+repr(childId)+")" ) 172 printEntityHierarchyImpl( dict[childId], ' '+indent ) 173 except KeyError: 174 pass
175 176 177 178 179 180 if __name__=='__main__': 181 # print( repr( getEntityHierarchy() ) ) 182 printEntityHierarchy() 183