<class name="Executor">
    <relationships>
        <association name="parent" class="MvKController" min="1" max="1"/>
    </relationships>
    <constructor>
        <parameter name="mvk"/>
        <body>
            <![CDATA[
            self.request_queue = [("", "", "load_primitives", [], None)]
            self.mvk = mvk
            self.first = True
            self.mvs_response = None
            ]]>
        </body>
    </constructor>
    <scxml initial="init">
        <parallel id="init">
            <state id="queue">
                <state id="queue">
                    <transition event="execute" target=".">
                        <parameter name="returnpath"/>
                        <parameter name="username"/>
                        <parameter name="operation"/>
                        <parameter name="params"/>
                        <parameter name="request_id"/>
                        <script>
                            self.request_queue.append(("/" + returnpath, username, operation, params, request_id))
                        </script>
                    </transition>
                </state>
            </state>

            <state id="execute" initial="idle">
                <state id="idle">
                    <transition cond="self.request_queue" target="../execution"/>
                </state>

                <state id="execution">
                    <onentry>
                        <script>
                            try:
                                _, username, operation, params, _ = self.request_queue[0]
                                self.mvs_commands = self.mvk.execute_yields(username, operation, params, self.mvs_response)
                            except:
                                import traceback
                                traceback.print_exc()
                                print("Exec error")
                                #TODO for debugging
                                sys.exit(0)

                                self.mvs_commands = None
                        </script>
                    </onentry>

                    <transition cond="self.mvs_commands is None" target="../idle">
                        <script>
                            returnpath, _, _, _, request_id = self.request_queue.pop(0)
                            self.mvs_response = None
                        </script>
                        <raise event="executed" scope="narrow" target="'parent' + returnpath">
                            <parameter expr="self.mvk.returnvalue"/>
                            <parameter expr="self.mvk.success"/>
                            <parameter expr="request_id"/>
                        </raise>
                        <script>
                            self.mvk.returnvalue = None
                            self.mvk.success = True
                            self.first = True
                        </script>
                    </transition>

                    <transition cond="len(self.mvs_commands) > 0" target="../waiting_for_response">
                        <raise event="HTTP_input" scope="narrow" target="'parent/to_mvs'">
                            <parameter expr="'requests=%s' % json.dumps(self.mvs_commands)"/>
                            <parameter expr="'parent/executor'"/>
                        </raise>
                    </transition>

                    <transition cond="len(self.mvs_commands) == 0" target=".">
                    </transition>
                </state>

                <state id="waiting_for_response">
                    <transition event="HTTP_output" target="../execution">
                        <parameter name="data"/>
                        <script>
                            self.mvs_response = [v[0] for v in json.loads(data["data"])]
                            if len(self.mvs_response) == 1:
                                self.mvs_response = self.mvs_response[0]
                        </script>
                    </transition>
                </state>
            </state>
        </parallel>
    </scxml>
</class>
