
    01inu                       % S SK Jr  S SKJrJrJrJrJrJrJ	r	J
r
JrJrJrJr  S SKJr  S SKJr  \(       a(  S SKJrJrJrJrJrJrJr  S SKJrJr  S SKJr  S SK J!r!J"r"  S	S
/r#Sr$S\%S'    " S S	\5      r& " S S
\5      r'g)    )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                  $  ^  \ rS rSrSr  S   SU 4S jjjr S       SS jjr S     SS jjr S     SS jjrSSS jjr	SSS jjr
SS	 jrSS
 jrSS jrSS jr            SU 4S jjr            SU 4S jjrSrU =r$ ) 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                ~   > [         [        U ]  U5        X l        0 U l        0 U l        0 U l        0 U l        0 U l        g N)	superr   __init__
identifier_SimpleMemory__spo_SimpleMemory__pos_SimpleMemory__osp_SimpleMemory__namespace_SimpleMemory__prefixselfconfigurationr(   	__class__s      V/home/james-whalen/.local/lib/python3.13/site-packages/rdflib/plugins/stores/memory.pyr'   SimpleMemory.__init__4   sN    
 	lD*=9$  	
  	
  	
 /1+-    c                F   Uu  pEnU R                   n Xt   n X   n	SX'   U R                  n
 X   n X   nSX'   U R                  n X   n X   nSX'   g!   0 =oU'    NH= f!   0 =oU'    NP= f!   0 =oU'    NH= f!   0 =oU'    NP= f!   0 =oU'    NH= f!   0 =oU'    NP= f)%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                   r2   addSimpleMemory.addN   s     &,"Fjj	#B	#A 	jj	%B	 
A 
jj	"B	!A 9	# ""BW	# ""A9	%"$$BY	 A6
	"!!BV	!  A7sD   A A" A/ A< B	 B A"A,/A9<B	BB c                    [        U R                  U5      5       HA  u  u  p4pVU R                  U   U   U	 U R                  U   U   U	 U R                  U   U   U	 MC     g r%   )listtriplesr)   r*   r+   )r/   triple_patternr9   r;   r<   r=   cs          r2   removeSimpleMemory.removez   sg    
 04DLL4P/Q+(W

7#I.v6

9%f-g6

6"7+I6 0Rr4   c              #    #    Uu  p4nU[         :w  a  U R                  nX6;   a  Xc   nU[         :w  a`  XG;   aZ  U[         :w  a   XWU   ;   a  X4U4U R                  5       4v   ggXt   R                  5        H  nX4U4U R                  5       4v   M     ggUR                  5        H^  n	U[         :w  a"  XWU	   ;   a  X9U4U R                  5       4v   M-  M/  Xy   R                  5        H  nX9U4U R                  5       4v   M     M`     ggU[         :w  a  U R                  n
XJ;   a  X   nU[         :w  a6  X[;   a0  X   R                  5        H  nXU4U R                  5       4v   M     ggUR                  5        H2  nX   R                  5        H  nXU4U R                  5       4v   M     M4     ggU[         :w  a]  U R
                  nX];   aK  X   nUR                  5        H2  nX   R                  5        H  n	XU4U R                  5       4v   M     M4     ggU R                  nUR                  5        HM  nXl   nUR                  5        H2  n	Xy   R                  5        H  nXU4U R                  5       4v   M     M4     MO     g7fz)A generator over all the triples matchingN)r!   r)   _SimpleMemory__contextskeysr*   r+   )r/   rL   r9   r;   r<   r=   r>   subjectDictionaryr@   rF   rA   predicateDictionaryrC   rD   objectDictionarys                  r2   rK   SimpleMemory.triples   su     &4"Fc>**C~$'L!# 5!S=%9)EE'.6&BDOODU&U U $%6%A%F%F%H'.1&=t?P&P P &I .335!S=%1)=='.6&:DOO<M&M M $%6%9%>%>%@'.1ot7H&H H &A 6 #**C&)n#S=4!4!<!A!A!CA#$"8$//:K"KK "D 0557!4!7!<!<!>A#$"3T__5F"FF "? 8   s]**C}#&; )..0A-0557 Vndoo.??? 8 1  **CXXZ$'F!*//1A.1668 Qi)::: 9 2  s   I?Jc                D    SnU R                  S5       H  nUS-  nM
     U$ )Nr   NNNr7   )rK   )r/   r9   ir8   s       r2   __len__SimpleMemory.__len__   s)    ll#56FFA 7r4   c                   U R                   R                  U5      n[        U R                  R                  U5      U R                  R                  U5      5      nU(       a=  Ub  U R                   U	 Ub  U R                  U	 XR                  U'   X R                   U'   g [        XQS9U R                  [        XB5      '   [        XBS9U R                   [        XQ5      '   g N)default)r,   getr   r-   r/   prefix	namespaceoverridebound_namespacebound_prefixs         r2   bindSimpleMemory.bind   s    **..v6 MMi(MMo.

 '$$\2*MM/2'-MM)$'0V$ DMDDMM)O?@ AJADY|<=r4   c                :    U R                   R                  US 5      $ r%   )r,   r`   r/   rb   s     r2   rc   SimpleMemory.namespace       ##FD11r4   c                :    U R                   R                  US 5      $ r%   )r-   r`   r/   rc   s     r2   rb   SimpleMemory.prefix       }}  D11r4   c              #  \   #    U R                   R                  5        H
  u  pX4v   M     g 7fr%   )r,   itemsr/   rb   rc   s      r2   
namespacesSimpleMemory.namespaces   *     !%!1!1!7!7!9F## ":   *,c                    S S 5       $ )Nc              3  $   #    U  H  ov   M     g 7fr%    ).0rM   s     r2   	<genexpr>*SimpleMemory.__contexts.<locals>.<genexpr>   s     2a2   rz   rz   )r/   s    r2   
__contextsSimpleMemory.__contexts   s     2r4   c                2   > [         [        U ]
  " XX440 UD6  g r%   )r&   r   queryr/   r   initNsinitBindings
queryGraphkwargsr1   s         r2   r   SimpleMemory.query   s!     	lD'<	
7=	
r4   c                2   > [         [        U ]
  " XX440 UD6  g r%   )r&   r   updater/   r   r   r   r   r   r1   s         r2   r   SimpleMemory.update   s!     	lD(L	
8>	
r4   )__namespace__osp__pos__prefix__spor(   NNr0   Optional[str]r(   zOptional[Identifier]Fr8   r   r9   r   r:   boolreturnr    r%   rL   r   r9   Optional[_ContextType]r   r    )rL   r   r9   r   r   z>Iterator[Tuple[_TripleType, Iterator[Optional[_ContextType]]]]r9   r   r   intTrb   strrc   r   rd   r   r   r    rb   r   r   zOptional[URIRef]rc   r   r   r   r   zIterator[Tuple[str, URIRef]])r   #Generator[_ContextType, None, None])r   Union[Query, str]r   Mapping[str, Any]r   Mapping[str, Identifier]r   r   r   r   r   r   )r   zUnion[Update, str]r   r   r   r   r   r   r   r   r   r    )__name__
__module____qualname____firstlineno____doc__r'   rG   rN   rK   r[   rg   rc   rb   rt   rR   r   r   __static_attributes____classcell__r1   s   @r2   r   r   *   sL    (,+/.$. ). .< 	** * 	*
 
*^ +/7*7 (7 
	7 +/>;*>; (>; 
H	>;@222$

 

 "

 /	


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

 

 

 


 

r4   c                    ^  \ rS rSrSrSrSrSr  S   SU 4S jjjr S       SS jjr	 S     S S jjr
 S     S!S jjrS"S#S jjrS$S	 jrS%S
 jrS&S jr S   S'S jjrSS(S jjrS)S jrS)S jr          S*S jr S     S+S jjrS,S jrS-S jr\S.S j5       r\S/S j5       rS0S jrS1S jr          S2U 4S jjr          S3U 4S jjrSrU =r$ )4r   i  zAn in memory implementation of a triple store.

Same as SimpleMemory above, but is Context-aware, Graph-aware, and Formula-aware
Authors: Ashley Sommer
Tc                   > [         [        U ]  U5        X l        0 U l        0 U l        0 U l        0 U l        0 U l        0 U l	        0 U l
        S [        5       0U l        [        5       U l        S U l        g 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      r2   r'   Memory.__init__  s~    
 	fd$]3$  	
  	
  	
 /1+-35NPHLce}*-%FJr4   c                   [         R                  " XX#S9  Ub  U R                  R                  U5        Uu  pEnU R                  n Xt   n X   n	 X   n
SnU R                  XX#5        U(       a  gU R                  n X   n X   nSX'   U R                  n X   n UU   nSUU'   g! [         a	    0 =oU'    Nof = f! [         a	    0 =oU'    Nf = f! [
         a	    SX'   Sn Nf = f! [         a	    0 =oU'    Nzf = f! [         a	    0 =oU'    Nf = f! [         a	    0 =nX'    Nf = f! [         a
    0 =nUU'    Nf = f)r6   )r:   NTr7   F)	r   rG   r   r   LookupErrorKeyError_Memory__add_triple_contextr   r   )r/   r8   r9   r:   r;   r<   object_r>   r?   r@   _triple_existsrA   rB   rC   rD   rE   rF   s                     r2   rG   
Memory.add9  s    			$7##G,&,#Gjj	#B	#A	"
A M 	!!&Ijj	%B	!A 
jj	#B	!7A )Q  	# ""BW	#  	# ""A9	#  	"AJ!M	"  	%"$$BY	%  	!  A7	!  	# ""B	#  	!  A7	!s~   B' B= C =C) C? D D+ 'B:9B:=CCC&%C&)C<;C<?DDD('D(+D?>D?c                   U R                  U5      nU R                  XS9 H  u  pEUu  pgnU R                  U5       H  n	Ub  X9:w  a  M  U R                  XI5        M      U R                  USS9n
S U
;   a$  Ub  [	        U
5      S:X  a  U R                  US 5        [	        U R                  U5      5      S:X  d  M  U R
                  U   U   U	 U R                  U   U   U	 U R                  U   U   U	 U R                  U	 M     Ub8  X0R                  ;   a)  [	        U R                  U   5      S:X  a  U R                  U	 US:X  a>  X R                  ;   a.  U R                  (       d  U R                  R                  U5        g g g g )N)r9   T
skipQuotedr7   r   rY   )_Memory__ctx_to_strrK   _Memory__get_context_for_triple_Memory__remove_triple_contextlenr   r   r   r   r   r   graph_awarerN   )r/   rL   r9   req_ctxr8   rM   r;   r<   r   ctxctxss              r2   rN   Memory.removeu  s   
 ##G,nFIF*0'G44V<&7>,,V9 = 00D0IDt|CIN,,VT:40089Q>JJw'	27;JJy)'27;JJw'0;))&1 G  000D))'23q8 %%g. 00...$$ &&w/ % / 1r4   c              #    #    U R                  U5      nUu  pEnUcO  UcL  UcI  X0R                  ;  a  gU R                  U   R                  5        H  nXpR                  U5      4v   M     gUbJ  UbG  UbD  Un U R                  U   U   U   nU R                  Xs5      (       a  XpR                  U5      4v   ggUGba  U R                  n	XI;   GaN  X   n
Ub  XZ;   a  Ub9  XjU   ;   a0  XEU4nU R                  Xs5      (       a  XpR                  U5      4v   ggg[        X   R                  5       5       H3  nXEU4nU R                  Xs5      (       d  M  XpR                  U5      4v   M5     gg[        U
R                  5       5       H  nUb<  XjU   ;   a2  XLU4nU R                  Xs5      (       a  XpR                  U5      4v   M>  M@  MB  [        X   R                  5       5       H3  nXLU4nU R                  Xs5      (       d  M  XpR                  U5      4v   M5     M     ggUb  U R                  nX];   a  X   nUbY  Xn;   aS  [        X   R                  5       5       H3  nXU4nU R                  Xs5      (       d  M  XpR                  U5      4v   M5     gg[        UR                  5       5       HU  n[        X   R                  5       5       H3  nXU4nU R                  Xs5      (       d  M  XpR                  U5      4v   M5     MW     ggUb  U R                  nUU;   ay  UU   n[        UR                  5       5       HV  n[        UU   R                  5       5       H3  nXU4nU R                  Xs5      (       d  M  XpR                  U5      4v   M5     MX     ggU R                  n	[        U	R                  5       5       Hy  nX   n
[        U
R                  5       5       HU  n[        X   R                  5       5       H3  nXU4nU R                  Xs5      (       d  M  XpR                  U5      4v   M5     MW     M{     g! [         a     gf = f7frQ   )r   r   copy_Memory__contextsr   _Memory__triple_has_contextr   rJ   rS   r   r   )r/   rL   r9   r   r;   r<   r   r8   r   r>   rT   r@   rF   rA   rU   rC   rD   rV   s                     r2   rK   Memory.triples  sH     ##G,&4#G ?y0W_333//8==?oof555 @  Y%:w?R $FJJw'	27;,,V== //&"999 >
  **C~$'L!( 5".&I*FF*1g)F#'#<#<V#M#M*0//&2I*I$I $N !%%)*;*F*K*K*M%N*1a)@#'#<#<V#M#M*0//&2I*I$I &O
 !"3"8"8":;".&A*>>*1g)>#'#<#<V#M#M*0//&2I*I$I $N !%%)*;*>*C*C*E%F*1a#'#<#<V#M#M*0//&2I*I$I &G < "**C&)n#&5!%&9&B&G&G&I!JA&'G%<F#88II&,oof.E&E E "K
 !"5":":"<=!%&9&<&A&A&C!DA&'A%6F#88II&,oof.E&E E "E >     **C#~#&w< .3356A!"21"5":":"<="#44VEE"(//&*A"AA > 7  **C#((*%$'F!/4467A!"3"6";";"=>"#44VEE"(//&*A"AA ? 8 &K  sT   A4Q7?P7 6BQB+Q?A4Q7A,Q'BQ6BQ!Q7
QQQQc                   U R                   R                  U5      n[        U R                  R                  U5      U R                  R                  U5      5      nU(       a=  Ub  U R                   U	 Ub  U R                  U	 XR                  U'   X R                   U'   g [        XQS9U R                  [        XB5      '   [        XBS9U R                   [        XQ5      '   g r^   )r   r`   r   r   ra   s         r2   rg   Memory.bind  s    **..v6 MMi(MMo.

 '$$\2*MM/2'-MM)$'0V$ DMDDMM)O?@
 AJADY|<=r4   c                :    U R                   R                  US 5      $ r%   )r   r`   rj   s     r2   rc   Memory.namespace!  rl   r4   c                :    U R                   R                  US 5      $ r%   )r   r`   rn   s     r2   rb   Memory.prefix$  rp   r4   c              #  \   #    U R                   R                  5        H
  u  pX4v   M     g 7fr%   )r   rr   rs   s      r2   rt   Memory.namespaces'  rv   rw   c                    Ub  US:X  a  S [        U R                  5       5       $ Uu  p#n U R                  U   U   U   nU R                  U5      $ ! [         a    S S 5       s $ f = f)NrY   c              3  $   #    U  H  ov   M     g 7fr%   rz   )r{   r9   s     r2   r|   "Memory.contexts.<locals>.<genexpr>/  s     E+DG+Dr~   c              3  $   #    U  H  ov   M     g 7fr%   rz   )r{   r   s     r2   r|   r   6  s     ?r!Arr~   rz   )rJ   r   r   r   r   )r/   r8   subjpredobjr   s         r2   contextsMemory.contexts+  su     >V'99E40C0C+DEE C	#

4 &s+A??6** 	#"r?"	#s   %A A('A(c                t    U R                  U5      nX R                  ;  a  g[        U R                  U   5      $ )Nr   )r   r   r   )r/   r9   r   s      r2   r[   Memory.__len__8  s7    (+++4((-..r4   c                    U R                   (       d  [        R                  " X5        g U R                  R	                  U5        g r%   )r   r   	add_graphr   rG   r/   graphs     r2   r   Memory.add_graph>  s+    OOD(##E*r4   c                    U R                   (       d  [        R                  " X5        g U R                  SU5         U R                  R                  U5        g ! [
         a     g f = f)NrY   )r   r   remove_graphrN   r   r   r   s     r2   r   Memory.remove_graphD  sS    t+KK*E2##**51 s   A 
A%$A%c                   U R                  U5      n[        U5      nU(       a    U R                  U   nXFU'   U(       d  XFS'   O,U(       a  XT0=o`R                  U'   OXTSU0=o`R                  U'   U(       d  U R                  S   R                  U5        XPR                  ;  a  [        5       U R                  U'   U R                  U   R                  U5        U R                  c  X`l        X`R                  :X  a  U R                  U	 gg! [         a,    U R                  R                  5       =o`R                  U'    GNf = f)z;add the given context to the set of contexts for the tripleN)	r   r   r   r   r   r   r   rG   r   )r/   r8   r   r9   r:   r   triple_contexts          r2   __add_triple_contextMemory.__add_triple_contextO  sI    (f!%!6!6v!> #)3'-t$ BEN!6!6v!> &B !6!6v!> !!$'++F3 +++),D!!#&c"&&v. !!)%3"333%%f- 4I  
 **//1!6!6v!>	s   D
 
2E ?E c                    U R                   R                  XR                  5      nU(       d  UR                  5       $ UR	                  5        VVs/ s H  u  pEU(       a  M  UPM     snn$ s  snnf )z\return a list of contexts (str) for the triple, skipping
quoted contexts if skipQuoted==True)r   r`   r   rS   rr   )r/   r8   r   r   r   r:   s         r2   __get_context_for_tripleMemory.__get_context_for_triple  sS     $$((1G1GH99; (,zz|B|6|BBBs   A+!A+c                P    X R                   R                  XR                  5      ;   $ )z5return True if the triple exists in the given context)r   r`   r   )r/   r8   r   s      r2   __triple_has_contextMemory.__triple_has_context  s$     ++//8N8NOOOr4   c                    U R                   R                  XR                  5      R                  5       nX2	 X0R                  :X  a  U R                   U	 OX0R                   U'   U R                  U   R                  U5        g)z"remove the context from the tripleN)r   r`   r   r   r   rN   )r/   r8   r   r   s       r2   __remove_triple_contextMemory.__remove_triple_context  so     $$((1G1GHMMOI)))%%f-,0!!&)c"))&1r4   c                    g r%   rz   r/   r   s     r2   __ctx_to_strMemory.__ctx_to_str  s    69r4   c                    g r%   rz   r   s     r2   r   r    s    /2r4   c                   Uc  g  SR                  UR                  R                  R                  UR                  5      nXR                  U'   U$ ! [
         al    [        U[        5      (       aK  SR                  UR                  R                  U5      nX R                  ;   a  Us $ XR                  U'   Us $ [        S5      ef = f)Nz{}:{}z1Cannot use that type of object as a Graph context)	formatr(   r1   r   r   AttributeError
isinstancer   RuntimeError)r/   r   ctx_strs      r2   r   r    s    ;	TnnS^^%=%=%F%FWG.1""7+N 	T #s##!..)?)?E444"N25&&w/RSS	Ts   A	A AC(C:Cc                8   ^  U 4S jT R                  USS9 5       $ )z_return a generator for all the non-quoted contexts
(dereferenced) the encoded triple appears inc              3  d   >#    U  H%  nUc  M  TR                   R                  X5      v   M'     g 7fr%   )r   r`   )r{   r  r/   s     r2   r|   $Memory.__contexts.<locals>.<genexpr>  s1      
Q 9D""&&w88Qs   0"0Tr   )r   )r/   r8   s   ` r2   r   Memory.__contexts  s%    
88D8Q
 	
r4   c                2   > [         [        U ]
  " XX440 UD6  g r%   )r&   r   r   r   s         r2   r   Memory.query  s     	fd!%TVTr4   c                2   > [         [        U ]
  " XX440 UD6  g r%   )r&   r   r   r   s         r2   r   Memory.update  s     	fd"6<VvVr4   )__all_contexts__contextTriples__context_obj_map__defaultContextsr   r   r   r   r   __tripleContextsr(   r   r   r   r   r%   r   )rL   r   r9   r   r   zXGenerator[Tuple[_TripleType, Generator[Optional[_ContextType], None, None]], None, None]r   r   r   r   r   )r8   zOptional[_TripleType]r   r   r   )r   r   r   r    )
r8   r   r   r   r9   r   r:   r   r   r    )r8   r   r   r   r   zCollection[Optional[str]])r8   r   r   r   r   r   )r8   r   )r   r   r   r   )r   r    r   r    )r   r   r   r   )r8   r   r   r   )
r   r   r   r   r   r   r   r   r   r   )
r   zUnion[Update, Any]r   r   r   r   r   r   r   r    )r   r   r   r   r   context_awareformula_awarer   r'   rG   rN   rK   rg   rc   rb   rt   r   r[   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s   @r2   r   r     s    MMK (,+/K$K )K KJ 	:: : 	:
 
:~ +/$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r4   )(
__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   rz   r4   r2   <module>r"     sv    #     !   ;#.8
$T `
5 `
FNWU NWr4   