U
    ,Wj  ã                   @  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d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.
    r   r   ©ÚstateÚsymbolc                   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__© úXc:\users\psant\downloads\hotstarbackend\school\venv38\Lib\site-packages\automat/_core.pyr   "   s
    	

ÿzNoTransition.__init__)Ú__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œdd„Zedd	œd
d„ƒZejdddœdd„ƒZdddddœdd„Zddddœdd„Z	dd	œdd„Z
dd	œdd„Zdd	œdd„Zd d	œd!d"„Zddd#d$œd%d&„ZdS )(Ú	Automatonzr
    A declaration of a finite state machine.

    Note that this is not the machine itself; it is immutable.
    NzState | NoneÚNone)ÚinitialÚreturnc                 C  s0   |dkrt }|dk	st‚|| _tƒ | _d| _dS )zJ
        Initialize the set of transitions and the initial state.
        N)Ú	_NO_STATEÚAssertionErrorÚ_initialStateÚsetÚ_transitionsÚ_unhandledTransition)r   r%   r   r   r   r   9   s    zAutomaton.__init__r   )r&   c                 C  s   | j S )z8
        Return this automaton's initial state.
        )r)   ©r   r   r   r   ÚinitialStateD   s    zAutomaton.initialState)r   r&   c                 C  s$   | j tk	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
    

ÿr   ztuple[Output, ...])ÚinStateÚinputSymbolÚoutStateÚoutputSymbolsc           	      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   r0   r1   r2   r3   Ú	anInStateÚanInputSymbolZ
anOutStateÚ_r   r   r   ÚaddTransitionY   s      ÿÿzAutomaton.addTransitionzSequence[Output])r2   r3   r&   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   r2   r3   r   r   r   ÚunhandledTransitionp   s    zAutomaton.unhandledTransitionz7frozenset[tuple[State, Input, State, Sequence[Output]]]c                 C  s
   t | jƒS )z"
        All transitions.
        )Ú	frozensetr+   r-   r   r   r   ÚallTransitionsy   s    zAutomaton.allTransitionsz
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   ©Ú.0r0   r1   r2   ZoutputSymbolr   r   r   Ú	<setcomp>ƒ   s   
ÿz*Automaton.inputAlphabet.<locals>.<setcomp>)r+   r-   r   r   r   ÚinputAlphabet   s    þzAutomaton.inputAlphabetzset[Output]c                 C  s   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   )r>   r0   r1   r2   r3   r   r   r   Ú	<genexpr>�   s   
ÿz+Automaton.outputAlphabet.<locals>.<genexpr>)r*   r   Úfrom_iterabler+   r-   r   r   r   ÚoutputAlphabetˆ   s
    
þÿzAutomaton.outputAlphabetzfrozenset[State]c                 C  s   t t dd„ | jD ƒ¡ƒS )zc
        All valid states; "Q" in the mathematical description of a state
        machine.
        c                 s  s   | ]\}}}}||fV  qd S rA   r   r=   r   r   r   rB   ™   s   
ÿz#Automaton.states.<locals>.<genexpr>)r;   r   rC   r+   r-   r   r   r   Ústates“   s
    
þÿzAutomaton.statesztuple[State, Sequence[Output]])r0   r1   r&   c                 C  sP   | j D ],\}}}}||f||fkr|t|ƒf  S q| jdkrJt||d�‚| jS )zI
        A 2-tuple of (outState, outputSymbols) for inputSymbol.
        Nr   )r+   Úlistr,   r   )r   r0   r1   r6   r7   r2   r3   r   r   r   ÚoutputForInputŸ   s    
zAutomaton.outputForInput)N)r   r   r    r!   r   Úpropertyr.   Úsetterr9   r:   r<   r@   rD   rE   rG   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œdd„Zddd	œd
d„Zdddœdd„ZdS )ÚTransitionerzA
    The combination of a current state and an L{Automaton}.
    zAutomaton[State, Input, Output]r   )Ú	automatonr.   c                 C  s   || _ || _d | _d S rA   )Ú
_automatonÚ_stateÚ_tracer)r   rL   r.   r   r   r   r   ¶   s    zTransitioner.__init__z#Tracer[State, Input, Output] | Noner$   )Útracerr&   c                 C  s
   || _ d S rA   )rO   )r   rP   r   r   r   ÚsetTrace»   s    zTransitioner.setTracer   z4tuple[Sequence[Output], OutputTracer[Output] | None])r1   r&   c                 C  s<   | j  | j|¡\}}d}| jr.|  | j||¡}|| _||fS )zC
        Transition between states, returning any outputs.
        N)rM   rG   rN   rO   )r   r1   r2   r3   Z	outTracerr   r   r   Ú
transition¾   s     ÿzTransitioner.transitionN)r   r   r    r!   r   rQ   rR   r   r   r   r   rK   ±   s   rK   )r!   Ú
__future__r   ÚsysÚ	itertoolsr   Útypingr   r   r   r   r   r	   Úversion_infor   Ztyping_extensionsr'   r   r   r   r   r   r#   ZOutputTracerrJ   Ú__annotations__rK   r   r   r   r   Ú<module>   s     
{