
    Vh                    F   U d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
mZmZ ej                  dk\  rddlmZ nddlmZ dZ ed	e
      Z ede
      Z ede
      Z G d deeeef         Z G d deeeef         Zeegdf   ZdZded<    G d deeeef         Zy)zp
A core state-machine abstraction.

Perhaps something that could be replaced with or integrated into machinist.
    )annotationsN)chain)CallableGenericOptionalSequenceTypeVarHashable)   
   )	TypeAliasz
<no state>State)boundInputOutputc                  $     e Zd ZdZd fdZ xZS )NoTransitionz
    A finite state machine in C{state} has no transition for C{symbol}.

    @ivar state: See C{state} init parameter.

    @ivar symbol: See C{symbol} init parameter.
    c                h    || _         || _        t        t        |   dj                  ||             y)z
        Construct a L{NoTransition}.

        @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.
        zno transition for {} in {}N)statesymbolsuper	Exception__init__format)selfr   r   	__class__s      =/home/dcms/DCMS/lib/python3.12/site-packages/automat/_core.pyr   zNoTransition.__init__"   s2     
i'(//>	
    )r   r   r   r   )__name__
__module____qualname____doc__r   __classcell__)r   s   @r   r   r      s    
 
r   r   c                      e Zd ZdZdddZedd       Zej                  dd       Z	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ	ddZ
dd	Zdd
ZddZ	 	 	 	 	 	 ddZy)	Automatonzr
    A declaration of a finite state machine.

    Note that this is not the machine itself; it is immutable.
    Nc                V    |t         }|J || _        t               | _        d| _        y)zJ
        Initialize the set of transitions and the initial state.
        N)	_NO_STATE_initialStateset_transitions_unhandledTransition)r   initials     r   r   zAutomaton.__init__9   s4     ?G"""$+ORuNR!r   c                    | j                   S )z8
        Return this automaton's initial state.
        )r(   r   s    r   initialStatezAutomaton.initialStateD   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.initialStateK   s=     Y.1889K9KL  #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 {} to {} via {}N)r*   r1   r   addtuple)	r   inStateinputSymboloutStateoutputSymbols	anInStateanInputSymbol
anOutState_s	            r   addTransitionzAutomaton.addTransitionY   sw     8<7H7H 	3I}j!G#(D BII[ 	 	wXu]?STUr   c                (    |t        |      f| _        y)z
        All unhandled transitions will be handled by transitioning to the given
        error state and error-handling output symbols.
        N)r4   r+   )r   r7   r8   s      r   unhandledTransitionzAutomaton.unhandledTransitionp   s     &.u]/C$D!r   c                ,    t        | j                        S )z"
        All transitions.
        )	frozensetr*   r.   s    r   allTransitionszAutomaton.allTransitionsy   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   r5   r6   r7   outputSymbols        r   inputAlphabetzAutomaton.inputAlphabet   s9     CGBSBS
 
>+x 
 	
 
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 ).0r5   r6   r7   r8   s        r   	<genexpr>z+Automaton.outputAlphabet.<locals>.<genexpr>   s        CWk8]  s   )r)   r   from_iterabler*   r.   s    r   outputAlphabetzAutomaton.outputAlphabet   s2       GKGXGX  
 	
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rH   rI   )rJ   r5   r6   r7   rD   s        r   rK   z#Automaton.states.<locals>.<genexpr>   s%       BWk8\ (# s   )rA   r   rL   r*   r.   s    r   stateszAutomaton.states   s2    
   FJFWFW  
 	
r   c                    | j                   D ]  \  }}}}||f||fk(  s|t        |      fc S  | j                  t        ||      | j                  S )zI
        A 2-tuple of (outState, outputSymbols) for inputSymbol.
        )r   r   )r*   listr+   r   )r   r5   r6   r9   r:   r7   r8   s          r   outputForInputzAutomaton.outputForInput   sm     BFARAR 	7=I}h%)])CC $}"566	7 $$,W[AA(((r   rH   )r,   zState | NonereturnNone)rT   r   )r   r   rT   rU   )r5   r   r6   r   r7   r   r8   ztuple[Output, ...])r7   r   r8   zSequence[Output]rT   rU   )rT   z7frozenset[tuple[State, Input, State, Sequence[Output]]])rT   z
set[Input])rT   zset[Output])rT   zfrozenset[State])r5   r   r6   r   rT   ztuple[State, Sequence[Output]])r   r    r!   r"   r   propertyr/   setterr=   r?   rB   rE   rM   rP   rS   rI   r   r   r%   r%   2   s    	S " " # #VV V 	V
 *V.EE.>E	E,
	


))+0)	')r   r%   z<Callable[[State, Input, State], OutputTracer[Output] | None]r   Tracerc                  0    e Zd ZdZddZddZ	 	 	 	 ddZy)	TransitionerzA
    The combination of a current state and an L{Automaton}.
    c                .    || _         || _        d | _        y rH   )
_automaton_state_tracer)r   	automatonr/   s      r   r   zTransitioner.__init__   s    ;D)<@r   c                    || _         y rH   )r^   )r   tracers     r   setTracezTransitioner.setTrace   s	    r   c                    | j                   j                  | j                  |      \  }}d}| j                  r| j                  | j                  ||      }|| _        ||fS )zC
        Transition between states, returning any outputs.
        N)r\   rS   r]   r^   )r   r6   r7   r8   	outTracers        r   
transitionzTransitioner.transition   s\     #'//"@"@KK#
- 	<<T[[+xHIy))r   N)r_   zAutomaton[State, Input, Output]r/   r   )ra   z#Tracer[State, Input, Output] | NonerT   rU   )r6   r   rT   z4tuple[Sequence[Output], OutputTracer[Output] | None])r   r    r!   r"   r   rb   re   rI   r   r   rZ   rZ      s'    A
* *	=*r   rZ   )r"   
__future__r   sys	itertoolsr   typingr   r   r   r   r	   r
   version_infor   typing_extensionsr'   r   r   r   r   r   r%   OutputTracerrX   __annotations__rZ   rI   r   r   <module>rn      s   
 # 
  K Kw +	x(x(		*
9geUl3 
2x)uf,- x)v $'R	 R*75%/0 *r   