
    i#                          d dl Z d dlmZ d dlmZ d dlmZmZ g dZ G d d      Z	 G d d	e	      Z
 G d
 de	      Z G d de	      Z G d de      Zy)    N)randint)RDF)BNodeURIRef)	ContainerBagSeqAltNoElementExceptionc                   p    e Zd ZdZg dfdZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zy)r   a{  A class for constructing RDF containers, as per https://www.w3.org/TR/rdf11-mt/#rdf-containers

    Basic usage, creating a ``Bag`` and adding to it::

        >>> from rdflib import Graph, BNode, Literal, Bag
        >>> g = Graph()
        >>> b = Bag(g, BNode(), [Literal("One"), Literal("Two"), Literal("Three")])
        >>> print(g.serialize(format="turtle"))
        @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
        <BLANKLINE>
        [] a rdf:Bag ;
            rdf:_1 "One" ;
            rdf:_2 "Two" ;
            rdf:_3 "Three" .
        <BLANKLINE>
        <BLANKLINE>

        >>> # print out an item using an index reference
        >>> print(b[2])
        Two

        >>> # add a new item
        >>> b.append(Literal("Hello")) # doctest: +ELLIPSIS
        <rdflib.container.Bag object at ...>
        >>> print(g.serialize(format="turtle"))
        @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
        <BLANKLINE>
        [] a rdf:Bag ;
            rdf:_1 "One" ;
            rdf:_2 "Two" ;
            rdf:_3 "Three" ;
            rdf:_4 "Hello" .
        <BLANKLINE>
        <BLANKLINE>

    r   c                    || _         |xs
 t               | _        d| _        || _        | j                  |       | j                   j                  | j                  t        j                  t        | j                     f       y)zCreates a Container

        :param graph: a Graph instance
        :param uri: URI or Blank Node of the Container
        :param seq: the elements of the Container
        :param rtype: the type of Container, one of "Bag", "Seq" or "Alt"
        r   N)	graphr   uri_len_rtypeappend_multipleaddr   type)selfr   r   seqrtypes        ?C:\Projects\mas-dev\.venv\Lib\site-packages\rdflib/container.py__init__zContainer.__init__0   s^     
>%'	S! 	

#((C,<=>    c                     g }t        t        |             D ]  }| |dz      }|j                  |        ddj                  |D cg c]  }|j	                          c}      z  S c c}w )N   z( %s ) )rangelenappendjoinn3)r   itemsivas        r   r"   zContainer.n3C   s`    s4y!AQUALLO " #((E#:EqADDFE#:;;;#:s   A%
c                     | j                   S )z Returns the URI of the container)r   r   s    r   _get_containerzContainer._get_containerK   s     xxr   c                     | j                   S )zNumber of items in container)r   r(   s    r   __len__zContainer.__len__P   s     yyr   c                 R    t        j                  dt        d       | j                  S )Nzardflib.container.Container.type_of_conatiner is deprecated. Use type_of_container method instead.   )
stacklevel)warningswarnDeprecationWarningr   r(   s    r   type_of_conatinerzContainer.type_of_conatinerU   s%    4		
 {{r   c                     | j                   S N)r   r(   s    r   type_of_containerzContainer.type_of_container^   s    {{r   c                     | j                   j                  | j                  |      }|st        |dd      t	        t        t              dz         }d}|D ]  }t        |j                  |d            } |S )z@Returns the 1-based numerical index of the item in the containerz is not in 	container_N )	r   
predicatesr   
ValueErrorr   strr   intreplace)r   itempredli_indexr$   ps         r   indexzContainer.indexa   sl     zz$$TXXt4$DEE#c(S.)AAIIh+,A r   c                 &   | j                         }t        |t              sJ t        t              dz   t        |      z   }|dk  s|t        |       kD  rt        |      | j                  j                  |t        |            }|r|S t        |      )z*Returns item of the container at index keyr8   r   )
r)   
isinstancer=   r<   r   r   KeyErrorr   valuer   )r   keycelem_urir%   s        r   __getitem__zContainer.__getitem__n   s     !#s###s8c>CH,!8sSY3-JJQx 01H3-r   c                    t        |t              sJ | j                         }t        t              dz   t        |      z   }|dk  s|t        |       kD  rt        |      | j                  j                  |t        |      |f       y)zJSets the item at index key or predicate rdf:_key of the container to valuer8   r   N)
rE   r=   r)   r<   r   r   rF   r   setr   )r   rH   rG   rI   rJ   s        r   __setitem__zContainer.__setitem__}   so     #s###!s8c>CH,!8sSY3-

6(+U34r   c                    t        |t              sJ |dk  s|t        |       kD  rt        |      | j                  }| j
                  }t        t              dz   t        |      z   }|j                  |t        |      df       t        |dz   t        |       dz         D ]  }t        t              dz   t        |      z   }|j                  |t        |            }|j                  |t        |      |f       t        t              dz   t        |dz
        z   }|j                  |t        |      |f        | xj                  dz  c_        y)z6Removing the item with index key or predicate rdf:_keyr   r8   Nr   )rE   r=   r   rF   r   r   r<   r   remover   r   rG   r   r   )r   rH   r   r7   rJ   jr%   s          r   __delitem__zContainer.__delitem__   s    #s###!8sSY3-

HH	s8c>CH,i!1489sQwD	A.A3x#~A.HIvh'78ALL)VH%5q9:3x#~AE
2HIIy&"2A67 / 			Q	r   c                    g }| j                   }d}	 t        t              dz   t        |      z   }|t        |      df| j                  v r:|dz  }|j                  | j                  j                  |t        |                   n	 |S v)z,Returns a list of all items in the containerr   r8   N)r   r<   r   r   r   r    rG   )r   l_r7   r$   rJ   s        r   r#   zContainer.items   s     HH	3x#~A.H6(+T2djj@Q		$****9fX6FGH	 r   c                     | j                   }d}	 t        t              dz   t        |      z   }|t        |      d f| j                  v r|dz  }n|dz
  S D)Nr   r8   )r   r<   r   r   r   )r   r7   r$   rJ   s       r   endzContainer.end   sZ     HH	3x#~A.H6(+T2djj@Q1u r   c                     | j                         }t        t              dz   t        |dz         z   }| j                  }| j                  j                  |t        |      |f       | xj                  dz  c_        | S )z'Adding item to the end of the containerr8   r   )rV   r<   r   r   r   r   r   r   )r   r?   rV   rJ   r7   s        r   r    zContainer.append   s`     hhjs8c>CaL0HH	

	6(#3T:;		Q	r   c                 
   | j                         }| j                  }|D ]a  }|dz  }| xj                  dz  c_        t        t              dz   t        |      z   }| j
                  j                  |t        |      |f       c | S )zSAdding multiple elements to the container to the end which are in python list otherr   r8   )rV   r   r   r<   r   r   r   r   )r   otherrV   r7   r?   rJ   s         r   r   zContainer.append_multiple   sq     hhjHH	D1HCIINI3x#~C0HJJNNIvh'7>?	  r   c                    | j                   }| j                  }d}	 t        t              dz   t        |      z   }|t	        |      df| j                  v r#|j                  |t	        |      df       |dz  }nn]d| _        | S )z(Removing all elements from the containerr   r8   Nr   )r   r   r<   r   r   rP   r   )r   r7   r   r$   rJ   s        r   clearzContainer.clear   s     HH	

3x#~A.H6(+T2djj@i)94@AQ  	r   N)__name__
__module____qualname____doc__r   r"   r)   r+   r2   r5   rC   rK   rN   rR   r#   rV   r    r   r[    r   r   r   r   
   sZ    #J (* ?&<

 
5( 	r   r   c                       e Zd ZdZg fdZy)r   z5Unordered container (no preference order of elements)c                 6    t         j                  | |||d       y )Nr   r   r   r   r   r   r   s       r   r   zBag.__init__       4S%8r   N)r\   r]   r^   r_   r   r`   r   r   r   r      s    ?') 9r   r   c                       e Zd Zg fdZd Zy)r
   c                 6    t         j                  | |||d       y )Nr
   rc   rd   s       r   r   zAlt.__init__   re   r   c                     t        |       dk(  r
t               t        dt        |             }| j                  |      }|S )Nr   r   )r   r   r   rK   )r   rB   r?   s      r   anyonez
Alt.anyone   s;    t9>$&&3t9%A##A&DKr   N)r\   r]   r^   r   ri   r`   r   r   r
   r
      s    ') 9r   r
   c                       e Zd Zg fdZd Zy)r	   c                 6    t         j                  | |||d       y )Nr	   rc   rd   s       r   r   zSeq.__init__   re   r   c                 (   t        |t              sJ |dk  s|t        |       dz   kD  rt        d      |t        |       dz   k(  r| j	                  |       | S t        t        |       |dz
  d      D ]  }| j                         }t        t              dz   t        |      z   }| j                  j                  |t        |            }| j                  j                  |t        |      |f       t        t              dz   t        |dz         z   }| j                  j                  |t        |      |f        t        t              dz   t        |      z   }| j                  j                  t        |      |f       | xj                  dz  c_        | S )Nr   r   z1Invalid Position for inserting element in rdf:Seqr8   )rE   r=   r   r;   r    r   r)   r<   r   r   rG   r   rP   r   r   )r   posr?   rQ   r7   rJ   r%   elem_uri_poss           r   add_at_positionzSeq.add_at_position   sM   #s###!8sSY]*PQQ#d)a-KK  3t9cAgr2 //1	s8c>CF2JJ$$Yx0@A

!!9fX.>"BCs8c>CAJ6

	6(+;Q?@ 3 s8c>CH4LJJNNIvl';TBCIINIr   N)r\   r]   r^   r   rp   r`   r   r   r	   r	      s    ') 9r   r	   c                       e Zd ZddZd Zy)r   c                     || _         y r4   message)r   rt   s     r   r   zNoElementException.__init__  s	    r   c                     | j                   S r4   rs   r(   s    r   __str__zNoElementException.__str__  s    ||r   N)zrdf:Alt Container is empty)r\   r]   r^   r   rv   r`   r   r   r   r     s    r   r   )r/   randomr   rdflib.namespacer   rdflib.termr   r   __all__r   r   r
   r	   	Exceptionr   r`   r   r   <module>r|      sS        %
BW Wt9) 9
) 
) 2 r   