Giter VIP home page Giter VIP logo

py-fsm's Introduction

Python FSM

Python FSM is a finite state machine library for python

class SimpleFSM(pyfsm.FSM):
    """Simple finite state machine."""

    @pyfsm.as_state()
    def initial_state(self):
        yield
        self.set_state(self.first_state())

    @pyfsm.as_state()
    def first_state(self):
        yield
        self.set_state(self.second_state())

    @pyfsm.as_state(forbid_proposal=False)
    def second_state(self):
        proposed_state = yield
        # if proposal state passed set it else finite state
        if proposed_state:
            self.set_state(proposed_state)
        else:
            self.set_state(self.final_state())

    @pyfsm.as_state()
    def final_state(self):
        yield


class LinearFSM(pyfsm.FSM):
    """Linear finite state machine."""

    @pyfsm.as_state()
    def initial_state(self):
        yield
        self.set_state(self.first_state())

    @pyfsm.as_state()
    def first_state(self):
        yield
        self.set_state(self.second_state())

    @pyfsm.as_state()
    def second_state(self):
        yield
        self.set_state(self.final_state())

    @pyfsm.as_state()
    def final_state(self):
        yield


class CycleFSM(pyfsm.FSM):
    """Cycle finite state machine."""

    @pyfsm.as_state()
    def initial_state(self):
        # Entry point
        yield
        self.set_state(self.ping_state())

    @pyfsm.as_state()
    def ping_state(self):
        yield
        self.set_state(self.pong_state())

    @pyfsm.as_state()
    def pong_state(self):
        yield
        self.set_state(self.ping_state())


class ProposalFSM(pyfsm.FSM):
    """Proposal finite state machine."""

    def __init__(self) -> None:
        super().__init__()

        self.current_direction = None

        self._available_states = [
            self.left_direction(),
            self.right_direction(),
            self.forward_direction(),
            self.back_direction(),
            self.stop()
        ]

    def set_proposal_state(self, state):
        if state in self._available_states:
            self.set_state(state)
        else:
            raise pyfsm.UnreleasedTransition()

    @pyfsm.as_state(forbid_proposal=False)
    def initial_state(self):
        proposal_state = yield
        self.set_state(proposal_state)

    @pyfsm.as_state(forbid_proposal=False)
    def left_direction(self):
        self.current_direction = 'left'
        proposal_state = yield
        self.set_proposal_state(proposal_state)

    @pyfsm.as_state(forbid_proposal=False)
    def right_direction(self):
        self.current_direction = 'right'
        proposal_state = yield
        self.set_proposal_state(proposal_state)

    @pyfsm.as_state(forbid_proposal=False)
    def forward_direction(self):
        self.current_direction = 'forward'
        proposal_state = yield
        self.set_proposal_state(proposal_state)

    @pyfsm.as_state(forbid_proposal=False)
    def back_direction(self):
        self.current_direction = 'back'
        proposal_state = yield
        self.set_proposal_state(proposal_state)

    @pyfsm.as_state(forbid_proposal=False)
    def stop(self):
        self.current_direction = 'stop'
        proposal_state = yield
        self.set_proposal_state(proposal_state)

py-fsm's People

Contributors

l4ent avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

originalbroshka

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.