
    ~b                     Z    d Z ddlmZ dZ G d de      Z G d de      Z G d d	e      Zy
)zp
A core state-machine abstraction.

Perhaps something that could be replaced with or integrated into machinist.
    )chainz
<no state>c                   "     e Zd ZdZ fdZ xZS )NoTransitionz
    A finite state machine in C{state} has no transition for C{symbol}.

    @param state: the finite state machine's state at the time of the
        illegal transition.

    @param symbol: the input symbol for which no transition exists.
    c                 h    || _         || _        t        t        |   dj                  ||             y )Nzno transition for {} in {})statesymbolsuper	Exception__init__format)selfr   r   	__class__s      //usr/lib/python3/dist-packages/automat/_core.pyr   zNoTransition.__init__   s0    
i'(//>	
    )__name__
__module____qualname____doc__r   __classcell__)r   s   @r   r   r      s    
 
r   r   c                   n    e Zd ZdZd Zed        Zej                  d        Zd Zd Z	d Z
d Zd	 Zd
 Zy)	Automatonzr
    A declaration of a finite state machine.

    Note that this is not the machine itself; it is immutable.
    c                 8    t         | _        t               | _        y)zJ
        Initialize the set of transitions and the initial state.
        N)	_NO_STATE_initialStateset_transitionsr   s    r   r   zAutomaton.__init__'   s     'Er   c                     | j                   S )z8
        Return this automaton's initial state.
        )r   r   s    r   initialStatezAutomaton.initialState/   s    
 !!!r   c                 ~    | j                   t        ur$t        dj                  | j                               || _         y)z
        Set this automaton's initial state.  Raises a ValueError if
        this automaton already has an initial state.
        zinitial state already set to {}N)r   r   
ValueErrorr   )r   r   s     r   r   zAutomaton.initialState7   s?     Y.1889K9KLN N #r   c                     | j                   D ]-  \  }}}}||k(  s||k(  st        dj                  ||             | j                   j                  |||t	        |      f       y)z
        Add the given transition to the outputSymbol. Raise ValueError if
        there is already a transition with the same inState and inputSymbol.
        z&already have transition from {} via {}N)r   r!   r   addtuple)	r   inStateinputSymboloutStateoutputSymbols	anInStateanInputSymbol
anOutState_s	            r   addTransitionzAutomaton.addTransitionE   sy     :>9J9J 	[5Yz1W$+)E <CCG[Y[ [	[ 	k8U=-AB	
r   c                 ,    t        | j                        S )z"
        All transitions.
        )	frozensetr   r   s    r   allTransitionszAutomaton.allTransitionsV   s     **++r   c           
      \    | j                   D ch c]	  \  }}}}| c}}}}S c c}}}}w )zG
        The full set of symbols acceptable to this automaton.
        )r   )r   r%   r&   r'   outputSymbols        r   inputAlphabetzAutomaton.inputAlphabet]   s?    
 372C2CE E !/+x!-  E 	E Es   &
c                 `    t        t        j                  d | j                  D                    S )zR
        The full set of symbols which can be produced by this automaton.
        c              3   *   K   | ]  \  }}}}|  y wN ).0r%   r&   r'   r(   s        r   	<genexpr>z+Automaton.outputAlphabet.<locals>.<genexpr>j   s        ?+x  s   )r   r   from_iterabler   r   s    r   outputAlphabetzAutomaton.outputAlphabete   s4        $$  
 	
r   c                 `    t        t        j                  d | j                  D                    S )zc
        All valid states; "Q" in the mathematical description of a state
        machine.
        c              3   .   K   | ]  \  }}}}||f  y wr6   r7   )r8   r%   r&   r'   r2   s        r   r9   z#Automaton.states.<locals>.<genexpr>x   s'        ?+x (# s   )r/   r   r:   r   r   s    r   stateszAutomaton.statesr   s4    
    $$	  
 	
r   c                 x    | j                   D ]  \  }}}}||f||fk(  s|t        |      fc S  t        ||      )zI
        A 2-tuple of (outState, outputSymbols) for inputSymbol.
        )r   r   )r   listr   )r   r%   r&   r)   r*   r'   r(   s          r   outputForInputzAutomaton.outputForInput   sV    
 *.):):	7 &Y}%)])CC $}"566	7 ==r   N)r   r   r   r   r   propertyr   setterr-   r0   r3   r;   r>   rA   r7   r   r   r   r       sY    " " " 
# 
#
",E


>r   r   c                   "    e Zd ZdZd Zd Zd Zy)TransitionerzA
    The combination of a current state and an L{Automaton}.
    c                 .    || _         || _        d | _        y r6   )
_automaton_state_tracer)r   	automatonr   s      r   r   zTransitioner.__init__   s    #"r   c                     || _         y r6   )rI   )r   tracers     r   setTracezTransitioner.setTrace   s	    r   c                    | j                   j                  | j                  |      \  }}d}| j                  rG| j                  | j                  j	                         |j	                         |j	                               }|| _        ||fS )zC
        Transition between states, returning any outputs.
        N)rG   rA   rH   rI   _name)r   r&   r'   r(   	outTracers        r   
transitionzTransitioner.transition   sz     #'//"@"@AL#N-	<<T[[%6%6%8%0%6%6%8%-^^%57I y))r   N)r   r   r   r   r   rM   rQ   r7   r   r   rE   rE      s    
*r   rE   N)	r   	itertoolsr   r   r
   r   objectr   rE   r7   r   r   <module>rT      s;    	
9 
$i> i>X*6 *r   