include "primitives.alh"
include "core_algorithm.alh"
include "transform.alh"
include "modelling.alh"

Diagram dynamic_trafficlight {
    name = "Dynamic Traffic Light"
    author = "Yentl Van Tendeloo"
}

Class manager {
    name = "Manager"
    default = True
    constructor_body = $
            Void function constructor(attributes : Element, parameters : Element):
                return !
        $

    {behaviour} CompositeState manager_main {
        name = "main"
        isInitial = True
        {composite_children} BasicState manager_main_init {
            name = "init"
            isInitial = True
        }
        {composite_children} BasicState manager_main_did_transform {
            name = "MT OK"
            isInitial = False
        }
        {composite_children} BasicState manager_main_did_actionlanguage {
            name = "AL OK"
            isInitial = False
        }
    }
}

transition (manager_main_init, manager_main_did_transform) {
    name = "model transformation"

    script = $
            Void function script(attributes : Element, parameters : Element):
                Element m
                Element models

                // Merge models
                models = dict_create()
                dict_add(models, "A", "ModelA")
                dict_add(models, "B", "ModelB")
                m = merge_models(models, "TransformY")

                // Do transformation
                transform(m, get_model("TransformY", get_ramified_metamodel("TransformY")))

                // Split up the result
                Element metamodels
                Element split
                metamodels = dict_create()
                dict_add(metamodels, "C", "MetamodelC")
                dict_add(metamodels, "B", "MetamodelB")
                split = split_model(m, metamodels)

                // Store the results
                store_model("ModelC", "MetamodelC", split["C"])
                store_model("ModelB", "MetamodelB", split["B"])
                return!
        $
}

transition (manager_main_did_transform, manager_main_did_actionlanguage) {
    name = "action language"

    script = $
            Void function script(attributes : Element, parameters : Element):
                Element m
                Element models

                // Merge models
                models = dict_create()
                dict_add(models, "C", "ModelC")
                dict_add(models, "B", "ModelB")
                m = merge_models(models, "OperationZ")

                // Do transformation
                Element func
                func = get_func_AL_model(get_model("OperationZ", "ActionLanguage"))
                func(m)

                // Split up the result
                Element metamodels
                Element split
                metamodels = dict_create()
                dict_add(metamodels, "A", "MetamodelA")
                dict_add(metamodels, "B", "MetamodelB")
                split = split_model(m, metamodels)

                // Store the results
                store_model("ModelA", "MetamodelA", split["A"])
                store_model("ModelB", "MetamodelB", split["B"])
                return!
        $
}
