def create_state_machine(self, path_to_scxml_file): """ TODO :param path_to_scxml_file: """ self._statemachine = StateMachine(path_to_scxml_file, log_function=advanced_pyscxml_logfunction) self.datamodel = self._statemachine.datamodel self.interpreter = self._statemachine.interpreter self.exit_grace = False self.block_diagram = {'opening': 'blockdiag {', 'content': 'initialise_state', 'ending': '}'}
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())
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 on_exit(self, sender, final): self.didPass = final == "pass" StateMachine.on_exit(self, sender, final)
from scxml.pyscxml import StateMachine, default_logfunction from louie import dispatcher import logging import os #os.chdir("assertions_all/failed") #os.chdir("assertions_ecma/failed") #os.chdir("stoplist/failed") # os.chdir("assertions_xpath/failed") os.chdir("ecma_schema/failed") logging.basicConfig(level=logging.NOTSET) nextFile = filter(lambda x: x.endswith("xml"), os.listdir("."))[0] xml = open(nextFile).read() import re #xml = re.sub("datamodel=.python.", 'datamodel="ecmascript"', xml) dispatcher.connect(default_logfunction, "invoke_log") sm = StateMachine(xml) sm.start()
class StateMachineWrapper(object): """ Wrapper class for the PYSCXML state machine. """ _instance = None datamodel = None _statemachine = None def __new__(cls, *args, **kwargs): """ :param args: :param kwargs: :return: """ if not cls._instance: cls._instance = super(StateMachineWrapper, cls).__new__(cls, *args, **kwargs) return cls._instance def create_state_machine(self, path_to_scxml_file): """ TODO :param path_to_scxml_file: """ self._statemachine = StateMachine(path_to_scxml_file, log_function=advanced_pyscxml_logfunction) self.datamodel = self._statemachine.datamodel self.interpreter = self._statemachine.interpreter self.exit_grace = False self.block_diagram = {'opening': 'blockdiag {', 'content': 'initialise_state', 'ending': '}'} def log_setup(self, log_, log_folder_, log_folder_fsm_, log_folder_images_, log_folder_plots_, log_folder_videos_, log_folder_data_, log_folder_logs_, kill_timeout): """ TODO :param log: :param log_folder: :param log_folder_fsm: :param log_folder_images: :param log_folder_plots: :param log_folder_videos: :param log_folder_data: :param log_folder_logs: """ self.log = log_ self.log_folder = log_folder_ self.killtimeout = float(kill_timeout) self.log_folder_fsm = log_folder_fsm_ self.log_folder_data = log_folder_data_ self.log_folder_logs = log_folder_logs_ self.log_folder_plots = log_folder_plots_ self.log_folder_videos = log_folder_videos_ self.log_folder_images = log_folder_images_ def send(self, name, data={}): """ Wraps the send function of the PYSCXML state machine. :param name: :param data: """ self._statemachine.send(name, data=data) def start(self): """ Wraps the start function of the PYSCXML state machine. """ self._statemachine.start()
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)))
import sys from scxml.pyscxml import StateMachine path_to_file = sys.argv[1] list_element = sys.argv[2] file_event = open(list_element, "r") events = file_event.read().split("\n") file_event.close() print("******Starting Simulation******") sm = StateMachine(path_to_file) sm.start_threaded() element = events.pop() while (len(events) != 0): element = events.pop() print("** Injecting event:" + element + " **") sm.send(element) print("******Ending Simulation******")
def cancel(self): StateMachine.cancel(self) self.isCancelled = True
def on_exit(self, sender, final): self.didPass = not self.isCancelled and final == "pass" StateMachine.on_exit(self, sender, final)
def __init__(self, path): self.machine = StateMachine(path) self.data = dict() self.parent = dict()
def __init__(self, xml, log_function=lambda fn, y:None, sessionid=None): self.didPass = False self.isCancelled = False StateMachine.__init__(self, xml, log_function, None)
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)))
def __init__(self, xml, log_function=lambda fn, y: None, sessionid=None): self.didPass = False self.isCancelled = False StateMachine.__init__(self, xml, log_function, None)