Skip to main content

Deterministic, Finite State Machine

Contents


Installation

python setyp.py install

StateMachine

Members

blueprint

This is a propery with a getter and setter that defines the state machine. Setting the blueprint will also reset the state machine.

{
    "initialState": state,
    "initialContext": dict(),
    "alphabet": set(),
    "validStates": set(),
    "acceptedStates": set(),
    "finalStates": set(),
    "transition": lambda state, context, event: new_state,
    "lifecycles": dict()
}
Initial Context

An optional dictionary, which can be used to share information between states and updated during state transitions.

Alphabet

A set of events, which are used to drive state transitions.

Initial State

The starting state. Must be a valid state.

Valid States

A set of valid states.

Accepted States

A set of accepted states.

Final States

A set of final states, once reached, new transitions will raise StopMachine.

Transition

A function, which takes state, context and event as parameters and returns the next state. The event must be a member of the alphabet and the new state must be a valid state.

Lifecycles

Lifecycle actions can be ran before or after specific events.

Events

A set of events, which are used to drive lifecycle actions.

Actions

A list of functions, which take state, context and event as parameters.

{
    "lifecycles": {
        "before": [{
            "events": {0, 1, 2, 3},
            "actions": [before_any]
        }],
        "after": [{
            events: {3},
            "actions": [after_three]
        }, {
            "events": {2, 3},
            "actions": [action1, action2]
        }]
    }
}
  • before_any executes before events 0, 1, 2 & 3 transitions are executed
  • after_three executres after event 3 transition is executed
  • action1, then action2 executes after events 2 & 3 transitions are executed

state

The current state of the state machine.

context

The current context of the state machine.

initial

True or False if the current state is the initial state.

accepted

True or False if the current state is an accepted state.

final

True or False if the current state is a final state.

Methods

reset()

Resets the state machine's state and context to their initial values defined in the blueprint. The initial state must be a valid state.

  • ValueError - Invalid initial state

set_state(state, context)

Set the state machine's state and context. The state must be a valid state.

  • ValueError - Invalid state

transition(event)

Transitions the state machine to the next state by executing the transition defined in the blueprint. The event must be a valid member of the alphabet defined in the blueprint. The state must be a valid state.

  • StopMachine - Current state is final
  • ValueError - Invalid event

is_initial(state) -> True | False

is_valid(state) -> True | False

is_accepted(state) -> True | False

is_final(state) -> True | False

is_event(event) -> True | False


Usage

Create a state machine with a blueprint and transition from the initial state 1 to accepted, final state 2.

from dfsmpy import StateMachine

machine = StateMachine({
    "alphabet": {1, 2},
    "initialState": 1,
    "validStates": {1, 2},
    "acceptedStates": {2},
    "finalStates": {2},
    "transition": lambda a, c, e: e
})

machine.transition(2)

Release files for dfsmpy 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dfsmpy 0.0.1
File Size Uploaded
dfsmpy-0.0.1.tar.gz 3.5 kB Details

Release files / dfsmpy-0.0.1.tar.gz

Download URL dfsmpy-0.0.1.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ad64b03a3bc18725362b06d4c2b9e8f07e4a8b466699a6aa5e27b1871be8626a
BLAKE2b-256 checksum
How to use checksums
da2c1ba84020d29be5d7183595d91c0c6f70d0d339e5a62074509db62c791557
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/2.7.16

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page