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
10 """Tests the XML packing and unpacking of scenarios (and some of the components)"""
11
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
33 """Tests the equality of the two given entities
34 """
35 self.assertEqual(e1.name,e2.name)
36
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
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
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
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
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
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
92
109
116
117 if __name__ == '__main__':
118 unittest.main()
119