Skip to content

Commit 1519e3a

Browse files
authored
First Implementation of StateMachine
( with only state transition over event injection )
1 parent 4fca27c commit 1519e3a

File tree

9 files changed

+171
-17
lines changed

9 files changed

+171
-17
lines changed

CHANGELOG.md

Whitespace-only changes.

setup.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
from __future__ import absolute_import
4+
from __future__ import print_function
5+
6+
import io
7+
import re
8+
from glob import glob
9+
from os.path import basename
10+
from os.path import dirname
11+
from os.path import join
12+
from os.path import splitext
13+
14+
from setuptools import find_packages
15+
from setuptools import setup
16+
17+
18+
def read(*names, **kwargs):
19+
with io.open(
20+
join(dirname(__file__), *names),
21+
encoding=kwargs.get('encoding', 'utf8')
22+
) as fh:
23+
return fh.read()
24+
25+
26+
setup(
27+
name='PyStateMachine',
28+
version='0.0.1',
29+
license='GNU GENERAL PUBLIC LICENSE',
30+
description='PyStateMachine Package',
31+
long_description='%s\n%s' % (
32+
re.compile('^.. start-badges.*^.. end-badges', re.M | re.S).sub('', read('README.md')),
33+
re.sub(':[a-z]+:`~?(.*?)`', r'``\1``', read('CHANGELOG.md'))
34+
),
35+
author='ZigRazor',
36+
author_email='[email protected]',
37+
url='https://github.com/ZigRazor/PyStateMachine',
38+
packages=find_packages('src'),
39+
package_dir={'': 'src'},
40+
py_modules=[splitext(basename(path))[0] for path in glob('src/*.py')],
41+
include_package_data=True,
42+
zip_safe=False,
43+
classifiers=[
44+
# complete classifier list: http://pypi.python.org/pypi?%3Aaction=list_classifiers
45+
'Development Status :: 5 - Production/Stable',
46+
'Intended Audience :: Developers',
47+
'License :: OSI Approved :: GNU GENERAL PUBLIC LICENSE',
48+
'Operating System :: Unix',
49+
'Operating System :: POSIX',
50+
'Operating System :: Microsoft :: Windows',
51+
'Programming Language :: Python',
52+
'Programming Language :: Python :: 3',
53+
'Programming Language :: Python :: 3.5',
54+
'Programming Language :: Python :: 3.6',
55+
'Programming Language :: Python :: 3.7',
56+
'Programming Language :: Python :: 3.8',
57+
'Programming Language :: Python :: 3.9',
58+
# uncomment if you test on these interpreters:
59+
# 'Programming Language :: Python :: Implementation :: IronPython',
60+
# 'Programming Language :: Python :: Implementation :: Jython',
61+
# 'Programming Language :: Python :: Implementation :: Stackless',
62+
'Topic :: Utilities',
63+
],
64+
project_urls={
65+
'Changelog': 'https://github.com/ZigRazor/PyStateMachine/blob/master/CHANGELOG.md',
66+
'Issue Tracker': 'https://github.com/ZigRazor/PyStateMachine/issues',
67+
},
68+
keywords=[
69+
# eg: 'keyword1', 'keyword2', 'keyword3',
70+
],
71+
python_requires='>=3',
72+
install_requires=[
73+
# eg: 'aspectlib==1.1.1', 'six>=1.7',
74+
],
75+
extras_require={
76+
# eg:
77+
# 'rst': ['docutils>=0.11'],
78+
# ':python_version=="2.6"': ['argparse'],
79+
},
80+
setup_requires=[
81+
'pytest-runner',
82+
],
83+
entry_points={
84+
'console_scripts': [
85+
'nameless = nameless.cli:main',
86+
]
87+
},
88+
)

src/Event.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ def __init__(self, name :str, to_state: str, pre_conditions: Conditions, post_co
99
self.post_conditions = post_conditions
1010
self.pre_actions = pre_actions
1111
self.post_actions = post_actions
12+
13+
def get_name(self):
14+
return self.name
15+
16+
def get_to_state(self):
17+
return self.to_state
18+
19+
def get_pre_conditions(self):
20+
return self.pre_conditions
21+
22+
def get_post_conditions(self):
23+
return self.post_conditions
24+
25+
def get_pre_actions(self):
26+
return self.pre_actions
27+
28+
def get_post_actions(self):
29+
return self.post_actions
1230

1331
def to_string(self):
1432
result_s = "Event: \n"

src/Main.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/State.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ class State:
44
def __init__(self, name: str, events = {}):
55
self.name = name
66
self.events = events
7+
8+
def get_name(self):
9+
return self.name
10+
11+
def get_events(self):
12+
return self.events
713

814
def to_string(self):
915
result_s = "State:\n"

src/StateMachine.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from ReadStateMachine import ReadStateMachineFile
2+
from State import State
3+
from Event import Event
4+
5+
class StateMachine:
6+
def __init__(self, xml_file : str):
7+
self.xml_file = xml_file
8+
self.states = None
9+
self.current_state = ""
10+
11+
def get_current_state(self):
12+
return self.current_state
13+
14+
def LoadStateMachine(self):
15+
if (self.states != None):
16+
print("State Machine already loaded")
17+
else:
18+
self.xml_file
19+
self.states , self.current_state = ReadStateMachineFile(self.xml_file)
20+
21+
def InjectEvent(self, event : str):
22+
my_state = self.states[self.current_state]
23+
possible_events = my_state.get_events()
24+
if event in possible_events:
25+
handled_event = possible_events[event]
26+
## Preconditions
27+
## Preactions
28+
print("Transition ", self.current_state, " ------> ", handled_event.get_to_state())
29+
self.current_state = handled_event.get_to_state()
30+
## Postactions
31+
## Postconditions
32+
else:
33+
print("Not a possible event")
34+

src/__init__.py

Whitespace-only changes.

tests/__init__.py

Whitespace-only changes.

tests/test1.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import sys
2+
sys.path.append('../src')
3+
4+
from StateMachine import StateMachine
5+
6+
def main():
7+
8+
sm = StateMachine("../sample/sample1.xml")
9+
10+
sm.LoadStateMachine()
11+
12+
print(sm.get_current_state())
13+
sm.InjectEvent("ToExit")
14+
print(sm.get_current_state())
15+
sm.InjectEvent("ToExit")
16+
print(sm.get_current_state())
17+
sm.InjectEvent("ToNull")
18+
print(sm.get_current_state())
19+
sm.InjectEvent("ToExit")
20+
print(sm.get_current_state())
21+
sm.InjectEvent("ToNull")
22+
23+
24+
if __name__ == "__main__":
25+
main()

0 commit comments

Comments
 (0)