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

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

### live modelling FSA

model_add("formalisms/FSA/Design_MM", "formalisms/SimpleClassDiagrams", open("models/FiniteStateAutomata/metamodels/FSA_MM.mvc", 'r').read())
model_add("formalisms/FSA/PartialRuntime_MM", "formalisms/SimpleClassDiagrams", open("models/FiniteStateAutomata/metamodels/FSA_MM_partial_runtime.mvc", 'r').read())
model_add("formalisms/FSA/FullRuntime_MM", "formalisms/SimpleClassDiagrams", open("models/FiniteStateAutomata/metamodels/FSA_MM_runtime.mvc", 'r').read())

model_add("models/FSA_model", "formalisms/FSA/Design_MM", open("models/FiniteStateAutomata/models/alarm.mvc", 'r').read())

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

def trace_D2P(model):
    instantiate(model, "Association", ("Design/State", "PartialRuntime/State"), ID="D2P_state")

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

def trace_P2F(model):
    instantiate(model, "Association", ("PartialRuntime/State", "FullRuntime/State"), ID="P2F_state")

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

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

def edit_FSA(model):
    elements = element_list_nice(model)
    for e in elements:
        if e.get("name", None) == "idle":
            delete_element(model, e["__id"])
            break
    else:
        while 1:
            time.sleep(1)

    # Removed, so set a new element as initial
    for e in elements:
        if e.get("name", None) == "armed":
            attr_assign(model, e["__id"], "initial", True)
            break

process_execute("models/live_modelling_FSA", {"design_model": "models/FSA_model"}, {"models/FSA/edit": edit_FSA})
