from ..test_base.test_roundtrip import TestLogicRoundtrip
from itertools import chain

from gavel.dialects.base.parser import LogicElement
from gavel.dialects.base.parser import LogicParser, StringBasedParser
from gavel.dialects.base.compiler import Compiler
from gavel.dialects.tptp.compiler import TPTPCompiler
from gavel.dialects.tptp.parser import TPTPParser


class TestTPTPLogicRoundtrip(TestLogicRoundtrip):
    _parser_cls = TPTPParser
    _compiler_cls = TPTPCompiler

    def test_bio(self):
        expected = """fof(a527188,axiom,(
            ! [X] :
              ( number_1_comma_3_bisphosphoglycerate_1(X)
             => ( original_name_2(X,'1,3-Bisphosphoglycerate')
                & user_description_2(X,'Three carbon organic molecule which serves as a metabolic intermediate in cellular respiration and photosynthesis.')
                & concept2words_2(X,'1,3 bisphosphoglycerate')
                & concept2words_2(X,'1,3-bisphosphoglycerate')
                & concept2words_2(X,'1 3 bisphosphoglyceric acid')
                & concept2words_2(X,'1-3-bisphosphoglyceric-acid')
                & concept2words_2(X,'1 3 bisphosphoglycerate')
                & concept2words_2(X,'1-3-bisphosphoglycerate')
                & concept2words_2(X,'1 3bpg')
                & concept2words_2(X,'1-3bpg')
                & concept2words_2(X,'1,3 diphosphoglycerate')
                & concept2words_2(X,'1,3-diphosphoglycerate')
                & concept2words_2(X,'1,3bpg')
                & concept2words_2(X,pgap)
                & phosphorylated_intermediate_1(X)
                & phosphorylation_1(fn_1_comma_3_bisphosphoglycerate_1(X))
                & molecule_1(fn_1_comma_3_bisphosphoglycerate_2(X))
                & kinetic_energy_1(fn_1_comma_3_bisphosphoglycerate_3(X))
                & constant_motion_1(fn_1_comma_3_bisphosphoglycerate_4(X))
                & organic_molecule_1(fn_1_comma_3_bisphosphoglycerate_5(X))
                & speed_value_1(fn_1_comma_3_bisphosphoglycerate_6(X))
                & quantity_value_1(fn_1_comma_3_bisphosphoglycerate_7(X))
                & entity_1(fn_1_comma_3_bisphosphoglycerate_8(X))
                & chemical_entity_1(fn_1_comma_3_bisphosphoglycerate_8(X))
                & atom_1(fn_1_comma_3_bisphosphoglycerate_8(X))
                & carbon_1(fn_1_comma_3_bisphosphoglycerate_8(X))
                & oxygen_1(fn_1_comma_3_bisphosphoglycerate_9(X))
                & entity_1(fn_1_comma_3_bisphosphoglycerate_9(X))
                & chemical_entity_1(fn_1_comma_3_bisphosphoglycerate_9(X))
                & atom_1(fn_1_comma_3_bisphosphoglycerate_9(X))
                & carbon_skeleton_1(fn_1_comma_3_bisphosphoglycerate_10(X))
                & carboxyl_group_1(fn_1_comma_3_bisphosphoglycerate_11(X))
                & double_bond_1(fn_1_comma_3_bisphosphoglycerate_12(X))
                & covalent_bond_1(fn_1_comma_3_bisphosphoglycerate_13(X))
                & single_bond_1(fn_1_comma_3_bisphosphoglycerate_13(X))
                & double_bond_1(fn_1_comma_3_bisphosphoglycerate_14(X))
                & functional_group_1(fn_1_comma_3_bisphosphoglycerate_15(X))
                & phosphate_group_1(fn_1_comma_3_bisphosphoglycerate_15(X))
                & entity_1(fn_1_comma_3_bisphosphoglycerate_16(X))
                & chemical_entity_1(fn_1_comma_3_bisphosphoglycerate_16(X))
                & atom_1(fn_1_comma_3_bisphosphoglycerate_16(X))
                & hydrogen_1(fn_1_comma_3_bisphosphoglycerate_16(X))
                & entity_1(fn_1_comma_3_bisphosphoglycerate_17(X))
                & chemical_entity_1(fn_1_comma_3_bisphosphoglycerate_17(X))
                & atom_1(fn_1_comma_3_bisphosphoglycerate_17(X))
                & oxygen_1(fn_1_comma_3_bisphosphoglycerate_17(X))
                & entity_1(fn_1_comma_3_bisphosphoglycerate_18(X))
                & chemical_entity_1(fn_1_comma_3_bisphosphoglycerate_18(X))
                & atom_1(fn_1_comma_3_bisphosphoglycerate_18(X))
                & phosphorus_1(fn_1_comma_3_bisphosphoglycerate_18(X))
                & chemical_entity_1(fn_functional_group_1(fn_1_comma_3_bisphosphoglycerate_11(X)))
                & single_bond_1(fn_carboxyl_group_69(fn_1_comma_3_bisphosphoglycerate_11(X)))
                & entity_1(fn_carboxyl_group_25(fn_1_comma_3_bisphosphoglycerate_11(X)))
                & chemical_entity_1(fn_carboxyl_group_25(fn_1_comma_3_bisphosphoglycerate_11(X)))
                & atom_1(fn_carboxyl_group_25(fn_1_comma_3_bisphosphoglycerate_11(X)))
                & hydrogen_1(fn_carboxyl_group_25(fn_1_comma_3_bisphosphoglycerate_11(X)))
                & double_bond_1(fn_carboxyl_group_70(fn_1_comma_3_bisphosphoglycerate_11(X)))
                & entity_1(fn_covalent_bond_19(fn_1_comma_3_bisphosphoglycerate_13(X)))
                & chemical_entity_1(fn_covalent_bond_19(fn_1_comma_3_bisphosphoglycerate_13(X)))
                & atom_1(fn_covalent_bond_19(fn_1_comma_3_bisphosphoglycerate_13(X)))
                & oxygen_1(fn_carboxyl_group_23(fn_1_comma_3_bisphosphoglycerate_11(X)))
                & entity_1(fn_covalent_bond_20(fn_1_comma_3_bisphosphoglycerate_13(X)))
                & chemical_entity_1(fn_covalent_bond_20(fn_1_comma_3_bisphosphoglycerate_13(X)))
                & atom_1(fn_covalent_bond_20(fn_1_comma_3_bisphosphoglycerate_13(X)))
                & entity_1(fn_covalent_bond_19(fn_1_comma_3_bisphosphoglycerate_14(X)))
                & chemical_entity_1(fn_covalent_bond_19(fn_1_comma_3_bisphosphoglycerate_14(X)))
                & atom_1(fn_covalent_bond_19(fn_1_comma_3_bisphosphoglycerate_14(X)))
                & oxygen_1(fn_carboxyl_group_24(fn_1_comma_3_bisphosphoglycerate_11(X)))
                & entity_1(fn_covalent_bond_20(fn_1_comma_3_bisphosphoglycerate_14(X)))
                & chemical_entity_1(fn_covalent_bond_20(fn_1_comma_3_bisphosphoglycerate_14(X)))
                & atom_1(fn_covalent_bond_20(fn_1_comma_3_bisphosphoglycerate_14(X)))
                & entity_1(fn_carboxyl_group_19(fn_1_comma_3_bisphosphoglycerate_11(X)))
                & chemical_entity_1(fn_carboxyl_group_19(fn_1_comma_3_bisphosphoglycerate_11(X)))
                & atom_1(fn_carboxyl_group_19(fn_1_comma_3_bisphosphoglycerate_11(X)))
                & carbon_1(fn_carboxyl_group_19(fn_1_comma_3_bisphosphoglycerate_11(X)))
                & has_part_2(X,fn_1_comma_3_bisphosphoglycerate_16(X))
                & exactCardinality(X,has_region_0,"2",phosphate_group_0)
                & possesses_2(X,fn_1_comma_3_bisphosphoglycerate_13(X))
                & possesses_2(X,fn_1_comma_3_bisphosphoglycerate_12(X))
                & is_between_2(fn_1_comma_3_bisphosphoglycerate_14(X),fn_1_comma_3_bisphosphoglycerate_17(X))
                & is_between_2(fn_1_comma_3_bisphosphoglycerate_13(X),fn_1_comma_3_bisphosphoglycerate_18(X))
                & is_between_2(fn_1_comma_3_bisphosphoglycerate_14(X),fn_1_comma_3_bisphosphoglycerate_8(X))
                & is_between_2(fn_1_comma_3_bisphosphoglycerate_13(X),fn_1_comma_3_bisphosphoglycerate_9(X))
                & is_between_2(fn_1_comma_3_bisphosphoglycerate_14(X),fn_1_comma_3_bisphosphoglycerate_18(X))
                & possesses_2(X,fn_1_comma_3_bisphosphoglycerate_14(X))
                & is_between_2(fn_1_comma_3_bisphosphoglycerate_14(X),fn_1_comma_3_bisphosphoglycerate_9(X))
                & has_region_2(X,fn_1_comma_3_bisphosphoglycerate_11(X))
                & has_state_2(fn_1_comma_3_bisphosphoglycerate_5(X),X)
                & raw_material_2(fn_1_comma_3_bisphosphoglycerate_1(X),fn_1_comma_3_bisphosphoglycerate_5(X))
                & is_between_2(fn_1_comma_3_bisphosphoglycerate_13(X),fn_1_comma_3_bisphosphoglycerate_17(X))
                & is_inside_2(fn_1_comma_3_bisphosphoglycerate_16(X),fn_1_comma_3_bisphosphoglycerate_11(X))
                & fn_1_comma_3_bisphosphoglycerate_16(X) != fn_1_comma_3_bisphosphoglycerate_17(X)
                & is_inside_2(fn_1_comma_3_bisphosphoglycerate_17(X),fn_1_comma_3_bisphosphoglycerate_11(X))
                & is_inside_2(fn_1_comma_3_bisphosphoglycerate_9(X),fn_1_comma_3_bisphosphoglycerate_11(X))
                & fn_1_comma_3_bisphosphoglycerate_17(X) != fn_1_comma_3_bisphosphoglycerate_16(X)
                & is_inside_2(fn_1_comma_3_bisphosphoglycerate_8(X),fn_1_comma_3_bisphosphoglycerate_11(X))
                & is_between_2(fn_1_comma_3_bisphosphoglycerate_12(X),fn_1_comma_3_bisphosphoglycerate_8(X))
                & is_between_2(fn_1_comma_3_bisphosphoglycerate_12(X),fn_1_comma_3_bisphosphoglycerate_9(X))
                & fn_1_comma_3_bisphosphoglycerate_9(X) != fn_1_comma_3_bisphosphoglycerate_8(X)
                & fn_1_comma_3_bisphosphoglycerate_8(X) != fn_1_comma_3_bisphosphoglycerate_9(X)
                & has_part_2(X,fn_1_comma_3_bisphosphoglycerate_18(X))
                & has_part_2(X,fn_1_comma_3_bisphosphoglycerate_17(X))
                & object_2(fn_1_comma_3_bisphosphoglycerate_4(X),X)
                & speed_2(fn_1_comma_3_bisphosphoglycerate_4(X),fn_1_comma_3_bisphosphoglycerate_6(X))
                & is_inside_2(fn_1_comma_3_bisphosphoglycerate_17(X),fn_1_comma_3_bisphosphoglycerate_15(X))
                & has_region_2(X,fn_1_comma_3_bisphosphoglycerate_15(X))
                & possesses_2(X,fn_1_comma_3_bisphosphoglycerate_3(X))
                & quantity_2(fn_1_comma_3_bisphosphoglycerate_3(X),fn_1_comma_3_bisphosphoglycerate_7(X))
                & directly_proportional_2(fn_1_comma_3_bisphosphoglycerate_6(X),fn_1_comma_3_bisphosphoglycerate_7(X))
                & result_2(fn_1_comma_3_bisphosphoglycerate_1(X),X)
                & raw_material_2(fn_1_comma_3_bisphosphoglycerate_1(X),fn_1_comma_3_bisphosphoglycerate_2(X))
                & fn_1_comma_3_bisphosphoglycerate_2(X) != X
                & has_part_2(X,fn_1_comma_3_bisphosphoglycerate_9(X))
                & fn_1_comma_3_bisphosphoglycerate_9(X) != fn_1_comma_3_bisphosphoglycerate_17(X)
                & X != fn_1_comma_3_bisphosphoglycerate_2(X)
                & fn_1_comma_3_bisphosphoglycerate_17(X) != fn_1_comma_3_bisphosphoglycerate_9(X)
                & fn_1_comma_3_bisphosphoglycerate_18(X) != fn_1_comma_3_bisphosphoglycerate_17(X)
                & is_inside_2(fn_1_comma_3_bisphosphoglycerate_18(X),fn_1_comma_3_bisphosphoglycerate_15(X))
                & is_inside_2(fn_1_comma_3_bisphosphoglycerate_9(X),fn_1_comma_3_bisphosphoglycerate_15(X))
                & fn_1_comma_3_bisphosphoglycerate_17(X) != fn_1_comma_3_bisphosphoglycerate_18(X)
                & fn_1_comma_3_bisphosphoglycerate_9(X) != fn_1_comma_3_bisphosphoglycerate_18(X)
                & fn_1_comma_3_bisphosphoglycerate_18(X) != fn_1_comma_3_bisphosphoglycerate_9(X)
                & has_part_2(X,fn_1_comma_3_bisphosphoglycerate_8(X))
                & is_inside_2(fn_1_comma_3_bisphosphoglycerate_8(X),fn_1_comma_3_bisphosphoglycerate_10(X))
                & has_region_2(X,fn_1_comma_3_bisphosphoglycerate_10(X))
                & fn_1_comma_3_bisphosphoglycerate_17(X) != fn_1_comma_3_bisphosphoglycerate_8(X)
                & fn_1_comma_3_bisphosphoglycerate_8(X) != fn_1_comma_3_bisphosphoglycerate_17(X)
                & fn_1_comma_3_bisphosphoglycerate_18(X) = fn_covalent_bond_20(fn_1_comma_3_bisphosphoglycerate_13(X))
                & fn_1_comma_3_bisphosphoglycerate_8(X) = fn_covalent_bond_20(fn_1_comma_3_bisphosphoglycerate_14(X))
                & fn_1_comma_3_bisphosphoglycerate_9(X) = fn_covalent_bond_19(fn_1_comma_3_bisphosphoglycerate_13(X))
                & fn_1_comma_3_bisphosphoglycerate_18(X) = fn_covalent_bond_19(fn_1_comma_3_bisphosphoglycerate_14(X))
                & fn_1_comma_3_bisphosphoglycerate_9(X) = fn_molecule_11(X)
                & fn_1_comma_3_bisphosphoglycerate_14(X) = fn_molecule_10(X)
                & X = fn_functional_group_1(fn_1_comma_3_bisphosphoglycerate_11(X))
                & fn_1_comma_3_bisphosphoglycerate_5(X) = fn_phosphorylated_intermediate_1(X)
                & fn_1_comma_3_bisphosphoglycerate_13(X) = fn_carboxyl_group_69(fn_1_comma_3_bisphosphoglycerate_11(X))
                & fn_1_comma_3_bisphosphoglycerate_17(X) = fn_carboxyl_group_24(fn_1_comma_3_bisphosphoglycerate_11(X))
                & fn_1_comma_3_bisphosphoglycerate_16(X) = fn_carboxyl_group_25(fn_1_comma_3_bisphosphoglycerate_11(X))
                & fn_1_comma_3_bisphosphoglycerate_9(X) = fn_carboxyl_group_23(fn_1_comma_3_bisphosphoglycerate_11(X))
                & fn_1_comma_3_bisphosphoglycerate_12(X) = fn_carboxyl_group_70(fn_1_comma_3_bisphosphoglycerate_11(X))
                & fn_1_comma_3_bisphosphoglycerate_9(X) = fn_carboxyl_group_24(fn_1_comma_3_bisphosphoglycerate_11(X))
                & fn_1_comma_3_bisphosphoglycerate_8(X) = fn_carboxyl_group_19(fn_1_comma_3_bisphosphoglycerate_11(X))
                & fn_1_comma_3_bisphosphoglycerate_19(X) = fn_phosphorylated_intermediate_14(X)
                & fn_1_comma_3_bisphosphoglycerate_20(X) = fn_phosphorylated_intermediate_15(X)
                & fn_1_comma_3_bisphosphoglycerate_4(X) = fn_phosphorylated_intermediate_9(X)
                & fn_1_comma_3_bisphosphoglycerate_6(X) = fn_phosphorylated_intermediate_12(X)
                & fn_1_comma_3_bisphosphoglycerate_7(X) = fn_phosphorylated_intermediate_13(X)
                & fn_1_comma_3_bisphosphoglycerate_3(X) = fn_phosphorylated_intermediate_4(X)
                & fn_1_comma_3_bisphosphoglycerate_2(X) = fn_phosphorylated_intermediate_3(X)
                & fn_1_comma_3_bisphosphoglycerate_1(X) = fn_phosphorylated_intermediate_2(X)
                & fn_1_comma_3_bisphosphoglycerate_15(X) = fn_phosphorylated_intermediate_11(X)
                & fn_1_comma_3_bisphosphoglycerate_9(X) = fn_phosphorylated_intermediate_5(X)
                & fn_1_comma_3_bisphosphoglycerate_18(X) = fn_phosphorylated_intermediate_6(X)
                & fn_1_comma_3_bisphosphoglycerate_10(X) = fn_phosphorylated_intermediate_10(X)
                & fn_1_comma_3_bisphosphoglycerate_17(X) = fn_phosphorylated_intermediate_7(X)
                & fn_1_comma_3_bisphosphoglycerate_8(X) = fn_phosphorylated_intermediate_8(X)
                & fn_covalent_bond_19(fn_1_comma_3_bisphosphoglycerate_13(X)) = fn_molecule_11(X)
                & fn_molecule_11(X) = fn_molecule_11(X)
                & fn_molecule_11(X) = fn_carboxyl_group_23(fn_1_comma_3_bisphosphoglycerate_11(X))
                & fn_carboxyl_group_23(fn_1_comma_3_bisphosphoglycerate_11(X)) = fn_carboxyl_group_23(fn_1_comma_3_bisphosphoglycerate_11(X))
                & fn_carboxyl_group_23(fn_1_comma_3_bisphosphoglycerate_11(X)) = fn_carboxyl_group_24(fn_1_comma_3_bisphosphoglycerate_11(X))
                & fn_carboxyl_group_24(fn_1_comma_3_bisphosphoglycerate_11(X)) = fn_phosphorylated_intermediate_5(X)
                & fn_phosphorylated_intermediate_5(X) = fn_phosphorylated_intermediate_5(X)
                & fn_phosphorylated_intermediate_5(X) = fn_covalent_bond_20(fn_1_comma_3_bisphosphoglycerate_13(X))
                & fn_covalent_bond_20(fn_1_comma_3_bisphosphoglycerate_13(X)) = fn_covalent_bond_19(fn_1_comma_3_bisphosphoglycerate_14(X))
                & fn_covalent_bond_19(fn_1_comma_3_bisphosphoglycerate_14(X)) = fn_covalent_bond_19(fn_1_comma_3_bisphosphoglycerate_14(X))
                & fn_covalent_bond_19(fn_1_comma_3_bisphosphoglycerate_14(X)) = fn_phosphorylated_intermediate_6(X)
                & fn_phosphorylated_intermediate_6(X) = fn_phosphorylated_intermediate_6(X)
                & fn_phosphorylated_intermediate_6(X) = fn_carboxyl_group_24(fn_1_comma_3_bisphosphoglycerate_11(X))
                & fn_carboxyl_group_24(fn_1_comma_3_bisphosphoglycerate_11(X)) = fn_carboxyl_group_24(fn_1_comma_3_bisphosphoglycerate_11(X))
                & fn_carboxyl_group_24(fn_1_comma_3_bisphosphoglycerate_11(X)) = fn_phosphorylated_intermediate_7(X)
                & fn_phosphorylated_intermediate_7(X) = fn_phosphorylated_intermediate_7(X)
                & fn_phosphorylated_intermediate_7(X) = fn_covalent_bond_20(fn_1_comma_3_bisphosphoglycerate_14(X))
                & fn_covalent_bond_20(fn_1_comma_3_bisphosphoglycerate_14(X)) = fn_carboxyl_group_19(fn_1_comma_3_bisphosphoglycerate_11(X))
                & fn_carboxyl_group_19(fn_1_comma_3_bisphosphoglycerate_11(X)) = fn_carboxyl_group_19(fn_1_comma_3_bisphosphoglycerate_11(X))
                & fn_carboxyl_group_19(fn_1_comma_3_bisphosphoglycerate_11(X)) = fn_phosphorylated_intermediate_8(X)
                & fn_phosphorylated_intermediate_8(X) = fn_phosphorylated_intermediate_8(X) ) ) ))."""
        result = self.compiler.visit(self.parser.parse_single_from_string(expected))
        self.assertEqual(result, expected)

