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

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

### live modelling DTCBD

model_add("formalisms/DTCBD/Design_MM", "formalisms/SimpleClassDiagrams", open("models/DTCBD/metamodels/DTCBD_MM.mvc", 'r').read())
model_add("formalisms/DTCBD/PartialRuntime_MM", "formalisms/SimpleClassDiagrams", open("models/DTCBD/metamodels/DTCBD_MM_partial_runtime.mvc", 'r').read())
model_add("formalisms/DTCBD/FullRuntime_MM", "formalisms/SimpleClassDiagrams", open("models/DTCBD/metamodels/DTCBD_MM_runtime.mvc", 'r').read())

model_add("models/DTCBD_model", "formalisms/DTCBD/Design_MM", open("models/DTCBD/models/simple_equation.mvc", 'r').read())

transformation_add_MANUAL({"Design": "formalisms/DTCBD/Design_MM"}, {"Design": "formalisms/DTCBD/Design_MM"}, "models/DTCBD/edit")

def trace_D2P(model):
    instantiate(model, "Association", ("Design/Block", "PartialRuntime/Block"), ID="D2P_block")

transformation_add_AL({"Design": "formalisms/DTCBD/Design_MM", "PartialRuntime": "formalisms/DTCBD/PartialRuntime_MM"}, {"PartialRuntime": "formalisms/DTCBD/PartialRuntime_MM"}, "models/DTCBD/toRuntime", open("models/DTCBD/transformations/to_runtime.alc", 'r').read(), trace_D2P)

def trace_P2F(model):
    instantiate(model, "Association", ("PartialRuntime/Block", "FullRuntime/Block"), ID="P2F_block")

transformation_add_AL({"PartialRuntime": "formalisms/DTCBD/PartialRuntime_MM", "FullRuntime": "formalisms/DTCBD/FullRuntime_MM"}, {"NewFullRuntime": "formalisms/DTCBD/FullRuntime_MM"}, "models/DTCBD/merge", open("models/DTCBD/transformations/merge.alc", 'r').read(), trace_P2F)
transformation_add_AL({"FullRuntime": "formalisms/DTCBD/FullRuntime_MM"}, {"FullRuntime": "formalisms/DTCBD/FullRuntime_MM"}, "models/DTCBD/simulate", open("models/DTCBD/transformations/simulate.alc", 'r').read())
transformation_add_AL({}, {}, "models/DTCBD/restartSim", open("models/generic/restart_simulation.alc", 'r').read())

model_add("models/live_modelling_DTCBD", "formalisms/ProcessModel", open("models/DTCBD/PM_live_modelling.mvc", 'r').read())

def modify_model(model):
    elements = element_list_nice(model)
    for e in elements:
        if e.get("value", None) == 2.0:
            while 1:
                time.sleep(1)
        elif e.get("__type", None) == "Design/AdditionBlock":
            adder = e["__id"]
            links = read_outgoing(model, adder, "Design/Link")
            destinations = [read_association_destination(model, link) for link in links]
    else:
        mult = instantiate(model, "Design/MultiplyBlock")
        two = instantiate(model, "Design/ConstantBlock")
        attr_assign(model, two, "value", 2.0)
        instantiate(model, "Design/Link", (adder, mult))
        instantiate(model, "Design/Link", (two, mult))
        for link in links:
            delete_element(model, link)
        for destination in destinations:
            instantiate(model, "Design/Link", (mult, destination))

process_execute("models/live_modelling_DTCBD", {"design_model": "models/DTCBD_model"}, {"models/DTCBD/edit": modify_model})
