
    iu                        U d dl mZ d dlmZmZmZmZmZmZm	Z	m
Z
mZmZmZmZ d dlmZ d dlmZ er(d dlmZmZmZmZmZmZmZ d dlmZmZ d dlmZ d dl m!Z!m"Z" d	d
gZ#dZ$de%d<    G d d	e      Z& G d d
e      Z'y)    )annotations)TYPE_CHECKINGAny
CollectionDict	GeneratorIteratorMappingOptionalSetTupleUnionoverload)Store)	_coalesce)Graph_ContextType_ObjectType_PredicateType_SubjectType_TriplePatternType_TripleType)QueryUpdate)Result)
IdentifierURIRefSimpleMemoryMemoryNNoneANYc                       e Zd ZdZ	 	 d	 	 	 d fdZ	 d	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 ddZ	 d	 	 	 	 	 ddZdddZdddZ	ddZ
dd	Zdd
ZddZ	 	 	 	 	 	 	 	 	 	 	 	 d fdZ	 	 	 	 	 	 	 	 	 	 	 	 d fdZ xZS )r   a)      A fast naive in memory implementation of a triple store.

    This triple store uses nested dictionaries to store triples. Each
    triple is stored in two such indices as follows spo[s][p][o] = 1 and
    pos[p][o][s] = 1.

    Authors: Michel Pelletier, Daniel Krech, Stefan Niederhauser
    c                    t         t        |   |       || _        i | _        i | _        i | _        i | _        i | _        y N)	superr   __init__
identifier_SimpleMemory__spo_SimpleMemory__pos_SimpleMemory__osp_SimpleMemory__namespace_SimpleMemory__prefixselfconfigurationr'   	__class__s      KC:\Projects\mas-dev\.venv\Lib\site-packages\rdflib/plugins/stores/memory.pyr&   zSimpleMemory.__init__5   sP    
 	lD*=9$  	
  	
  	
 /1+-    c                f   |\  }}}| j                   }	 ||   }	 ||   }	d|	|<   | j                  }
	 |
|   }	 ||   }d||<   | j                  }	 ||   }	 ||   }d||<   y#  i x}||<   Y QxY w#  i x}	||<   Y YxY w#  i x}|
|<   Y PxY w#  i x}||<   Y XxY w#  i x}||<   Y OxY w#  i x}||<   Y WxY w)6        Add a triple to the store of triples.
           N)r(   r)   r*   )r.   triplecontextquotedsubject	predicateobjectspopooposossospspps                   r1   addzSimpleMemory.addO   s    &,"Fjj	#WB	#9A &	jj	%YB	 6
A '
jj	"VB	!7A )9	# ""BW	# ""A9	%"$$BY	 A6
	"!!BV	!  A7sD   A A- A; B	 B B% 	A*-	A8;	B		B	B"%	B0c                    t        | j                  |            D ]B  \  \  }}}}| j                  |   |   |= | j                  |   |   |= | j                  |   |   |= D y r$   )listtriplesr(   r)   r*   )r.   triple_patternr7   r9   r:   r;   cs          r1   removezSimpleMemory.remove}   sk    
 04DLL4P/Q+(Wi!

7#I.v6

9%f-g6

6"7+I6 0Rr2   c              #    K   |\  }}}|t         k7  r| j                  }||v r||   }|t         k7  r^||v rY|t         k7  r |||   v r|||f| j                         f yy||   j                         D ]  }|||f| j                         f  yy|j                         D ]Z  }	|t         k7  r |||	   v r||	|f| j                         f +,||	   j                         D ]  }||	|f| j                         f  \ yy|t         k7  r| j                  }
||
v r|
|   }|t         k7  r5||v r0||   j                         D ]  }|||f| j                         f  yy|j                         D ]1  }||   j                         D ]  }|||f| j                         f  3 yy|t         k7  r[| j
                  }||v rJ||   }|j                         D ]1  }||   j                         D ]  }	||	|f| j                         f  3 yy| j                  }|j                         D ]K  }||   }|j                         D ]1  }	||	   j                         D ]  }||	|f| j                         f  3 M ywz)A generator over all the triples matchingN)r!   r(   _SimpleMemory__contextskeysr)   r*   )r.   rI   r7   r9   r:   r;   r<   subjectDictionaryr>   rD   r?   predicateDictionaryrA   rB   objectDictionarys                  r1   rH   zSimpleMemory.triples   s     &4"Fc>**C#~$'L!# $55!S=%):9)EE'.	6&BDOODU&U U $%6y%A%F%F%H'.	1&=t?P&P P &I .335!S=%):1)=='.6&:DOO<M&M M $%6q%9%>%>%@'.1ot7H&H H &A 6 #**CC&))n#S=!44!4V!<!A!A!CA#$i"8$//:K"KK "D 0557!4Q!7!<!<!>A#$i"3T__5F"FF "? 8   s]**C}#&v; )..0A-a0557 !Vndoo.??? 8 1  **CXXZ$'F!*//1A.q1668 !Qi)::: 9 2  s   I,I.c                @    d}| j                  d      D ]  }|dz  }	 |S )Nr   NNNr5   )rH   )r.   r7   ir6   s       r1   __len__zSimpleMemory.__len__   s)    ll#56FFA 7r2   c                   | j                   j                  |      }t        | j                  j                  |      | j                  j                  |            }|r=|| j                   |= || j                  |= || j                  |<   || j                   |<   y t        ||      | j                  t        ||      <   t        ||      | j                   t        ||      <   y N)default)r+   getr   r,   r.   prefix	namespaceoverridebound_namespacebound_prefixs         r1   bindzSimpleMemory.bind   s    **..v6 MMi(MMo.

 '$$\2*MM/2'-DMM)$'0DV$ DMfDDMM)OY?@ AJADY|V<=r2   c                :    | j                   j                  |d       S r$   )r+   rZ   r.   r\   s     r1   r]   zSimpleMemory.namespace       ##FD11r2   c                :    | j                   j                  |d       S r$   )r,   rZ   r.   r]   s     r1   r\   zSimpleMemory.prefix       }}  D11r2   c              #  \   K   | j                   j                         D ]  \  }}||f  y wr$   )r+   itemsr.   r\   r]   s      r1   
namespaceszSimpleMemory.namespaces   /     !%!1!1!7!7!9FI)## ":   *,c                    d dD        S )Nc              3      K   | ]  }|  y wr$    ).0rJ   s     r1   	<genexpr>z*SimpleMemory.__contexts.<locals>.<genexpr>   s     2a2   rp   rp   )r.   s    r1   
__contextszSimpleMemory.__contexts   s     2r2   c                4    t        t        | 
  ||||fi | y r$   )r%   r   queryr.   rv   initNsinitBindings
queryGraphkwargsr0   s         r1   rv   zSimpleMemory.query   s%     	lD'6<	
7=	
r2   c                4    t        t        | 
  ||||fi | y r$   )r%   r   updater.   r}   rx   ry   rz   r{   r0   s         r1   r}   zSimpleMemory.update  s%     	lD(FL*	
8>	
r2   NNr/   Optional[str]r'   zOptional[Identifier]Fr6   r   r7   r   r8   boolreturnr    r$   rI   r   r7   Optional[_ContextType]r   r    )rI   r   r7   r   r   z>Iterator[Tuple[_TripleType, Iterator[Optional[_ContextType]]]]r7   r   r   intTr\   strr]   r   r^   r   r   r    r\   r   r   zOptional[URIRef]r]   r   r   r   r   zIterator[Tuple[str, URIRef]])r   #Generator[_ContextType, None, None])rv   Union[Query, str]rx   Mapping[str, Any]ry   Mapping[str, Identifier]rz   r   r{   r   r   r   )r}   zUnion[Update, str]rx   r   ry   r   rz   r   r{   r   r   r    )__name__
__module____qualname____doc__r&   rE   rK   rH   rV   ra   r]   r\   rk   rN   rv   r}   __classcell__r0   s   @r1   r   r   *   sG    (,+/.$. ).< 	,, , 	,
 
,b +/7*7 (7 
	7 +/>;*>; (>; 
H	>;@222$

 

 "

 /	


 

 

 




"

 "

 /	


 

 

 


 

r2   c                      e Zd ZdZdZdZdZ	 	 d	 	 	 d fdZ	 d	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 ddZ		 d	 	 	 	 	 ddZ
d d!dZd"dZd#d	Zd$d
Z	 d	 	 	 d%dZdd&dZd'dZd'dZ	 	 	 	 	 	 	 	 	 	 d(dZ	 d	 	 	 	 	 d)dZd*dZd+dZed,d       Zed-d       Zd.dZd/dZ	 	 	 	 	 	 	 	 	 	 d0 fdZ	 	 	 	 	 	 	 	 	 	 d1 fdZ xZS )2r   z    An in memory implementation of a triple store.

    Same as SimpleMemory above, but is Context-aware, Graph-aware, and Formula-aware
    Authors: Ashley Sommer
    Tc                    t         t        |   |       || _        i | _        i | _        i | _        i | _        i | _        i | _	        i | _
        d t               i| _        t               | _        d | _        y r$   )r%   r   r&   r'   _Memory__spo_Memory__pos_Memory__osp_Memory__namespace_Memory__prefix_Memory__context_obj_map_Memory__tripleContextsset_Memory__contextTriples_Memory__all_contexts_Memory__defaultContextsr-   s      r1   r&   zMemory.__init__  s    
 	fd$]3$  	
  	
  	
 /1+-35NPHLce}*-%FJr2   c                   t        j                  | |||       || j                  j                  |       |\  }}}| j                  }	 ||   }	 ||   }		 |	|   }
d}| j                  ||||       |ry| j                  }	 ||   }	 ||   }d||<   | j                  }	 ||   }	 ||   }d||<   y# t        $ r
 i x}||<   Y rw xY w# t        $ r
 i x}	||<   Y w xY w# t
        $ r
 d|	|<   d}Y w xY w# t        $ r
 i x}||<   Y }w xY w# t        $ r
 i x}||<   Y w xY w# t        $ r
 i x}||<   Y w xY w# t        $ r
 i x}||<   Y w xY w)r4   )r8   NTr5   F)	r   rE   r   r   LookupErrorKeyError_Memory__add_triple_contextr   r   )r.   r6   r7   r8   r9   r:   object_r<   r=   r>   _triple_existsr?   r@   rA   rB   rC   rD   s                     r1   rE   z
Memory.add=  s    			$7##G,&,#Gjj	#WB	#9A	"'
A M 	!!&-&Ijj	%YB	!7A '
jj	#WB	!7A )Q  	# ""BW	#  	# ""A9	#  	"AgJ!M	"  	%"$$BY	%  	!  A7	!  	# ""BW	#  	!  A7	!s~   
B/ C C C1 D D $D3 /CCCCC.-C.1DDDDD0/D03EEc                   | j                  |      }| j                  ||      D ]  \  }}|\  }}}| j                  |      D ]  }	|||	k7  r| j                  ||	        | j                  |d      }
d |
v r"|t	        |
      dk(  r| j                  |d        t	        | j                  |            dk(  s| j
                  |   |   |= | j                  |   |   |= | j                  |   |   |= | j                  |=  |6|| j                  v r(t	        | j                  |         dk(  r| j                  |= |dk(  r8|| j                  v r)| j                  s| j                  j                  |       y y y y )N)r7   T
skipQuotedr5   r   rT   )_Memory__ctx_to_strrH   _Memory__get_context_for_triple_Memory__remove_triple_contextlenr   r   r   r   r   r   graph_awarerK   )r.   rI   r7   req_ctxr6   rJ   r9   r:   r   ctxctxss              r1   rK   zMemory.remove{  s   
 ##G,ngFIFA*0'GY44V<&7c>,,VS9 = 00D0IDt|CIN,,VT:40089Q>JJw'	27;JJy)'27;JJw'0;))&1 G  4000D))'23q8 %%g. 004...$$ &&w/ % / 1r2   c              #    K   | j                  |      }|\  }}}|K|I|G|| j                  vry| j                  |   j                         D ]  }|| j                  |      f  y|E|C|A|}	 | j                  |   |   |   }| j                  ||      r|| j                  |      f yy|K| j                  }	||	v r8|	|   }
|||
v r|6||
|   v r.|||f}| j                  ||      r|| j                  |      f yyt        |
|   j                               D ]/  }|||f}| j                  ||      s|| j                  |      f 1 yyt        |
j                               D ]  }|6||
|   v r.|||f}| j                  ||      s$|| j                  |      f :;t        |
|   j                               D ]/  }|||f}| j                  ||      s|| j                  |      f 1  yyy|| j                  }||v r||   }|T||v rOt        ||   j                               D ]/  }|||f}| j                  ||      s|| j                  |      f 1 yyt        |j                               D ]P  }t        ||   j                               D ]/  }|||f}| j                  ||      s|| j                  |      f 1 R yy|| j                  }||v rr||   }t        |j                               D ]P  }t        ||   j                               D ]/  }|||f}| j                  ||      s|| j                  |      f 1 R yy| j                  }	t        |	j                               D ]s  }|	|   }
t        |
j                               D ]P  }t        |
|   j                               D ]/  }|||f}| j                  ||      s|| j                  |      f 1 R u y# t        $ r Y yw xY wwrM   )r   r   copy_Memory__contextsr   _Memory__triple_has_contextr   rG   rO   r   r   )r.   rI   r7   r   r9   r:   r   r6   r   r<   rP   r>   rD   r?   rQ   rA   rB   rR   s                     r1   rH   zMemory.triples  s     ##G,&4#G ?y0W_d333//8==?doof555 @  Y%:w?R $FJJw'	27;,,VW= $//&"999 >
  **C#~$'L!( $55".&*;I*FF*19g)F#'#<#<VW#M*0$//&2I*I$I $%)*;I*F*K*K*M%N*19a)@#'#<#<VW#M*0$//&2I*I$I &O
 !"3"8"8":;".&*;A*>>*11g)>#'#<#<VW#M*0$//&2I*I$I $%)*;A*>*C*C*E%F*11a#'#<#<VW#M*0$//&2I*I$I &G < 5 $N6 "**CC&))n#&"55!%&9'&B&G&G&I!JA&'G%<F#88I&,doof.E&E E "K
 !"5":":"<=!%&9!&<&A&A&C!DA&'A%6F#88I&,doof.E&E E "E >     **C#~#&w< .3356A!"21"5":":"<="#Q44VWE"($//&*A"AA > 7  **C#((*%$'F!/4467A!"3A"6";";"=>"#Q44VWE"($//&*A"AA ? 8 &K  s[   A-P0<O8 ,BP<APAP A/PA+P<BPBPP8	PPPPc                   | j                   j                  |      }t        | j                  j                  |      | j                  j                  |            }|r=|| j                   |= || j                  |= || j                  |<   || j                   |<   y t        ||      | j                  t        ||      <   t        ||      | j                   t        ||      <   y rX   )r   rZ   r   r   r[   s         r1   ra   zMemory.bind  s    **..v6 MMi(MMo.

 '$$\2*MM/2'-DMM)$'0DV$ DMfDDMM)OY?@
 AJADY|V<=r2   c                :    | j                   j                  |d       S r$   )r   rZ   rc   s     r1   r]   zMemory.namespace'  rd   r2   c                :    | j                   j                  |d       S r$   )r   rZ   rf   s     r1   r\   zMemory.prefix*  rg   r2   c              #  \   K   | j                   j                         D ]  \  }}||f  y wr$   )r   ri   rj   s      r1   rk   zMemory.namespaces-  rl   rm   c                    ||dk(  rd | j                   D        S |\  }}}	 | j                  |   |   |   }| j                  |      S # t        $ r d dD        cY S w xY w)NrT   c              3      K   | ]  }|  y wr$   rp   )rq   r7   s     r1   rr   z"Memory.contexts.<locals>.<genexpr>5  s     ?+>G+>rs   c              3      K   | ]  }|  y wr$   rp   )rq   r   s     r1   rr   z"Memory.contexts.<locals>.<genexpr><  s     ?r!Arrs   rp   )r   r   r   r   )r.   r6   subjpredobjr   s         r1   contextszMemory.contexts1  sr     >V'99?4+>+>?? dC	#

4 &s+A??6** 	#"r?"	#s   %A AAc                r    | j                  |      }|| j                  vryt        | j                  |         S )Nr   )r   r   r   )r.   r7   r   s      r1   rV   zMemory.__len__>  s9    (d+++4((-..r2   c                    | j                   st        j                  | |       y | j                  j	                  |       y r$   )r   r   	add_graphr   rE   r.   graphs     r1   r   zMemory.add_graphD  s-    OOD%(##E*r2   c                    | j                   st        j                  | |       y | j                  d|       	 | j                  j                  |       y # t
        $ r Y y w xY w)NrT   )r   r   remove_graphrK   r   r   r   s     r1   r   zMemory.remove_graphJ  sU    tU+KK*E2##**51 s   A 	AAc                h   | j                  |      }t        |      }|r	 | j                  |   }|||<   |s1||d<   n+|r||ix}| j                  |<   n||d|ix}| j                  |<   |s| j                  d   j                  |       || j                  vrt               | j                  |<   | j                  |   j                  |       | j                  || _        || j                  k(  r| j                  |= yy# t        $ r- | j                  j                         x}| j                  |<   Y w xY w)z;add the given context to the set of contexts for the tripleN)	r   r   r   r   r   r   r   rE   r   )r.   r6   r   r7   r8   r   triple_contexts          r1   __add_triple_contextzMemory.__add_triple_contextU  s\    (f!%!6!6v!> #)N3'-t$ BEvN!6!6v!> &B !6!6v!> !!$'++F3 d+++),D!!#&c"&&v. !!)%3D"T333%%f- 4I  
 **//1!6!6v!>	s   C; ;2D10D1c                    | j                   j                  || j                        }|s|j                         S |j	                         D cg c]
  \  }}|r	| c}}S c c}}w )zdreturn a list of contexts (str) for the triple, skipping
        quoted contexts if skipQuoted==True)r   rZ   r   rO   ri   )r.   r6   r   r   r   r8   s         r1   __get_context_for_triplezMemory.__get_context_for_triple  sW     $$((1G1GH99; (,zz|B|V6|BBBs   
AAc                R    || j                   j                  || j                        v S )z5return True if the triple exists in the given context)r   rZ   r   )r.   r6   r   s      r1   __triple_has_contextzMemory.__triple_has_context  s(     d++//8N8NOOOr2   c                   | j                   j                  || j                        j                         }||= || j                  k(  r| j                   |= n|| j                   |<   | j                  |   j                  |       y)z"remove the context from the tripleN)r   rZ   r   r   r   rK   )r.   r6   r   r   s       r1   __remove_triple_contextzMemory.__remove_triple_context  sw     $$((1G1GHMMOI4)))%%f-,0D!!&)c"))&1r2   c                     y r$   rp   r.   r   s     r1   __ctx_to_strzMemory.__ctx_to_str  s    69r2   c                     y r$   rp   r   s     r1   r   zMemory.__ctx_to_str  s    /2r2   c                   |y 	 dj                  |j                  j                  j                  |j                        }|| j                  |<   |S # t
        $ rg t        |t              rKdj                  |j                  j                  |      }|| j                  v r|cY S || j                  |<   |cY S t        d      w xY w)Nz{}:{}z1Cannot use that type of object as a Graph context)	formatr'   r0   r   r   AttributeError
isinstancer   RuntimeError)r.   r   ctx_strs      r1   r   zMemory.__ctx_to_str  s    ;	TnnS^^%=%=%F%FWG.1D""7+N 	T #s#!..)?)?Ed444"N25&&w/RSS	Ts   A
A AC !C 4C c                <      fd j                  |d      D        S )zgreturn a generator for all the non-quoted contexts
        (dereferenced) the encoded triple appears inc              3  Z   K   | ]"  }|j                   j                  ||       $ y wr$   )r   rZ   )rq   r   r.   s     r1   rr   z$Memory.__contexts.<locals>.<genexpr>  s2      
Q" ""&&w8Qs   (+Tr   )r   )r.   r6   s   ` r1   rt   zMemory.__contexts  s%    
88D8Q
 	
r2   c                4    t        t        | 
  ||||fi | y r$   )r%   r   rv   rw   s         r1   rv   zMemory.query  s     	fd!%zTVTr2   c                4    t        t        | 
  ||||fi | y r$   )r%   r   r}   r~   s         r1   r}   zMemory.update  s     	fd"66<VvVr2   r   r   r   r   r$   r   )rI   r   r7   r   r   zXGenerator[Tuple[_TripleType, Generator[Optional[_ContextType], None, None]], None, None]r   r   r   r   r   )r6   zOptional[_TripleType]r   r   r   )r   r   r   r    )
r6   r   r   r   r7   r   r8   r   r   r    )r6   r   r   r   r   zCollection[Optional[str]])r6   r   r   r   r   r   )r6   r   )r   r   r   r   )r   r    r   r    )r   r   r   r   )r6   r   r   r   )
rv   r   rx   r   ry   r   rz   r   r   r   )
r}   zUnion[Update, Any]rx   r   ry   r   rz   r   r   r    )r   r   r   r   context_awareformula_awarer   r&   rE   rK   rH   ra   r]   r\   rk   r   rV   r   r   r   r   r   r   r   r   r   rv   r}   r   r   s   @r1   r   r     s    MMK (,+/K$K )KJ 	<< < 	<
 
<B +/$0*$0 ($0 
	$0R +/jB*jB (jB
	jBX422$
 /3#+#	,#/+3.3. 3. (	3.
 3. 
3.l 7<C!C/3C	"CP
	2 9 92 2T*
U U "U /	U
 U 
UW"W "W /	W
 W 
W Wr2   )(
__future__r   typingr   r   r   r   r   r	   r
   r   r   r   r   r   rdflib.storer   rdflib.utilr   rdflib.graphr   r   r   r   r   r   r   rdflib.plugins.sparql.sparqlr   r   rdflib.queryr   rdflib.termr   r   __all__r!   __annotations__r   r   rp   r2   r1   <module>r      sv    #     !   ;#.8
$T c
5 c
LQWU QWr2   