PyStateMachine is a Framework that support state machines in Python
How do we import the framework?
Insure that Python3 is installed:
Install the PyStateMachines framework
After framework is installed, import and create a small example. Create xml file from sample with the State machine states.
myStateMachine.xml
<?xml version="1.0"?>
<x:States xmlns:x="pystatemachine:sm">
<State>
<Name>State1</Name>
<Event>
<Name>ToState2</Name>
<ToState>State2</ToState>
</Event>
</State>
<State>
<Name>State2</Name>
<Event>
<Name>ToState3</Name>
<ToState>State3</ToState>
</Event>
</State>
<State>
<Name>State3</Name>
<Event>
<Name>ToState1</Name>
<ToState>State1</ToState>
</Event>
</State>
<Initial_State>State1</Initial_State>
</x:States>
Create Python Script to run state machine pyStateMachine.py
# importing necessary packages
from StateMachine import StateMachine
"""Test StateMachine"""
sm = StateMachine("myStateMachine.xml")
sm.LoadStateMachine()
# print initial state
print(sm.get_current_state()) # current_state == State1
sm.InjectEvent("ToState2")
print(sm.get_current_state()) # current_state == State2
sm.InjectEvent("ToState3")
print(sm.get_current_state()) # current_state == State3
sm.InjectEvent("ToState1")
print(sm.get_current_state()) # current_state == State1
run Python script to execute state machine
python pyStateMachine.py
pyStateMachineUnitTest.py
## Test Suite
# importing necessary packages
from StateMachine import StateMachine
import unittest
class TestBaseStateMachine(unittest.TestCase):
"""Test state machine using various samples"""
# test all valid state transitions
def test1(self):
"""Test Statemachine"""
sm = StateMachine("myStateMachine.xml")
sm.LoadStateMachine()
self.assertEqual(sm.get_current_state(), "State1", "Should be State1")
# OK Event
sm.InjectEvent("ToState2")
self.assertEqual(sm.get_current_state(), "State2", "Should be State2")
# OK Event
sm.InjectEvent("ToState3")
self.assertEqual(sm.get_current_state(), "State3", "Should be State3")
# OK Event
sm.InjectEvent("ToState1")
self.assertEqual(sm.get_current_state(), "State1", "Should be State1")
# test invalid State1 transitions
def test2(self):
"""Test Statemachine"""
sm = StateMachine("myStateMachine.xml")
sm.LoadStateMachine()
self.assertEqual(sm.get_current_state(), "State1", "Should be State1")
# Invalid transition
sm.InjectEvent("ToState3")
self.assertEqual(sm.get_current_state(), "State1", "Should be State1")
# test invalid State2 transitions
def test3(self):
"""Test Statemachine"""
sm = StateMachine("myStateMachine.xml")
sm.LoadStateMachine()
self.assertEqual(sm.get_current_state(), "State1", "Should be State1")
# OK Event
sm.InjectEvent("ToState2")
self.assertEqual(sm.get_current_state(), "State2", "Should be State2")
# Invalid transition
sm.InjectEvent("ToState1")
self.assertEqual(sm.get_current_state(), "State2", "Should be State2")
# test invalid State3 transitions
def test4(self):
"""Test Statemachine"""
sm = StateMachine("myStateMachine.xml")
sm.LoadStateMachine()
self.assertEqual(sm.get_current_state(), "State1", "Should be State1")
# OK Event
sm.InjectEvent("ToState2")
self.assertEqual(sm.get_current_state(), "State2", "Should be State2")
# OK Event
sm.InjectEvent("ToState3")
self.assertEqual(sm.get_current_state(), "State3", "Should be State3")
# Invalid transition
sm.InjectEvent("ToState2")
self.assertEqual(sm.get_current_state(), "State3", "Should be State3")
if __name__ == '__main__':
unittest.main()
run unit tests
python pyStateMachineUnitTest.py
Read the CONTRIBUTING GUIDE
We are pleased to inform you that this repository is participating in the #Hacktoberfest!
Happy Coding!
E-Mail : zigrazor@gmail.com
To support me just add Star the project or follow me
To get updated watch the project