o
    Ñó·i  ã                   @  sþ   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d
�Zeded
�Zeded
�ZG dd„ deeeef ƒZG dd„ deeeef ƒZeegdf ZdZded< G dd„ deeeef ƒZdS )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                      s"   e Zd ZdZd‡ fdd„Z‡  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.
    Ústater   Úsymbolr   c                   s(   || _ || _tt| ƒ d ||¡¡ dS )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)r   r   ÚsuperÚ	ExceptionÚ__init__Úformat)Úselfr   r   ©Ú	__class__© úSC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\automat/_core.pyr   "   s
   	

ÿzNoTransition.__init__)r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__classcell__r   r   r   r   r      s    r   c                   @  s€   e Zd ZdZd(d)dd„Zed*d
d„ƒZejd+dd„ƒZd,dd„Zd-dd„Z	d.dd„Z
d/dd„Zd0d d!„Zd1d#d$„Zd2d&d'„ZdS )3Ú	Automatonzr
    A declaration of a finite state machine.

    Note that this is not the machine itself; it is immutable.
    NÚinitialúState | NoneÚreturnÚNonec                 C  s0   |du rt }|dusJ ‚|| _tƒ | _d| _dS )zJ
        Initialize the set of transitions and the initial state.
        N)Ú	_NO_STATEÚ_initialStateÚsetÚ_transitionsÚ_unhandledTransition)r   r#   r   r   r   r   9   s   
zAutomaton.__init__r   c                 C  s   | j S )z8
        Return this automaton's initial state.
        N)r(   ©r   r   r   r   ÚinitialStateD   s   zAutomaton.initialStater   c                 C  s$   | j turtd | j ¡ƒ‚|| _ dS )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   r   r   r   r-   K   s
   

ÿ
ÚinStateÚinputSymbolr   ÚoutStateÚoutputSymbolsútuple[Output, ...]c           	      C  sR   | j D ]\}}}}||kr||krtd |||¡ƒ‚q| j  |||t|ƒf¡ dS )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*   r.   r   ÚaddÚtuple)	r   r/   r0   r1   r2   Ú	anInStateÚanInputSymbolZ
anOutStateÚ_r   r   r   ÚaddTransitionY   s   ÿÿ€zAutomaton.addTransitionúSequence[Output]c                 C  s   |t |ƒf| _dS )z�
        All unhandled transitions will be handled by transitioning to the given
        error state and error-handling output symbols.
        N)r5   r+   )r   r1   r2   r   r   r   ÚunhandledTransitionp   s   zAutomaton.unhandledTransitionú7frozenset[tuple[State, Input, State, Sequence[Output]]]c                 C  s
   t | jƒS )z"
        All transitions.
        N)Ú	frozensetr*   r,   r   r   r   ÚallTransitionsy   s   
zAutomaton.allTransitionsú
set[Input]c                 C  s   dd„ | j D ƒS )zG
        The full set of symbols acceptable to this automaton.
        c                 S  s   h | ]\}}}}|’qS r   r   ©Ú.0r/   r0   r1   ZoutputSymbolr   r   r   Ú	<setcomp>ƒ   s    
ÿÿz*Automaton.inputAlphabet.<locals>.<setcomp>N)r*   r,   r   r   r   ÚinputAlphabet   s   þzAutomaton.inputAlphabetúset[Output]c                 C  ó   t t dd„ | jD ƒ¡ƒS )zR
        The full set of symbols which can be produced by this automaton.
        c                 s  s   � | ]	\}}}}|V  qd S ©Nr   )rA   r/   r0   r1   r2   r   r   r   Ú	<genexpr>�   s
   € 
ÿ
ÿz+Automaton.outputAlphabet.<locals>.<genexpr>N)r)   r   Úfrom_iterabler*   r,   r   r   r   ÚoutputAlphabetˆ   s
   
þÿzAutomaton.outputAlphabetúfrozenset[State]c                 C  rE   )zc
        All valid states; "Q" in the mathematical description of a state
        machine.
        c                 s  s    � | ]\}}}}||fV  qd S rF   r   r@   r   r   r   rG   ™   s
   € 
ÿ
ÿz#Automaton.states.<locals>.<genexpr>N)r=   r   rH   r*   r,   r   r   r   Ústates“   s
   
þÿzAutomaton.statesútuple[State, Sequence[Output]]c                 C  sP   | j D ]\}}}}||f||fkr|t|ƒf  S q| jdu r%t||d�‚| jS )zI
        A 2-tuple of (outState, outputSymbols) for inputSymbol.
        N)r   r   )r*   Úlistr+   r   )r   r/   r0   r6   r7   r1   r2   r   r   r   ÚoutputForInputŸ   s   ÿ
zAutomaton.outputForInputrF   )r#   r$   r%   r&   )r%   r   )r   r   r%   r&   )r/   r   r0   r   r1   r   r2   r3   )r1   r   r2   r:   r%   r&   )r%   r<   )r%   r?   )r%   rD   )r%   rJ   )r/   r   r0   r   r%   rL   )r   r   r   r    r   Úpropertyr-   Úsetterr9   r;   r>   rC   rI   rK   rN   r   r   r   r   r"   2   s    


	

	
r"   z<Callable[[State, Input, State], OutputTracer[Output] | None]r   ÚTracerc                   @  s.   e Zd ZdZddd„Zddd„Zddd„ZdS )ÚTransitionerzA
    The combination of a current state and an L{Automaton}.
    Ú	automatonúAutomaton[State, Input, Output]r-   r   c                 C  s   || _ || _d | _d S rF   )Ú
_automatonÚ_stateÚ_tracer)r   rS   r-   r   r   r   r   ¶   s   
zTransitioner.__init__Útracerú#Tracer[State, Input, Output] | Noner%   r&   c                 C  s
   || _ d S rF   )rW   )r   rX   r   r   r   ÚsetTrace»   s   
zTransitioner.setTracer0   r   ú4tuple[Sequence[Output], OutputTracer[Output] | None]c                 C  s<   | j  | j|¡\}}d}| jr|  | j||¡}|| _||fS )zC
        Transition between states, returning any outputs.
        N)rU   rN   rV   rW   )r   r0   r1   r2   Z	outTracerr   r   r   Ú
transition¾   s   ÿzTransitioner.transitionN)rS   rT   r-   r   )rX   rY   r%   r&   )r0   r   r%   r[   )r   r   r   r    r   rZ   r\   r   r   r   r   rR   ±   s
    

rR   )r    Ú
__future__r   ÚsysÚ	itertoolsr   Útypingr   r   r   r   r   r	   Úversion_infor   Ztyping_extensionsr'   r   r   r   r   r   r"   ZOutputTracerrQ   Ú__annotations__rR   r   r   r   r   Ú<module>   s"    
{