Skip to main content

A simple package implementing a finite state machine.

Project description

FiKnight

FiKnight is a simple implementation of a Finite State Machine implemented in Python. There are five essential moving parts, each of these is its own object:

  1. Machine
  2. State
  3. Event
  4. Transition Table
  5. Display

The chain of belonging is as follows: Machine > State > Transition Table > Event --> All events are designed to be triggered from the display and also change items on the display.

Example usage:

##Imports
from fiknight.fsm.core import *
from fiknight.fsm.common.events import *

##Instantiate the machine
fsm = Machine(name='Prototype')

##Define the states:
s1 = State(name="Stage_1", machine=fsm)
s2 = State(name="Stage_2", machine=fsm)
s3 = State(name="Stage_3", machine=fsm)

##Define the events that carry the transitions from common events:
s1.addEvent(clickEvent(name="clickEvent", destinations=s2))
s2.addEvent(clickEvent(name="clickEvent", destinations=s3))
s3.addEvent(clickEvent(name="clickEvent", destinations=s1))

##Compile the states into the machine:
fsm.addState(s1) #the first state that you add is the initial state.
fsm.addState(s2)
fsm.addState(s3)

##Execute each event
for i in range(3):
    fsm.current_state.resolveEventTransition("clickEvent")
    print(fsm.current_state.name)
    print(fsm.history)

At any point, you can view the transition tables with some rudimentary print modification:

##Walk through the machine:
for ev in fsm.state_stack:
    ev.transitions.showTable()

Instead of creating a machine piece-by-piece, some boilerplates have been built in:

##Imports
from fiknight.fsm.core import *
from fiknight.fsm.common.events import *
from fiknight.fsm.common.generator import *

##Build a three cycle machine.
fsm = sequentialMachine("Prototype", n_states=3)

##Walk through the machine.
for i in range(3):
    fsm.current_state.resolveEventTransition("baseTransition")
    print(fsm.current_state.name)
    print(fsm.history)

Instead of creating a machine piece-by-piece, some boilerplates have been built in:

##Imports
from fiknight.fsm.core import *
from fiknight.fsm.common.events import *
from fiknight.fsm.common.generator import *

##Build a three cycle machine.
fsm = sequentialMachine("Prototype", n_states=3)

##Walk through the machine.
for i in range(3):
    fsm.current_state.resolveEventTransition("baseTransition")
    print(fsm.current_state.name)
    print(fsm.history)

A state machine can also be connected and translated to NetworkX easily:

##Imports
from fiknight.fsm.core import *
from fiknight.fsm.common.events import *
from fiknight.fsm.common.generator import *
from fiknight.fsm.export.networkx import *

##Build a three cycle machine.
fsm = sequentialMachine("Prototype", n_states=3)

##Walk through the machine.
for i in range(3):
    fsm.current_state.resolveEventTransition("baseTransition")
    print(fsm.current_state.name)
    print(fsm.history)

##Get a nx object for algorithms or inference:
toNetworkx(fsm)
##Just access the nx graphing mechanisms:
getGraph(machine=fsm, output="../test.png")

Support for saving a machine forthcoming

Support for GUI creation of events forthcoming

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

fiknight-0.1.0-py3-none-any.whl (22.7 kB view details)

Uploaded Python 3

File details

Details for the file fiknight-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: fiknight-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 22.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for fiknight-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ef5019c00da1d2f22888ac867e70f4c24c882b650f709b373def01686b90af1d
MD5 f69a7399716295b2f7d4e8b72cdac0a2
BLAKE2b-256 aaff4160b94693f9ebd23f3ecef4e9f27d6d90159b353311cf0fc9f7b1f7b554

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page