
    e!h              	           d Z ddlmZ ddlmZ ddlmZ ddlmZ  G d d edd	d
g            Z	 G d d edg d            Z
y)z>Token object and related objects needed by the RETE algorithm.    )
namedtuple)Mapping)Enum)Factc                   .     e Zd ZdZ fdZd Zd Z xZS )	TokenInfoz2Tag agnostig version of Token with inmutable data.c                 n    t         t        |   | t        |      t        |j	                                     S )z1Return a namedtuple instance with inmutable data.)superr   __new__	frozensetitems)clsdatacontext	__class__s      c/var/www/html/diagnosisapp-backend/venv/lib/python3.12/site-packages/experta/matchers/rete/token.pyr   zTokenInfo.__new__   s0    Y,S-6t_-6w}}-GI 	I    c                 h    t         j                  | j                  t        | j                              S )z%Create a VALID token using this data.)Tokenvalidr   dictr   selfs    r   to_valid_tokenzTokenInfo.to_valid_token   s     {{499d4<<&899r   c                 h    t         j                  | j                  t        | j                              S )z(Create an INVALID token using this data.)r   invalidr   r   r   r   s    r   to_invalid_tokenzTokenInfo.to_invalid_token   s     }}TYYT\\(:;;r   )__name__
__module____qualname____doc__r   r   r   __classcell__r   s   @r   r   r   
   s    <I:<r   r   
_TokenInfor   r   c                   p     e Zd ZdZ G d de      Zd
 fd	Zd Zed
d       Z	ed
d       Z
d Zd	 Z xZS )r   z-Token, as described by RETE but with context.c                       e Zd ZdZdZdZy)Token.TagTypezTypes of Token TAG data.TFN)r   r   r    r!   VALIDINVALID r   r   TagTyper'      s    &r   r+   c                 f   |i }	 t        || j                        sJ d       t        |t              st        d |D              sJ d       t        |t              sJ 	 t        |t              r|hn
t        |      }t        t        | +  | |||      }|S # t
        $ r}t        |      |d}~ww xY w)a"  
        Instantiate a new Token with the given tag, data and context.

        If the context is None an empty context will be generated.

        - `tag`: Must be an instance of `TagType`.
        - `data`: A Fact or an iterable of Facts.
        - `context`: A mapping or None.

        Nz#tag must be of `Token.TagType` typec              3   <   K   | ]  }t        |t                y wN)
isinstancer   ).0fs     r   	<genexpr>z Token.__new__.<locals>.<genexpr>7   s     :
1d+:s   z-data must be either Fact or iterable of Facts)r/   r+   r   allr   AssertionError	TypeErrorsetr
   r   r   )r   tagr   r   excr   r   s         r   r   zToken.__new__%   s     ?G	*c3;;/ 656/tT*:T::@?@ ; gw/// $D$/vSYUC(c4A  	*C.c)	*s   AB 	B0B++B0c                 B    t        | j                  | j                        S )z
        Create and return an instance of TokenInfo with this token.

        This is useful, for example, to use this token information as a
        dictionary key.

        )r   r   r   r   s    r   to_infozToken.to_infoA   s     DLL11r   c                 >     | | j                   j                  ||      S )z!Shortcut to create a VALID Token.)r+   r(   r   r   r   s      r   r   zToken.validK   s     3;;$$dG44r   c                 >     | | j                   j                  ||      S )z$Shortcut to create an INVALID Token.)r+   r)   r<   s      r   r   zToken.invalidP   s     3;;&&g66r   c                 H    | j                   | j                  j                  k(  S )zTest if this Token is VALID.)r7   r+   r(   r   s    r   is_validzToken.is_validU   s    xx4<<----r   c                     | j                  | j                  | j                  j                         | j                  j                               S )z
        Make a new instance of this Token.

        This method makes a copy of the mutable part of the token before
        making the instance.

        )r   r7   r   copyr   r   s    r   rA   z
Token.copyY   s3     ~~dhh		(8$,,:K:K:MNNr   r.   )r   r   r    r!   r   r+   r   r:   classmethodr   r   r?   rA   r"   r#   s   @r   r   r      sO    7$ 82 5 5 7 7.Or   r   _Token)r7   r   r   N)r!   collectionsr   collections.abcr   enumr   experta.factr   r   r   r*   r   r   <module>rH      sD    D " #  <
<&))<= <$EOJx!;< EOr   