| | / _ \| | | | '_ \| __/ _ \ '__| | |_ \___ \| |\/| | | |__| (_) | |_| | | | | || __/ | | _| ___) | | | | \____\___/ \__,_|_| |_|\__\___|_| |_| |____/|_| |_| FILENAME: CounterExample.py 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"
NOTE: For more advanced programmers this is a first level abstraction and for working code would need a refactor. I know this ;-) This is a teaching example so it is written this way so as not to confuse less advanced students. Once they understand this we can move on to passing around references more aggresively with an aggresive refactoring pass. If it is not here when you read this: Feel Free to PR and get credit <wink> """ import datetime as dt import pyDGW # 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
|_| |_| 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): """Our start state initializes our flip flop""" print("Beginning FlipFlopping...")
1. modifying the problem's state variable DGW_state 2. choosing the next ooperator 3. calling that operator and passing along DGW_state So sketch out the nodes in your machine, write-up logic for each node, and connect them together with pyDGW.DGWalker 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")
FILENAME: SimpleExample.py AUTHOR: "Brig Young, https://github.com/Sonophoto/" PURPOSE: "Implements the simplest possible FSM, Extra Comments" COPYRIGHT: "Copyright 2016-2020 Brig Young, Sonophotostudios.com" 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