
    4YshGU                     H    d dl mZ ddgZdZ G d de      Z G d de      Zy)    )StoreSimpleMemoryMemoryNc                   v     e Zd ZdZd fd	ZddZddZddZddZd Z	d Z
d	 Zd
 Zd Z fdZ 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      |/var/www/sten-cake5-migrate2.hellocrow.space/lexinfo-master/env/lib/python3.12/site-packages/rdflib/plugins/stores/memory.pyr
   zSimpleMemory.__init__   sC    lD*=9$ 
 
 
    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                   r   addzSimpleMemory.add%   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_patternr   r   r   r   cs          r   removezSimpleMemory.removeN   sm    /3DLL4P/Q 	7+(Wi!

7#I.v6

9%f-g6

6"7+I6	7r   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)ANYr   _SimpleMemory__contextskeysr   r   )r   r-   r   r   r   r   r    subjectDictionaryr"   r(   r#   predicateDictionaryr%   r&   objectDictionarys                  r   r,   zSimpleMemory.triplesT   s    %3"Fc>**C#~$'L!# $55!S=%):9)EE'.	6&BDOODU&U U $%6y%A%F%F%H Q'.	1&=t?P&P PQ .335 I!S=%):1)=='.6&:DOO<M&M M $%6q%9%>%>%@ I'.1ot7H&H HII #**CC&))n#S=!44!4V!<!A!A!C LA#$i"8$//:K"KKL 0557 G!4Q!7!<!<!> GA#$i"3T__5F"FFGG   s]**C}#&v; )..0 @A-a0557 @ !Vndoo.???@@  **CXXZ ;$'F!*//1 ;A.q1668 ; !Qi):::;;;s   I,I.c                 @    d}| j                  d      D ]  }|dz  }	 |S )Nr   NNNr   )r,   )r   r   ir   s       r   __len__zSimpleMemory.__len__   s.    ll#56 	FFA	r   c                 @    || j                   |<   || j                  |<   y r   )r   r   r   prefix	namespaces      r   bindzSimpleMemory.bind        #)i #, r   c                 :    | j                   j                  |d       S r   )r   getr   r>   s     r   r?   zSimpleMemory.namespace       ##FD11r   c                 :    | j                   j                  |d       S r   )r   rC   r   r?   s     r   r>   zSimpleMemory.prefix       }}  D11r   c              #   \   K   | j                   j                         D ]  \  }}||f  y wr   )r   itemsr=   s      r   
namespaceszSimpleMemory.namespaces   4     !%!1!1!7!7!9 	$FI)##	$   *,c                     d dD        S )Nc              3       K   | ]  }|  y wr    ).0r.   s     r   	<genexpr>z*SimpleMemory.__contexts.<locals>.<genexpr>   s     a   rP   rP   )r   s    r   
__contextszSimpleMemory.__contexts   s    2r   c                 4    t        t        | 
  ||||fi | y r   )r	   r   queryr   rV   initNsinitBindings
queryGraphkwargsr   s         r   rV   zSimpleMemory.query   s#    lD'6<	
7=	
r   c                 4    t        t        | 
  ||||fi | y r   )r	   r   updater   r]   rX   rY   rZ   r[   r   s         r   r]   zSimpleMemory.update   s#    lD(FL*	
8>	
r   NNFr   )__name__
__module____qualname____doc__r
   r)   r/   r,   r;   r@   r?   r>   rK   r3   rV   r]   __classcell__r   s   @r   r   r   
   sK     'R7:;x-22$


 
r   c                        e Zd ZdZdZdZdZd fd	ZddZddZ	ddZ
d Zd Zd	 Zd
 ZddZddZd Zd Zd ZddZd Zd Zd Zd Z fdZ fdZ xZS )r   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      r   r
   zMemory.__init__   ss    fd$]3$ 
 
 
!# "!%su!e!%r   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)r   )r   NTr   F)	r   r)   rr   ri   LookupErrorKeyError_Memory__add_triple_contextrj   rk   )r   r   r   r   r   r   object_r    r!   r"   _triple_existsr#   r$   r%   r&   r'   r(   s                     r   r)   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)r   T
skipQuotedr   r   r9   )_Memory__ctx_to_strr,   _Memory__get_context_for_triple_Memory__remove_triple_contextlenri   rj   rk   ro   rq   rr   graph_awarer/   )r   r-   r   req_ctxr   r.   r   r   rx   ctxctxss              r   r/   zMemory.remove  s   ##G,ngF 	2IFA*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	2  4000D))'23q8 %%g. 004...$$ &&w/ % / 1r   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r1   )r~   rq   copy_Memory__contextsri   _Memory__triple_has_contextrv   r+   r4   rj   rk   )r   r-   r   r   r   r   rx   r   ry   r    r5   r"   r(   r#   r6   r%   r&   r7   s                     r   r,   zMemory.triples1  s    ##G,&4#G ?y0W_d333//8==? 6doof5556  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 J*19a)@#'#<#<VW#M*0$//&2I*I$IJ
 !"3"8"8":; J".&*;A*>>*11g)>#'#<#<VW#M*0$//&2I*I$I $%)*;A*>*C*C*E%F J*11a#'#<#<VW#M*0$//&2I*I$IJJ 5 $N6 "**CC&))n#&"55!%&9'&B&G&G&I!J FA&'G%<F#88I&,doof.E&E EF
 !"5":":"<= F!%&9!&<&A&A&C!D FA&'A%6F#88I&,doof.E&E EFF     **C#~#&w< .3356 BA!"21"5":":"<= B"#Q44VWE"($//&*A"AABB  **C#((*% B$'F!/4467 BA!"3A"6";";"=> B"#Q44VWE"($//&*A"AABBB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                  |<   y r   )rm   rl   r=   s      r   r@   zMemory.bind  rA   r   c                 :    | j                   j                  |d       S r   )rl   rC   rD   s     r   r?   zMemory.namespace  rE   r   c                 :    | j                   j                  |d       S r   )rm   rC   rG   s     r   r>   zMemory.prefix  rH   r   c              #   \   K   | j                   j                         D ]  \  }}||f  y wr   )rl   rJ   r=   s      r   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)Nr9   c              3       K   | ]  }|  y wr   rP   )rQ   r   s     r   rR   z"Memory.contexts.<locals>.<genexpr>  s     ?G?rS   c              3       K   | ]  }|  y wr   rP   )rQ   ry   s     r   rR   z"Memory.contexts.<locals>.<genexpr>  s     ?!A?rS   rP   )rr   ri   r   rv   )r   r   subjpredobjry   s         r   contextszMemory.contexts  sp    >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~   rq   r   )r   r   r   s      r   r;   zMemory.__len__  s9    (d+++4((-..r   c                     | j                   st        j                  | |       y | j                  j	                  |       y r   )r   r   	add_graphrr   r)   r   graphs     r   r   zMemory.add_graph  s-    OOD%(##E*r   c                     | j                   st        j                  | |       y | j                  d|       	 | j                  j                  |       y # t
        $ r Y y w xY w)Nr9   )r   r   remove_graphr/   rr   rv   r   s     r   r   zMemory.remove_graph  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~   boolro   rv   rs   r   rq   r)   rp   )r   r   rz   r   r   r   triple_contexts          r   __add_triple_contextzMemory.__add_triple_context  s]   (f2!%!6!6v!> #)N3'-t$ BEvN!6!6v!> &B !6!6v!> !!$'++F3 d+++),D!!#&c"&&v. !!)%3D"T333%%f- 4G  2
 **//12!6!6"2s   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)ro   rC   rs   r4   rJ   )r   r   r}   r   r   r   s         r   __get_context_for_triplezMemory.__get_context_for_triple  sQ     $$((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)ro   rC   rs   )r   r   r   s      r   __triple_has_contextzMemory.__triple_has_context  s&    d++//8N8NOOOr   c                    | j                   j                  || j                        j                         }||= || j                  k(  r| j                   |= n|| j                   |<   | j                  |   j                  |       y)z"remove the context from the tripleN)ro   rC   rs   r   rq   r/   )r   r   r   r   s       r   __remove_triple_contextzMemory.__remove_triple_context  su    $$((1G1GHMMOI4)))%%f-,0D!!&)c"))&1r   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   r   ra   rn   AttributeError
isinstancestrRuntimeError)r   r   ctx_strs      r   __ctx_to_strz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   )rn   rC   )rQ   r   r   s     r   rR   z$Memory.__contexts.<locals>.<genexpr>  s2      
" ""&&w8
s   (+Tr|   )r   )r   r   s   ` r   rT   zMemory.__contexts  s%    
88D8Q
 	
r   c                 4    t        t        | 
  ||||fi | y r   )r	   r   rV   rW   s         r   rV   zMemory.query$  s    fd!%zTVTr   c                 4    t        t        | 
  ||||fi | y r   )r	   r   r]   r^   s         r   r]   zMemory.update'  s    fd"66<VvVr   r_   r`   r   )ra   rb   rc   rd   context_awareformula_awarer   r
   r)   r/   r,   r@   r?   r>   rK   r   r;   r   r   rw   r   r   r   r~   r   rV   r]   re   rf   s   @r   r   r      s     MMK&.7r 0D_BB-22$	#/+,.\	CP2T$
UW Wr   )rdflib.storer   __all__r2   r   r   rP   r   r   <module>r      s8    8
$
f
5 f
RuWU uWr   