From 33704d1b18c4ba8a3f6ebd67614f5d4bae67fbf7 Mon Sep 17 00:00:00 2001 From: chillray <52360130+chillray@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:12:01 +1000 Subject: [PATCH] [19] Update unittest for action cost support Resolves #19 --- server/unit_test/test_cases.py | 179 +++++++++++++++++++-------------- 1 file changed, 101 insertions(+), 78 deletions(-) diff --git a/server/unit_test/test_cases.py b/server/unit_test/test_cases.py index 8e1fbbd..3d38e41 100644 --- a/server/unit_test/test_cases.py +++ b/server/unit_test/test_cases.py @@ -15,7 +15,18 @@ import Solver, Initialise import Transfer + class TestStringMethods(unittest.TestCase): + # Empty animation profile for test case + animation_profile_test = {"objects": {"default": {}, + "predefine": {}, + "custom": {}}, + "predicates_rules": {}, + "visual": {}, + "imageTable": {"m_keys": [], + "m_values": []}, + "cost_keyword": None + } # Initial test case def test_upper(self): @@ -48,16 +59,16 @@ def test_planimation_process(self): url_link = '' - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + 'domain_ap.pddl'), 'r', encoding='utf-8-sig') as f: + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + 'domain_ap.pddl'), 'r', encoding='utf-8-sig') as f: animation_content = f.read() - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + 'domain.pddl'), 'r', encoding='utf-8-sig') as f: + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + 'domain.pddl'), 'r', encoding='utf-8-sig') as f: domain_content = f.read().lower() - - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + 'problem.pddl'), 'r', encoding='utf-8-sig') as f: + + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + 'problem.pddl'), 'r', encoding='utf-8-sig') as f: problem_content = f.read().lower() plan = Plan_generator.get_plan(domain_content, problem_content, url_link) @@ -68,28 +79,30 @@ def test_planimation_process(self): animation_profile = json.loads(Animation_parser.get_animation_profile(animation_content, object_list)) - stages = get_stages(plan, problem_dic, problem_content, predicates_list) + stages = get_stages(plan, problem_dic, problem_content, predicates_list, animation_profile) result = Solver.get_visualisation_dic(stages, animation_profile, plan['result']['plan'], problem_dic) objects_dic = Initialise.initialise_objects(stages["objects"], animation_profile) - final = Transfer.generate_visualisation_file(result, list(objects_dic.keys()), animation_profile, plan['result']['plan']) + final = Transfer.generate_visualisation_file(result, list(objects_dic.keys()), animation_profile, + plan['result']['plan']) - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + "test.vfg"), "w") as f: + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + "test.vfg"), "w") as f: json.dump(final, f) - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + "test.vfg"), 'r', encoding='utf-8-sig') as f: + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + "test.vfg"), 'r', encoding='utf-8-sig') as f: output_content = f.read() - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + "expect_output.vfg"), 'r', encoding='utf-8-sig') as f: + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + "expect_output.vfg"), 'r', encoding='utf-8-sig') as f: expected_content = f.read() # Check that the generated vfg file is the same as the expected output self.assertTrue(filecmp.cmp(os.path.abspath(os.path.dirname(__file__) + '/test_files/' + 'test.vfg') - , os.path.abspath(os.path.dirname(__file__) + '/test_files/' + 'expect_output.vfg'), shallow=False) - , msg="The generated vfg file is different from the expected output.") + , os.path.abspath(os.path.dirname(__file__) + '/test_files/' + 'expect_output.vfg'), + shallow=False) + , msg="The generated vfg file is different from the expected output.") def test_domain_parser(self): """ Test case for testing the domain parser functions @@ -103,24 +116,24 @@ def test_domain_parser(self): complex_predicates_list(JSON): The JSON structure generated by the Domain_parser. """ - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + 'domain.pddl'), 'r', encoding='utf-8-sig') as f: + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + 'domain.pddl'), 'r', encoding='utf-8-sig') as f: simple_domain_content = f.read().lower() simple_predicates_list = Domain_parser.get_domain_json(simple_domain_content) # simple domain predicates: # (:predicates # (connected ?x ?y - place) - # (at-robot ?x - place) - # (visited ?x - place)) + # (at-robot ?x - place) + # (visited ?x - place)) simple_expected_predicates = {'connected': 2, 'at-robot': 1, 'visited': 1} # Check that the generated simple predicates list is the same as the expected list self.assertTrue(simple_predicates_list == simple_expected_predicates - , msg="The generated simple predicates list is different from the expected list") + , msg="The generated simple predicates list is different from the expected list") - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + '4_ops_domain.pddl'), 'r', encoding='utf-8-sig') as f: + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + '4_ops_domain.pddl'), 'r', encoding='utf-8-sig') as f: complex_domain_content = f.read().lower() complex_predicates_list = Domain_parser.get_domain_json(complex_domain_content) @@ -150,12 +163,12 @@ def test_problem_parser(self): complex_problem_list(JSON): The JSON structure generated by the Problem_parser. """ - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + '4_ops_problem.pddl'), 'r', encoding='utf-8-sig') as f: + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + '4_ops_problem.pddl'), 'r', encoding='utf-8-sig') as f: simple_problem_content = f.read().lower() - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + '4_ops_domain.pddl'), 'r', encoding='utf-8-sig') as f: + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + '4_ops_domain.pddl'), 'r', encoding='utf-8-sig') as f: simple_domain_content = f.read().lower() simple_predicates_list = Domain_parser.get_domain_json(simple_domain_content) @@ -172,30 +185,30 @@ def test_problem_parser(self): {'goal': [ {'name': 'on', 'objectNames': ['a', 'b']}, {'name': 'on', 'objectNames': ['b', 'c']}], - 'goal-condition': ['and']}] + 'goal-condition': ['and']}] # Check that the generated simple problem list is the same as the expected output self.assertTrue(simple_problem_list == simple_expected_problem - , msg="The generated simple problem list is different from the expected output") + , msg="The generated simple problem list is different from the expected output") - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + 'problem.pddl'), 'r', encoding='utf-8-sig') as f: + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + 'problem.pddl'), 'r', encoding='utf-8-sig') as f: complex_problem_content = f.read().lower() - - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + 'domain.pddl'), 'r', encoding='utf-8-sig') as f: + + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + 'domain.pddl'), 'r', encoding='utf-8-sig') as f: complex_domain_content = f.read().lower() - + complex_predicates_list = Domain_parser.get_domain_json(complex_domain_content) complex_problem_list = get_problem_dic(complex_problem_content, complex_predicates_list) - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + "complex_expected_problem.json"), "r") as f: + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + "complex_expected_problem.json"), "r") as f: complex_expected_problem = json.load(f) # Check that the generated complex problem list is the same as the expected output self.assertTrue(complex_expected_problem == complex_problem_list - , msg="The generated complex problem list is different from the expected output") + , msg="The generated complex problem list is different from the expected output") def test_predicates_generator(self): """ Test case for testing the predicates generator function @@ -213,12 +226,12 @@ def test_predicates_generator(self): complex_stages(stage): The stage structure generated by the Predicates_parser. """ - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + '4_ops_domain.pddl'), 'r', encoding='utf-8-sig') as f: + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + '4_ops_domain.pddl'), 'r', encoding='utf-8-sig') as f: simple_domain_content = f.read().lower() - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + '4_ops_problem.pddl'), 'r', encoding='utf-8-sig') as f: + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + '4_ops_problem.pddl'), 'r', encoding='utf-8-sig') as f: simple_problem_content = f.read().lower() with open(os.path.abspath(os.path.dirname(__file__) @@ -228,22 +241,23 @@ def test_predicates_generator(self): simple_predicates_list = Domain_parser.get_domain_json(simple_domain_content) simple_problem_dic = get_problem_dic(simple_problem_content, simple_predicates_list) simple_object_list = get_object_list(simple_problem_content) - simple_stages = get_stages(simple_plan, simple_problem_dic, simple_problem_content, simple_predicates_list) - - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + "simple_expected_stages.json"), "r") as f: + simple_stages = get_stages(simple_plan, simple_problem_dic, simple_problem_content, simple_predicates_list, + self.animation_profile_test) + + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + "simple_expected_stages.json"), "r") as f: simple_expected_stages = json.load(f) - + # Check that the generated simple stages are the same as the expected output self.assertTrue(simple_stages == simple_expected_stages - , msg="The generated simple stages are different from the expected output") + , msg="The generated simple stages are different from the expected output") - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + 'domain.pddl'), 'r', encoding='utf-8-sig') as f: + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + 'domain.pddl'), 'r', encoding='utf-8-sig') as f: complex_domain_content = f.read().lower() - - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + 'problem.pddl'), 'r', encoding='utf-8-sig') as f: + + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + 'problem.pddl'), 'r', encoding='utf-8-sig') as f: complex_problem_content = f.read().lower() with open(os.path.abspath(os.path.dirname(__file__) @@ -253,19 +267,19 @@ def test_predicates_generator(self): complex_predicates_list = Domain_parser.get_domain_json(complex_domain_content) complex_problem_dic = get_problem_dic(complex_problem_content, complex_predicates_list) complex_object_list = get_object_list(complex_problem_content) - complex_stages = get_stages(complex_plan, complex_problem_dic, complex_problem_content, complex_predicates_list) - - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + "complex_expected_stages.json"), "r") as f: + complex_stages = get_stages(complex_plan, complex_problem_dic, complex_problem_content, complex_predicates_list, + self.animation_profile_test) + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + "complex_expected_stages.json"), "r") as f: complex_expected_stages = json.load(f) # Check that the generated complex stages are the same as the expected output self.assertTrue(complex_stages == complex_expected_stages - , msg="The generated complex stages are different from the expected output") + , msg="The generated complex stages are different from the expected output") def test_solver(self): """ Test case for testing the solver functions - + Expected Input: domain_ap.pddl, problem.pddl: The animation and problem PDDL files. expected_initialised_obj_dict.json: The expected output JSON file. @@ -275,47 +289,56 @@ def test_solver(self): expected_obj_name_prop(object): The object generated by the solver functions. """ - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + 'domain_ap.pddl'), 'r', encoding='utf-8-sig') as f: + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + 'domain_ap.pddl'), 'r', encoding='utf-8-sig') as f: animation_content = f.read() - - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + 'problem.pddl'), 'r', encoding='utf-8-sig') as f: + + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + 'problem.pddl'), 'r', encoding='utf-8-sig') as f: problem_content = f.read().lower() object_list = get_object_list(problem_content) animation_profile = json.loads(Animation_parser.get_animation_profile(animation_content, object_list)) initialised_obj_dict = Initialise.initialise_objects(object_list, animation_profile) - - with open(os.path.abspath(os.path.dirname(__file__) - + '/test_files/' + "expected_initialised_obj_dict.json"), "r") as f: + + with open(os.path.abspath(os.path.dirname(__file__) + + '/test_files/' + "expected_initialised_obj_dict.json"), "r") as f: expected_initialised_obj_dict = json.load(f) - + # Check that the generated object dict is the same as the expected output self.assertTrue(initialised_obj_dict == expected_initialised_obj_dict - , msg="The generated object dict is different from the expected output") + , msg="The generated object dict is different from the expected output") # testing check rule true_predicate = {'name': 'at-robot', 'objectNames': ['loc1_1']} false_predicate = {'name': 'at-robot', 'objectNames': ['loc1_2']} objects_dic = { - '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'}, - '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'}, - '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'}} + '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'}, + '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'}, + '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'}} predicates_rules = {'at-robot': {'rules': ['rule1', 'rule2', 'rule3'], - 'rule1': {'left': {'robot': ['x']},'value': {'equal': {'?x': 'x'}}}, + 'rule1': {'left': {'robot': ['x']}, 'value': {'equal': {'?x': 'x'}}}, 'rule2': {'left': {'robot': ['y']}, 'value': {'equal': {'?x': 'y'}}}, - 'rule3': {'left': {'?x': ['color']}, 'value': {'equal': {'r': 0.9804, 'g': 0.6353, 'b': 0.7098, 'a': 1.0}}}, + 'rule3': {'left': {'?x': ['color']}, + 'value': {'equal': {'r': 0.9804, 'g': 0.6353, 'b': 0.7098, 'a': 1.0}}}, 'require': {'?x': ['x', 'y']}, 'objects': ['?x'], 'custom_obj': ['robot']}} # Check that the solver working with the expected predicate, objects dict and predicates rules self.assertTrue(Solver.check_rule_complete(true_predicate, objects_dic, predicates_rules) - , msg="The input predicate list, objects dict and predicates rules didn't match the expected input params for the solver") + , + msg="The input predicate list, objects dict and predicates rules didn't match the expected input params for the solver") self.assertFalse(Solver.check_rule_complete(false_predicate, objects_dic, predicates_rules) - , msg="The input params to the solver are fake data, but they match the expected input for the solver") + , + msg="The input params to the solver are fake data, but they match the expected input for the solver") # test get obj name property_dic = {'?x': ['property1', 'property2']} @@ -325,7 +348,7 @@ def test_solver(self): # Check that the generated object is the same as the expected output self.assertTrue(expected_obj_name_prop == obj_name_prop - , msg="The generated object is different from the expected output") + , msg="The generated object is different from the expected output") if __name__ == '__main__':