import sys
sys.path.append("wrappers")
from modelverse import *

init()
login("admin", "admin")

model_add("test/SCCD", "formalisms/SimpleClassDiagrams", open("models/SCCD/metamodels/SCCD.mvc", 'r').read())
model_add("test/SCCD_Trace", "formalisms/SimpleClassDiagrams", open("models/DiscreteEventTrace/metamodels/trace.mvc", 'r').read())

model_add("test/my_SCCD", "test/SCCD", open("models/SCCD/models/dynamic_trafficlight.mvc", 'r').read())

#transformation_add_AL({"SCCD": "test/SCCD"}, {"trace": "test/SCCD_Trace"}, "test/SCCD_execute_afap", open("models/SCCD/transformations/execute.alc", 'r').read().replace("afap = False", "afap = True"))
transformation_add_AL({"SCCD": "test/SCCD"}, {"trace": "test/SCCD_Trace"}, "test/SCCD_execute_afap", open("models/SCCD/transformations/execute.alc", 'r').read())

import poll_print
ctrl = poll_print.Controller(keep_running=False)

thrd = threading.Thread(target=ctrl.start)
thrd.daemon = True
thrd.start()

def input_thread():
    import select
    while thrd.is_alive():
        if select.select([sys.stdin], [], [], 0.1)[0]:
            ctrl.addInput(Event("raw_inp", "user_inp", [sys.stdin.readline().strip()]))

thrd2 = threading.Thread(target=input_thread)
thrd2.daemon = True
thrd2.start()

transformation_execute_AL("test/SCCD_execute_afap", {"SCCD": "test/my_SCCD"}, {"trace": "test/my_SCCD_trace"}, statechart=(ctrl, "inp", "outp"))

alter_context("test/my_SCCD_trace", "test/SCCD_Trace")
lst = element_list_nice("test/my_SCCD_trace")

lst.sort(key=lambda i: (i["timestamp"], i["name"]))
result = [(i["timestamp"], str(i["name"])) for i in lst if i["name"] not in ["updateTimerValue", "updateTimerColour", "resetTimer"]]
print(result)
