
    e!h                     F    d Z ddlmZmZ ddlmZ ddlmZ  G d de      Zy)aR  
``fact-list`` implementation from CLIPS.

See `Making a List <http://clipsrules.sourceforge.net/docume    ntation/v624/ug.htm#_Toc412126071>`_  section on the user guide. Also see `retrieving the fact-list   <http://clipsrules.sourceforge.net/d          ocumentation/v624/bpg.htm#_Toc11859921>`_ on the clips          programming manual
    )OrderedDictCounter)Fact)watchersc                   T     e Zd ZdZ fdZd Zed        Zd Zd Z	e
d        Z xZS )FactLista  
    Contains a list of facts (``asserted`` data).

    In clips, there is the concept of "modules"
    (:obj:`experta.engine.KnowledgeEngine`), wich have their own
    :obj:`experta.factlist.FactList` and :obj:`experta.agenda.Agenda`

    A factlist acts as both the module's factlist and a ``fact-set``
    yet currently most methods from a ``fact-set`` are not yet
    implemented
    c                     t         |           d| _        t               | _        t               | _        t               | _        d| _        y )Nr   F)	super__init__
last_indexr   reference_counterlistaddedremovedduplication)self	__class__s    X/var/www/html/diagnosisapp-backend/venv/lib/python3.12/site-packages/experta/factlist.pyr   zFactList.__init__   s9    !(V
v     c                 N    dj                  d | j                         D              S )N
c              3   0   K   | ]  \  }}|d |  yw)z: N ).0idxfacts      r   	<genexpr>z#FactList.__str__.<locals>.<genexpr>'   s!      +T d#+s   )joinitemsr   s    r   __str__zFactList.__str__&   s&    yy +!ZZ\+ + 	+r   c           
          t        | j                  g| j                         D cg c]  \  }}| j                  |      s||f c}}z         S c c}}w )N)	frozensetr   r   
is_special)r   kvs      r   _get_fact_idzFactList._get_fact_id+   sR    $..)(,

7 $1#'??1#5 F 77 8 	87s   Ac                    t        |t              st        d      |j                          | j	                  |      }| j
                  s|| j                  vr| j                  }||_        || |<   | xj                  dz  c_        | j                  j                  |       | j                  |xx   dz  cc<   t        j                  j                  d||       |S y)a  
        Assert (in clips terminology) a fact.

        This keeps insertion order.

        .. warning:: This will reject any object that not descend
                     from the Fact class.

        :param fact: The fact to declare, **must** be derived from
                     :obj:`experta.fact.Fact`.
        :return: (int) The index of the fact in the list.
        :throws ValueError: If the fact providen is not a Fact object

        z%The fact must descend the Fact class.   z ==> %s: %rN)
isinstancer   
ValueErrorvalidater'   r   r   r   
__factid__r   appendr   FACTSinfo)r   r   fact_idr   s       r   declarezFactList.declare2   s      $%DEE 	##D)wd.D.DD//C!DO DIOOq OJJd#""7+q0+NNtT:Kr   c                    t        |t              r|}n|j                  }|| vrt        d      | |   }| j	                  |      }| j
                  |xx   dz  cc<   | j
                  |   dk(  r| j
                  j                  |       t        j                  j                  d||       | j                  j                  |       | |= |S )a  
        Retract a previously asserted fact.

        See `"Retract that fact" in Clips User Guide
        <http://clipsrules.sourceforge.net/doc                umentation/v624/ug.htm#_Toc412126077>`_.

        :param idx: The index of the fact to retract in the factlist
        :return: (int) The retracted fact's index
        :throws IndexError: If the fact's index providen does not exist
        zFact not found.r)   r   z <== %s: %r)r*   intr-   
IndexErrorr'   r   popr   r/   r0   r   r.   )r   idx_or_factr   r   r1   s        r   retractzFactList.retract\   s     k3'C((Cd?.//Cy ##D)w'1,'!!'*a/""&&w/M46D!I
r   c                     	 | j                   | j                  ft               | _         t               | _        S # t               | _         t               | _        w xY w)zQ
        Return a tuple with the removed and added facts since last run.
        )r   r   r   r    s    r   changeszFactList.changes   s?    
	"::t||+DJ6DL DJ6DLs	   8  A)__name__
__module____qualname____doc__r   r!   staticmethodr'   r2   r8   propertyr:   __classcell__)r   s   @r   r   r      sF    
!+
 8 8(T"H " "r   r   N)	r>   collectionsr   r   experta.factr   expertar   r   r   r   r   <module>rE      s#   	 -  x"{ x"r   