Skip to main content

A python state machine framework based on statecharts (scxml)

Project description

This is a python library supporting statechart (in scxml format) originated by David Harel. Features like hierarchical states, parallel states, and history are ready for you to command. You won’t find another lib as easy and flexible to use as SCM.

Just take a look and run scm_tutorial.py please. The code is listed here:

from scm import StateMachineManager

client_scxml = """
<scxml>
    <state id='appear'>
        <transition event='born' ontransit='say_hello' target='live'/>
    </state>
    <parallel id='live'>
            <transition event='hp_zero' target='dead'/>
            <state id='eat'>
            </state>
            <state id='move'>
            </state>
    </parallel>
    <final id='dead'/>
    </scxml>
"""

class Life:
    def __init__(self):
        self.mach_ = StateMachineManager.instance().getMach('the life')
        self.mach_.set_do_exit_state_on_destroy(True)
        self.mach_.register_state_slot("appear", self.onentry_appear, self.onexit_appear)
        self.mach_.register_state_slot("live", self.onentry_live, self.onexit_live)
        self.mach_.register_state_slot("eat", self.onentry_eat, self.onexit_eat)
        self.mach_.register_state_slot("move", self.onentry_move, self.onexit_move)
        self.mach_.register_state_slot("dead", self.onentry_dead, self.onexit_dead)
        self.mach_.register_action_slot('say_hello', self.say_hello)
        self.mach_.StartEngine()

    def test(self):
        self.mach_.enqueEvent("born")
        #self.mach_.frame_move(0) # state change to 'live'
        StateMachineManager.instance().pumpMachEvents()
        self.mach_.enqueEvent("hp_zero")
        #self.mach_.frame_move(0) # state change to 'dead'
        StateMachineManager.instance().pumpMachEvents()

    def onentry_appear(self):
        print("come to exist")

    def onexit_appear(self):
        print("we are going to...")

    def onentry_live(self):
        print("start living")

    def onexit_live(self):
        print("no longer live")

    def onentry_eat(self):
        print("start eating")

    def onexit_eat(self):
        print("stop eating")

    def onentry_move(self):
        print("start moving")

    def onexit_move(self):
        print("stop moving")

    def onentry_dead(self):
        print("end")

    def onexit_dead(self):
        assert (0 and "should not exit final state");
        print("no, this won't get called.")

    def say_hello(self):
        print("\n*** Hello, World! ***\n")

if __name__ == '__main__':
    StateMachineManager.instance().set_scxml("the life", client_scxml)
    life = Life()
    life.test()
    StateMachineManager.instance().pumpMachEvents()

The output you should see

come to exist
we are going to...

*** Hello, World! ***

start living
start eating
start moving
stop eating
stop moving
no longer live
end

Simply

  1. you load the scxml from external file or from a string defined in your code.

  2. you connect these onentry_ onexit_, etc. slots

  3. you start the engine, call the framemove in main loop.

Done.

It’s that easy!

Read the tutorials at:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_scm-1.0.4.tar.gz (16.4 kB view details)

Uploaded Source

File details

Details for the file py_scm-1.0.4.tar.gz.

File metadata

  • Download URL: py_scm-1.0.4.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for py_scm-1.0.4.tar.gz
Algorithm Hash digest
SHA256 d1ae494a2d88c22d9ccf620047fab3efab6c4ed7fc914054def82fb07eedf109
MD5 72fdc2eecc36d098d8a30013058347f4
BLAKE2b-256 44c67765dee1c599a8bef42db72625b79e528b807a2a420eb92ca8c03e143e50

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page