import unittest
import sys
import os

from utils import execute, kill, run_file, run_barebone, get_constructor

bottom = [
        "model",
        "instantiate_bottom", "1",
        "add_node", "1", "Element",
        "add_node", "1", "Class",
        "add_node", "1", "Attribute",
        "add_node", "1", "SimpleAttribute",
        "add_node", "1", "String",
        "add_value", "1", "name_value", "name",
        "add_edge", "1", "Association", "Class", "Class",
        "add_edge", "1", "Inheritance", "Element", "Element",
        "add_edge", "1", "AttributeLink", "Element", "Attribute",
        "add_edge", "1", "attr_name", "AttributeLink", "String",
        "add_edge", "1", "attr_name_name", "attr_name", "name_value",
        "add_edge", "1", "class_inh_element", "Class", "Element",
        "add_edge", "1", "attribute_inh_element", "Attribute", "Element",
        "add_edge", "1", "simple_inh_attribute", "SimpleAttribute", "Attribute",
        "add_edge", "1", "association_inh_element", "Association", "Element",
        "add_edge", "1", "attributelink_inh_element", "AttributeLink", "Element",

        "retype_model", "1", "1",
        "retype", "1", "Element", "Class",
        "retype", "1", "Class", "Class",
        "retype", "1", "Attribute", "Class",
        "retype", "1", "SimpleAttribute", "Class",
        "retype", "1", "String", "SimpleAttribute",
        "retype", "1", "name_value", "String",
        "retype", "1", "Association", "Association",
        "retype", "1", "Inheritance", "Association",
        "retype", "1", "AttributeLink", "Association",
        "retype", "1", "attr_name", "AttributeLink",
        "retype", "1", "attr_name_name", "attr_name",
        "retype", "1", "class_inh_element", "Inheritance",
        "retype", "1", "attribute_inh_element", "Inheritance",
        "retype", "1", "simple_inh_attribute", "Inheritance",
        "retype", "1", "association_inh_element", "Inheritance",
        "retype", "1", "attributelink_inh_element", "Inheritance",

        "instantiate_node", "1", "SimpleAttribute", "Location",
        "instantiate_node", "1", "SimpleAttribute", "Natural",
        "instantiate_node", "1", "SimpleAttribute", "Boolean",
        "instantiate_link", "1", "AttributeLink", "attr_optional", "AttributeLink", "Boolean",
        "instantiate_attribute", "1", "attr_optional", "name", "optional",
        "instantiate_attribute", "1", "attr_optional", "optional", False,
        "instantiate_attribute", "1", "attr_name", "optional", False,

        "instantiate_node", "1", "Class", "ComplexAttribute",
        "instantiate_link", "1", "Inheritance", "", "ComplexAttribute", "Attribute",

        "model_define_attribute", "1", "Class", "lower_cardinality", True, "Natural",
        "model_define_attribute", "1", "Class", "upper_cardinality", True, "Natural",
        "model_define_attribute", "1", "Association", "source_lower_cardinality", True, "Natural",
        "model_define_attribute", "1", "Association", "source_upper_cardinality", True, "Natural",
        "model_define_attribute", "1", "Association", "target_lower_cardinality", True, "Natural",
        "model_define_attribute", "1", "Association", "target_upper_cardinality", True, "Natural",
        "model_define_attribute", "1", "ComplexAttribute", "type", False, "Location",

        "export_node", "1", "models/SimpleClassDiagrams_new",
        "exit",
    ]

bottom_link_al = [
        "model",
        "instantiate_node", "1", "ComplexAttribute", "ActionLanguage",
        "instantiate_attribute", "1", "ActionLanguage", "type", "models/ActionLanguage",

        "model_define_attribute", "1", "Element", "constraint", True, "ActionLanguage",
        "exit",
    ]

action_language = [
        "model",
        "instantiate_model", "1", "2",
        "instantiate_node", "2", "Class", "Element",
        "instantiate_node", "2", "Class", "Action",
        "instantiate_node", "2", "Class", "Statement",
        "instantiate_node", "2", "Class", "Expression",
        "instantiate_node", "2", "Class", "funcdef",
        "instantiate_node", "2", "Class", "param",
        "instantiate_node", "2", "Class", "if",
        "instantiate_node", "2", "Class", "break",
        "instantiate_node", "2", "Class", "while",
        "instantiate_node", "2", "Class", "continue",
        "instantiate_node", "2", "Class", "assign",
        "instantiate_node", "2", "Class", "return",
        "instantiate_node", "2", "Class", "output",
        "instantiate_node", "2", "Class", "declare",
        "instantiate_node", "2", "Class", "global",
        "instantiate_node", "2", "Class", "access",
        "instantiate_node", "2", "Class", "constant",
        "instantiate_node", "2", "Class", "input",
        "instantiate_node", "2", "Class", "resolve",
        "instantiate_node", "2", "Class", "call",
        "instantiate_node", "2", "Class", "String",
        "instantiate_node", "2", "SimpleAttribute", "StringAttr",

        "instantiate_node", "2", "Class", "Initial",
        "instantiate_attribute", "2", "Initial", "lower_cardinality", 1,
        "instantiate_attribute", "2", "Initial", "upper_cardinality", 1,
        "instantiate_link", "2", "Association", "initial_funcdef", "Initial", "Action",
        "instantiate_attribute", "2", "initial_funcdef", "target_lower_cardinality", 1,
        "instantiate_attribute", "2", "initial_funcdef", "target_upper_cardinality", 1,

        "instantiate_link", "2", "Inheritance", "", "Action", "Element",
        "instantiate_link", "2", "Inheritance", "", "funcdef", "Action",
        "instantiate_link", "2", "Inheritance", "", "param", "Action",
        "instantiate_link", "2", "Inheritance", "", "Statement", "Action",
        "instantiate_link", "2", "Inheritance", "", "Expression", "Action",
        "instantiate_link", "2", "Inheritance", "", "resolve", "Statement",
        "instantiate_link", "2", "Inheritance", "", "if", "Statement",
        "instantiate_link", "2", "Inheritance", "", "break", "Statement",
        "instantiate_link", "2", "Inheritance", "", "continue", "Statement",
        "instantiate_link", "2", "Inheritance", "", "global", "Statement",
        "instantiate_link", "2", "Inheritance", "", "while", "Statement",
        "instantiate_link", "2", "Inheritance", "", "assign", "Statement",
        "instantiate_link", "2", "Inheritance", "", "return", "Statement",
        "instantiate_link", "2", "Inheritance", "", "call", "Statement",
        "instantiate_link", "2", "Inheritance", "", "declare", "Statement",
        "instantiate_link", "2", "Inheritance", "", "call", "Expression",
        "instantiate_link", "2", "Inheritance", "", "access", "Expression",
        "instantiate_link", "2", "Inheritance", "", "constant", "Expression",
        "instantiate_link", "2", "Inheritance", "", "input", "Expression",
        "instantiate_link", "2", "Inheritance", "", "String", "Element",

        "instantiate_link", "2", "Association", "dict_link", "Action", "Element",
        "model_define_attribute", "2", "dict_link", "name", False, "StringAttr",

        "instantiate_link", "2", "Association", "Statement_next", "Statement", "Statement",
        "instantiate_link", "2", "Association", "if_cond", "if", "Expression",
        "instantiate_link", "2", "Association", "if_then", "if", "Statement",
        "instantiate_link", "2", "Association", "if_else", "if", "Statement",
        "instantiate_link", "2", "Association", "while_cond", "while", "Expression",
        "instantiate_link", "2", "Association", "while_body", "while", "Statement",
        "instantiate_link", "2", "Association", "assign_var", "assign", "resolve",
        "instantiate_link", "2", "Association", "assign_value", "assign", "Expression",
        "instantiate_link", "2", "Association", "break_while", "break", "while",
        "instantiate_link", "2", "Association", "continue_while", "continue", "while",
        "instantiate_link", "2", "Association", "return_value", "return", "Expression",
        "instantiate_link", "2", "Association", "resolve_var", "resolve", "Element",
        "instantiate_link", "2", "Association", "access_var", "access", "resolve",
        "instantiate_link", "2", "Association", "constant_node", "constant", "Element",
        "instantiate_link", "2", "Association", "output_node", "output", "Expression",
        "instantiate_link", "2", "Association", "global_var", "global", "String",
        "instantiate_link", "2", "Association", "param_name", "param", "String",
        "instantiate_link", "2", "Association", "param_value", "param", "Expression",
        "instantiate_link", "2", "Association", "param_next_param", "param", "param",
        "instantiate_link", "2", "Association", "funcdef_body", "funcdef", "Statement",
        "instantiate_link", "2", "Association", "call_func", "call", "Expression",
        "instantiate_link", "2", "Association", "call_params", "call", "param",
        "instantiate_link", "2", "Association", "call_last_param", "call", "param",

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

        "instantiate_attribute", "2", "if_cond", "target_lower_cardinality", 1,
        "instantiate_attribute", "2", "if_then", "target_lower_cardinality", 1,
        "instantiate_attribute", "2", "while_cond", "target_lower_cardinality", 1,
        "instantiate_attribute", "2", "while_body", "target_lower_cardinality", 1,
        "instantiate_attribute", "2", "assign_var", "target_lower_cardinality", 1,
        "instantiate_attribute", "2", "assign_value", "target_lower_cardinality", 1,
        "instantiate_attribute", "2", "break_while", "target_lower_cardinality", 1,
        "instantiate_attribute", "2", "continue_while", "target_lower_cardinality", 1,
        "instantiate_attribute", "2", "resolve_var", "target_lower_cardinality", 1,
        "instantiate_attribute", "2", "access_var", "target_lower_cardinality", 1,
        "instantiate_attribute", "2", "constant_node", "target_lower_cardinality", 1,
        "instantiate_attribute", "2", "output_node", "target_lower_cardinality", 1,
        "instantiate_attribute", "2", "global_var", "target_lower_cardinality", 1,
        "instantiate_attribute", "2", "param_name", "target_lower_cardinality", 1,
        "instantiate_attribute", "2", "param_value", "target_lower_cardinality", 1,
        "instantiate_attribute", "2", "funcdef_body", "target_lower_cardinality", 1,
        "instantiate_attribute", "2", "call_func", "target_lower_cardinality", 1,

        "instantiate_attribute", "2", "Statement_next", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "if_cond", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "if_then", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "if_else", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "while_cond", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "while_body", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "assign_var", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "assign_value", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "break_while", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "continue_while", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "return_value", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "resolve_var", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "access_var", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "constant_node", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "output_node", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "global_var", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "param_name", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "param_value", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "param_next_param", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "funcdef_body", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "call_func", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "call_params", "target_upper_cardinality", 1,
        "instantiate_attribute", "2", "call_last_param", "target_upper_cardinality", 1,

        "export_node", "2", "models/ActionLanguage_new",
        "exit",
    ]

def add_constraint(model, element, code):
    return ["model",
            "instantiate_attribute_code", model, element, "constraint"] + \
                get_constructor(code) + \
            ["exit",]

instantiate_scd = [
        "model",
        "instantiate_model", "1", "3",
        "instantiate_node", "3", "Class", "Place",
        "instantiate_node", "3", "Class", "Transition",
        "instantiate_node", "3", "SimpleAttribute", "Natural",
        "instantiate_link", "3", "Association", "P2T", "Place", "Transition",
        "instantiate_link", "3", "Association", "T2P", "Transition", "Place",
        "model_define_attribute", "3", "Place", "tokens", False, "Natural",
        "model_define_attribute", "3", "P2T", "weight", False, "Natural",
        "model_define_attribute", "3", "T2P", "weight", False, "Natural",
        "export_node", "3", "models/PetriNets_new",
        "exit",
    ]

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

instantiate_example = [
        "model",
        "instantiate_model", "1", "2",
        "instantiate_node", "2", "Class", "A",
        "instantiate_node", "2", "Class", "B",
        "instantiate_node", "2", "SimpleAttribute", "C",
        "instantiate_link", "2", "Inheritance", "b_inherits_a", "B", "A",
        "instantiate_link", "2", "Association", "A_to_B", "A", "B",
        "model_define_attribute", "2", "A", "tokens", False, "C",
        "export_node", "2", "models/example_MM",
        "exit",
        "model",
        "instantiate_model", "2", "3",
        "instantiate_node", "3", "A", "a",
        "instantiate_node", "3", "B", "b",
        "instantiate_link", "3", "A_to_B", "a_to_b", "a", "b",
        "instantiate_attribute", "3", "a", "tokens", 123,
        "instantiate_attribute", "3", "b", "tokens", 234,
        "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", "a", "B") + \
    conformance_call("is_direct_instance", "b", "B") + \
    conformance_call("is_direct_instance", "a", "C") + \
    conformance_call("is_direct_instance", "b", "C")

is_nominal_instance = \
    conformance_call("is_nominal_instance", "a", "A") + \
    conformance_call("is_nominal_instance", "b", "A") + \
    conformance_call("is_nominal_instance", "a", "B") + \
    conformance_call("is_nominal_instance", "b", "B") + \
    conformance_call("is_nominal_instance", "a", "C") + \
    conformance_call("is_nominal_instance", "b", "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,
            ]

code_natural = \
    """
        include "primitives.alh"
        String function constraint(model : Element, name : String):
            Element self
            self = model["model"][name]
            if (is_physical_int(self)):
                if (integer_gte(self, 0)):
                    return "OK"!
                else:
                    return "Natural number not larger than or equal to zero"!
            else:
                return "Natural number not larger than or equal to zero"!
    """

code_string = \
    """
        include "primitives.alh"
        String function constraint(model : Element, name : String):
            Element self
            self = model["model"][name]
            log("In constraint")
            if (is_physical_string(self)):
                return "OK"!
            else:
                return "String has non-string instance"!
    """

code_boolean = \
    """
        include "primitives.alh"
        String function constraint(model : Element, name : String):
            Element self
            self = model["model"][name]
            if (is_physical_boolean(self)):
                return "OK"!
            else:
                return "Boolean has non-boolean instance"!
    """

code_location = \
    """
        include "primitives.alh"
        include "library.alh"
        String function constraint(model : Element, name : String):
            Element self
            self = model["model"][name]
            if (is_physical_string(self)):
                if (element_neq(import_node(self), read_root())):
                    return "OK"!
                else:
                    return "Location references non-existing element!"!
            else:
                return "Location has non-string instance"!
    """

code_complex_attribute = \
    """
        include "primitives.alh"
        include "library.alh"
        String function constraint(model : Element, name : String):
            Element self
            self = model["model"][name]
            if (is_physical_string(self)):
                if (element_neq(import_node(self), read_root())):
                    return "OK"!
                else:
                    return "Complex Attribute references non-existing element!"!
            else:
                return ("Complex Attribute has non-string value: " + cast_e2s(self))!
    """

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

    def test_constructors_action_language(self):
        commands = bottom + action_language + bottom_link_al + \
                    add_constraint("2", "String", code_string) + \
                    add_constraint("2", "StringAttr", code_string) + \
                    conformance_check("models/ActionLanguage_new") + ["return", False]
        self.assertTrue(run_barebone(commands, ["OK"], 1))

    def test_constructors_constraints_scd(self):
        commands = bottom + action_language + bottom_link_al + \
                    add_constraint("2", "String", code_string) + \
                    add_constraint("2", "StringAttr", code_string) + \
                    add_constraint("1", "Natural", code_natural) + \
                    add_constraint("1", "String", code_string) + \
                    add_constraint("1", "Location", code_location) + \
                    add_constraint("1", "Boolean", code_boolean) + \
                    add_constraint("1", "ActionLanguage", code_complex_attribute) + \
                    conformance_check("models/SimpleClassDiagrams_new") + ["return", False]
        self.assertTrue(run_barebone(commands, ["OK"], 1))

    def test_constructors_constraints_pn(self):
        commands = bottom + action_language + bottom_link_al + \
                    add_constraint("2", "String", code_string) + \
                    add_constraint("2", "StringAttr", code_string) + \
                    add_constraint("1", "Natural", code_natural) + \
                    add_constraint("1", "String", code_string) + \
                    add_constraint("1", "Location", code_location) + \
                    add_constraint("1", "Boolean", code_boolean) + \
                    add_constraint("1", "ActionLanguage", code_complex_attribute) + \
                    instantiate_scd + \
                    add_constraint("3", "Natural", code_natural) + \
                    conformance_check("models/PetriNets_new") + \
                    instantiate_pn + \
                    conformance_check("models/PN_instance") + ["return", False]
        self.assertTrue(run_barebone(commands, ["OK"], 1))

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

    def test_constructors_instantiate_pn(self):
        commands = bottom + 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 + 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 + instantiate_example + is_direct_instance + ["return", False]
        expected = [True, False,
                    False, True,
                    False, False]
        self.assertTrue(run_barebone(commands, expected, 1))

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