11""" A general finite automaton representation """
22
3- # pylint: disable=function-redefined
4-
53from typing import Dict , List , Set , Tuple , \
64 Iterable , Iterator , Optional , Hashable , Any , TypeVar
75from abc import abstractmethod
1715from .transition_function import TransitionFunction
1816from .utils import to_state , to_symbol
1917
20- fa_type = TypeVar ("fa_type " , bound = "FiniteAutomaton" )
18+ AutomatonT = TypeVar ("AutomatonT " , bound = "FiniteAutomaton" )
2119
2220
2321class FiniteAutomaton (Iterable [Tuple [State , Symbol , State ]]):
@@ -41,6 +39,7 @@ class FiniteAutomaton(Iterable[Tuple[State, Symbol, State]]):
4139 A set of final or accepting states. It is a subset of states.
4240 """
4341
42+ @abstractmethod
4443 def __init__ (self ) -> None :
4544 self ._states : Set [State ]
4645 self ._input_symbols : Set [Symbol ]
@@ -553,13 +552,18 @@ def write_as_dot(self, filename: str) -> None:
553552 """
554553 write_dot (self .to_networkx (), filename )
555554
555+ @abstractmethod
556+ def accepts (self , word : Iterable [Hashable ]) -> bool :
557+ """ Checks whether the finite automaton accepts a given word """
558+ raise NotImplementedError
559+
556560 def get_accepted_words (self , max_length : Optional [int ] = None ) \
557561 -> Iterable [List [Symbol ]]:
558562 """
559563 Gets words accepted by the finite automaton.
560564 """
561565 if max_length is not None and max_length < 0 :
562- return []
566+ return
563567 states_to_visit = deque ((start_state , [])
564568 for start_state in self .start_states )
565569 states_leading_to_final = self ._get_states_leading_to_final ()
@@ -657,14 +661,14 @@ def to_dict(self) -> Dict[State, Dict[Symbol, Set[State]]]:
657661 return self ._transition_function .to_dict ()
658662
659663 @abstractmethod
660- def copy (self : fa_type ) -> fa_type :
664+ def copy (self : AutomatonT ) -> AutomatonT :
661665 """ Copies the current Finite Automaton instance """
662666 raise NotImplementedError
663667
664- def __copy__ (self : fa_type ) -> fa_type :
668+ def __copy__ (self : AutomatonT ) -> AutomatonT :
665669 return self .copy ()
666670
667- def _copy_to (self , fa_to_copy_to : fa_type ) -> fa_type :
671+ def _copy_to (self , fa_to_copy_to : AutomatonT ) -> AutomatonT :
668672 """ Copies current automaton properties to the given one """
669673 for start in self ._start_states :
670674 fa_to_copy_to .add_start_state (start )
0 commit comments