import unittest
import sys
import os

from utils import execute, kill, run_file, run_barebone

bottom = [
        "model",
        "instantiate_bottom", "1",
        "add_node", "1", "Class",
        "add_node", "1", "Any",
        "add_node", "1", "String",
        "add_value", "1", "name", "name",
        "add_edge", "1", "Association", "Class", "Any",
        "add_edge", "1", "Inheritance", "Class", "Class",
        "add_edge", "1", "Association_attribute", "Association", "String",
        "add_edge", "1", "Association_name", "Association_attribute", "name",
        "add_edge", "1", "assoc_inh_class", "Association", "Class",
        "add_edge", "1", "class_inh_any", "Class", "Any",
        "add_edge", "1", "string_inh_any", "String", "Any",
        "retype_model", "1", "1",
        "retype", "1", "Class", "Class",
        "retype", "1", "Any", "Class",
        "retype", "1", "String", "Class",
        "retype", "1", "name", "String",
        "retype", "1", "Association", "Association",
        "retype", "1", "Inheritance", "Association",
        "retype", "1", "Association_attribute", "Association",
        "retype", "1", "Association_name", "Association_attribute",
        "retype", "1", "assoc_inh_class", "Inheritance",
        "retype", "1", "class_inh_any", "Inheritance",
        "retype", "1", "string_inh_any", "Inheritance",
        "export_node", "1", "models/SimpleClassDiagrams",
        "exit",
    ]

action_language = [
        "model",
        "instantiate_node", "1", "Class", "Action",
        "instantiate_node", "1", "Class", "Statement",
        "instantiate_node", "1", "Class", "Expression",
        "instantiate_node", "1", "Class", "funcdef",
        "instantiate_node", "1", "Class", "param",
        "instantiate_node", "1", "Class", "if",
        "instantiate_node", "1", "Class", "break",
        "instantiate_node", "1", "Class", "while",
        "instantiate_node", "1", "Class", "continue",
        "instantiate_node", "1", "Class", "assign",
        "instantiate_node", "1", "Class", "return",
        "instantiate_node", "1", "Class", "output",
        "instantiate_node", "1", "Class", "declare",
        "instantiate_node", "1", "Class", "global",
        "instantiate_node", "1", "Class", "access",
        "instantiate_node", "1", "Class", "constant",
        "instantiate_node", "1", "Class", "input",
        "instantiate_node", "1", "Class", "resolve",
        "instantiate_node", "1", "Class", "call",
        "instantiate_link", "1", "Association", "dict_link", "Action", "Any",
        "instantiate_link", "1", "Association", "to_str", "dict_link", "String",
        "instantiate_attribute", "1", "to_str", "name", "name",
        "instantiate_link", "1", "Inheritance", "", "Action", "Any",
        "instantiate_link", "1", "Inheritance", "", "funcdef", "Action",
        "instantiate_link", "1", "Inheritance", "", "param", "Action",
        "instantiate_link", "1", "Inheritance", "", "Statement", "Action",
        "instantiate_link", "1", "Inheritance", "", "Expression", "Action",
        "instantiate_link", "1", "Inheritance", "", "resolve", "Statement",
        "instantiate_link", "1", "Inheritance", "", "if", "Statement",
        "instantiate_link", "1", "Inheritance", "", "break", "Statement",
        "instantiate_link", "1", "Inheritance", "", "continue", "Statement",
        "instantiate_link", "1", "Inheritance", "", "global", "Statement",
        "instantiate_link", "1", "Inheritance", "", "while", "Statement",
        "instantiate_link", "1", "Inheritance", "", "assign", "Statement",
        "instantiate_link", "1", "Inheritance", "", "return", "Statement",
        "instantiate_link", "1", "Inheritance", "", "call", "Statement",
        "instantiate_link", "1", "Inheritance", "", "declare", "Statement",
        "instantiate_link", "1", "Inheritance", "", "call", "Expression",
        "instantiate_link", "1", "Inheritance", "", "access", "Expression",
        "instantiate_link", "1", "Inheritance", "", "constant", "Expression",
        "instantiate_link", "1", "Inheritance", "", "input", "Expression",
        "instantiate_link", "1", "Association", "statement_next", "Statement", "Statement",
        "instantiate_attribute", "1", "statement_next", "name", "next",
        "instantiate_link", "1", "Association", "if_cond", "if", "Expression",
        "instantiate_attribute", "1", "if_cond", "name", "cond",
        "instantiate_link", "1", "Association", "if_then", "if", "Statement",
        "instantiate_attribute", "1", "if_then", "name", "true",
        "instantiate_link", "1", "Association", "if_else", "if", "Statement",
        "instantiate_attribute", "1", "if_else", "name", "false",
        "instantiate_link", "1", "Association", "while_cond", "while", "Expression",
        "instantiate_attribute", "1", "while_cond", "name", "cond",
        "instantiate_link", "1", "Association", "while_body", "while", "Statement",
        "instantiate_attribute", "1", "while_body", "name", "body",
        "instantiate_link", "1", "Association", "assign_var", "assign", "Any",
        "instantiate_attribute", "1", "assign_var", "name", "var",
        "instantiate_link", "1", "Association", "assign_value", "assign", "Expression",
        "instantiate_attribute", "1", "assign_value", "name", "value",
        "instantiate_link", "1", "Association", "break_while", "break", "while",
        "instantiate_attribute", "1", "break_while", "name", "while",
        "instantiate_link", "1", "Association", "continue_while", "continue", "while",
        "instantiate_attribute", "1", "continue_while", "name", "while",
        "instantiate_link", "1", "Association", "return_value", "return", "Expression",
        "instantiate_attribute", "1", "return_value", "name", "value",
        "instantiate_link", "1", "Association", "resolve_var", "resolve", "Any",
        "instantiate_attribute", "1", "resolve_var", "name", "var",
        "instantiate_link", "1", "Association", "access_var", "access", "Any",
        "instantiate_attribute", "1", "access_var", "name", "var",
        "instantiate_link", "1", "Association", "constant_node", "constant", "Any",
        "instantiate_attribute", "1", "constant_node", "name", "node",
        "instantiate_link", "1", "Association", "output_node", "output", "Expression",
        "instantiate_attribute", "1", "output_node", "name", "node",
        "instantiate_link", "1", "Association", "global_var", "global", "String",
        "instantiate_attribute", "1", "global_var", "name", "var",
        "instantiate_link", "1", "Association", "param_name", "param", "String",
        "instantiate_attribute", "1", "param_name", "name", "name",
        "instantiate_link", "1", "Association", "param_value", "param", "Expression",
        "instantiate_attribute", "1", "param_value", "name", "value",
        "instantiate_link", "1", "Association", "param_next_param", "param", "param",
        "instantiate_attribute", "1", "param_next_param", "name", "next_param",
        "instantiate_link", "1", "Association", "funcdef_body", "funcdef", "Statement",
        "instantiate_attribute", "1", "funcdef_body", "name", "body",
        "instantiate_link", "1", "Association", "call_func", "call", "Expression",
        "instantiate_attribute", "1", "call_func", "name", "func",
        "instantiate_link", "1", "Association", "call_params", "call", "param",
        "instantiate_attribute", "1", "call_params", "name", "params",
        "instantiate_link", "1", "Association", "call_last_param", "call", "param",
        "instantiate_attribute", "1", "call_last_param", "name", "last_param",

        "instantiate_link", "1", "Inheritance", "", "statement_next", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "if_cond", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "if_then", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "if_else", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "while_cond", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "while_body", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "assign_var", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "assign_value", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "break_while", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "continue_while", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "return_value", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "resolve_var", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "access_var", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "constant_node", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "output_node", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "global_var", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "param_name", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "param_value", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "param_next_param", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "funcdef_body", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "call_func", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "call_params", "dict_link",
        "instantiate_link", "1", "Inheritance", "", "call_last_param", "dict_link",

        "instantiate_link", "1", "Association", "constraint", "Class", "funcdef",
        "instantiate_attribute", "1", "constraint", "name", "constraint",
        "exit",
    ]

bottom_attributes = [
        "model",
        "instantiate_node", "1", "Class", "Integer",
        "instantiate_link", "1", "Inheritance", "integer_inh_any", "Integer", "Any",
        "instantiate_link", "1", "Association", "lc", "Class", "Integer",
        "instantiate_attribute", "1", "lc", "name", "lower_cardinality",
        "instantiate_link", "1", "Association", "uc", "Class", "Integer",
        "instantiate_attribute", "1", "uc", "name", "upper_cardinality",
        "instantiate_link", "1", "Association", "slc", "Association", "Integer",
        "instantiate_attribute", "1", "slc", "name", "source_lower_cardinality",
        "instantiate_link", "1", "Association", "suc", "Association", "Integer",
        "instantiate_attribute", "1", "suc", "name", "source_upper_cardinality",
        "instantiate_link", "1", "Association", "tlc", "Association", "Integer",
        "instantiate_attribute", "1", "tlc", "name", "target_lower_cardinality",
        "instantiate_link", "1", "Association", "tuc", "Association", "Integer",
        "instantiate_attribute", "1", "tuc", "name", "target_upper_cardinality",
        "exit",
    ]

def add_constraints(model):
    return [
            "model",
            "add_constraint", str(model), "Integer",
                "funcdef",
                "constraint",
                2, str(model*100), str(model*100+1),
                    "if",
                        "call",
                            "access", "resolve", "is_physical_int",
                            1,
                            "call",
                                "access", "resolve", "dict_read",
                                2,
                                "call",
                                    "access", "resolve", "dict_read",
                                    2,
                                    "access", "resolve", str(model*100),
                                    "const", "model",
                                    False,
                                "access", "resolve", str(model*100+1),
                                False,
                            False,
                        "return",
                            True,
                                "const", "OK",
                        True,
                        "return",
                            True,
                                "const", "Integer instance is not an integer.",
                        False,
                    False,
            "exit",
        ]

instantiate_scd = [
        "model",
        "instantiate_model", "1", "2",
        "instantiate_node", "2", "Class", "Place",
        "instantiate_node", "2", "Class", "Transition",
        "instantiate_node", "2", "Class", "Integer",
        "instantiate_link", "2", "Association", "P2T", "Place", "Transition",
        "instantiate_link", "2", "Association", "T2P", "Transition", "Place",
        "instantiate_link", "2", "Association", "Place_tokens", "Place", "Integer",
        "instantiate_attribute", "2", "Place_tokens", "name", "tokens",
        "instantiate_link", "2", "Association", "P2T_weight", "P2T", "Integer",
        "instantiate_attribute", "2", "P2T_weight", "name", "weight",
        "instantiate_link", "2", "Association", "T2P_weight", "T2P", "Integer",
        "instantiate_attribute", "2", "T2P_weight", "name", "weight",
        "export_node", "2", "models/PetriNets",
        "exit",
    ]

instantiate_pn = [
        "model",
        "instantiate_model", "2", "3",
        "instantiate_node", "3", "Place", "p1",
        "instantiate_node", "3", "Place", "p2",
        "instantiate_node", "3", "Transition", "t1",
        "instantiate_node", "3", "Transition", "t2",
        "instantiate_link", "3", "P2T", "p1_t1", "p1", "t1",
        "instantiate_link", "3", "T2P", "t1_p2", "t1", "p2",
        "instantiate_link", "3", "P2T", "p2_t2", "p2", "t2",
        "instantiate_link", "3", "T2P", "t2_p1", "t2", "p1",
        "instantiate_attribute", "3", "p1_t1", "weight", 1,
        "instantiate_attribute", "3", "t1_p2", "weight", 2,
        "instantiate_attribute", "3", "p2_t2", "weight", 3,
        "instantiate_attribute", "3", "t2_p1", "weight", 4,
        "instantiate_attribute", "3", "p1", "tokens", 5,
        "instantiate_attribute", "3", "p2", "tokens", 6,
        "export_node", "3", "models/PN_instance",
        "exit",
    ]

instantiate_example = [
        "model",
        "instantiate_model", "1", "2",
        "instantiate_node", "2", "Class", "A",
        "instantiate_node", "2", "Class", "B",
        "instantiate_node", "2", "Class", "C",
        "instantiate_link", "2", "Inheritance", "b_inherits_a", "B", "A",
        "instantiate_link", "2", "Association", "A_tokens", "A", "B",
        "instantiate_attribute", "2", "A_tokens", "name", "tokens",
        "instantiate_link", "2", "Association", "C_tokens", "C", "B",
        "instantiate_attribute", "2", "C_tokens", "name", "tokens",
        "export_node", "2", "models/example_MM",
        "exit",
        "model",
        "instantiate_model", "2", "3",
        "instantiate_node", "3", "A", "a",
        "instantiate_attribute", "3", "a", "tokens", "b",
        "instantiate_node", "3", "B", "b",
        "instantiate_node", "3", "C", "c",
        "instantiate_attribute", "3", "c", "tokens", "b",
        "export_node", "3", "models/example_M",
        "exit",
    ]

def conformance_call(operation, model, metamodel):
    return [
            "output",
                "call",
                    "access", "resolve", str(operation),
                    3,
                    "call", "access", "resolve", "import_node", 1, "const", "models/example_M", False,
                    "const", str(model),
                    "const", str(metamodel),
                False,
            True,
            ]

is_direct_instance = \
    conformance_call("is_direct_instance", "a", "A") + \
    conformance_call("is_direct_instance", "b", "A") + \
    conformance_call("is_direct_instance", "c", "A") + \
    conformance_call("is_direct_instance", "a", "B") + \
    conformance_call("is_direct_instance", "b", "B") + \
    conformance_call("is_direct_instance", "c", "B") + \
    conformance_call("is_direct_instance", "a", "C") + \
    conformance_call("is_direct_instance", "b", "C") + \
    conformance_call("is_direct_instance", "c", "C")

is_nominal_instance = \
    conformance_call("is_nominal_instance", "a", "A") + \
    conformance_call("is_nominal_instance", "b", "A") + \
    conformance_call("is_nominal_instance", "c", "A") + \
    conformance_call("is_nominal_instance", "a", "B") + \
    conformance_call("is_nominal_instance", "b", "B") + \
    conformance_call("is_nominal_instance", "c", "B") + \
    conformance_call("is_nominal_instance", "a", "C") + \
    conformance_call("is_nominal_instance", "b", "C") + \
    conformance_call("is_nominal_instance", "c", "C")

def conformance_check(node):
    return [
            "output",
                "call",
                    "access", "resolve", "conformance_scd",
                    1,
                    "call", "access", "resolve", "import_node", 1, "const", str(node), False,
                    False,
                True,
            ]

class TestConstructorsModels(unittest.TestCase):
    def test_constructors_instantiate_bottom(self):
        commands = bottom + bottom_attributes + conformance_check("models/SimpleClassDiagrams") + ["return", False]
        self.assertTrue(run_barebone(commands, ["OK"], 1))

    def test_constructors_action_language(self):
        commands = bottom + action_language + bottom_attributes + conformance_check("models/SimpleClassDiagrams") + ["return", False]
        self.assertTrue(run_barebone(commands, ["OK"], 1))

    def test_constructors_constraints(self):
        commands = bottom + action_language + bottom_attributes + add_constraints(1) + conformance_check("models/SimpleClassDiagrams") + ["return", False]
        self.assertTrue(run_barebone(commands, ["OK"], 1))

    def test_constructors_constraints_executed(self):
        commands = bottom + action_language + bottom_attributes + add_constraints(1) + instantiate_scd + add_constraints(2) + instantiate_pn + conformance_check("models/PN_instance") + ["return", False]
        self.assertTrue(run_barebone(commands, ["OK"], 1))

    def test_constructors_instantiate_scd(self):
        commands = bottom + bottom_attributes + instantiate_scd + conformance_check("models/PetriNets") + ["return", False]
        self.assertTrue(run_barebone(commands, ["OK"], 1))

    def test_constructors_instantiate_pn(self):
        commands = bottom + bottom_attributes + instantiate_scd + instantiate_pn + conformance_check("models/PN_instance") + ["return", False]
        self.assertTrue(run_barebone(commands, ["OK"], 1))

    def test_constructors_instantiate_example(self):
        commands = bottom + bottom_attributes + instantiate_example + conformance_check("models/example_MM") + conformance_check("models/example_M") + ["return", False]
        self.assertTrue(run_barebone(commands, ["OK", "OK"], 1))

    def test_constructors_is_direct_instance(self):
        commands = bottom + bottom_attributes + instantiate_example + is_direct_instance + ["return", False]
        expected = [True, False, False,
                    False, True, False,
                    False, False, True]
        self.assertTrue(run_barebone(commands, expected, 1))

    def test_constructors_is_nominal_instance(self):
        commands = bottom + bottom_attributes + instantiate_example + is_nominal_instance + ["return", False]
        expected = [True, True, False,
                    False, True, False,
                    False, False, True]
        self.assertTrue(run_barebone(commands, expected, 1))
