parameters: (self, fsm)
return_statement: return fsm
docstring: An FSM optimisation step.  Removes all states and transitions which
          can't be reached.  Also works out nodes from which it is not possible
          to get to a 'hit' state.  Those can be called 'fail'.
docstring_summary: An FSM optimisation step.  Removes all states and transitions which
          can't be reached.  Also works out nodes from which it is not possible
          to get to a 'hit' state.  Those can be called 'fail'.
docstring_tokens: An FSM optimisation step . Removes all states and transitions which can t be reached . Also works out nodes from which it is not possible to get to a hit state . Those can be called fail .
function: def remove_invalid_transitions(self, fsm):
          """
          An FSM optimisation step.  Removes all states and transitions which
          can't be reached.  Also works out nodes from which it is not possible
          to get to a 'hit' state.  Those can be called 'fail'.
          """
        
          # Get a list of states which can lead to 'hit'.  All other states are
          # fail states, because you can't travel to hit.  Do this by starting
          # with a set containing just the 'hit' state, and keep adding states
          # which can get there.  Then re-iterate adding all states that get
          # to that state.
          hitstates = set(["hit"])
          while True:
              nhs = hitstates.copy()
              for v in fsm:
                  if v[2] in hitstates:
                      hitstates.add(v[0])
              if nhs == hitstates:
                  break
  
          # Rework the 'fail' state into the FSM.  Fail states are states
          # from which it is not possible to arrive at hit.
          fsm2 = []
          for v in fsm:
  
              # Remove transitions which lead from fail states
              if v[0] not in hitstates:
                  continue
  
              # Transitions to states which can't lead to hits are fails.
              if v[2] in hitstates:
                  fsm2.append(v)
              else:
                  fsm2.append((v[0], v[1], 'fail'))
  
          fsm = fsm2
  
          # Remove transitions which can't be reached from init.  There will
          # areas of the FSM which cannot be reached, and this can be chopped
          # out from the FSM.
          while True:
  
              # Get a list of navigable states, which is states it is possible
              # to navigate to.  Add 'init' because it's the start state, and
              # you never navigate to it.
              navstates=set(['init'])
              for v in fsm:
                  navstates.add(v[2])
              
              fsm2 = []
  
              # Remove all transitions which don't go from navigable states.
              for v in fsm:
                  if v[0] in navstates:
                      fsm2.append(v)
  
              # This won't succeed in a single operation.  Keep going until
              # chopping out transitions results in no change.
              if fsm == fsm2:
                  break
  
              fsm = fsm2
  
          return fsm
function_tokens: def remove_invalid_transitions ( self , fsm ) : # Get a list of states which can lead to 'hit'.  All other states are # fail states, because you can't travel to hit.  Do this by starting # with a set containing just the 'hit' state, and keep adding states # which can get there.  Then re-iterate adding all states that get # to that state. hitstates = set ( [ "hit" ] ) while True : nhs = hitstates . copy ( ) for v in fsm : if v [ 2 ] in hitstates : hitstates . add ( v [ 0 ] ) if nhs == hitstates : break # Rework the 'fail' state into the FSM.  Fail states are states # from which it is not possible to arrive at hit. fsm2 = [ ] for v in fsm : # Remove transitions which lead from fail states if v [ 0 ] not in hitstates : continue # Transitions to states which can't lead to hits are fails. if v [ 2 ] in hitstates : fsm2 . append ( v ) else : fsm2 . append ( ( v [ 0 ] , v [ 1 ] , 'fail' ) ) fsm = fsm2 # Remove transitions which can't be reached from init.  There will # areas of the FSM which cannot be reached, and this can be chopped # out from the FSM. while True : # Get a list of navigable states, which is states it is possible # to navigate to.  Add 'init' because it's the start state, and # you never navigate to it. navstates = set ( [ 'init' ] ) for v in fsm : navstates . add ( v [ 2 ] ) fsm2 = [ ] # Remove all transitions which don't go from navigable states. for v in fsm : if v [ 0 ] in navstates : fsm2 . append ( v ) # This won't succeed in a single operation.  Keep going until # chopping out transitions results in no change. if fsm == fsm2 : break fsm = fsm2 return fsm