o
    $#÷[Ó  ã                   @   sH   d Z ddlmZ dZG dd„ deƒZG dd„ deƒZG dd	„ d	eƒZd
S )zp
A core state-machine abstraction.

Perhaps something that could be replaced with or integrated into machinist.
é    )Úchainz
<no state>c                       s    e Zd ZdZ‡ fdd„Z‡  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                    s(   || _ || _tt| ƒ d ||¡¡ d S )Nzno transition for {} in {})ÚstateÚsymbolÚsuperÚ	ExceptionÚ__init__Úformat)Úselfr   r   ©Ú	__class__© ú//usr/lib/python3/dist-packages/automat/_core.pyr      s
   

ÿzNoTransition.__init__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__classcell__r   r   r   r   r      s    	r   c                   @   sb   e Zd ZdZ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S )Ú	Automatonzr
    A declaration of a finite state machine.

    Note that this is not the machine itself; it is immutable.
    c                 C   s   t | _tƒ | _dS )zJ
        Initialize the set of transitions and the initial state.
        N)Ú	_NO_STATEÚ_initialStateÚsetÚ_transitions©r
   r   r   r   r   '   s   zAutomaton.__init__c                 C   s   | j S )z8
        Return this automaton's initial state.
        )r   r   r   r   r   ÚinitialState/   s   zAutomaton.initialStatec                 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   7   s
   

ÿ
c           	      C   sP   | 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 {} via {}N)r   r   r	   ÚaddÚtuple)	r
   ÚinStateÚinputSymbolÚoutStateÚoutputSymbolsÚ	anInStateÚanInputSymbolÚ
anOutStateÚ_r   r   r   ÚaddTransitionE   s   
ÿ€ÿzAutomaton.addTransitionc                 C   s
   t | jƒS )z"
        All transitions.
        )Ú	frozensetr   r   r   r   r   ÚallTransitionsV   s   
zAutomaton.allTransitionsc                 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   r   r    ÚoutputSymbolr   r   r   Ú	<setcomp>a   s    ÿz*Automaton.inputAlphabet.<locals>.<setcomp>)r   r   r   r   r   ÚinputAlphabet]   s   ÿzAutomaton.inputAlphabetc                 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   )r*   r   r   r    r!   r   r   r   Ú	<genexpr>j   s
   € 
ÿ
ÿz+Automaton.outputAlphabet.<locals>.<genexpr>)r   r   Úfrom_iterabler   r   r   r   r   ÚoutputAlphabete   s
   
ýÿzAutomaton.outputAlphabetc                 C   r.   )zc
        All valid states; "Q" in the mathematical description of a state
        machine.
        c                 s   s    � | ]\}}}}||fV  qd S r/   r   r)   r   r   r   r0   x   s
   € 
þ
ÿz#Automaton.states.<locals>.<genexpr>)r'   r   r1   r   r   r   r   r   Ústatesr   s
   
üÿzAutomaton.statesc                 C   s@   | j D ]\}}}}||f||fkr|t|ƒf  S qt||d�‚)zI
        A 2-tuple of (outState, outputSymbols) for inputSymbol.
        )r   r   )r   Úlistr   )r
   r   r   r"   r#   r    r!   r   r   r   ÚoutputForInput�   s   
ÿÿzAutomaton.outputForInputN)r   r   r   r   r   Úpropertyr   Úsetterr&   r(   r-   r2   r3   r5   r   r   r   r   r       s    

r   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚTransitionerzA
    The combination of a current state and an L{Automaton}.
    c                 C   s   || _ || _d | _d S r/   )Ú
_automatonÚ_stateÚ_tracer)r
   Ú	automatonr   r   r   r   r   ‘   s   
zTransitioner.__init__c                 C   s
   || _ d S r/   )r;   )r
   Útracerr   r   r   ÚsetTrace–   s   
zTransitioner.setTracec                 C   sH   | j  | j|¡\}}d}| jr|  | j ¡ | ¡ | ¡ ¡}|| _||fS )zC
        Transition between states, returning any outputs.
        N)r9   r5   r:   r;   Ú_name)r
   r   r    r!   Ú	outTracerr   r   r   Ú
transition™   s   
ÿþzTransitioner.transitionN)r   r   r   r   r   r>   rA   r   r   r   r   r8   Œ   s
    r8   N)	r   Ú	itertoolsr   r   r   r   Úobjectr   r8   r   r   r   r   Ú<module>   s   l