Ejemplo n.º 1
0
class InvokeSCXML(BaseFetchingInvoke):
    def __init__(self, data):
        BaseFetchingInvoke.__init__(self)
        self.sm = None
        self.parentQueue = None
        self.content = None
        self.initData = data
        self.cancelled = False
        self.default_datamodel = "python"
    
    def start(self, parentId):
        self.parentId = parentId
        if self.src:
            self.getter.get_async(self.src, None)
        else:
            self._start(self.content)
    
    def _start(self, doc):
        if self.cancelled: return
        from scxml.pyscxml import StateMachine
        
        self.sm = StateMachine(doc, 
                               sessionid=self.parentSessionid + "." + self.invokeid, 
                               default_datamodel=self.default_datamodel,
                               log_function=lambda label, val: dispatcher.send(signal="invoke_log", sender=self, label=label, msg=val),
                               setup_session=False)
        self.interpreter = self.sm.interpreter
        self.sm.compiler.initData = self.initData
        self.sm.compiler.parentId = self.parentId
        self.sm.interpreter.parentId = self.parentId
        dispatcher.send("created", sender=self, sm=self.sm)
        self.sm._start_invoke(self.invokeid)
        eventlet.spawn(self.sm.interpreter.mainEventLoop)

    
    def send(self, eventobj):
        if self.sm and not self.sm.isFinished():
            self.sm.interpreter.externalQueue.put(eventobj)
    
    def onHttpResult(self, signal, result, **named):
        self.logger.debug("onHttpResult " + str(named))
        self._start(result)
        
    def cancel(self):
        self.cancelled = True
        if not self.sm: return;
        self.sm.interpreter.cancelled = True
#        self.sm.interpreter.running = False
#        self.sm._send(["cancel", "invoke", self.invokeid], {}, self.invokeid)
        self.sm.interpreter.externalQueue.put(CancelEvent())
Ejemplo n.º 2
0
    def testInterpreter(self):
        os.environ["PYSCXMLPATH"] = "../../unittest_xml:./unittest_xml"
        runToCompletionList = ["colors.xml", "parallel.xml", "issue_164.xml", "twolock_door.xml", 
                               "if_block.xml", "parallel2.xml", "parallel3.xml", "parallel4.xml", 
                               "donedata.xml", "error_management.xml", "invoke.xml", "history.xml", 
                               "internal_transition.xml", "binding.xml", "finalize.xml",
                               "internal_parallel.xml", "xpath_basic.xml"]
#        logging.basicConfig(level=logging.NOTSET)
        for name in runToCompletionList:
            print "Running " + name 
            sm = StateMachine(name)
            sm.start()
            self.assert_(sm.isFinished())
        
        sm = StateMachine("factorial.xml")
        sm.start()
        self.assertEquals(sm.datamodel['fac'], 720)

        with StateMachine("all_configs.xml") as sm: 
            sm.send("a")
            sm.send("b")
            sm.send("c")
            sm.send("d")
            sm.send("e")
            sm.send("f")
            sm.send("g")
            sm.send("h")
            self.assert_(sm.isFinished())
        
        # I think this test is obsolete because of the exit in a parallel block
#        sm = StateMachine(open(xmlDir + "issue_626.xml").read())
#        sm.start()
#        self.assertEquals(sm.datamodel["x"], 584346861767418750)

        
        '''
        sm = StateMachine(open(xmlDir + "xinclude.xml").read())
        sm.start()
        
        self.assert_(sm.isFinished())
        '''
        
        listener = '''
            <scxml>
                <state>
                    <transition event="e1" target="f">
                        <send event="e2" targetexpr="_event.origin"  />
                    </transition>
                </state>
                <final id="f" />
            </scxml>
        '''
        sender = '''
        <scxml>
            <state>
                <onentry>
                    <log label="sending event" />
                    <send event="e1" target="#_scxml_session1"  />
                </onentry>
                <transition event="e2" target="f" />
            </state>
            <final id="f" />
        </scxml>
        '''
        
        ms = MultiSession(init_sessions={"session1" : listener, "session2" : sender})
        ms.start()
        self.assert_(all(map(lambda x: x.isFinished(), ms)))
Ejemplo n.º 3
0
    def testInterpreter(self):
        os.environ["PYSCXMLPATH"] = "../../unittest_xml:./unittest_xml"
        runToCompletionList = [
            "colors.xml", "parallel.xml", "issue_164.xml", "twolock_door.xml",
            "if_block.xml", "parallel2.xml", "parallel3.xml", "parallel4.xml",
            "donedata.xml", "error_management.xml", "invoke.xml",
            "history.xml", "internal_transition.xml", "binding.xml",
            "finalize.xml", "internal_parallel.xml", "xpath_basic.xml"
        ]
        #        logging.basicConfig(level=logging.NOTSET)
        for name in runToCompletionList:
            print "Running " + name
            sm = StateMachine(name)
            sm.start()
            self.assert_(sm.isFinished())

        sm = StateMachine("factorial.xml")
        sm.start()
        self.assertEquals(sm.datamodel['fac'], 720)

        with StateMachine("all_configs.xml") as sm:
            sm.send("a")
            sm.send("b")
            sm.send("c")
            sm.send("d")
            sm.send("e")
            sm.send("f")
            sm.send("g")
            sm.send("h")
            self.assert_(sm.isFinished())

        # I think this test is obsolete because of the exit in a parallel block


#        sm = StateMachine(open(xmlDir + "issue_626.xml").read())
#        sm.start()
#        self.assertEquals(sm.datamodel["x"], 584346861767418750)
        '''
        sm = StateMachine(open(xmlDir + "xinclude.xml").read())
        sm.start()
        
        self.assert_(sm.isFinished())
        '''

        listener = '''
            <scxml>
                <state>
                    <transition event="e1" target="f">
                        <send event="e2" targetexpr="_event.origin"  />
                    </transition>
                </state>
                <final id="f" />
            </scxml>
        '''
        sender = '''
        <scxml>
            <state>
                <onentry>
                    <log label="sending event" />
                    <send event="e1" target="#_scxml_session1"  />
                </onentry>
                <transition event="e2" target="f" />
            </state>
            <final id="f" />
        </scxml>
        '''

        ms = MultiSession(init_sessions={
            "session1": listener,
            "session2": sender
        })
        ms.start()
        self.assert_(all(map(lambda x: x.isFinished(), ms)))