U
    Ձ]                     @   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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   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anInputSymbolZ
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    ZoutputSymbolr   r   r   	<setcomp>a   s    z*Automaton.inputAlphabet.<locals>.<setcomp>)r   r   r   r   r   inputAlphabet]   s    zAutomaton.inputAlphabetc                 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)   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   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 r,   r   r(   r   r   r   r-   x   s   
z#Automaton.states.<locals>.<genexpr>)r&   r   r.   r   r   r   r   r   statesr   s
    
zAutomaton.statesc                 C   sD   | j D ],\}}}}||f||fkr|t|f  S qt||ddS )zI
        A 2-tuple of (outState, outputSymbols) for inputSymbol.
        )r   r   N)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+   r/   r0   r2   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
   Z	automatonr   r   r   r   r      s    zTransitioner.__init__c                 C   s
   || _ d S r,   )r8   )r
   Z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)r6   r2   r7   r8   _name)r
   r   r    r!   Z	outTracerr   r   r   
transition   s    
zTransitioner.transitionN)r   r   r   r   r   r9   r;   r   r   r   r   r5      s   r5   N)	r   	itertoolsr   r   r   r   objectr   r5   r   r   r   r   <module>   s
   l