Skip to main content

A Finite Transactional State Machine.

Project description

Finite Transactional State Machine

Finite Transactional State Machine is a Transaction driven finite state machine. Transaction can be any Python callable object that is reverted when exceptions occur.

Installation

pip3 install ftsm

How does it work ?

  1. Create states and list of possible transitions the state is allowed to transition to.
    UNLOCKED = State('UNLOCKED', initial=True, allowed_transitions=['LOCKED'])
    LOCKED = State('LOCKED', initial=False, allowed_transitions=['UNLOCKED'])
    
  2. Initialize the transitional state machine.
    tsm = TransactionalFiniteStateMachine(name='Lock')
    
  3. Add defined states to a state machine.
    tsm.add(LOCKED)
    tsm.add(UNLOCKED)
    
  4. Create transaction and define rollback transactions with or without conditions.
    t1 = Transaction(
    target=func,
    args=('name',),
    rb_transactions=[t2],
    rb_conditions=[ExceptionCondition(KeyError)])
    
  5. Transition to a new state with transactions.
    with tsm.managed_transition(
            state=LOCKED,
            pre_transactions=[t1, t3],
            on_error_transactions=[t4],
            post_transactions=[t5]):
        func()
    

Example

from ftsm import State, Transaction, TransactionalFiniteStateMachine

class LightController:
    def turn_off_light(self, room):
        print('turning the {} room light off.'.format(room))

    def turn_on_light(self, room):
        print('turning the {} room light on.'.format(room))

light_controller = LightController()

def turn_off_water():
    print('turning off the water.')

def turn_on_water():
    print('turning on the water.')

def water_plants():
    print('watering the plants.')

def lock_the_door():
    print('locking the door.')

def unlock_the_door():
    print('unlocking the door.')

UNLOCKED = State('UNLOCKED', initial=True, allowed_transitions=['LOCKED'])
LOCKED = State('LOCKED', initial=False, allowed_transitions=['UNLOCKED'])

tsm = TransactionalFiniteStateMachine(name='Lock')
tsm.add(LOCKED)
tsm.add(UNLOCKED)

light_transaction = Transaction(
    target=light_controller.turn_off_light,
    args=('Living',),
    rb_transactions=[
        Transaction(target=light_controller.turn_on_light,
                    args=('Living',))
    ])

water_transaction = Transaction(
    target=turn_off_water,
    rb_transactions=[
        Transaction(target=turn_on_water)
    ]
)

with tsm.managed_transition(
        state=LOCKED,
        pre_transactions=[light_transaction, water_transaction],
        on_error_transactions=[Transaction(unlock_the_door)],
        post_transactions=[Transaction(water_plants)]):
    lock_the_door()

print(tsm.current_state)

Above sample code would result in following output.

turning the Living room light off.
turning off the water.
locking the door.
watering the plants.
<State name=LOCKED initial=False>

If errors occur while performing the transactions, revert transactions are performed in the reverse order and state transition does not happens.

Rollback transaction can also be made conditional using the ExceptionCondition class provided.

light_transaction = Transaction(
    target=light_controller.turn_off_light,
    args=('Living',),
    rb_transactions=[
        Transaction(target=light_controller.turn_on_light,
                    args=('Living',))
    ],
    rb_conditions=[ExceptionCondition(KeyError)])

Above transaction now only be reverted if KeyError is encountered during the transaction execution.

User can extend the abstract Condition class to defined new Condition.

Project details


Download files

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

Source Distribution

ftsm-0.1.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ftsm-0.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file ftsm-0.1.0.tar.gz.

File metadata

  • Download URL: ftsm-0.1.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using 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/3.7.4

File hashes

Hashes for ftsm-0.1.0.tar.gz
Algorithm Hash digest
SHA256 84303d9a975d54473116df46605f35a514d6eabcfde346590d6a4a64c5ae2153
MD5 cdf0529c8bddf9ad34bf2d6c72ff7513
BLAKE2b-256 481792669b96219ab2ec6e400a0c42743acd6cae2cb3ef83436ad61cf0af3c62

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ftsm-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using 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/3.7.4

File hashes

Hashes for ftsm-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 64f7dc795c4b7cf01bedd9fdfd91f0dc1d89497b23d246a9bd9932e494a49a38
MD5 8e41bcf2e5882c3d023ccd68fa63e042
BLAKE2b-256 07072f9f780cd379ef8ae093a1256d1107ba37f0d2657dd6127d31ed7c6b6f72

See more details on using hashes here.

Supported by

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