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

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

### live modelling CTCBD

model_add("formalisms/CTCBD/Design_MM", "formalisms/SimpleClassDiagrams", open("models/CTCBD/metamodels/CTCBD_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/CTCBD_model", "formalisms/CTCBD/Design_MM", open("models/CTCBD/models/spring.mvc", 'r').read())

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

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

transformation_add_MT({"Design": "formalisms/CTCBD/Design_MM"}, {"PartialRuntime": "formalisms/DTCBD/PartialRuntime_MM"}, "models/CTCBD/toRuntime", open("models/CTCBD/transformations/to_partial_runtime.mvc", '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_CTCBD", "formalisms/ProcessModel", open("models/CTCBD/PM_live_modelling.mvc", 'r').read())

def modify_model(model):
    elements = element_list_nice(model)
    for e in elements:
        if e.get("value", None) == 10.0:
            attr_assign(model, e["__id"], "value", 100.0)
            break
    else:
        while 1:
            time.sleep(1)

process_execute("models/live_modelling_CTCBD", {"design_model": "models/CTCBD_model"}, {"models/CTCBD/edit": modify_model})
