include "primitives.alh"

Diagram my_SCCD {
    name = "SimpleSCCD"
    author = "Yentl"
}

Class main {
    name = "Main"
    default = True
    constructor_body = $
            Void function const(attributes : Element, parameters : Element):
                dict_overwrite(attributes, "a", 0)
                if (element_eq(parameters, read_root())):
                    dict_overwrite(attributes, "id", 0)
                else:
                    dict_overwrite(attributes, "id", parameters)
                return !
        $
}

Attribute attr_a {
    name = "a"
}

Attribute attr_b {
    name = "id"
}

class_attributes (main, attr_a) {}
class_attributes (main, attr_b) {}

CompositeState main_statechart {
    name = "root"
    isInitial = True
    {composite_children} BasicState init {
        name = "initial"
        isInitial = True
        onEntryScript = $
            Void function entry(attributes : Element):
                log("ENTRY initial")
                return!
            $
    }

    {composite_children} BasicState exit {
        name = "exit"
        isInitial = False
        onEntryScript = $
            Void function entry(attributes : Element):
                log("ENTRY exit")
                return!
            $
    }

    {composite_children} ParallelState main_parallel {
        name = "parallel"
        isInitial = False

        {parallel_children} CompositeState parallel_x {
            {composite_children} BasicState x_a {
                name = "xa"
                isInitial = True
                onEntryScript = $
                        Void function entry(attributes : Element):
                            log("ENTRY xa")
                            return!
                    $
                onExitScript = $
                        Void function exit(attributes : Element):
                            log("EXIT xa")
                            return!
                    $
            }
            {composite_children} BasicState x_b {
                name = "xb"
                isInitial = False
                onEntryScript = $
                        Void function entry(attributes : Element):
                            log("ENTRY xb")
                            return!
                    $
                onExitScript = $
                        Void function exit(attributes : Element):
                            log("EXIT xb")
                            return!
                    $
            }
            {composite_children} BasicState x_c {
                name = "xc"
                isInitial = False
            }
            {composite_children} BasicState x_d {
                name = "xd"
                isInitial = False
            }
        }

        {parallel_children} CompositeState parallel_y {
            {composite_children} BasicState y_a {
                name = "ya"
                isInitial = True
            }
            {composite_children} BasicState y_b {
                name = "yb"
                isInitial = False
            }
        }
    }
}

transition (x_a, x_b) {
    name = "X"
    event = "X"
    script = $
            Void function script(attributes : Element, evt_param : Element):
                log("In script")
                dict_overwrite(attributes, "a", 1)
                dict_overwrite(attributes, "id", 2)
                return!
        $
}

transition (x_a, x_d) {
    name = "timeout"
    after = $
            Float function after(attributes : Element):
                log("In after")
                return 3.0!
        $
}

transition (x_b, x_c) {
    name = "Z"
    cond = $
            Boolean function cond(attributes : Element, evt_param : Element):
                log("In condition")
                return integer_lt(attributes["a"], attributes["id"])!
        $
}

transition transition_y (y_a, y_b) {
    name = "Y"
    event = "Y"
}

Raise create_new_class {
    scope = "cd"
    event = "create_instance"
    parameter = $
            Element function func(attributes : Element, parameters : Element):
                Element result
                log("Executing create_instance!")
                result = create_node()
                list_append(result, "Main")
                list_append(result, attributes["id"])
                dict_overwrite(attributes, "id", integer_addition(attributes["id"], 1))
                list_append(result, attributes["id"])
                return result!
        $
}

transition_raises (transition_y, create_new_class) {}

transition (init, main_parallel) {
    name = "init"
    event = "init"
}

transition (main_parallel, exit) {
    name = "exit"
    event = "exit"
}

diagram_classes (my_SCCD, main) {}
behaviour (main, main_statechart) {}
