Package teamwork :: Package test :: Package multiagent :: Module testXML
[hide private]
[frames] | no frames]

Source Code for Module teamwork.test.multiagent.testXML

  1  from teamwork.agent.Entities import * 
  2  from teamwork.agent.Generic import * 
  3  from teamwork.multiagent.sequential import * 
  4  from teamwork.multiagent.GenericSociety import * 
  5  from teamwork.agent.DefaultBased import createEntity 
  6  from teamwork.examples.PSYOP.Society import * 
  7  import unittest 
  8   
9 -class TestXML(unittest.TestCase):
10 """Tests the XML packing and unpacking of scenarios (and some of the components)""" 11
12 - def setUp(self):
13 """Creates the instantiated scenario used for testing""" 14 self.society = GenericSociety() 15 self.society.importDict(classHierarchy) 16 entities = [] 17 self.instances = {'Turkomen':['ITF'], 18 'Kurds':['KDP'], 19 'US':['US'], 20 'GeographicArea':['NorthernIraq'] 21 } 22 for cls,names in self.instances.items(): 23 for name in names: 24 entity = createEntity(cls,name,self.society,PsychEntity) 25 entities.append(entity) 26 if cls in ['Turkomen','Kurds']: 27 entity.relationships = {'location':['NorthernIraq']} 28 self.entities = SequentialAgents(entities) 29 self.entities.applyDefaults() 30 self.entities.compileDynamics()
31
32 - def verifyEntities(self,e1,e2):
33 """Tests the equality of the two given entities 34 """ 35 self.assertEqual(e1.name,e2.name) 36 # Test state 37 self.assertEqual(e1.state,e2.state) 38 for feature in e1.getStateFeatures(): 39 self.assert_(feature in e2.getStateFeatures()) 40 for feature in e2.getStateFeatures(): 41 self.assert_(feature in e1.getStateFeatures()) 42 # Test beliefs 43 for belief in e1.getEntityBeliefs(): 44 self.verifyEntities(belief,e2[belief.name]) 45 for belief in e2.getEntityBeliefs(): 46 self.assert_(belief.name in e1.getEntities()) 47 if e1.parent: 48 self.assertEqual(e1.parent.ancestry(),e2.parent.ancestry()) 49 else: 50 self.assertEqual(e2.parent,None) 51 # Test actions 52 for action in e1.actions.getOptions(): 53 self.assert_(action in e2.actions.getOptions()) 54 for action in e2.actions.getOptions(): 55 self.assert_(action in e1.actions.getOptions()) 56 # Test relationships 57 for label,agents in e1.relationships.items(): 58 self.assert_(e2.relationships.has_key(label)) 59 for name in agents: 60 self.assert_(name in e2.relationships[label]) 61 for label,agents in e2.relationships.items(): 62 self.assert_(e1.relationships.has_key(label)) 63 for name in agents: 64 self.assert_(name in e1.relationships[label]) 65 # Test goals 66 for goal in e1.getGoals(): 67 self.assertAlmostEqual(e1.getGoalWeight(goal),e2.getGoalWeight(goal),8, 68 '%s has incorrect weight for goal %s' % \ 69 (e2.ancestry(),goal)) 70 for goal in e2.getGoals(): 71 self.assertAlmostEqual(e1.getGoalWeight(goal),e2.getGoalWeight(goal),8) 72 # Test dynamics 73 for feature,subDict in e1.dynamics.items(): 74 self.assert_(e2.dynamics.has_key(feature)) 75 for actType,dynamics in subDict.items(): 76 if isinstance(actType,str): 77 self.assert_(e2.dynamics[feature].has_key(actType), 78 '%s missing dynamics of %s in response to %s' % \ 79 (e2.ancestry(),feature,actType)) 80 tree1 = dynamics.getTree() 81 tree2 = e2.dynamics[feature][actType].getTree() 82 self.assertEqual(tree2,tree1, 83 'Error in %s\'s dynamics of %s in response to %s' % \ 84 (e2.ancestry(),feature,actType))
85
86 - def DONTtestEntityXML(self):
87 for entity in self.entities.members(): 88 doc = entity.__xml__() 89 new = PsychEntity('dummy') 90 new.parse(doc.documentElement) 91 self.verifyEntities(entity,new)
92
93 - def testScenarioXML(self):
94 doc = self.entities.__xml__() 95 entities = SequentialAgents() 96 entities.parse(doc.documentElement,PsychEntity) 97 for entity in self.entities.members(): 98 self.verifyEntities(entity,entities[entity.name]) 99 self.assertEqual(len(self.entities.dynamics),len(entities.dynamics)) 100 for action,old in self.entities.dynamics.items(): 101 self.assert_(entities.dynamics.has_key(action)) 102 new = entities.dynamics[action] 103 self.assert_(new.has_key('state'), 104 'Missing state dynamics for %s' % (action)) 105 self.assertEqual(old['state'].getTree(),new['state'].getTree()) 106 self.assert_(new.has_key('actions'), 107 'Missing action dynamics for %s' % (action)) 108 self.assertEqual(old['actions'],new['actions'])
109
110 - def DONTtestSocietyXML(self):
111 doc = self.society.__xml__() 112 society = GenericSociety() 113 society.parse(doc.documentElement) 114 for entity in self.society.members(): 115 self.verifyEntities(entity,society[entity.name])
116 117 if __name__ == '__main__': 118 unittest.main() 119