# pyfsm-tool
pyfsm-tool is a module to create and manipulate finite states machines (fsm).
## Getting started
### Import modules
FiniteStateMachine module:
```python
from pyfsm_tool import FiniteStateMachine, StateBehaviour
```
Exceptions module:
```python
from pyfsm_tool import FSMException
```
### Create new FSM
The new fsm is empty (No state and no transition).
```python
fsm: FiniteStateMachine = FiniteStateMachine()
```
### Initialize the fsm data store
Initialize fsm data store wit 'message' and 'count' available for all states.
```python
fsm.fsm_data_store = {"message": "", "count": 0}
```
### Create new behaviours
Create new behaviours for fsm states.
- First behaviour: add 'Hello' in fsm data store (key = 'message') and next transition is 't1'
```python
class Behaviour1(StateBehaviour):
def action(self) -> None:
self.state_data_store["message"] += "Hello"
def next_transition_id(self) -> str:
return "t1"
```
- Second behaviour: add ' ' in fsm data store (key = 'message') and next transition is 't2'
```python
class Behaviour2(StateBehaviour):
def action(self) -> None:
self.state_data_store["message"] += " "
def next_transition_id(self) -> str:
return "t2"
```
- Third behaviour: add 'world' in fsm data store (key = 'message') and next transition is 't3'
```python
class Behaviour3(StateBehaviour):
def action(self) -> None:
self.state_data_store["message"] += "world"
def next_transition_id(self) -> str:
return "t3"
```
- Fourth behaviour: add '!' in fsm data store (key = 'message') and next transition is 't4' (itself) if the key 'count' in fsm data store is less than 2 (there is incrementation of fsm data store for key 'count'), else next transition is 't5'
```python
class Behaviour4(StateBehaviour):
def action(self) -> None:
self.state_data_store["message"] += "!"
def next_transition_id(self) -> str:
if self.state_data_store.get("count") < 2:
self.state_data_store["count"] += 1
return "t4"
return "t5"
```
- Fifth behaviour: display fsm data store (key = 'message') and finish fsm.
```python
class Behaviour5(StateBehaviour):
def action(self) -> None:
print(self.state_data_store.get("message"))
```
### Register all states
- Register first state with first behaviour
```python
fsm.register_first_state(Behaviour1(), "b1")
```
- Register next states with next behaviours
```python
fsm.register_state(Behaviour2(), "b2")
fsm.register_state(Behaviour3(), "b3")
fsm.register_state(Behaviour4(), "b4")
```
- Register last state with last behaviour
```python
fsm.register_last_state(Behaviour5(), "b5")
```
If state behaviour not inherits `StateBehaviour` class or if state already
exists(same id) or if an argument is `None`, then `FSMException` is raise.
```python
try:
fsm.register_state(Behaviour2(), "b2")
except FSMException as error:
pass # or do something...
```
### Register all transitions
```python
fsm.register_transition("b1", "b2", "t1")
fsm.register_transition("b2", "b3", "t2")
fsm.register_transition("b3", "b4", "t3")
fsm.register_transition("b4", "b4", "t4")
fsm.register_transition("b4", "b5", "t5")
```
If transition already exists(same id) or if an argument is `None`, then `FSMException` is raise.
```python
try:
fsm.register_transition("b1", "b2", "t1")
except FSMException as error:
pass # or do something...
```
### Run FSM
```python
fsm.run()
```
## Author
If you have any questions or suggestions, please don't hesitate to contact me : <belaich.david@outlook.fr>.
Raw data
{
"_id": null,
"home_page": "https://github.com/DevAI-64/pyfsm-tool",
"name": "pyfsm-tool",
"maintainer": "BEL AICH David",
"docs_url": null,
"requires_python": "<4.0.0,>=3.8.1",
"maintainer_email": "belaich.david@outlook.fr",
"keywords": "fsm, pyfsm, pyfsm-tool, state, states, transition, transitions, fintite state machine, fintite states machine, finite states machines, graph, pygraph, pygraph-tool",
"author": "BEL AICH David",
"author_email": "belaich.david@outlook.fr",
"download_url": "https://files.pythonhosted.org/packages/ba/f6/c9f0e7a7c9587cf99e285300474f79798026248af0c7ec565b49326114c8/pyfsm_tool-0.9.3.tar.gz",
"platform": null,
"description": "# pyfsm-tool\n\npyfsm-tool is a module to create and manipulate finite states machines (fsm).\n\n## Getting started\n\n### Import modules\nFiniteStateMachine module:\n```python\nfrom pyfsm_tool import FiniteStateMachine, StateBehaviour\n```\nExceptions module:\n```python\nfrom pyfsm_tool import FSMException\n```\n\n### Create new FSM\nThe new fsm is empty (No state and no transition).\n```python\nfsm: FiniteStateMachine = FiniteStateMachine()\n```\n\n### Initialize the fsm data store\nInitialize fsm data store wit 'message' and 'count' available for all states.\n```python\nfsm.fsm_data_store = {\"message\": \"\", \"count\": 0}\n```\n\n### Create new behaviours\nCreate new behaviours for fsm states.\n- First behaviour: add 'Hello' in fsm data store (key = 'message') and next transition is 't1'\n```python\nclass Behaviour1(StateBehaviour):\n def action(self) -> None:\n self.state_data_store[\"message\"] += \"Hello\"\n\n def next_transition_id(self) -> str:\n return \"t1\"\n```\n- Second behaviour: add ' ' in fsm data store (key = 'message') and next transition is 't2'\n```python\nclass Behaviour2(StateBehaviour):\n def action(self) -> None:\n self.state_data_store[\"message\"] += \" \"\n\n def next_transition_id(self) -> str:\n return \"t2\"\n```\n- Third behaviour: add 'world' in fsm data store (key = 'message') and next transition is 't3'\n```python\nclass Behaviour3(StateBehaviour):\n def action(self) -> None:\n self.state_data_store[\"message\"] += \"world\"\n\n def next_transition_id(self) -> str:\n return \"t3\"\n```\n- Fourth behaviour: add '!' in fsm data store (key = 'message') and next transition is 't4' (itself) if the key 'count' in fsm data store is less than 2 (there is incrementation of fsm data store for key 'count'), else next transition is 't5' \n```python\nclass Behaviour4(StateBehaviour):\n def action(self) -> None:\n self.state_data_store[\"message\"] += \"!\"\n\n def next_transition_id(self) -> str:\n if self.state_data_store.get(\"count\") < 2:\n self.state_data_store[\"count\"] += 1\n return \"t4\"\n return \"t5\"\n```\n- Fifth behaviour: display fsm data store (key = 'message') and finish fsm. \n```python\nclass Behaviour5(StateBehaviour):\n def action(self) -> None:\n print(self.state_data_store.get(\"message\"))\n```\n\n### Register all states\n- Register first state with first behaviour\n```python\nfsm.register_first_state(Behaviour1(), \"b1\")\n```\n- Register next states with next behaviours\n```python\nfsm.register_state(Behaviour2(), \"b2\")\nfsm.register_state(Behaviour3(), \"b3\")\nfsm.register_state(Behaviour4(), \"b4\")\n```\n- Register last state with last behaviour\n```python\nfsm.register_last_state(Behaviour5(), \"b5\")\n```\nIf state behaviour not inherits `StateBehaviour` class or if state already \nexists(same id) or if an argument is `None`, then `FSMException` is raise.\n```python\ntry:\n fsm.register_state(Behaviour2(), \"b2\")\nexcept FSMException as error:\n pass # or do something...\n```\n\n### Register all transitions\n```python\nfsm.register_transition(\"b1\", \"b2\", \"t1\")\nfsm.register_transition(\"b2\", \"b3\", \"t2\")\nfsm.register_transition(\"b3\", \"b4\", \"t3\")\nfsm.register_transition(\"b4\", \"b4\", \"t4\")\nfsm.register_transition(\"b4\", \"b5\", \"t5\")\n```\nIf transition already exists(same id) or if an argument is `None`, then `FSMException` is raise.\n```python\ntry:\n fsm.register_transition(\"b1\", \"b2\", \"t1\")\nexcept FSMException as error:\n pass # or do something...\n```\n\n### Run FSM\n```python\nfsm.run()\n```\n\n## Author\nIf you have any questions or suggestions, please don't hesitate to contact me : <belaich.david@outlook.fr>.\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "pyfsm-tool is a module to create and manipulate finite states machines.",
"version": "0.9.3",
"project_urls": {
"Homepage": "https://github.com/DevAI-64/pyfsm-tool",
"Repository": "https://github.com/DevAI-64/pyfsm-tool"
},
"split_keywords": [
"fsm",
" pyfsm",
" pyfsm-tool",
" state",
" states",
" transition",
" transitions",
" fintite state machine",
" fintite states machine",
" finite states machines",
" graph",
" pygraph",
" pygraph-tool"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "de0484c119b9dc512498ca345740df11e4c97c785183c892d9edf3e014158f23",
"md5": "a767c8d7c8bbf7a62734ef3e56f8b54c",
"sha256": "3977db87a477d217833d6810dbfb3c8f75be309de8c4fdd298a56a8f5c04f44d"
},
"downloads": -1,
"filename": "pyfsm_tool-0.9.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "a767c8d7c8bbf7a62734ef3e56f8b54c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0.0,>=3.8.1",
"size": 8079,
"upload_time": "2024-06-05T21:34:20",
"upload_time_iso_8601": "2024-06-05T21:34:20.089184Z",
"url": "https://files.pythonhosted.org/packages/de/04/84c119b9dc512498ca345740df11e4c97c785183c892d9edf3e014158f23/pyfsm_tool-0.9.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "baf6c9f0e7a7c9587cf99e285300474f79798026248af0c7ec565b49326114c8",
"md5": "05b6c36c23d62ec0424ae674b9ddd735",
"sha256": "17b654fd9cf4300280fda808e13554e944f57038cd9a432203597b806a696d77"
},
"downloads": -1,
"filename": "pyfsm_tool-0.9.3.tar.gz",
"has_sig": false,
"md5_digest": "05b6c36c23d62ec0424ae674b9ddd735",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0.0,>=3.8.1",
"size": 5169,
"upload_time": "2024-06-05T21:34:21",
"upload_time_iso_8601": "2024-06-05T21:34:21.795209Z",
"url": "https://files.pythonhosted.org/packages/ba/f6/c9f0e7a7c9587cf99e285300474f79798026248af0c7ec565b49326114c8/pyfsm_tool-0.9.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-06-05 21:34:21",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "DevAI-64",
"github_project": "pyfsm-tool",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "pyfsm-tool"
}