
    i '                    r    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y)	    )annotations)TYPE_CHECKINGIterableIteratorListOptional)RDF)BNodeNode)Graph
Collectionc                  z    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y)r   aV  
    See "Emulating container types":
    https://docs.python.org/reference/datamodel.html#emulating-container-types

    >>> 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``).
    c                H    || _         |xs
 t               | _        |r| |z  } y y N)graphr
   uri)selfr   r   seqs       @C:\Projects\mas-dev\.venv\Lib\site-packages\rdflib/collection.py__init__zCollection.__init__9   s'    
>%'CKD     c                l    ddj                  | D cg c]  }|j                          c}      z  S c c}w )a  
        >>> 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 ) )joinn3)r   is     r   r   zCollection.n3?   s0    4 388T$:TQTTVT$:;<<$:s   1
c                    t        |t              sJ | j                  }| j                  }d}||k  r0|dz  }|j	                  |t
        j                        }|	 |S ||k  r0|S )z+Gets the first, rest holding node at index.r      )
isinstanceintr   r   valuer	   rest)r   indexr   	containerr   s        r   _get_containerzCollection._get_container[   sm    %%%%

$(HH	%iFAIsxx8I  %i
 r   c                p    t        t        | j                  j                  | j                                    S )zlength of items in collection.)lenlistr   itemsr   r   s    r   __len__zCollection.__len__h   s%    4

((2344r   c                   | j                   }d}	 |t        j                  |f| j                  v r|S t	        | j                  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   r   z is not in zMalformed RDF Collection: %s)r   r	   firstr   r(   objectsr"   nil
ValueError	Exceptionr'   )r   itemlistnamer#   newlinks        r   r#   zCollection.indexl   s     88#))T*djj8tzz11(CHHEF
swwi'$$%IJJ #$BTXX$MNNw<1,W.Ltxx.WW,&qzH r   c                    | j                  |      }|r9| j                  j                  |t        j                        }|r|S t        |      t        |      )TODO)r%   r   r!   r	   r-   KeyError
IndexError)r   keycvs       r   __getitem__zCollection.__getitem__   sK    $

  CII.Asm#S/!r   c                    | j                  |      }|r-| j                  j                  |t        j                  |f       yt        |      )r6   N)r%   r   setr	   r-   r8   )r   r9   r!   r:   s       r   __setitem__zCollection.__setitem__   s:    $JJNNAsyy%01S/!r   c                (   | |    | j                   }| j                  |      }|sJ t        |       dk(  r|dkD  ry|t        |       dz
  k(  rc| j                  |dz
        }| j                   j                  |t        j
                  t        j                  f       |j                  |ddf       y| j                  |dz         }| j                  |dz
        }|r|sJ |j                  |ddf       |j                  |t        j
                  |f       y)a   
        >>> 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%   r'   r>   r	   r"   r/   remove)r   r9   r   current	priorlinknextpriors          r   __delitem__zCollection.__delitem__   s    V 	S	

%%c*wt9>cAgCIM!++C!G4IJJNNIsxx9:LL'4./&&sQw/D''a0EE!>LL'4./IIuchh-.r   c                L    | j                   j                  | j                        S )z"Iterator over items in Collections)r   r)   r   r*   s    r   __iter__zCollection.__iter__   s    zz))r   c                    | j                   }	 | j                  j                  |t        j                        }||t        j
                  k(  r|S |}Dr   )r   r   r!   r	   r"   r/   )r   r$   r"   s      r   _endzCollection._end   sG    HH	::##Isxx8D|tsww   	 r   c                   | j                         }|t        j                  k(  rt        d      |t        j                  df| j
                  v r8t               }| j
                  j                  |t        j                  |f       |}| j
                  j                  |t        j                  |f       | j
                  j                  |t        j                  t        j                  f       | S )a  
        >>> 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)
rJ   r	   r/   r0   r-   r   r
   r>   r"   add)r   r2   endnodes       r   appendzCollection.append   s     iik#''>:;;D!TZZ/7DJJNNC401C

SYY-.

SXXsww/0r   c                B   | j                         }|t        j                  k(  rt        d      | j                  j                  |t        j                  d f       |D ]  }|t        j                  d f| j                  v r8t               }| j                  j                  |t        j                  |f       |}| j                  j                  |t        j                  |f        | j                  j                  |t        j                  t        j                  f       | S )NrL   )
rJ   r	   r/   r0   r   rA   r"   r-   r
   rM   )r   otherrN   r2   nxts        r   __iadd__zCollection.__iadd__   s    iik#''>:;;

3$/0DSYY%3g

SXXs34JJNNCD12  	

SXXsww/0r   c                   | j                   }| j                  }|ri|j                  |t        j                        }|j                  |t        j                  d f       |j                  |t        j                  d f       |}|ri| S r   )r   r   r!   r	   r"   rA   r-   )r   r$   r   r"   s       r   clearzCollection.clear  sj    $(HH	

;;y#((3DLL)SYY56LL)SXXt45I	 
 r   N)r   r   r   r   r   z
List[Node])returnstr)r#   r    rW   zOptional[Node])rW   r    )r2   r   rW   r    )r9   r    rW   r   )r9   r    r!   r   rW   None)r9   r    rW   rY   )rW   zIterator[Node])rW   r   )r2   r   rW   r   )rR   zIterable[Node])__name__
__module____qualname____doc__r   r   r%   r+   r#   r<   r?   rF   rH   rJ   rP   rT   rV    r   r   r   r      sQ    (T CE =85*(
""</|*!8"r   N)
__future__r   typingr   r   r   r   r   rdflib.namespacer	   rdflib.termr
   r   rdflib.graphr   __all__r   r^   r   r   <module>re      s-    " D D   #".G Gr   