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