def test_dfs_in_set_2args(): tc = get_toy_strings_context() simp_strings = tc.get_type_by_name("ToySimpleStrs") ast = ObjectChoiceNode(simp_strings) two_strs = ObjectNode(tc.get_object_by_name("two_string")) ast.set_choice(two_strs) a1 = ObjectChoiceNode(tc.get_type_by_name("ToyMetasyntactic")) two_strs.set_arg_value("arg1", a1) a1v = ObjectNode(tc.get_object_by_name("foo")) a1.set_choice(a1v) a2 = ObjectChoiceNode(tc.get_type_by_name("ToyMetasyntactic")) two_strs.set_arg_value("arg2", a2) a2v = ObjectNode(tc.get_object_by_name("bar")) a2.set_choice(a2v) path = [n.cur_node for n in ast.depth_first_iter()] assert path == [ast, two_strs, a1, a1v, a2, a2v] ast_set = AstObjectChoiceSet(tc.get_type_by_name("ToySimpleStrs")) ast_set.add(ast, True, 1, 1) result = list(depth_first_iterate_ast_set_along_path(ast_set, path)) assert len(result) == len(path) assert isinstance(result[0], AstObjectChoiceSet) assert result[0].type_to_choose == simp_strings assert isinstance(result[1], ObjectNodeSet) assert result[1].implementation == tc.get_object_by_name("two_string") assert isinstance(result[2], AstObjectChoiceSet) assert result[2].type_to_choose == a1.type_to_choose assert isinstance(result[3], ObjectNodeSet) assert result[3].implementation == a1v.implementation assert isinstance(result[4], AstObjectChoiceSet) assert result[4].type_to_choose == a2.type_to_choose assert isinstance(result[5], ObjectNodeSet) assert result[5].implementation == a2v.implementation
def test_dfs_in_set(): tc = get_toy_strings_context() footype = AInixType(tc, "footype") bartype = AInixType(tc, "bartype") arg1 = AInixArgument(tc, "arg1", "bartype", required=True) foo_object = AInixObject(tc, "foo_object", "footype", [arg1]) bar_object = AInixObject(tc, "bar_object", "bartype") other_bar_obj = AInixObject(tc, "other_bar_ob", "bartype") # arg_choice = ObjectChoiceNode(bartype) ob_chosen = ObjectNode(bar_object) arg_choice.set_choice(ob_chosen) instance = ObjectNode(foo_object) instance.set_arg_value("arg1", arg_choice) ast = ObjectChoiceNode(footype) ast.set_choice(instance) # ast_set = AstObjectChoiceSet(footype) ast_set.add(ast, True, 1, 1) path = [pointer.cur_node for pointer in list(ast.depth_first_iter())] assert path == [ast, instance, arg_choice, ob_chosen] result = list(depth_first_iterate_ast_set_along_path(ast_set, path)) assert len(result) == len(path) assert isinstance(result[0], AstObjectChoiceSet) assert result[0].type_to_choose == footype assert isinstance(result[1], ObjectNodeSet) assert result[1].implementation == foo_object assert isinstance(result[2], AstObjectChoiceSet) assert result[2].type_to_choose == bartype assert isinstance(result[3], ObjectNodeSet) assert result[3].implementation == bar_object
def test_dfs4half(): tc = get_toy_strings_context() two_strs = ObjectNode(tc.get_object_by_name("two_string")) a1 = ObjectChoiceNode(tc.get_type_by_name("ToyMetasyntactic")) two_strs.set_arg_value("arg1", a1) a2 = ObjectChoiceNode(tc.get_type_by_name("ToyMetasyntactic")) two_strs.set_arg_value("arg2", a2) assert [n.cur_node for n in two_strs.depth_first_iter()] == [two_strs, a1, a2]
def test_get_copy_paths(): tc = get_toy_strings_context() parser = StringParser(tc) unparser = AstUnparser(tc) ast = parser.create_parse_tree("TWO foo bar", "ToySimpleStrs") unpar_res = unparser.to_string(ast) assert unpar_res.total_string == "TWO foo bar" tokenizer = SpaceTokenizer() in_str = "Hello there foo cow" tokens, metadata = tokenizer.tokenize(in_str) result = make_copy_version_of_tree(ast, unparser, metadata) assert get_paths_to_all_copies(result) == ((0, 0, 0), )
def test_object_node_set_equals_with_copy(): tc = get_toy_strings_context() two_strs = ObjectNode(tc.get_object_by_name("two_string")) a1 = ObjectChoiceNode(tc.get_type_by_name("ToyMetasyntactic")) two_strs.set_arg_value("arg1", a1) a1v = ObjectNode(tc.get_object_by_name("foo")) a1.set_choice(a1v) a2 = ObjectChoiceNode(tc.get_type_by_name("ToyMetasyntactic")) two_strs.set_arg_value("arg2", a2) a2.set_choice(CopyNode(tc.get_type_by_name("ToyMetasyntactic"), 1, 4)) two_strs.freeze() set_instance = ObjectNodeSet(tc.get_object_by_name("two_string"), None) set_instance.add(two_strs, True, 1, 1) assert set_instance.is_node_known_valid(two_strs)
def test_dfs4(): tc = get_toy_strings_context() ast = ObjectChoiceNode(tc.get_type_by_name("ToySimpleStrs")) two_strs = ObjectNode(tc.get_object_by_name("two_string")) ast.set_choice(two_strs) a1 = ObjectChoiceNode(tc.get_type_by_name("ToyMetasyntactic")) two_strs.set_arg_value("arg1", a1) assert [n.cur_node for n in ast.depth_first_iter()] == [ast, two_strs, a1] a1v = ObjectNode(tc.get_object_by_name("foo")) a1.set_choice(a1v) assert [n.cur_node for n in ast.depth_first_iter()] == [ast, two_strs, a1, a1v]
def test_get_latents(): out_v = torch.Tensor([[1, 2, 3, 4]]) mock_cell = MagicMock(return_value=(out_v, torch.Tensor(1, 4))) mock_selector = MagicMock() mock_vectorizer = MagicMock() mock_vocab = MagicMock() decoder = TreeRNNDecoder(mock_cell, mock_selector, mock_vectorizer, mock_vocab) tc = get_toy_strings_context() parser = StringParser(tc) ast = parser.create_parse_tree("TWO foo bar", "ToySimpleStrs") latents = decoder.get_latent_select_states(torch.Tensor(1, 4), torch.Tensor(1, 3, 4), MagicMock(), ast) assert len(latents) == 3 assert latents == [out_v for _ in range(3)]
def test_add_copies_to_ast_set_other_arg(): tc = get_toy_strings_context() parser = StringParser(tc) unparser = AstUnparser(tc) ast = parser.create_parse_tree("TWO foo bar", "ToySimpleStrs") unpar_res = unparser.to_string(ast) assert unpar_res.total_string == "TWO foo bar" tokenizer = SpaceTokenizer() in_str = "Hello bar sf cow" tokens, metadata = tokenizer.tokenize(in_str) ast_set = AstObjectChoiceSet(tc.get_type_by_name("ToySimpleStrs")) ast_set.add(ast, True, 1, 1) n: ObjectNode = ast.next_node_not_copy arg1set = ast_set.get_next_node_for_choice("two_string").next_node. \ get_arg_set_data(n.as_childless_node()).get_next_node_for_arg("arg2") assert arg1set.is_known_choice("bar") assert not arg1set.copy_is_known_choice() add_copies_to_ast_set(ast, ast_set, unparser, metadata) assert n.implementation.name == "two_string" assert arg1set.copy_is_known_choice() assert arg1set.is_known_choice("bar")
def test_make_copy_ast_both2(): tc = get_toy_strings_context() parser = StringParser(tc) unparser = AstUnparser(tc) ast = parser.create_parse_tree("TWO foo bar", "ToySimpleStrs") tokenizer = SpaceTokenizer() in_str = "Hello foo sdf bar" tokens, metadata = tokenizer.tokenize(in_str) result = make_copy_version_of_tree(ast, unparser, metadata) toy_str_obj = result.next_node_not_copy assert toy_str_obj.implementation.name == "two_string" a1c = toy_str_obj.get_choice_node_for_arg("arg1") assert a1c.copy_was_chosen cpa1 = a1c.next_node_is_copy assert cpa1.start == 1 assert cpa1.end == 1 a2c = toy_str_obj.get_choice_node_for_arg("arg2") assert a2c.copy_was_chosen cpa2 = a2c.next_node_is_copy assert cpa2.start == 3 assert cpa2.end == 3
def test_dfs3(): tc = get_toy_strings_context() ast = ObjectChoiceNode(tc.get_type_by_name("ToySimpleStrs")) two_strs = ObjectNode(tc.get_object_by_name("two_string")) ast.set_choice(two_strs) assert [n.cur_node for n in ast.depth_first_iter()] == [ast, two_strs]
def test_dfs2(): tc = get_toy_strings_context() two_strs = ObjectNode(tc.get_object_by_name("two_string")) assert [n.cur_node for n in two_strs.depth_first_iter()] == [two_strs]
def test_dfs(): tc = get_toy_strings_context() ast = ObjectChoiceNode(tc.get_type_by_name("ToySimpleStrs")) assert [n.cur_node for n in ast.depth_first_iter()] == [ast]