AUTHOR: "Brig Young, https://github.com/Sonophoto/" PURPOSE: "Implements a dead simple 3 state FSM" COPYRIGHT: "Copyright 2016-2020 Brig Young, Sonophotostudios.com" LICENSE: " BSD 2-Clause, (Citation Required) See LICENSE file" ************************************************************************* """ import pyDGW # Setup your state variables counter_state = pyDGW.DGW_data() counter_state.count = 0 counter_state.limit = 10 # Instatiate a Directed Graph Walker object DGW_Counter = pyDGW.DGWalker() # Set to True to get debugging output from the graph walker DGW_Counter.DEBUG = True # Define your operators def OP_start(counter_state): """Our start state initializes counter_state.count = 0""" counter_state.count = 0 operator = "counter" return (operator, counter_state) def OP_counter(counter_state): """Our counter increments while .count < .limit"""
# Setup our data object CoinOp_state = pyDGW.DGW_data() CoinOp_state.tender_total = 0 CoinOp_state.soda_sales_total = 0 CoinOp_state.rootbeer_sales_total = 0 CoinOp_state.grape_sales_total = 0 CoinOp_state.orange_sales_total = 0 CoinOp_state.cola_sales_total = 0 CoinOp_state.rootbeer_inventory = 1 CoinOp_state.grape_inventory = 1 CoinOp_state.orange_inventory = 1 CoinOp_state.soda_price = 65 CoinOp_state.change_due = 0 # Setup our DGWalker object CoinOp = pyDGW.DGWalker() CoinOp.DEBUG = False # User debug watches for user code CoinOp.KDEBUG = False # Kernel debug watches from pyDGW CoinOp.SDEBUG = False # Verbose operating messages from pyDGW # Next we define each of the states in our machine def OP_start(CoinOp_state): print("\nSoda Machine power is ON, initializing...") print("Type 'shutdown' when idle to shutdown Soda Machine") print("Type 'restock' when idle to restock Soda Machine") print("Type 'report' when idle to to get sales report") return ("accept_coins", CoinOp_state)
| _| | | | |_) | \ \ / / | _| | | (_) | |_) | | _| ___) | | | | |_| |_|_| .__/ \_\/_/ |_| |_|\___/| .__/ |_| |____/|_| |_| |_| |_| FILENAME: FlipFlopExample.py AUTHOR: "Brig Young, https://github.com/Sonophoto/" PURPOSE: "Implements a simple bistable flip flop with a limit" COPYRIGHT: "Copyright 2016-2020 Brig Young, Sonophotostudios.com" LICENSE: " BSD 2-Clause, (Citation Required) See LICENSE file" ************************************************************************* """ import pyDGW DGW_FlipFlop = pyDGW.DGWalker() FF_state = pyDGW.DGW_data() FF_state.state = 0 FF_state.counter = 0 FF_state.limit = 10 # Set to True to get debugging output from the graph walker DGW_FlipFlop.KDEBUG = False # Set to True to use: "if DGW_FlipFlop.DEBUG: print()" for debugging DGW_FlipFlop.DEBUG = True # Set to True to get operating status messages from pyDGW DGW_FlipFlop.SDEBUG = False def OP_start(FF_state):
I hope this code encourages you to play with and learn more about Finite State Machines AKA Directed Graph Walkers. """ import datetime as dt import pyDGW Turnstyle_state = pyDGW.DGW_data() Turnstyle_state.alarm_status = 0 Turnstyle_state.token_total = 0 Turnstyle_state.pass_total = 0 Turnstyle_state.alarm_total = 0 DGW_Turnstyle = pyDGW.DGWalker() DGW_Turnstyle.DEBUG = False # User debug watches for user code DGW_Turnstyle.KDEBUG = False # Kernel debug watches from pyDGW DGW_Turnstyle.SDEBUG = False # Verbose operating messages from pyDGW def OP_start(Turnstyle_state): print("\nTurnstyle power is ON, initializing...") print("Type 'shutdown' at any input to shutdown Turnstyle") Turnstyle_state.alarm_status = 0 return ("accept_token", Turnstyle_state) def OP_accept_token(Turnstyle_state): print("\nWelcome to the pyDGW Turnstyle!")
LICENSE: " BSD 2-Clause, (Citation Required) See LICENSE file" ************************************************************************* """ import pyDGW # Import the pyDGW module # First we create the state variable that we pass around. # This is a trivial example but important Simple_state = pyDGW.DGW_data() # Instatiate a DGW_data object Simple_state.the_answer = 0 # Extend Simple_state with data members # Next we create the Graph Walker object that will hold our state methods DGW_Simple = pyDGW.DGWalker() # Instantiate a DGWalker object # Next we define the operator functions for each state in our state machine # We use logic in each operator to determine which edge we will follow when # we change state. # An operator could contain another state machine. def OP_start(Simple_state): # A Start Node is required """Our start state""" # Optional Docstring print("Entering the start node") # Optional messaging Simple_state.the_answer = 1 # Do something to change data if Simple_state.the_answer : # Use logic to determine next state operator = "stop" # operator is the next state print("Next Operator is:", operator) # Optional messaging return(operator, Simple_state) # Pass operator and modified state # back to DGWalker which will load # and execute the next node.