Skip to content

Commit 33704d1

Browse files
chillraynirlipo
authored andcommitted
[19] Update unittest for action cost support
Resolves #19
1 parent eb5926a commit 33704d1

File tree

1 file changed

+101
-78
lines changed

1 file changed

+101
-78
lines changed

server/unit_test/test_cases.py

+101-78
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,18 @@
1515
import Solver, Initialise
1616
import Transfer
1717

18+
1819
class TestStringMethods(unittest.TestCase):
20+
# Empty animation profile for test case
21+
animation_profile_test = {"objects": {"default": {},
22+
"predefine": {},
23+
"custom": {}},
24+
"predicates_rules": {},
25+
"visual": {},
26+
"imageTable": {"m_keys": [],
27+
"m_values": []},
28+
"cost_keyword": None
29+
}
1930

2031
# Initial test case
2132
def test_upper(self):
@@ -48,16 +59,16 @@ def test_planimation_process(self):
4859

4960
url_link = ''
5061

51-
with open(os.path.abspath(os.path.dirname(__file__)
52-
+ '/test_files/' + 'domain_ap.pddl'), 'r', encoding='utf-8-sig') as f:
62+
with open(os.path.abspath(os.path.dirname(__file__)
63+
+ '/test_files/' + 'domain_ap.pddl'), 'r', encoding='utf-8-sig') as f:
5364
animation_content = f.read()
5465

55-
with open(os.path.abspath(os.path.dirname(__file__)
56-
+ '/test_files/' + 'domain.pddl'), 'r', encoding='utf-8-sig') as f:
66+
with open(os.path.abspath(os.path.dirname(__file__)
67+
+ '/test_files/' + 'domain.pddl'), 'r', encoding='utf-8-sig') as f:
5768
domain_content = f.read().lower()
58-
59-
with open(os.path.abspath(os.path.dirname(__file__)
60-
+ '/test_files/' + 'problem.pddl'), 'r', encoding='utf-8-sig') as f:
69+
70+
with open(os.path.abspath(os.path.dirname(__file__)
71+
+ '/test_files/' + 'problem.pddl'), 'r', encoding='utf-8-sig') as f:
6172
problem_content = f.read().lower()
6273

6374
plan = Plan_generator.get_plan(domain_content, problem_content, url_link)
@@ -68,28 +79,30 @@ def test_planimation_process(self):
6879

6980
animation_profile = json.loads(Animation_parser.get_animation_profile(animation_content, object_list))
7081

71-
stages = get_stages(plan, problem_dic, problem_content, predicates_list)
82+
stages = get_stages(plan, problem_dic, problem_content, predicates_list, animation_profile)
7283

7384
result = Solver.get_visualisation_dic(stages, animation_profile, plan['result']['plan'], problem_dic)
7485
objects_dic = Initialise.initialise_objects(stages["objects"], animation_profile)
75-
final = Transfer.generate_visualisation_file(result, list(objects_dic.keys()), animation_profile, plan['result']['plan'])
86+
final = Transfer.generate_visualisation_file(result, list(objects_dic.keys()), animation_profile,
87+
plan['result']['plan'])
7688

77-
with open(os.path.abspath(os.path.dirname(__file__)
78-
+ '/test_files/' + "test.vfg"), "w") as f:
89+
with open(os.path.abspath(os.path.dirname(__file__)
90+
+ '/test_files/' + "test.vfg"), "w") as f:
7991
json.dump(final, f)
8092

81-
with open(os.path.abspath(os.path.dirname(__file__)
82-
+ '/test_files/' + "test.vfg"), 'r', encoding='utf-8-sig') as f:
93+
with open(os.path.abspath(os.path.dirname(__file__)
94+
+ '/test_files/' + "test.vfg"), 'r', encoding='utf-8-sig') as f:
8395
output_content = f.read()
8496

85-
with open(os.path.abspath(os.path.dirname(__file__)
86-
+ '/test_files/' + "expect_output.vfg"), 'r', encoding='utf-8-sig') as f:
97+
with open(os.path.abspath(os.path.dirname(__file__)
98+
+ '/test_files/' + "expect_output.vfg"), 'r', encoding='utf-8-sig') as f:
8799
expected_content = f.read()
88100

89101
# Check that the generated vfg file is the same as the expected output
90102
self.assertTrue(filecmp.cmp(os.path.abspath(os.path.dirname(__file__) + '/test_files/' + 'test.vfg')
91-
, os.path.abspath(os.path.dirname(__file__) + '/test_files/' + 'expect_output.vfg'), shallow=False)
92-
, msg="The generated vfg file is different from the expected output.")
103+
, os.path.abspath(os.path.dirname(__file__) + '/test_files/' + 'expect_output.vfg'),
104+
shallow=False)
105+
, msg="The generated vfg file is different from the expected output.")
93106

94107
def test_domain_parser(self):
95108
""" Test case for testing the domain parser functions
@@ -103,24 +116,24 @@ def test_domain_parser(self):
103116
complex_predicates_list(JSON): The JSON structure generated by the Domain_parser.
104117
"""
105118

106-
with open(os.path.abspath(os.path.dirname(__file__)
107-
+ '/test_files/' + 'domain.pddl'), 'r', encoding='utf-8-sig') as f:
119+
with open(os.path.abspath(os.path.dirname(__file__)
120+
+ '/test_files/' + 'domain.pddl'), 'r', encoding='utf-8-sig') as f:
108121
simple_domain_content = f.read().lower()
109122

110123
simple_predicates_list = Domain_parser.get_domain_json(simple_domain_content)
111124
# simple domain predicates:
112125
# (:predicates
113126
# (connected ?x ?y - place)
114-
# (at-robot ?x - place)
115-
# (visited ?x - place))
127+
# (at-robot ?x - place)
128+
# (visited ?x - place))
116129
simple_expected_predicates = {'connected': 2, 'at-robot': 1, 'visited': 1}
117130

118131
# Check that the generated simple predicates list is the same as the expected list
119132
self.assertTrue(simple_predicates_list == simple_expected_predicates
120-
, msg="The generated simple predicates list is different from the expected list")
133+
, msg="The generated simple predicates list is different from the expected list")
121134

122-
with open(os.path.abspath(os.path.dirname(__file__)
123-
+ '/test_files/' + '4_ops_domain.pddl'), 'r', encoding='utf-8-sig') as f:
135+
with open(os.path.abspath(os.path.dirname(__file__)
136+
+ '/test_files/' + '4_ops_domain.pddl'), 'r', encoding='utf-8-sig') as f:
124137
complex_domain_content = f.read().lower()
125138

126139
complex_predicates_list = Domain_parser.get_domain_json(complex_domain_content)
@@ -150,12 +163,12 @@ def test_problem_parser(self):
150163
complex_problem_list(JSON): The JSON structure generated by the Problem_parser.
151164
"""
152165

153-
with open(os.path.abspath(os.path.dirname(__file__)
154-
+ '/test_files/' + '4_ops_problem.pddl'), 'r', encoding='utf-8-sig') as f:
166+
with open(os.path.abspath(os.path.dirname(__file__)
167+
+ '/test_files/' + '4_ops_problem.pddl'), 'r', encoding='utf-8-sig') as f:
155168
simple_problem_content = f.read().lower()
156169

157-
with open(os.path.abspath(os.path.dirname(__file__)
158-
+ '/test_files/' + '4_ops_domain.pddl'), 'r', encoding='utf-8-sig') as f:
170+
with open(os.path.abspath(os.path.dirname(__file__)
171+
+ '/test_files/' + '4_ops_domain.pddl'), 'r', encoding='utf-8-sig') as f:
159172
simple_domain_content = f.read().lower()
160173

161174
simple_predicates_list = Domain_parser.get_domain_json(simple_domain_content)
@@ -172,30 +185,30 @@ def test_problem_parser(self):
172185
{'goal': [
173186
{'name': 'on', 'objectNames': ['a', 'b']},
174187
{'name': 'on', 'objectNames': ['b', 'c']}],
175-
'goal-condition': ['and']}]
188+
'goal-condition': ['and']}]
176189

177190
# Check that the generated simple problem list is the same as the expected output
178191
self.assertTrue(simple_problem_list == simple_expected_problem
179-
, msg="The generated simple problem list is different from the expected output")
192+
, msg="The generated simple problem list is different from the expected output")
180193

181-
with open(os.path.abspath(os.path.dirname(__file__)
182-
+ '/test_files/' + 'problem.pddl'), 'r', encoding='utf-8-sig') as f:
194+
with open(os.path.abspath(os.path.dirname(__file__)
195+
+ '/test_files/' + 'problem.pddl'), 'r', encoding='utf-8-sig') as f:
183196
complex_problem_content = f.read().lower()
184-
185-
with open(os.path.abspath(os.path.dirname(__file__)
186-
+ '/test_files/' + 'domain.pddl'), 'r', encoding='utf-8-sig') as f:
197+
198+
with open(os.path.abspath(os.path.dirname(__file__)
199+
+ '/test_files/' + 'domain.pddl'), 'r', encoding='utf-8-sig') as f:
187200
complex_domain_content = f.read().lower()
188-
201+
189202
complex_predicates_list = Domain_parser.get_domain_json(complex_domain_content)
190203
complex_problem_list = get_problem_dic(complex_problem_content, complex_predicates_list)
191204

192-
with open(os.path.abspath(os.path.dirname(__file__)
193-
+ '/test_files/' + "complex_expected_problem.json"), "r") as f:
205+
with open(os.path.abspath(os.path.dirname(__file__)
206+
+ '/test_files/' + "complex_expected_problem.json"), "r") as f:
194207
complex_expected_problem = json.load(f)
195208

196209
# Check that the generated complex problem list is the same as the expected output
197210
self.assertTrue(complex_expected_problem == complex_problem_list
198-
, msg="The generated complex problem list is different from the expected output")
211+
, msg="The generated complex problem list is different from the expected output")
199212

200213
def test_predicates_generator(self):
201214
""" Test case for testing the predicates generator function
@@ -213,12 +226,12 @@ def test_predicates_generator(self):
213226
complex_stages(stage): The stage structure generated by the Predicates_parser.
214227
"""
215228

216-
with open(os.path.abspath(os.path.dirname(__file__)
217-
+ '/test_files/' + '4_ops_domain.pddl'), 'r', encoding='utf-8-sig') as f:
229+
with open(os.path.abspath(os.path.dirname(__file__)
230+
+ '/test_files/' + '4_ops_domain.pddl'), 'r', encoding='utf-8-sig') as f:
218231
simple_domain_content = f.read().lower()
219232

220-
with open(os.path.abspath(os.path.dirname(__file__)
221-
+ '/test_files/' + '4_ops_problem.pddl'), 'r', encoding='utf-8-sig') as f:
233+
with open(os.path.abspath(os.path.dirname(__file__)
234+
+ '/test_files/' + '4_ops_problem.pddl'), 'r', encoding='utf-8-sig') as f:
222235
simple_problem_content = f.read().lower()
223236

224237
with open(os.path.abspath(os.path.dirname(__file__)
@@ -228,22 +241,23 @@ def test_predicates_generator(self):
228241
simple_predicates_list = Domain_parser.get_domain_json(simple_domain_content)
229242
simple_problem_dic = get_problem_dic(simple_problem_content, simple_predicates_list)
230243
simple_object_list = get_object_list(simple_problem_content)
231-
simple_stages = get_stages(simple_plan, simple_problem_dic, simple_problem_content, simple_predicates_list)
232-
233-
with open(os.path.abspath(os.path.dirname(__file__)
234-
+ '/test_files/' + "simple_expected_stages.json"), "r") as f:
244+
simple_stages = get_stages(simple_plan, simple_problem_dic, simple_problem_content, simple_predicates_list,
245+
self.animation_profile_test)
246+
247+
with open(os.path.abspath(os.path.dirname(__file__)
248+
+ '/test_files/' + "simple_expected_stages.json"), "r") as f:
235249
simple_expected_stages = json.load(f)
236-
250+
237251
# Check that the generated simple stages are the same as the expected output
238252
self.assertTrue(simple_stages == simple_expected_stages
239-
, msg="The generated simple stages are different from the expected output")
253+
, msg="The generated simple stages are different from the expected output")
240254

241-
with open(os.path.abspath(os.path.dirname(__file__)
242-
+ '/test_files/' + 'domain.pddl'), 'r', encoding='utf-8-sig') as f:
255+
with open(os.path.abspath(os.path.dirname(__file__)
256+
+ '/test_files/' + 'domain.pddl'), 'r', encoding='utf-8-sig') as f:
243257
complex_domain_content = f.read().lower()
244-
245-
with open(os.path.abspath(os.path.dirname(__file__)
246-
+ '/test_files/' + 'problem.pddl'), 'r', encoding='utf-8-sig') as f:
258+
259+
with open(os.path.abspath(os.path.dirname(__file__)
260+
+ '/test_files/' + 'problem.pddl'), 'r', encoding='utf-8-sig') as f:
247261
complex_problem_content = f.read().lower()
248262

249263
with open(os.path.abspath(os.path.dirname(__file__)
@@ -253,19 +267,19 @@ def test_predicates_generator(self):
253267
complex_predicates_list = Domain_parser.get_domain_json(complex_domain_content)
254268
complex_problem_dic = get_problem_dic(complex_problem_content, complex_predicates_list)
255269
complex_object_list = get_object_list(complex_problem_content)
256-
complex_stages = get_stages(complex_plan, complex_problem_dic, complex_problem_content, complex_predicates_list)
257-
258-
with open(os.path.abspath(os.path.dirname(__file__)
259-
+ '/test_files/' + "complex_expected_stages.json"), "r") as f:
270+
complex_stages = get_stages(complex_plan, complex_problem_dic, complex_problem_content, complex_predicates_list,
271+
self.animation_profile_test)
272+
with open(os.path.abspath(os.path.dirname(__file__)
273+
+ '/test_files/' + "complex_expected_stages.json"), "r") as f:
260274
complex_expected_stages = json.load(f)
261275

262276
# Check that the generated complex stages are the same as the expected output
263277
self.assertTrue(complex_stages == complex_expected_stages
264-
, msg="The generated complex stages are different from the expected output")
278+
, msg="The generated complex stages are different from the expected output")
265279

266280
def test_solver(self):
267281
""" Test case for testing the solver functions
268-
282+
269283
Expected Input:
270284
domain_ap.pddl, problem.pddl: The animation and problem PDDL files.
271285
expected_initialised_obj_dict.json: The expected output JSON file.
@@ -275,47 +289,56 @@ def test_solver(self):
275289
expected_obj_name_prop(object): The object generated by the solver functions.
276290
"""
277291

278-
with open(os.path.abspath(os.path.dirname(__file__)
279-
+ '/test_files/' + 'domain_ap.pddl'), 'r', encoding='utf-8-sig') as f:
292+
with open(os.path.abspath(os.path.dirname(__file__)
293+
+ '/test_files/' + 'domain_ap.pddl'), 'r', encoding='utf-8-sig') as f:
280294
animation_content = f.read()
281-
282-
with open(os.path.abspath(os.path.dirname(__file__)
283-
+ '/test_files/' + 'problem.pddl'), 'r', encoding='utf-8-sig') as f:
295+
296+
with open(os.path.abspath(os.path.dirname(__file__)
297+
+ '/test_files/' + 'problem.pddl'), 'r', encoding='utf-8-sig') as f:
284298
problem_content = f.read().lower()
285299

286300
object_list = get_object_list(problem_content)
287301
animation_profile = json.loads(Animation_parser.get_animation_profile(animation_content, object_list))
288302
initialised_obj_dict = Initialise.initialise_objects(object_list, animation_profile)
289-
290-
with open(os.path.abspath(os.path.dirname(__file__)
291-
+ '/test_files/' + "expected_initialised_obj_dict.json"), "r") as f:
303+
304+
with open(os.path.abspath(os.path.dirname(__file__)
305+
+ '/test_files/' + "expected_initialised_obj_dict.json"), "r") as f:
292306
expected_initialised_obj_dict = json.load(f)
293-
307+
294308
# Check that the generated object dict is the same as the expected output
295309
self.assertTrue(initialised_obj_dict == expected_initialised_obj_dict
296-
, msg="The generated object dict is different from the expected output")
310+
, msg="The generated object dict is different from the expected output")
297311

298312
# testing check rule
299313
true_predicate = {'name': 'at-robot', 'objectNames': ['loc1_1']}
300314
false_predicate = {'name': 'at-robot', 'objectNames': ['loc1_2']}
301315
objects_dic = {
302-
'loc1_1': {'prefabimage': 'img-square', 'showname': False, 'x': 100, 'y': 100, 'color': {'r': 0.0, 'g': 0.0, 'b': 1.0, 'a': 1.0}, 'width': 80, 'height': 80, 'depth': 1, 'name': 'loc1_1'},
303-
'loc1_2': {'prefabimage': 'img-square', 'showname': False, 'x': 100, 'y': False, 'color': {'r': 0.0, 'g': 0.0, 'b': 1.0, 'a': 1.0}, 'width': 80, 'height': 80, 'depth': 1, 'name': 'loc1_2'},
304-
'robot': {'prefabimage': 'img-robot', 'showname': False, 'x': 100, 'y': 100, 'color': {'r': 0.9804, 'g': 0.6353, 'b': 0.7098, 'a': 1.0}, 'width': 40, 'height': 40, 'depth': 2, 'name': 'robot'}}
316+
'loc1_1': {'prefabimage': 'img-square', 'showname': False, 'x': 100, 'y': 100,
317+
'color': {'r': 0.0, 'g': 0.0, 'b': 1.0, 'a': 1.0}, 'width': 80, 'height': 80, 'depth': 1,
318+
'name': 'loc1_1'},
319+
'loc1_2': {'prefabimage': 'img-square', 'showname': False, 'x': 100, 'y': False,
320+
'color': {'r': 0.0, 'g': 0.0, 'b': 1.0, 'a': 1.0}, 'width': 80, 'height': 80, 'depth': 1,
321+
'name': 'loc1_2'},
322+
'robot': {'prefabimage': 'img-robot', 'showname': False, 'x': 100, 'y': 100,
323+
'color': {'r': 0.9804, 'g': 0.6353, 'b': 0.7098, 'a': 1.0}, 'width': 40, 'height': 40, 'depth': 2,
324+
'name': 'robot'}}
305325
predicates_rules = {'at-robot':
306326
{'rules': ['rule1', 'rule2', 'rule3'],
307-
'rule1': {'left': {'robot': ['x']},'value': {'equal': {'?x': 'x'}}},
327+
'rule1': {'left': {'robot': ['x']}, 'value': {'equal': {'?x': 'x'}}},
308328
'rule2': {'left': {'robot': ['y']}, 'value': {'equal': {'?x': 'y'}}},
309-
'rule3': {'left': {'?x': ['color']}, 'value': {'equal': {'r': 0.9804, 'g': 0.6353, 'b': 0.7098, 'a': 1.0}}},
329+
'rule3': {'left': {'?x': ['color']},
330+
'value': {'equal': {'r': 0.9804, 'g': 0.6353, 'b': 0.7098, 'a': 1.0}}},
310331
'require': {'?x': ['x', 'y']},
311332
'objects': ['?x'],
312333
'custom_obj': ['robot']}}
313334

314335
# Check that the solver working with the expected predicate, objects dict and predicates rules
315336
self.assertTrue(Solver.check_rule_complete(true_predicate, objects_dic, predicates_rules)
316-
, msg="The input predicate list, objects dict and predicates rules didn't match the expected input params for the solver")
337+
,
338+
msg="The input predicate list, objects dict and predicates rules didn't match the expected input params for the solver")
317339
self.assertFalse(Solver.check_rule_complete(false_predicate, objects_dic, predicates_rules)
318-
, msg="The input params to the solver are fake data, but they match the expected input for the solver")
340+
,
341+
msg="The input params to the solver are fake data, but they match the expected input for the solver")
319342

320343
# test get obj name
321344
property_dic = {'?x': ['property1', 'property2']}
@@ -325,7 +348,7 @@ def test_solver(self):
325348

326349
# Check that the generated object is the same as the expected output
327350
self.assertTrue(expected_obj_name_prop == obj_name_prop
328-
, msg="The generated object is different from the expected output")
351+
, msg="The generated object is different from the expected output")
329352

330353

331354
if __name__ == '__main__':

0 commit comments

Comments
 (0)