<class name="Modelverse">
    <relationships>
        <association name="http_client" class="HTTPClient" min="2" max="2"/>
    </relationships>

    <method name="load_action">
        <parameter name="context"/>
        <body>
            action = self.actions[context].pop(0)
            self.parameters = action["parameters"]
            self.current_ID = action["ID"]
            self.action_name = action["name"]
        </body>
    </method>

    <method name="dict_to_list">
        <parameter name="d"/>
        <body>
            l = []
            for k, v in d.items():
                l.append(k)
                l.append(v)
            l.append("")
            return l
        </body>
    </method>

    <method name="split_response">
        <parameter name="response"/>
        <body>
            splitted = response.strip().split(": ", 1)
            if len(splitted) > 1:
                return splitted[1].split("\n")
            else:
                return []
        </body>
    </method>

    <method name="expect_response">
        <parameter name="expected"/>
        <parameter name="pop" default="True"/>
        <body>
            if self.responses and self.responses[0] == expected:
                if pop:
                    del self.responses[0]
                return True
            else:
                return False
        </body>
    </method>

    <method name="expect_response_partial">
        <parameter name="expected"/>
        <parameter name="pop" default="False"/>
        <body>
            if self.responses and self.responses[0].startswith(expected):
                if pop:
                    del self.responses[0]
                return True
            else:
                return False
        </body>
    </method>

    <method name="expect_action">
        <parameter name="context"/>
        <parameter name="expected"/>
        <body>
            if isinstance(expected, list):
                return self.actions[context] and self.actions[context][0]["name"] in expected
            else:
                return self.actions[context] and self.actions[context][0]["name"] == expected
        </body>
    </method>

    <method name="expect_input">
        <parameter name="context"/>
        <parameter name="expected"/>
        <body>
            if context not in self.inputs:
                return False
            if isinstance(expected, list):
                return self.inputs[context] and self.inputs[context][0]["name"] in expected
            else:
                return self.inputs[context] and self.inputs[context][0]["name"] == expected
        </body>
    </method>

    <constructor>
        <parameter name="taskname" default="None"/>
        <body>
            self.controller.taskname = taskname
            self.actions = {None: []}
            self.responses = []
            self.http_clients = []
            self.context = None
            self.registered_metamodel = {}
            self.inputs = {}
            self.finish_output_thread = False
        </body>
    </constructor>

    <scxml initial="init" priority="source_child">
        <state id="init">
            <onentry>
                <raise scope="cd" event="create_instance">
                    <parameter expr="'http_client'"/>
                    <parameter expr="'HTTPClient'"/>
                </raise>

                <raise scope="cd" event="create_instance">
                    <parameter expr="'http_client'"/>
                    <parameter expr="'HTTPClient'"/>
                </raise>
            </onentry>

            <transition event="instance_created" target=".">
                <parameter name="instance"/>
                <script>
                    self.http_clients.append(instance)
                </script>
                <raise scope="cd" event="start_instance">
                    <parameter expr="instance"/>
                </raise>
            </transition>

            <transition cond="len(self.http_clients) == 2" target="../waiting"/>
        </state>

        <state id="waiting">
            <transition event="http_client_initialized" target="../initialized"/>
        </state>

        <parallel id="initialized">
            <onentry>
                <raise event="ready"/>
            </onentry>

            <state id="http_mapper" initial="init">
                <state id="init">
                    <transition event="request" cond="isinstance(value, type([]))" target=".">
                        <parameter name="value"/>
                        <raise event="HTTP_input" target="self.http_clients[0]">
                            <parameter expr='urlencode({"op": "set_input", "data": json.dumps(value), "taskname": self.taskname})'/>
                            <parameter expr='None'/>
                        </raise>
                    </transition>

                    <transition event="request" cond="not isinstance(value, type([]))" target=".">
                        <parameter name="value"/>
                        <raise event="HTTP_input" target="self.http_clients[0]">
                            <parameter expr='urlencode({"op": "set_input", "value": json.dumps(value), "taskname": self.taskname})'/>
                            <parameter expr='None'/>
                        </raise>
                    </transition>

                    <transition event="request_raw" target=".">
                        <parameter name="value"/>
                        <parameter name="taskname"/>
                        <parameter name="http_client"/>
                        <raise event="HTTP_input" target="self.http_clients[http_client]">
                            <parameter expr='urlencode({"op": "set_input", "value": json.dumps(value), "taskname": taskname})'/>
                            <parameter expr='"parent"'/>
                        </raise>
                    </transition>

                    <transition event="HTTP_output" cond="not self.finish_output_thread" target=".">
                        <parameter name="data"/>
                        <raise event="HTTP_input" target="self.http_clients[1]">
                            <parameter expr='urlencode({"op": "get_output", "taskname": self.taskname})'/>
                            <parameter expr='"parent"'/>
                        </raise>
                        <script>
                            self.responses.append(json.loads(data))
                            #print("Got data at %s: %s" % (self.controller.taskname, str(json.loads(data))))
                        </script>
                    </transition>

                    <transition event="HTTP_output" cond="self.finish_output_thread" target="../finished">
                        <parameter name="data"/>
                        <raise event="HTTP_input" target="self.http_clients[1]">
                            <parameter expr='urlencode({"op": "get_output", "taskname": self.taskname})'/>
                            <parameter expr='"parent"'/>
                        </raise>
                        <script>
                            self.responses.append(json.loads(data))
                        </script>
                    </transition>
                </state>

                <state id="finished"/>
            </state>

            <state id="behaviour" initial="wait_for_action">
                <state id="init" initial="connect_http_client">
                    <onentry>
                        <script>
                            self.address, self.timeout = self.parameters
                            self.address = self.address.rsplit(":", 1)
                            self.address = (self.address[0], int(self.address[1]))

                            self.i = 0
                            taskname = self.controller.taskname
                            if taskname is None:
                                taskname = str(uuid.uuid4())
                                self.skip_init = False
                            else:
                                self.skip_init = True
                            
                            self.controller.taskname = self.taskname = taskname
                        </script>
                    </onentry>

                    <state id="connect_http_client">
                        <transition cond="self.i &lt; 2" target="../waiting_http_client">
                            <raise scope="narrow" target="self.http_clients[self.i]" event="connect">
                                <parameter expr="self.address"/>
                                <parameter expr="self.timeout"/>
                            </raise>
                        </transition>

                        <transition cond="self.i == 2" target="../../wait_for_action/connected">
                            <!-- Start polling for output -->
                            <raise event="HTTP_input" target="self.http_clients[1]">
                                <parameter expr='urlencode({"op": "get_output", "taskname": self.taskname})'/>
                                <parameter expr='"parent"'/>
                            </raise>

                            <!-- Sent out completion -->
                            <raise event="result">
                                <parameter expr="[]"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="waiting_http_client">
                        <transition event="http_client_ready" cond="self.skip_init" target="../connect_http_client">
                            <script>
                                self.i += 1
                            </script>
                        </transition>

                        <transition event="http_client_ready" cond="not self.skip_init" target="../wait_for_taskname_ack">
                            <!-- Request the task to be created -->
                            <raise event="request_raw">
                                <parameter expr="self.taskname"/>
                                <parameter expr="'task_manager'"/>
                                <parameter expr="self.i"/>
                            </raise>

                            <script>
                                self.i += 1
                            </script>
                        </transition>

                        <transition event="http_client_timeout" target="../../wait_for_action/disconnected">
                            <raise event="exception">
                                <parameter expr="'NetworkException'"/>
                                <parameter expr="'Connection timeout'"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="wait_for_taskname_ack">
                        <transition cond="self.expect_response('OK', pop=True)" target="../connect_http_client"/>
                    </state>
                </state>

                <state id="operations" initial="model_list">
                    <history id="history" type="deep"/>

                    <state id="login" initial="wait_prompt_1">
                        <state id="wait_prompt_1">
                            <transition cond="self.expect_response('Log on as which user?', pop=True)" target="../wait_prompt_2">
                                <raise event="request">
                                    <parameter expr="self.parameters[0]"/>
                                </raise>
                            </transition>
                        </state>

                        <state id="wait_prompt_2">
                            <transition cond="self.expect_response('Password for existing user?', pop=True)" target="../wait_prompt_existing">
                                <raise event="request">
                                    <parameter expr="self.parameters[1]"/>
                                </raise>
                            </transition>

                            <transition cond="self.expect_response('This is a new user: please give password!', pop=True)" target="../wait_prompt_existing">
                                <raise event="request">
                                    <parameter expr="self.parameters[1]"/>
                                </raise>
                            </transition>
                        </state>

                        <state id="wait_prompt_existing">
                            <transition cond="self.expect_response('Please repeat the password', pop=True)" target=".">
                                <raise event="request">
                                    <parameter expr="self.parameters[1]"/>
                                </raise>
                            </transition>
                            <transition cond="self.expect_response('Passwords match!', pop=True)" target="."/>
                            <transition cond="self.expect_response('Welcome to the Model Management Interface v2.0!', pop=True)" target="../login_ok"/>
                            <transition cond="self.expect_response('Wrong password!', pop=True)" target="../../../wait_for_action/connected">
                                <raise event="exception">
                                    <parameter expr="'PermissionDenied'"/>
                                    <parameter expr="'Permission denied!'"/>
                                </raise>
                            </transition>
                        </state>

                        <state id="login_ok">
                            <transition cond="self.expect_response('Use the \'help\' command for a list of possible commands', pop=True)" target="../../../wait_for_action/megamodelling">
                                <raise event="request">
                                    <parameter expr="'quiet'"/>
                                </raise>
                                <raise event="result">
                                    <parameter expr="None"/>
                                </raise>
                            </transition>
                        </state>
                    </state>

                    <state id="model_list">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['model_list', self.parameters[0]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="set(self.split_response(self.responses.pop(0)))"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="model_add" initial="send_metadata">
                        <state id="send_metadata">
                            <onentry>
                                <raise event="request">
                                    <parameter expr="['model_add', self.parameters[1], self.parameters[0], self.parameters[2]]"/>
                                </raise>
                            </onentry>

                            <transition cond="self.expect_response('Success')" target="../../../wait_for_action/history">
                                <script>
                                    self.registered_metamodel[self.parameters[0]] = self.parameters[1]
                                </script>
                                <raise event="result">
                                    <parameter expr="None"/>
                                </raise>
                            </transition>
                        </state>
                    </state>

                    <state id="model_delete">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['model_delete', self.parameters[0]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Success')" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="None"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="model_list_full">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['model_list_full', self.parameters[0]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <script>
                                response = self.split_response(self.responses.pop(0))
                                result = set()
                                for res in response:
                                    i = res.split(" ", 3)
                                    result.add((i[3], i[1], i[2], i[0]))
                            </script>
                            <raise event="result">
                                <parameter expr="result"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="verify">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['verify', self.parameters[0], self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <state id="waiting">
                            <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../../wait_for_action/history">
                                <raise event="result">
                                    <parameter expr="self.split_response(self.responses.pop(0))[0]"/>
                                </raise>
                            </transition>
                        </state>
                    </state>

                    <state id="alter_context">
                        <transition target="../../wait_for_action/history">
                            <script>
                                self.registered_metamodel[self.parameters[0]] = self.parameters[1]
                            </script>
                        </transition>
                    </state>

                    <state id="model_overwrite" initial="init">
                        <state id="init">
                            <transition cond="self.parameters[0] in self.registered_metamodel" target="../send_metadata"/>
                            <transition cond="self.parameters[0] not in self.registered_metamodel" target="../search"/>
                        </state>

                        <state id="search">
                            <onentry>
                                <raise event="request">
                                    <parameter expr="['model_types', self.parameters[0]]"/>
                                </raise>
                            </onentry>

                            <transition cond="self.expect_response_partial('Success: ')" target="../send_metadata">
                                <script>
                                    self.registered_metamodel[self.parameters[0]] = set(self.split_response(self.responses.pop(0))).pop()
                                    #print("Registering " + str(self.parameters[0]))
                                </script>
                            </transition>
                        </state>

                        <state id="send_metadata">
                            <onentry>
                                <raise event="request">
                                    <parameter expr="['model_overwrite', self.parameters[0], self.registered_metamodel[self.parameters[0]]]"/>
                                </raise>
                            </onentry>

                            <transition cond="self.expect_response('Waiting for model constructors...')" target="../send_model"/>
                        </state>

                        <state id="send_model">
                            <onentry>
                                <raise event="request">
                                    <parameter expr="self.parameters[1]"/>
                                </raise>
                            </onentry>

                            <transition cond="self.expect_response('Success')" target="../../../wait_for_action/history">
                                <raise event="result">
                                    <parameter expr="None"/>
                                </raise>
                            </transition>
                        </state>
                    </state>

                    <state id="disconnect">
                        <onentry>
                            <raise event="request">
                                <parameter expr="'exit'"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Success')" target="../../wait_for_action/finished">
                            <raise event="result">
                                <parameter expr="None"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="user_logout">
                        <onentry>
                            <raise event="request">
                                <parameter expr="'user_logout'"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Success')" target="../../wait_for_action/connected">
                            <raise event="result">
                                <parameter expr="None"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="user_delete">
                        <onentry>
                            <raise event="request">
                                <parameter expr="'self-destruct'"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Success')" target="../../wait_for_action/finished">
                            <raise event="result">
                                <parameter expr="None"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="model_render">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['model_render', self.parameters[0], self.parameters[1], self.parameters[2]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="json.loads(self.split_response(self.responses.pop(0))[0])"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="model_rendered">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['model_rendered', self.parameters[0], self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="set(self.split_response(self.responses.pop(0)))"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="transformation_between">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['transformation_between'] + self.dict_to_list(self.parameters[0]) + self.dict_to_list(self.parameters[1])"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="set(self.split_response(self.responses.pop(0)))"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="transformation_signature">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['transformation_read_signature', self.parameters[0]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <script>
                                results = self.split_response(self.responses.pop(0))
                                inputs = {}
                                outputs = {}
                                for result in results:
                                    key, value = result[1:].strip().split(" ", 1)
                                    if result[0] == "I":
                                        inputs[key] = value
                                    elif result[0] == "O":
                                        outputs[key] = value
                            </script>
                            <raise event="result">
                                <parameter expr="(inputs, outputs)"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="process_signature">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['process_signature', self.parameters[0]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <script>
                                results = self.split_response(self.responses.pop(0))
                                signature = {}
                                for result in results:
                                    key, value = result.strip().split(" : ", 1)
                                    signature[key] = value
                            </script>
                            <raise event="result">
                                <parameter expr="signature"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="store_on_scripted" initial="transformation_add">
                        <state id="transformation_add" initial="send_metadata">
                            <state id="send_metadata">
                                <onentry>
                                    <raise event="request">
                                        <parameter expr="[self.action_name] + self.dict_to_list(self.parameters[0]) + self.dict_to_list(self.parameters[1]) + [self.parameters[2]]"/>
                                    </raise>
                                </onentry>

                                <transition cond="self.expect_response_partial('Please edit this model before sending next input: ', pop=False)" target="../wait_for_user">
                                    <script>
                                        model = self.responses.pop(0).split(": ", 1)[1]
                                    </script>
                                    <raise event="result">
                                        <parameter expr="model"/>
                                    </raise>
                                </transition>

                                <transition cond="self.expect_response('Waiting for code constructors...')" target="../upload_changes">
                                    <raise event="result">
                                        <parameter expr="None"/>
                                    </raise>
                                    <raise event="request">
                                        <parameter expr="self.parameters[3]"/>
                                    </raise>
                                </transition>
                            </state>

                            <state id="wait_for_user">
                                <transition cond="None in self.inputs and self.inputs[None]" target="../upload_changes">
                                    <script>
                                        self.inputs[None].pop(0)
                                    </script>
                                    <raise event="request">
                                        <parameter expr="True"/>
                                    </raise>
                                </transition>
                            </state>

                            <state id="upload_changes">
                                <transition cond="self.expect_response('Waiting for code constructors...')" target=".">
                                    <raise event="request">
                                        <parameter expr="self.parameters[3]"/>
                                    </raise>
                                </transition>

                                <transition cond="self.expect_response('Waiting for model constructors...')" target=".">
                                    <raise event="request">
                                        <parameter expr="self.parameters[3]"/>
                                    </raise>
                                </transition>

                                <transition cond="self.expect_response('Success')" target="../../../../wait_for_action/megamodelling">
                                    <raise event="result">
                                        <parameter expr="None"/>
                                    </raise>
                                </transition>
                            </state>
                        </state>

                        <state id="transformation_execute" initial="send_operation">
                            <state id="send_operation">
                                <onentry>
                                    <raise event="request">
                                        <parameter expr="['transformation_execute', self.parameters[0]] + self.dict_to_list(self.parameters[1]) + self.dict_to_list(self.parameters[2]) + [self.parameters[3]]"/>
                                    </raise>
                                    <script>
                                        if len(self.parameters) > 4 and self.parameters[4] == False:
                                            self.finish_output_thread = True
                                    </script>
                                </onentry>

                                <transition cond="self.expect_response_partial('Spawned activity on task: ', pop=False)" target="../waiting">
                                    <script>
                                        self.sub_sc_taskname = self.responses.pop(0).split(": ", 1)[1]
                                    </script>
                                    <raise event="result">
                                        <parameter expr="self.sub_sc_taskname"/>
                                    </raise>
                                </transition>
                            </state>

                            <state id="waiting">
                                <transition cond="self.expect_response('Finished task: ' + self.sub_sc_taskname)" target="."/>
                                <transition cond="self.expect_response('Success', pop=False) or self.expect_response('Failure', pop=False)" target="../../../../wait_for_action/megamodelling">
                                    <raise event="result">
                                        <parameter expr="self.responses.pop(0) == 'Success'"/>
                                    </raise>
                                </transition>
                            </state>
                        </state>

                        <state id="process_execute" initial="init">
                            <state id="init">
                                <onentry>
                                    <raise event="request">
                                        <parameter expr="['process_execute', self.parameters[0]] + self.dict_to_list(self.parameters[1])"/>
                                    </raise>
                                </onentry>

                                <transition cond="self.expect_response('Success', pop=True)" target="../executing"/>
                            </state>

                            <state id="executing">
                                <transition cond="self.responses and ' : ' in self.responses[0]" target=".">
                                    <script>
                                        taskname, operation = self.responses.pop(0).split(" : ")
                                    </script>
                                    <raise event="result">
                                        <parameter expr="[taskname, operation]"/>
                                    </raise>
                                </transition>

                                <transition cond="self.expect_response('Success', pop=True)" target="../../../../wait_for_action/megamodelling">
                                    <script>
                                        print("FINISH in SC")
                                    </script>
                                    <raise event="result">
                                        <parameter expr="'Success'"/>
                                    </raise>
                                    <script>
                                        print("RAISED")
                                    </script>
                                </transition>
                            </state>
                        </state>

                        <history id="history" type="deep"/>
                    </state>

                    <state id="permission_modify">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['permission_modify', self.parameters[0], self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="self.responses.pop(0)"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="permission_owner">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['permission_owner', self.parameters[0], self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="self.responses.pop(0)"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="permission_group">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['permission_group', self.parameters[0], self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="self.responses.pop(0)"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="group_create">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['group_create', self.parameters[0], self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="self.responses.pop(0)"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="group_delete">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['group_delete', self.parameters[0], self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="self.responses.pop(0)"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="group_owner_add">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['group_owner_add', self.parameters[0], self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="self.responses.pop(0)"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="group_owner_delete">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['group_owner_delete', self.parameters[0], self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="self.responses.pop(0)"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="group_join">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['group_join', self.parameters[0], self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="self.responses.pop(0)"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="group_kick">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['group_kick', self.parameters[0], self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="self.responses.pop(0)"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="group_list">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['group_list', self.parameters[0], self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="set(self.split_response(self.responses.pop(0)))"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="admin_promote">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['admin_promote', self.parameters[0], self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="self.responses.pop(0)"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="admin_demote">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['admin_demote', self.parameters[0], self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="self.responses.pop(0)"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="conformance_delete">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['remove_conformance', self.parameters[0], self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Success')" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="None"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="conformance_add">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['add_conformance', self.parameters[0], self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Success')" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="None"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="folder_create">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['folder_create', self.parameters[0]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Success')" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="None"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="model_types">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['model_types', self.parameters[0]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ')" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="set(self.split_response(self.responses.pop(0)))"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="element_list">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['list_full']"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <script>
                                responses = self.split_response(self.responses.pop(0))
                                result = set([tuple([j.strip() for j in i.split(':')]) for i in responses])
                            </script>
                            <raise event="result">
                                <parameter expr="result"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="element_list_nice">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['JSON']"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="json.loads(self.split_response(self.responses.pop(0))[0])"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="types">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['types']"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="set([tuple(entry.strip().split(' : ')) for entry in self.split_response(self.responses.pop(0))])"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="types_full">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['types_full']"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="set([tuple(entry.strip().split(' : ')) for entry in self.split_response(self.responses.pop(0))])"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="read_info">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['read_info', self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <script>
                                response = self.responses.pop(0).split(": ", 1)[1].strip()
                                values = {line.split(": ", 1)[0]: line.split(": ", 1)[1] for line in response.split("\n", 2)}
                                if "Source" in values:
                                    result = (values["Type"], (values["Source"], values["Destination"]))
                                else:
                                    result = (values["Type"], None)
                            </script>
                            <raise event="result">
                                <parameter expr="result"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="read_attrs">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['read_attrs', self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <script>
                                response = self.responses.pop(0).split(": ", 1)[1].strip()
                                values = {line.split(" : ", 1)[0]: line.split(" : ", 1)[1] for line in response.split("\n")}
                                values = {key: values[key].split(" = ", 1)[1] for key in values}
                            </script>
                            <raise event="result">
                                <parameter expr="values"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="read_defined_attrs">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['read_defined_attrs', self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <script>
                                response = self.responses.pop(0).split(": ", 1)[1].strip()
                                value_mandatory = {line.split(" : ", 1)[0]: line.split(" : ", 1)[1] for line in response.split("\n") if " : " in line}
                                value_optional = {line.split(" ?: ", 1)[0]: line.split(" ?: ", 1)[1] for line in response.split("\n") if " ?: " in line}
                            </script>
                            <raise event="result">
                                <parameter expr="(value_mandatory, value_optional)"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="instantiate" initial="decide_node_or_edge">
                        <state id="decide_node_or_edge">
                            <transition cond="self.parameters[2] is None" target="../created">
                                <raise event="request">
                                    <parameter expr="['instantiate_node', self.parameters[1], self.parameters[3]]"/>
                                </raise>
                            </transition>

                            <transition cond="self.parameters[2] is not None" target="../created">
                                <raise event="request">
                                    <parameter expr="['instantiate_edge', self.parameters[1], self.parameters[3], self.parameters[2][0], self.parameters[2][1]]"/>
                                </raise>
                            </transition>
                        </state>

                        <state id="created">
                            <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../../wait_for_action/history">
                                <raise event="result">
                                    <parameter expr="self.split_response(self.responses.pop(0))[0]"/>
                                </raise>
                            </transition>
                        </state>
                    </state>

                    <state id="delete_element">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['delete', self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Success')" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="None"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="attr_assign">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['attr_add', self.parameters[1], self.parameters[2], self.parameters[3]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Success')" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="None"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="attr_assign_code">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['attr_add_code', self.parameters[1], self.parameters[2], self.parameters[3]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Success')" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="None"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="attr_delete">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['attr_delete', self.parameters[1], self.parameters[2]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Success')" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="None"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="attr_type">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['attr_type', self.parameters[1], self.parameters[2], self.parameters[3]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Success')" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="None"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="attr_name">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['attr_name', self.parameters[1], self.parameters[2], self.parameters[3]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Success')" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="None"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="attr_optional">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['attr_optional', self.parameters[1], self.parameters[2], self.parameters[3]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Success')" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="None"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="read_outgoing">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['read_outgoing', self.parameters[1], self.parameters[2]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="set(self.split_response(self.responses.pop(0)))"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="read_incoming">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['read_incoming', self.parameters[1], self.parameters[2]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="set(self.split_response(self.responses.pop(0)))"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="read_association_source">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['read_association_source', self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="self.split_response(self.responses.pop(0))"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="read_association_destination">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['read_association_destination', self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="self.split_response(self.responses.pop(0))"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="connections_between">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['connections_between', self.parameters[1], self.parameters[2]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="set(self.split_response(self.responses.pop(0)))"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="define_attribute">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['define_attribute', self.parameters[1], self.parameters[2], self.parameters[3]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Success')" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="None"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="undefine_attribute">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['undefine_attribute', self.parameters[1], self.parameters[2]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Success')" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="None"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="all_instances">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['all_instances', self.parameters[1]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/history">
                            <raise event="result">
                                <parameter expr="set(self.split_response(self.responses.pop(0)))"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="exit_save">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['exit']"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Success')" target="../../wait_for_action/megamodelling">
                            <raise event="result">
                                <parameter expr="None"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="service_register">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['service_register', self.parameters[0]]"/>
                            </raise>
                            <script>
                                self.service_function = self.parameters[1]
                            </script>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ', pop=False)" target="../../wait_for_action/service">
                            <script>
                                port = self.split_response(self.responses.pop(0))[0]
                            </script>
                            <raise event="result">
                                <parameter expr="port"/>
                            </raise>
                        </transition>
                    </state>

                    <state id="service_stop">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['service_stop']"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Success')" target="../../wait_for_action/megamodelling"/>
                    </state>

                    <transition cond="self.expect_response_partial('Compilation error: ', pop=False)" target="../wait_for_action/history">
                        <raise event="exception">
                            <parameter expr="'CompilationError'"/>
                            <parameter expr="self.split_response(self.responses.pop(0))[0]"/>
                        </raise>
                    </transition>

                    <transition cond="self.expect_response_partial('Attribute not found: ', pop=False)" target="../wait_for_action/history">
                        <raise event="exception">
                            <parameter expr="'UnknownAttribute'"/>
                            <parameter expr="self.split_response(self.responses.pop(0))[0]"/>
                        </raise>
                    </transition>

                    <transition cond="self.expect_response_partial('Model exists: ', pop=False)" target="../wait_for_action/history">
                        <raise event="exception">
                            <parameter expr="'ModelExists'"/>
                            <parameter expr="self.split_response(self.responses.pop(0))[0]"/>
                        </raise>
                    </transition>

                    <transition cond="self.expect_response_partial('Attribute exists: ', pop=False)" target="../wait_for_action/history">
                        <raise event="exception">
                            <parameter expr="'AttributeExists'"/>
                            <parameter expr="self.split_response(self.responses.pop(0))[0]"/>
                        </raise>
                    </transition>

                    <transition cond="self.expect_response_partial('Element exists: ', pop=False)" target="../wait_for_action/history">
                        <raise event="exception">
                            <parameter expr="'ElementExists'"/>
                            <parameter expr="self.split_response(self.responses.pop(0))[0]"/>
                        </raise>
                    </transition>

                    <transition cond="self.expect_response_partial('Read permission denied to: ', pop=False)" target="../wait_for_action/history">
                        <raise event="exception">
                            <parameter expr="'ReadPermissionDenied'"/>
                            <parameter expr="self.split_response(self.responses.pop(0))[0]"/>
                        </raise>
                    </transition>

                    <transition cond="self.expect_response_partial('Write permission denied to: ', pop=False)" target="../wait_for_action/history">
                        <raise event="exception">
                            <parameter expr="'WritePermissionDenied'"/>
                            <parameter expr="self.split_response(self.responses.pop(0))[0]"/>
                        </raise>
                    </transition>

                    <transition cond="self.expect_response('Write permission denied', pop=False)" target="../wait_for_action/history">
                        <raise event="exception">
                            <parameter expr="'WritePermissionDenied'"/>
                            <parameter expr="self.current_model"/>
                        </raise>
                    </transition>

                    <transition cond="self.expect_response_partial('Execute permission denied to: ', pop=False)" target="../wait_for_action/history">
                        <raise event="exception">
                            <parameter expr="'ExecutePermissionDenied'"/>
                            <parameter expr="self.split_response(self.responses.pop(0))[0]"/>
                        </raise>
                    </transition>

                    <transition cond="self.expect_response_partial('Group permission denied to: ', pop=False)" target="../wait_for_action/history">
                        <raise event="exception">
                            <parameter expr="'GroupPermissionDenied'"/>
                            <parameter expr="self.split_response(self.responses.pop(0))[0]"/>
                        </raise>
                    </transition>

                    <transition cond="self.expect_response_partial('User permission denied to: ', pop=False)" target="../wait_for_action/history">
                        <raise event="exception">
                            <parameter expr="'UserPermissionDenied'"/>
                            <parameter expr="self.split_response(self.responses.pop(0))[0]"/>
                        </raise>
                    </transition>

                    <transition cond="self.expect_response_partial('Admin permission denied', pop=False)" target="../wait_for_action/history">
                        <raise event="exception">
                            <parameter expr="'AdminPermissionDenied'"/>
                        </raise>
                    </transition>

                    <transition cond="self.expect_response_partial('Incorrect format: ', pop=False)" target="../wait_for_action/history">
                        <raise event="exception">
                            <parameter expr="'InterfaceMismatch'"/>
                            <parameter expr="self.split_response(self.responses.pop(0))[0]"/>
                        </raise>
                    </transition>

                    <transition cond="self.expect_response_partial('Element not found: ', pop=False)" target="../wait_for_action/history">
                        <raise event="exception">
                            <parameter expr="'UnknownElement'"/>
                            <parameter expr="self.split_response(self.responses.pop(0))[0]"/>
                        </raise>
                    </transition>

                    <transition cond="self.expect_response_partial('Location not found: ', pop=False)" target="../wait_for_action/history">
                        <raise event="exception">
                            <parameter expr="'UnknownLocation'"/>
                            <parameter expr="self.split_response(self.responses.pop(0))[0]"/>
                        </raise>
                    </transition>

                    <transition cond="self.expect_response_partial('No conformance relation can be found ', pop=False)" target="../wait_for_action/history">
                        <raise event="exception">
                            <parameter expr="'UnknownMetamodellingHierarchy'"/>
                        </raise>

                        <script>
                            self.responses.pop(0)
                        </script>
                    </transition>

                    <transition cond="self.expect_response_partial('', pop=False)" target="../wait_for_action/history">
                        <script>
                            #print("Unknown Error: " + self.responses[0])
                            pass
                        </script>
                        <raise event="exception">
                            <parameter expr="'UnknownError'"/>
                            <parameter expr="'Error: %s' % self.responses.pop(0)"/>
                        </raise>
                    </transition>
                </state>

                <state id="wait_for_action" initial="disconnected">
                    <state id="disconnected">
                        <transition cond="self.expect_action(None, 'init')" target="../../init">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>
                    </state>

                    <state id="connected">
                        <transition cond="self.expect_action(None, 'login')" target="../../operations/login">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_response('Success: ready for AL execution')" target="../activity/SC"/>
                        <transition cond="self.expect_response('Success: ready for MT execution')" target="../activity/SC"/>
                        <transition cond="self.expect_response('Success: ready for MANUAL execution')" target="../activity/OP"/>
                    </state>

                    <state id="activity" initial="SC">
                        <state id="SC">
                            <onentry>
                                <raise event="result">
                                    <parameter expr="'SC'"/>
                                </raise>
                            </onentry>

                            <state id="forwarding">
                                <transition cond="self.expect_response('Success', pop=False) or self.expect_response('Failure', pop=False)" target="../../../history">
                                    <raise event="result">
                                        <parameter expr="True if self.responses.pop(0) == 'Success' else False"/>
                                    </raise>
                                </transition>

                                <transition cond="not (self.expect_response('Success', pop=False) or self.expect_response('Failure', pop=False)) and self.expect_response_partial('', pop=False)" target=".">
                                    <raise event="data_output">
                                        <parameter expr="self.responses.pop(0)"/>
                                    </raise>
                                </transition>

                                <transition cond="self.expect_input(None, 'data_input')" target=".">
                                    <raise event="request">
                                        <parameter expr="self.inputs[None].pop(0)['parameters']"/>
                                    </raise>
                                </transition>
                            </state>
                        </state>

                        <state id="OP">
                            <onentry>
                                <raise event="result">
                                    <parameter expr="'OP'"/>
                                </raise>
                            </onentry>

                            <state id="forwarding">
                                <transition cond="self.expect_response_partial('Please perform manual operation ', pop=True)" target="."/>

                                <transition cond="self.expect_response_partial('Please edit this model before sending next input: ', pop=False)" target=".">
                                    <raise event="result">
                                        <parameter expr="self.responses.pop(0).split(': ')[1]"/>
                                    </raise>
                                </transition>

                                <transition cond="None in self.inputs and self.inputs[None]" target="../../../history">
                                    <script>
                                        self.inputs[None].pop(0)
                                    </script>
                                    <raise event="request">
                                        <parameter expr="0"/>
                                    </raise>
                                </transition>
                            </state>
                        </state>
                    </state>

                    <state id="megamodelling">
                        <transition cond="self.expect_action(None, 'model_list')" target="../../operations/model_list">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'model_add')" target="../../operations/model_add">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'model_delete')" target="../../operations/model_delete">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'model_list_full')" target="../../operations/model_list_full">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'verify')" target="../../operations/verify">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'alter_context')" target="../../operations/alter_context">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'model_overwrite')" target="../../operations/model_overwrite">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'disconnect')" target="../../operations/disconnect">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'user_logout')" target="../../operations/user_logout">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'user_delete')" target="../../operations/user_delete">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'model_render')" target="../../operations/model_render">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'model_rendered')" target="../../operations/model_rendered">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'transformation_between')" target="../../operations/transformation_between">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'transformation_signature')" target="../../operations/transformation_signature">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'process_signature')" target="../../operations/process_signature">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'transformation_add_MT')" target="../../operations/store_on_scripted/transformation_add">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'transformation_add_AL')" target="../../operations/store_on_scripted/transformation_add">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'transformation_add_MANUAL')" target="../../operations/store_on_scripted/transformation_add">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'transformation_execute')" target="../../operations/store_on_scripted/transformation_execute">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'process_execute')" target="../../operations/store_on_scripted/process_execute">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'permission_modify')" target="../../operations/permission_modify">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'permission_owner')" target="../../operations/permission_owner">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'permission_group')" target="../../operations/permission_group">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'group_create')" target="../../operations/group_create">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'group_delete')" target="../../operations/group_delete">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'group_owner_add')" target="../../operations/group_owner_add">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'group_owner_delete')" target="../../operations/group_owner_delete">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'group_join')" target="../../operations/group_join">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'group_kick')" target="../../operations/group_kick">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'group_list')" target="../../operations/group_list">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'admin_promote')" target="../../operations/admin_promote">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'admin_demote')" target="../../operations/admin_demote">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'conformance_delete')" target="../../operations/conformance_delete">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'conformance_add')" target="../../operations/conformance_add">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'folder_create')" target="../../operations/folder_create">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'model_types')" target="../../operations/model_types">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'service_register')" target="../../operations/service_register">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'exit_save')" target=".">
                            <raise event="result">
                                <parameter expr="None"/>
                            </raise>
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, ['element_list', 'element_list_nice', 'types', 'types_full', 'read_info', 'read_attrs', 'read_defined_attrs', 'instantiate', 'delete_element', 'attr_assign', 'attr_assign_code', 'attr_delete', 'read_outgoing', 'read_incoming', 'read_association_source', 'read_association_destination', 'connections_between', 'define_attribute', 'undefine_attribute', 'all_instances', 'attr_optional', 'attr_type', 'attr_name'])" target="../../going_manual">
                        </transition>
                    </state>

                    <state id="service">
                        <transition cond="self.expect_response_partial('', pop=False)" target=".">
                            <script>
                                import modelverse
                                thrd = threading.Thread(target=self.service_function, args=[self.responses.pop(0)])
                                thrd.daemon = True
                                thrd.start()
                            </script>
                        </transition>

                        <transition cond="self.expect_action(None, 'service_stop')" target="../../operations/service_stop">
                            <script>
                                self.load_action(None)
                            </script>
                        </transition>
                    </state>

                    <state id="modelling">
                        <state id="recognized" initial="manual">
                            <state id="manual">
                                <transition cond="self.expect_action(self.context, 'exit')" target="../../../../leaving_manual"/>
                                <transition cond="self.expect_action(self.context, 'drop')" target="../../../../leaving_manual_drop"/>

                                <transition cond="self.actions[self.context] and self.actions[self.context][0]['parameters'][0] != self.current_model" target="../../../../leaving_manual"/>
                            </state>

                            <state id="scripted">
                                <transition cond="self.expect_action(self.context, 'exit')" target="../../../../operations/store_on_scripted/history">
                                    <raise event="request">
                                        <parameter expr="'exit'"/>
                                    </raise>
                                </transition>

                                <transition cond="self.expect_action(self.context, 'drop')" target="../../../../operations/store_on_scripted/history">
                                    <raise event="request">
                                        <parameter expr="'drop'"/>
                                    </raise>
                                </transition>
                            </state>

                            <transition cond="self.expect_action(self.context, 'element_list')" target="../../../operations/element_list">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'element_list_nice')" target="../../../operations/element_list_nice">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'types')" target="../../../operations/types">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'types_full')" target="../../../operations/types_full">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'read_info')" target="../../../operations/read_info">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'read_attrs')" target="../../../operations/read_attrs">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'read_defined_attrs')" target="../../../operations/read_defined_attrs">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'instantiate')" target="../../../operations/instantiate">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'delete_element')" target="../../../operations/delete_element">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'attr_assign')" target="../../../operations/attr_assign">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'attr_assign_code')" target="../../../operations/attr_assign_code">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'attr_delete')" target="../../../operations/attr_delete">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'attr_name')" target="../../../operations/attr_name">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'attr_type')" target="../../../operations/attr_type">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'attr_optional')" target="../../../operations/attr_optional">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'read_outgoing')" target="../../../operations/read_outgoing">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'read_incoming')" target="../../../operations/read_incoming">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'read_association_source')" target="../../../operations/read_association_source">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'read_association_destination')" target="../../../operations/read_association_destination">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'connections_between')" target="../../../operations/connections_between">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'define_attribute')" target="../../../operations/define_attribute">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'undefine_attribute')" target="../../../operations/undefine_attribute">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'all_instances')" target="../../../operations/all_instances">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>

                            <transition cond="self.expect_action(self.context, 'exit_save')" target="../../../operations/exit_save">
                                <script>
                                    self.load_action(self.context)
                                </script>
                            </transition>
                        </state>

                        <transition cond="self.context is not None and self.actions[self.context]" target="../history">
                            <script>
                                print("Got unexpected operation: " + str(self.actions[self.context].pop(0)))
                            </script>

                            <raise event="exception">
                                <parameter expr="'IllegalContextOperation'"/>
                                <parameter expr="'Operation not allowed in a context!'"/>
                            </raise>
                        </transition>

                        <transition cond="self.context is None and self.actions[None]" target="../../leaving_manual"/>
                    </state>

                    <state id="finished"/>

                    <history id="history" type="deep"/>

                    <transition cond="self.context is None and self.actions[None]" target="history">
                        <script>
                            print("Got unknown operation: " + str(self.actions[None].pop(0)))
                        </script>

                        <raise event="exception">
                            <parameter expr="'UnknownOperation'"/>
                            <parameter expr="'Operation is unknown'"/>
                        </raise>
                    </transition>
                </state>

                <state id="going_manual" initial="init">
                    <state id="init">
                        <onentry>
                            <script>
                                self.context = None
                                self.current_model = self.actions[None][0]["parameters"][0]
                            </script>
                        </onentry>

                        <transition cond="self.current_model in self.registered_metamodel" target="../OK"/>
                        <transition cond="self.current_model not in self.registered_metamodel" target="../search"/>
                    </state>

                    <state id="search">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['model_types', self.current_model]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response_partial('Success: ')" target="../OK">
                            <script>
                                self.registered_metamodel[self.current_model] = set(self.split_response(self.responses.pop(0))).pop()
                            </script>
                        </transition>
                    </state>

                    <state id="OK">
                        <onentry>
                            <raise event="request">
                                <parameter expr="['model_modify', self.current_model, self.registered_metamodel[self.current_model]]"/>
                            </raise>
                        </onentry>

                        <transition cond="self.expect_response('Model loaded, ready for commands!', pop=True)" target="../../wait_for_action/modelling/recognized/manual"/>

                        <transition cond="self.expect_response_partial('No conformance relation can be found ', pop=False)" target="../../wait_for_action/megamodelling">
                            <raise event="exception">
                                <parameter expr="'UnknownMetamodellingHierarchy'"/>
                                <parameter expr="'Conformance relation not found'"/>
                            </raise>

                            <script>
                                self.responses.pop(0)
                                self.actions[None].pop(0)
                            </script>
                        </transition>
                    </state>
                </state>

                <state id="going_scripted">
                    <onentry>
                        <script>
                            self.current_model = None
                        </script>
                    </onentry>

                    <transition target="../wait_for_action/modelling/recognized/scripted"/>
                </state>

                <state id="leaving_manual">
                    <onentry>
                        <raise event="request">
                            <parameter expr="'exit'"/>
                        </raise>
                    </onentry>

                    <transition cond="self.expect_response('Success', pop=True)" target="../wait_for_action/megamodelling"/>
                </state>

                <state id="leaving_manual_drop">
                    <onentry>
                        <raise event="request">
                            <parameter expr="'drop'"/>
                        </raise>
                    </onentry>

                    <transition cond="self.expect_response('Success', pop=True)" target="../wait_for_action/megamodelling"/>
                </state>
            </state>

            <state id="queue">
                <state id="queue">
                    <transition event="action" target=".">
                        <parameter name="action_name"/>
                        <parameter name="ID"/>
                        <parameter name="context_ID"/>
                        <parameter name="parameters"/>
                        <script>
                            self.actions[context_ID].append({"name": action_name, "ID": ID, "parameters": parameters})
                        </script>
                    </transition>

                    <transition event="result" target=".">
                        <parameter name="parameters"/>
                        <raise scope="output" event="result" port="action_out">
                            <parameter expr="self.current_ID"/>
                            <parameter expr="parameters"/>
                        </raise>

                        <raise scope="broad" event="mv_result">
                            <parameter expr="self.current_ID"/>
                            <parameter expr="parameters"/>
                        </raise>
                    </transition>

                    <transition event="data_output" target=".">
                        <parameter name="parameters"/>
                        <raise scope="output" event="data_output" port="action_out">
                            <parameter expr="parameters"/>
                        </raise>

                        <raise scope="broad" event="mv_data_output">
                            <parameter expr="parameters"/>
                        </raise>
                    </transition>

                    <transition event="exception" target=".">
                        <parameter name="ID"/>
                        <parameter name="parameters"/>

                        <raise scope="output" event="exception" port="action_out">
                            <parameter expr="self.current_ID"/>
                            <parameter expr="ID"/>
                            <parameter expr="parameters"/>
                        </raise>

                        <raise scope="broad" event="mv_exception">
                            <parameter expr="self.current_ID"/>
                            <parameter expr="ID"/>
                            <parameter expr="parameters"/>
                        </raise>
                    </transition>

                    <transition event="ready" target=".">
                        <raise scope="output" event="ready" port="ready"/>
                        <raise scope="broad" event="mv_ready"/>
                    </transition>

                    <transition event="data_input" target=".">
                        <parameter name="value"/>
                        <parameter name="context_ID"/>
                        <script>
                            self.inputs.setdefault(context_ID, []).append({"name": "data_input", "parameters": value})
                        </script>
                    </transition>
                </state>
            </state>
        </parallel>
    </scxml>
</class>
