import unittest
from modelverse_kernel.main import ModelverseKernel
from utils import execute_until_finished, MvSWrapper, get_inst, get_phase, get_returnvalue_ref

class TestOutput(unittest.TestCase):
    def setUp(self):
        self.mvs = MvSWrapper()
        self.root = self.mvs.execute("RR", [])[0]
        self.mvk = ModelverseKernel(self.root)

        self.user_root = self.mvs.execute("CN", [])[0]
        self.user_frame = self.mvs.execute("CN", [])[0]
        self.user_input = self.mvs.execute("CN", [])[0]
        self.user_output = self.mvs.execute("CN", [])[0]
        self.symbols = self.mvs.execute("CN", [])[0]
        self.evalstack = self.mvs.execute("CN", [])[0]
        self.returnvalue = self.mvs.execute("CN", [])[0]
        self.phase = self.mvs.execute("CNV", ["init"])[0]

        self.mvs.execute("CD", [self.root, "user_1", self.user_root])
        self.mvs.execute("CD", [self.user_root, "input", self.user_input])
        self.mvs.execute("CD", [self.user_root, "last_input", self.user_input])
        self.mvs.execute("CD", [self.user_root, "output", self.user_output])
        self.mvs.execute("CD", [self.user_root, "last_output", self.user_output])
        self.mvs.execute("CD", [self.user_root, "frame", self.user_frame])
        self.mvs.execute("CD", [self.user_frame, "symbols", self.symbols])
        self.mvs.execute("CD", [self.user_frame, "evalstack", self.evalstack])
        self.mvs.execute("CD", [self.user_frame, "phase", self.phase])
        self.mvs.execute("CD", [self.user_frame, "returnvalue", self.returnvalue])

    def test_output_value(self):
        self.input_value = self.mvs.execute("CN", [])[0]
        self.inst = self.mvs.execute("CNV", [{"value":"output"}])[0]

        self.const = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.value_a = self.mvs.execute("CNV", ["a"])[0]

        self.mvs.execute("CD", [self.user_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "value", self.const])
        self.mvs.execute("CD", [self.const, "node", self.value_a])

        ### Execute rules
        self.assertEquals(get_inst(self.root, self.mvs), "output")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue_ref(self.root, self.mvs), self.value_a)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "output")
        self.assertEquals(get_phase(self.root, self.mvs), "output")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "output")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [self.user_output, "value"])[0]])[0], "a")

    def test_output_node(self):
        self.input_value = self.mvs.execute("CN", [])[0]
        self.inst = self.mvs.execute("CNV", [{"value":"output"}])[0]

        self.const = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        self.value = self.mvs.execute("CN", [])[0]

        self.mvs.execute("CD", [self.user_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "value", self.const])
        self.mvs.execute("CD", [self.const, "node", self.value])

        ### Execute rules
        self.assertEquals(get_inst(self.root, self.mvs), "output")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue_ref(self.root, self.mvs), self.value)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "output")
        self.assertEquals(get_phase(self.root, self.mvs), "output")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "output")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(self.mvs.execute("RD", [self.user_output, "value"])[0], self.value)

    def test_output_edge(self):
        self.input_value = self.mvs.execute("CN", [])[0]
        self.inst = self.mvs.execute("CNV", [{"value":"output"}])[0]

        self.const = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        a = self.mvs.execute("CN", [])[0]
        b = self.mvs.execute("CN", [])[0]
        self.value = self.mvs.execute("CE", [a,b])[0]

        self.mvs.execute("CD", [self.user_frame, "IP", self.inst])
        self.mvs.execute("CD", [self.inst, "value", self.const])
        self.mvs.execute("CD", [self.const, "node", self.value])

        ### Execute rules
        self.assertEquals(get_inst(self.root, self.mvs), "output")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue_ref(self.root, self.mvs), self.value)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "output")
        self.assertEquals(get_phase(self.root, self.mvs), "output")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "output")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(self.mvs.execute("RD", [self.user_output, "value"])[0], self.value)
