/PyStateMachine

Python State Machine

Primary LanguagePythonGNU General Public License v3.0GPL-3.0

PyStateMachine

python Python State Machine

CodeFactor Codacy Badge

DeepSource DeepSource

Introduction

PyStateMachine is a Framework that support state machines in Python

Requirements

  • Python3

How to Run

How do we import the framework?

  1. Insure that Python3 is installed:

  2. Install the PyStateMachines framework

    • pip3 install PyStateMachines

Example

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

How to contribute GitHub contributors

Read the CONTRIBUTING GUIDE

Hacktoberfest

We are pleased to inform you that this repository is participating in the #Hacktoberfest!

Happy Coding!

Contact

E-Mail : zigrazor@gmail.com

GitHub Profile Profile views

ZigRazor's github stats

Support

To support me just add Star the project GitHub stars or follow me GitHub followers

To get updated watch the project GitHub watchers

Project Info

Readme Card