
    Pie'                    z    d dl mZ d dlmZmZmZmZmZ d dlm	Z	 d dl
mZmZ erd dlmZ dgZ G d d          ZdS )	    )annotations)TYPE_CHECKINGIterableIteratorListOptional)RDF)BNodeNode)Graph
Collectionc                  |    e Zd ZdZg fd"dZd#dZd$dZd%dZd&dZd'dZ	d(dZ
d)dZd*dZd+dZd,dZd-dZd  Zd!S ).r   a]  See "Emulating container types": <https://docs.python.org/reference/datamodel.html#emulating-container-types>

    ```python
    >>> from rdflib.term import Literal
    >>> from rdflib.graph import Graph
    >>> from pprint import pprint
    >>> listname = BNode()
    >>> g = Graph('Memory')
    >>> listItem1 = BNode()
    >>> listItem2 = BNode()
    >>> g.add((listname, RDF.first, Literal(1))) # doctest: +ELLIPSIS
    <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
    >>> g.add((listname, RDF.rest, listItem1)) # doctest: +ELLIPSIS
    <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
    >>> g.add((listItem1, RDF.first, Literal(2))) # doctest: +ELLIPSIS
    <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
    >>> g.add((listItem1, RDF.rest, listItem2)) # doctest: +ELLIPSIS
    <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
    >>> g.add((listItem2, RDF.rest, RDF.nil)) # doctest: +ELLIPSIS
    <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
    >>> g.add((listItem2, RDF.first, Literal(3))) # doctest: +ELLIPSIS
    <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
    >>> c = Collection(g,listname)
    >>> pprint([term.n3() for term in c])
    ['"1"^^<http://www.w3.org/2001/XMLSchema#integer>',
     '"2"^^<http://www.w3.org/2001/XMLSchema#integer>',
     '"3"^^<http://www.w3.org/2001/XMLSchema#integer>']
    >>> Literal(1) in c
    True
    >>> len(c)
    3
    >>> c._get_container(1) == listItem1
    True
    >>> c.index(Literal(2)) == 1
    True

    ```

    The collection is immutable if `uri` is the empty list (`http://www.w3.org/1999/02/22-rdf-syntax-ns#nil`).
    graphr   urir   seq
List[Node]c                P    || _         |pt                      | _        |r| |z  } d S d S N)r   r
   r   )selfr   r   r   s       e/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/rdflib/collection.py__init__zCollection.__init__8   s7    
>%'' 	CKDDD	 	    returnstrc                F    dd                     d | D                       z  S )a@  
        ```python
        >>> from rdflib.term import Literal
        >>> from rdflib.graph import Graph
        >>> listname = BNode()
        >>> g = Graph('Memory')
        >>> listItem1 = BNode()
        >>> listItem2 = BNode()
        >>> g.add((listname, RDF.first, Literal(1))) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((listname, RDF.rest, listItem1)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((listItem1, RDF.first, Literal(2))) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((listItem1, RDF.rest, listItem2)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((listItem2, RDF.rest, RDF.nil)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((listItem2, RDF.first, Literal(3))) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> c = Collection(g, listname)
        >>> print(c.n3()) #doctest: +NORMALIZE_WHITESPACE
        ( "1"^^<http://www.w3.org/2001/XMLSchema#integer>
            "2"^^<http://www.w3.org/2001/XMLSchema#integer>
            "3"^^<http://www.w3.org/2001/XMLSchema#integer> )

        ```
        z( %s ) c                6    g | ]}|                                 S  )n3).0is     r   
<listcomp>z!Collection.n3.<locals>.<listcomp>[   s     $:$:$:QTTVV$:$:$:r   )joinr   s    r   r   zCollection.n3>   s*    : 388$:$:T$:$:$:;;<<r   indexintOptional[Node]c                    t          |t                    sJ | j        }| j        }d}||k     r.|dz  }|                    |t
          j                  }|n||k     .|S )z+Gets the first, rest holding node at index.r      )
isinstancer&   r   r   valuer	   rest)r   r%   r   	containerr!   s        r   _get_containerzCollection._get_container]   sn    %%%%%%
$(H	%iiFAIsx88I 	 %ii
 r   c                t    t          t          | j                            | j                                      S )zlength of items in collection.)lenlistr   itemsr   r$   s    r   __len__zCollection.__len__j   s*    4
((2233444r   itemc                   | j         }d}	 |t          j        |f| j        v r|S t	          | j                            |t          j                            }|dz  }|t          j        gk    rt          |d| j                   |st          d| j         z            t          |          dk    sJ d| j         z              |d         })zM
        Returns the 0-based numerical index of the item in the list
        r   Tr)   z is not in zMalformed RDF Collection: %s)r   r	   firstr   r1   objectsr,   nil
ValueError	Exceptionr0   )r   r4   listnamer%   newlinks        r   r%   zCollection.indexn   s     8	*#)T*dj88tz11(CHEEFF
swi''$$$$%IJJJ  *#$BTX$MNNNw<<1,,,.Ltx.W,,,&qzH	*r   keyc                    |                      |          }|r8| j                            |t          j                  }|r|S t          |          t          |          )TODO)r.   r   r+   r	   r6   KeyError
IndexError)r   r=   cvs       r   __getitem__zCollection.__getitem__   s[    $$ 	"
  CI..A $smm#S//!r   r+   Nonec                    |                      |          }|r)| j                            |t          j        |f           dS t          |          )r?   N)r.   r   setr	   r6   rA   )r   r=   r+   rB   s       r   __setitem__zCollection.__setitem__   sL    $$ 	"JNNAsy%011111S//!r   c                ^   | |          | j         }|                     |          }|sJ t          |           dk    r|dk    rdS |t          |           dz
  k    rc|                     |dz
            }| j                             |t          j        t          j        f           |                    |ddf           dS |                     |dz             }|                     |dz
            }|r|sJ |                    |ddf           |                    |t          j        |f           dS )a  
        ```python
        >>> from rdflib.namespace import RDF, RDFS
        >>> from rdflib import Graph
        >>> from pprint import pformat
        >>> g = Graph()
        >>> a = BNode('foo')
        >>> b = BNode('bar')
        >>> c = BNode('baz')
        >>> g.add((a, RDF.first, RDF.type)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((a, RDF.rest, b)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((b, RDF.first, RDFS.label)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((b, RDF.rest, c)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((c, RDF.first, RDFS.comment)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> g.add((c, RDF.rest, RDF.nil)) # doctest: +ELLIPSIS
        <Graph identifier=... (<class 'rdflib.graph.Graph'>)>
        >>> len(g)
        6
        >>> def listAncestry(node, graph):
        ...   for i in graph.subjects(RDF.rest, node):
        ...     yield i
        >>> [str(node.n3())
        ...   for node in g.transitiveClosure(listAncestry, RDF.nil)]
        ['_:baz', '_:bar', '_:foo']
        >>> lst = Collection(g, a)
        >>> len(lst)
        3
        >>> b == lst._get_container(1)
        True
        >>> c == lst._get_container(2)
        True
        >>> del lst[1]
        >>> len(lst)
        2
        >>> len(g)
        4

        ```
        r)   r   N)r   r.   r0   rG   r	   r,   r8   remove)r   r=   r   current	priorlinknextpriors          r   __delitem__zCollection.__delitem__   s/   Z 	S		
%%c**wt99>>cAggDCIIM!!++C!G44IJNNIsx9:::LL'4./////&&sQw//D''a00E!E!!>LL'4.///IIuch-.....r   Iterator[Node]c                @    | j                             | j                  S )z"Iterator over items in Collections)r   r2   r   r$   s    r   __iter__zCollection.__iter__   s    z)))r   c                    | j         }	 | j                            |t          j                  }||t          j        k    r|S |}<r   )r   r   r+   r	   r,   r8   )r   r-   r,   s      r   _endzCollection._end   sF    H		!:##Isx88D|tsw   		!r   c                   |                                  }|t          j        k    rt          d          |t          j        df| j        v r7t                      }| j                            |t          j        |f           |}| j        	                    |t          j        |f           | j        	                    |t          j        t          j        f           | S )a  
        ```python
        >>> from rdflib.term import Literal
        >>> from rdflib.graph import Graph
        >>> listname = BNode()
        >>> g = Graph()
        >>> c = Collection(g,listname,[Literal(1),Literal(2)])
        >>> links = [
        ...     list(g.subjects(object=i, predicate=RDF.first))[0] for i in c]
        >>> len([i for i in links if (i, RDF.rest, RDF.nil) in g])
        1

        ```
        Cannot append to empty listN)
rT   r	   r8   r9   r6   r   r
   rG   r,   add)r   r4   endnodes       r   appendzCollection.append   s     iikk#'>>:;;;D!TZ//77DJNNC40111C
SY-...
SXsw/000r   otherIterable[Node]c                   |                                  }|t          j        k    rt          d          | j                            |t          j        d f           |D ]v}|t          j        d f| j        v r7t                      }| j        	                    |t          j        |f           |}| j        	                    |t          j        |f           w| j        	                    |t          j        t          j        f           | S )NrV   )
rT   r	   r8   r9   r   rJ   r,   r6   r
   rW   )r   r[   rX   r4   nxts        r   __iadd__zCollection.__iadd__  s    iikk#'>>:;;;
3$/000 	3 	3DSY%33gg
SXs3444JNNCD12222
SXsw/000r   c                    | j         }| j        }|rh|                    |t          j                  }|                    |t          j        d f           |                    |t          j        d f           |}|h| S r   )r   r   r+   r	   r,   rJ   r6   )r   r-   r   r,   s       r   clearzCollection.clear  sy    $(H	
 	;;y#(33DLL)SY5666LL)SXt4555I	  	
 r   N)r   r   r   r   r   r   )r   r   )r%   r&   r   r'   )r   r&   )r4   r   r   r&   )r=   r&   r   r   )r=   r&   r+   r   r   rE   )r=   r&   r   rE   )r   rP   )r   r   )r4   r   r   r   )r[   r\   )__name__
__module____qualname____doc__r   r   r.   r3   r%   rD   rH   rO   rR   rT   rZ   r_   ra   r   r   r   r   r      s+       ' 'R CE     = = = =>   5 5 5 5* * * *(
" 
" 
" 
"" " " ">/ >/ >/ >/@* * * *! ! ! !   :   "    r   N)
__future__r   typingr   r   r   r   r   rdflib.namespacer	   rdflib.termr
   r   rdflib.graphr   __all__r   r   r   r   <module>rl      s    " " " " " " D D D D D D D D D D D D D D             # # # # # # # # #"""""".L L L L L L L L L Lr   