https://github.com/zen747/pyscm
A python state machine framework based on scxml.
https://github.com/zen747/pyscm
david-harel scxml state-machine statechart statecharts
Last synced: 5 months ago
JSON representation
A python state machine framework based on scxml.
- Host: GitHub
- URL: https://github.com/zen747/pyscm
- Owner: zen747
- License: apache-2.0
- Created: 2019-02-24T03:13:50.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-09-13T12:45:04.000Z (almost 5 years ago)
- Last Synced: 2024-08-08T15:03:59.299Z (almost 2 years ago)
- Topics: david-harel, scxml, state-machine, statechart, statecharts
- Language: Python
- Size: 23.4 KB
- Stars: 15
- Watchers: 0
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pyscm
A python state machine framework based on statecharts (scxml).
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.
The code is listed here:
```
from scm import StateMachineManager
client_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
[English] (http://zen747.blogspot.tw/2017/07/a-scm-framework-tutorial-statechart.html)
[Traditional Chinese] (http://zen747.blogspot.tw/2017/07/scm-framework.html)