import sys
sys.path.append("wrappers")
from modelverse import *
from sccd.runtime.statecharts_core import Event

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

# Assume that we don't change the SCCD MM
try:
    model_add("formalisms/SCCD", "formalisms/SimpleClassDiagrams", open("models/SCCD.mvc", 'r').read())
except ModelExists:
    pass

try:
    model_add("formalisms/SCCD_Trace", "formalisms/SimpleClassDiagrams", open("models/SCCD_Trace.mvc", 'r').read())
except ModelExists:
    pass

# Update the model of the traffic light
if "my_SCCD" in [i[0] for i in model_list("models")]:
    model_delete("models/my_SCCD")
model_add("models/my_SCCD", "formalisms/SCCD", open("models/dynamic_trafficlight.mvc", 'r').read())

# Add SCCD execution semantics
transformation_add_AL({"SCCD": "formalisms/SCCD"}, {"trace": "formalisms/SCCD_Trace"}, "models/SCCD_execute", open("models/SCCD_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()

transformation_execute_AL("models/SCCD_execute", {"SCCD": "models/my_SCCD"}, {"trace": "models/my_trace"}, statechart=(ctrl, "inp", "outp"))

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()]))

thrd.join()
