
    Pinu                       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'dS )    )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	 	 d6d7 fdZ	 d8d9dZ	 d:d;dZ	 d:d<dZd:d=dZd>d?d Z	d@d"Z
dAd#ZdBd%ZdCd'ZdD fd2ZdE fd5Z xZS )Fr   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
    NconfigurationOptional[str]
identifierOptional[Identifier]c                    t          t          |                               |           || _        i | _        i | _        i | _        i | _        i | _        d S N)	superr   __init__r%   _SimpleMemory__spo_SimpleMemory__pos_SimpleMemory__osp_SimpleMemory__namespace_SimpleMemory__prefixselfr#   r%   	__class__s      p/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/rdflib/plugins/stores/memory.pyr*   zSimpleMemory.__init__4   sc    
 	lD!!**=999$  	
  	
  	
 /1+-    Ftripler   contextr   quotedboolreturnr    c                z   |\  }}}| j         }	 ||         }n#  i x}||<   Y nxY w	 ||         }	n#  i x}	||<   Y nxY wd|	|<   | j        }
	 |
|         }n#  i x}|
|<   Y nxY w	 ||         }n#  i x}||<   Y nxY wd||<   | j        }	 ||         }n#  i x}||<   Y nxY w	 ||         }n#  i x}||<   Y nxY wd||<   dS )%Add a triple to the store of triples.   N)r+   r,   r-   )r1   r5   r6   r7   subject	predicateobjectspopooposossospspps                   r3   addzSimpleMemory.addN   sF    &,"Fj	#WBB	# ""BW	#9AA	# ""A9&	j	%YBB	%"$$BY	 6
AA	 A6


'
j	"VBB	"!!BV	!7AA	!  A7)s@    	#0 	;A 	A#A, ,	A7B 	BB( (	B3triple_patternr   Optional[_ContextType]c                    t          |                     |                    D ]E\  \  }}}}| j        |         |         |= | j        |         |         |= | j        |         |         |= Fd S r(   )listtriplesr+   r,   r-   )r1   rJ   r6   r=   r>   r?   cs          r3   removezSimpleMemory.removez   s|    
 04DLL4P4P/Q/Q 	7 	7+(Wi!
7#I.v6
9%f-g6
6"7+I66	7 	7r4   >Iterator[Tuple[_TripleType, Iterator[Optional[_ContextType]]]]c              #    K   |\  }}}|t           k    r| j        }||v r
||         }|t           k    rt||v rn|t           k    r)|||         v r|||f|                                 fV  dS dS ||                                         D ]}|||f|                                 fV  dS dS |                                D ]l}	|t           k    r'|||	         v r||	|f|                                 fV  34||	                                         D ]}||	|f|                                 fV  mdS dS |t           k    r| j        }
||
v r|
|         }|t           k    r@||v r:||                                         D ]}|||f|                                 fV  dS dS |                                D ]<}||                                         D ]}|||f|                                 fV  ;dS dS |t           k    rf| j        }||v rW||         }|                                D ]<}||                                         D ]}	||	|f|                                 fV  ;dS dS | j        }|                                D ]Y}||         }|                                D ]:}	||	                                         D ]}||	|f|                                 fV  ;ZdS z)A generator over all the triples matchingN)r!   r+   _SimpleMemory__contextskeysr,   r-   )r1   rJ   r6   r=   r>   r?   r@   subjectDictionaryrB   rH   rC   predicateDictionaryrE   rF   objectDictionarys                  r3   rN   zSimpleMemory.triples   s      &4"Fc>>*C#~~$'L!## $555!S==%):9)EEE'.	6&BDOODUDU&U U U U U U $%6y%A%F%F%H%H Q Q'.	1&=t?P?P&P P P P PQ Q .3355 I I!S==%):1)==='.6&:DOO<M<M&M M M M M $%6q%9%>%>%@%@ I I'.1ot7H7H&H H H H HII I #*CC&))n#S==!444!4V!<!A!A!C!C L LA#$i"8$//:K:K"KKKKKL L 05577 G G!4Q!7!<!<!>!> G GA#$i"3T__5F5F"FFFFFG  G G s]]*C}}#&v; )..00 @ @A-a05577 @ @ !Vndoo.?.??????@ }@ @ *CXXZZ ; ;$'F!*//11 ; ;A.q16688 ; ; !Qi):)::::::;;; ;r4   intc                D    d}|                      d          D ]}|dz  }|S )Nr   NNNr<   )rN   )r1   r6   ir5   s       r3   __len__zSimpleMemory.__len__   s3    ll#566 	 	FFAAr4   Tprefixstr	namespacer   overridec                   | j                             |          }t          | j                            |          | j                            |                    }|r*|| j         |= || j        |= || j        |<   || j         |<   d S t          ||          | j        t          ||          <   t          ||          | j         t          ||          <   d S N)default)r.   getr   r/   r1   r^   r`   ra   bound_namespacebound_prefixs         r3   bindzSimpleMemory.bind   s    *..v66 Mi((Mo..
 

  	'$\2*M/2'-DM)$'0DV$$$ DMfD D DDM)OY??@ AJA A ADY|V<<===r4   Optional[URIRef]c                8    | j                             |d           S r(   )r.   re   r1   r^   s     r3   r`   zSimpleMemory.namespace       ##FD111r4   c                8    | j                             |d           S r(   )r/   re   r1   r`   s     r3   r^   zSimpleMemory.prefix       }  D111r4   Iterator[Tuple[str, URIRef]]c              #  T   K   | j                                         D ]\  }}||fV  d S r(   )r.   itemsr1   r^   r`   s      r3   
namespaceszSimpleMemory.namespaces   G      !%!1!7!7!9!9 	$ 	$FI)#####	$ 	$r4   #Generator[_ContextType, None, None]c                    d dD             S )Nc              3     K   | ]}|V  d S r(    ).0rO   s     r3   	<genexpr>z*SimpleMemory.__contexts.<locals>.<genexpr>   s      ar4   rz   rz   )r1   s    r3   
__contextszSimpleMemory.__contexts   s     2r4   queryUnion[Query, str]initNsMapping[str, Any]initBindingsMapping[str, Identifier]
queryGraphkwargsr   r   c                P     t          t          |           j        ||||fi | d S r(   )r)   r   r~   r1   r~   r   r   r   r   r2   s         r3   r~   zSimpleMemory.query   sF     	(lD!!'6<	
 	
7=	
 	
 	
 	
 	
r4   updateUnion[Update, str]c                P     t          t          |           j        ||||fi | d S r(   )r)   r   r   r1   r   r   r   r   r   r2   s         r3   r   zSimpleMemory.update   sF     	)lD!!(FL*	
 	
8>	
 	
 	
 	
 	
r4   NNr#   r$   r%   r&   Fr5   r   r6   r   r7   r8   r9   r    r(   rJ   r   r6   rK   r9   r    )rJ   r   r6   rK   r9   rQ   r6   rK   r9   rY   Tr^   r_   r`   r   ra   r8   r9   r    r^   r_   r9   rj   r`   r   r9   r$   r9   rq   )r9   rw   )r~   r   r   r   r   r   r   r_   r   r   r9   r   )r   r   r   r   r   r   r   r_   r   r   r9   r    )__name__
__module____qualname____doc__r*   rI   rP   rN   r]   ri   r`   r^   ru   rT   r~   r   __classcell__r2   s   @r3   r   r   *   sn         (,+/. . . . . . .< 	* * * * *^ +/7 7 7 7 7 +/>; >; >; >; >;@        22 2 2 22 2 2 2$ $ $ $   

 

 

 

 

 



 

 

 

 

 

 

 

 

 

r4   c                      e Zd ZdZdZdZdZ	 	 dEdF fdZ	 dGdHdZ	 dIdJdZ		 dIdKdZ
dLdMdZdNd ZdOd!ZdPd#Z	 dIdQd&ZdIdRd(ZdSd+ZdSd,ZdTd.Z	 dGdUd1ZdVd3ZdWd4ZedXd5            ZedYd6            ZdZd7Zd[d8Zd\ fdAZd] fdDZ xZS )^r   zAn in memory implementation of a triple store.

    Same as SimpleMemory above, but is Context-aware, Graph-aware, and Formula-aware
    Authors: Ashley Sommer
    TNr#   r$   r%   r&   c                &   t          t          |                               |           || _        i | _        i | _        i | _        i | _        i | _        i | _	        i | _
        d t                      i| _        t                      | _        d | _        d S 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__defaultContextsr0   s      r3   r*   zMemory.__init__  s    
 	fd$$]333$  	
  	
  	
 /1+-35NPHLcee}*-%%FJr4   Fr5   r   r6   r   r7   r8   r9   r    c                   t          j        | |||           || j                            |           |\  }}}| j        }	 ||         }n# t          $ r
 i x}||<   Y nw xY w	 ||         }	n# t          $ r
 i x}	||<   Y nw xY w	 |	|         }
d}n# t
          $ r
 d|	|<   d}Y nw xY w|                     ||||           |rdS | j        }	 ||         }n# t          $ r
 i x}||<   Y nw xY w	 ||         }n# t          $ r
 i x}||<   Y nw xY wd||<   | j        }	 ||         }n# t          $ r
 i x}||<   Y nw xY w	 ||         }n# t          $ r
 i x}||<   Y nw xY wd||<   dS )r;   )r7   NTr<   F)	r   rI   r   r   LookupErrorKeyError_Memory__add_triple_contextr   r   )r1   r5   r6   r7   r=   r>   object_r@   rA   rB   _triple_existsrC   rD   rE   rF   rG   rH   s                     r3   rI   z
Memory.add9  sJ    		$7777##G,,,&,#Gj	#WBB 	# 	# 	# ""BW	#	#9AA 	# 	# 	# ""A9	#	"'
A MM 	" 	" 	"AgJ!MMM	" 	!!&-&III 	Fj	%YBB 	% 	% 	%"$$BY	%	!7AA 	! 	! 	!  A7	!'
j	#WBB 	# 	# 	# ""BW	#	!7AA 	! 	! 	!  A7	!)s~   A A A $A- -B B
B B$#B$C C('C(,C5 5D	D	D" "D65D6:E EErJ   r   rK   c                   |                      |          }|                     ||          D ]\  }}|\  }}}|                     |          D ]!}	|||	k    r|                     ||	           "|                     |d          }
d |
v r+|t	          |
          dk    r|                     |d            t	          |                     |                    dk    rD| j        |         |         |= | j        |         |         |= | j        |         |         |= | j        |= |/|| j	        v r&t	          | j	        |                   dk    r| j	        |= |dk    r,|| j
        v r%| j        s | j
                            |           d S d S d S d S )N)r6   T
skipQuotedr<   r   r[   )_Memory__ctx_to_strrN   _Memory__get_context_for_triple_Memory__remove_triple_contextlenr   r   r   r   r   r   graph_awarerP   )r1   rJ   r6   req_ctxr5   rO   r=   r>   r   ctxctxss              r3   rP   zMemory.removeu  s   
 ##G,,ngFF 	2 	2IFA*0'GY44V<< : :&7c>>,,VS999900D0IIDt||CIINN,,VT:::4008899Q>>Jw'	27;Jy)'27;Jw'0;)&14000D)'233q88 %g. 0004...$ / &&w///// 10....r4   XGenerator[Tuple[_TripleType, Generator[Optional[_ContextType], None, None]], None, None]c              #  j	  K   |                      |          }|\  }}}|L|J|H|| j        vrdS | j        |                                         D ]}||                     |          fV  dS |d|b|`|}	 | j        |         |         |         }|                     ||          r||                     |          fV  dS dS # t          $ r Y dS w xY w|| j        }	||	v r||	|         }
|||
v r|D||
|         v r8|||f}|                     ||          r||                     |          fV  dS dS dS t          |
|                                                   D ]6}|||f}|                     ||          r||                     |          fV  7dS dS t          |
                                          D ]}|@||
|         v r5|||f}|                     ||          r||                     |          fV  CDt          |
|                                                   D ]6}|||f}|                     ||          r||                     |          fV  7dS dS || j	        }||v r||         }|f||v r`t          ||                                                   D ]6}|||f}|                     ||          r||                     |          fV  7dS dS t          |                                          D ]b}t          ||                                                   D ]6}|||f}|                     ||          r||                     |          fV  7adS dS || j
        }||v r||         }t          |                                          D ]b}t          ||                                                   D ]6}|||f}|                     ||          r||                     |          fV  7adS dS | j        }	t          |	                                          D ]}|	|         }
t          |
                                          D ]`}t          |
|                                                   D ]6}|||f}|                     ||          r||                     |          fV  7adS rS   )r   r   copy_Memory__contextsr   _Memory__triple_has_contextr   rM   rU   r   r   )r1   rJ   r6   r   r=   r>   r   r5   r   r@   rV   rB   rH   rC   rW   rE   rF   rX   s                     r3   rN   zMemory.triples  sr      ##G,,&4#G ?y0W_d333/8==?? 6 6doof55555556 6  Y%:w?R $FJw'	27;,,VW== : $//&"9"9999999: :     *C#~~$'L!( $555".&*;I*FFF*19g)F#'#<#<VW#M#M !J*0$//&2I2I*I$I$I$I$I$I!J !J !%%)*;I*F*K*K*M*M%N%N J J*19a)@#'#<#<VW#M#M !J*0$//&2I2I*I$I$I$IJ J
 !"3"8"8":":;; J J".&*;A*>>>*11g)>#'#<#<VW#M#M !J*0$//&2I2I*I$I$I$I $%)*;A*>*C*C*E*E%F%F J J*11a#'#<#<VW#M#M !J*0$//&2I2I*I$I$I$IJJ J "*CC&))n#&"555!%&9'&B&G&G&I&I!J!J F FA&'G%<F#88II F&,doof.E.E&E E E EF F
 !"5":":"<"<== F F!%&9!&<&A&A&C&C!D!D F FA&'A%6F#88II F&,doof.E.E&E E E EF  F F
  *C#~~#&w< .335566 B BA!"21"5":":"<"<== B B"#Q44VWEE B"($//&*A*A"AAAAB ~B B *C#((**%% B B$'F!/446677 B BA!"3A"6";";"="=>> B B"#Q44VWEE B"($//&*A*A"AAAABBB Bs   5AC 
CCr^   r_   r`   r   ra   c                   | j                             |          }t          | j                            |          | j                            |                    }|r*|| j         |= || j        |= || j        |<   || j         |<   d S t          ||          | j        t          ||          <   t          ||          | j         t          ||          <   d S rc   )r   re   r   r   rf   s         r3   ri   zMemory.bind  s    *..v66 Mi((Mo..
 

  	'$\2*M/2'-DM)$'0DV$$$ DMfD D DDM)OY??@
 AJA A ADY|V<<===r4   rj   c                8    | j                             |d           S r(   )r   re   rl   s     r3   r`   zMemory.namespace!  rm   r4   c                8    | j                             |d           S r(   )r   re   ro   s     r3   r^   zMemory.prefix$  rp   r4   rq   c              #  T   K   | j                                         D ]\  }}||fV  d S r(   )r   rs   rt   s      r3   ru   zMemory.namespaces'  rv   r4   Optional[_TripleType]rw   c                    ||dk    rd t          | j                  D             S |\  }}}	 | j        |         |         |         }|                     |          S # t          $ r d dD             cY S w xY w)Nr[   c              3     K   | ]}|V  d S r(   rz   )r{   r6   s     r3   r|   z"Memory.contexts.<locals>.<genexpr>/  s"      EEGEEEEEEr4   c              3     K   | ]}|V  d S r(   rz   )r{   r   s     r3   r|   z"Memory.contexts.<locals>.<genexpr>6  s      ??!A??????r4   rz   )rM   r   r   r   r   )r1   r5   subjpredobjr   s         r3   contextszMemory.contexts+  s     >V'999EE40C+D+DEEEE dC	#
4 &s+A??6*** 	# 	# 	#"?r???"""	#s   -A A54A5rY   c                v    |                      |          }|| j        vrdS t          | j        |                   S )Nr   )r   r   r   )r1   r6   r   s      r3   r]   zMemory.__len__8  s>    ((d+++14(-...r4   graphr   c                v    | j         st          j        | |           d S | j                            |           d S r(   )r   r   	add_graphr   rI   r1   r   s     r3   r   zMemory.add_graph>  sB     	+OD%(((((##E*****r4   c                    | j         st          j        | |           d S |                     d|           	 | j                            |           d S # t
          $ r Y d S w xY w)Nr[   )r   r   remove_graphrP   r   r   r   s     r3   r   zMemory.remove_graphD  s     	tU+++++KK*E222#**511111   s   A 
A A r   c                :   |                      |          }t          |          }|rO	 | j        |         }n3# t          $ r& | j                                        x}| j        |<   Y nw xY w|||<   |s||d<   n!|r||ix}| j        |<   n||d|ix}| j        |<   |s | j        d                             |           || j        vrt                      | j        |<   | j        |                             |           | j        || _        || j        k    r
| j        |= dS dS )z;add the given context to the set of contexts for the tripleN)	r   r8   r   r   r   r   r   rI   r   )r1   r5   r   r6   r7   r   triple_contexts          r3   __add_triple_contextzMemory.__add_triple_contextO  s    ((f 	!%!6v!>   
 *//11!6v!>!>!>	 #)N3 .'-t$  BEvN!6v!>!> &B !6v!>  	4!$'++F333 d+++),D!#&c"&&v... !)%3D"T333%f--- 43s   6 -A&%A&r   Collection[Optional[str]]c                    | j                             || j                  }|s|                                S d |                                D             S )zdreturn a list of contexts (str) for the triple, skipping
        quoted contexts if skipQuoted==Truec                    g | ]	\  }}||
S rz   rz   )r{   r   r7   s      r3   
<listcomp>z3Memory.__get_context_for_triple.<locals>.<listcomp>  s!    BBBV6BBBBr4   )r   re   r   rU   rs   )r1   r5   r   r   s       r3   __get_context_for_triplezMemory.__get_context_for_triple  sT     $((1GHH 	99;; CBtzz||BBBBr4   r   c                F    || j                             || j                  v S )z5return True if the triple exists in the given context)r   re   r   )r1   r5   r   s      r3   __triple_has_contextzMemory.__triple_has_context  s%     d+//8NOOOOr4   c                    | j                             || j                                                  }||= || j        k    r	| j         |= n
|| j         |<   | j        |                             |           dS )z"remove the context from the tripleN)r   re   r   r   r   rP   )r1   r5   r   r   s       r3   __remove_triple_contextzMemory.__remove_triple_context  s}     $((1GHHMMOOI4)))%f--,0D!&)c"))&11111r4   c                    d S r(   rz   r1   r   s     r3   __ctx_to_strzMemory.__ctx_to_str  s    69cr4   c                    d S r(   rz   r   s     r3   r   zMemory.__ctx_to_str  s    /2sr4   c                R   |d S 	 d                     |j        j        j        |j                  }|| j        |<   |S # t
          $ r` t          |t                    r;d                     |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%   r2   r   r   AttributeError
isinstancer_   RuntimeError)r1   r   ctx_strs      r3   r   zMemory.__ctx_to_str  s    ;4	TnnS^%=%FWWG.1D"7+N 	T 	T 	T #s## !..)?EEd444"NNN25&w/RSSS	Ts   5< A
B&B&B&c                J      fd                      |d          D             S )zgreturn a generator for all the non-quoted contexts
        (dereferenced) the encoded triple appears inc              3  R   K   | ]!}|j                             ||          V  "d S r(   )r   re   )r{   r   r1   s     r3   r|   z$Memory.__contexts.<locals>.<genexpr>  sH       
 
" "&&w88""""
 
r4   Tr   )r   )r1   r5   s   ` r3   r}   zMemory.__contexts  sA    
 
 
 
88D8QQ
 
 
 	
r4   r~   r   r   r   r   r   r   r   c                P     t          t          |           j        ||||fi | d S r(   )r)   r   r~   r   s         r3   r~   zMemory.query  s6     	"fd!%zTTVTTTTTr4   r   Union[Update, Any]c                P     t          t          |           j        ||||fi | d S r(   )r)   r   r   r   s         r3   r   zMemory.update  s6     	#fd"66<VVvVVVVVr4   r   r   r   r   r(   r   )rJ   r   r6   rK   r9   r   r   r   r   r   r   )r5   r   r9   rw   r   )r   r   r9   r    )
r5   r   r   r8   r6   rK   r7   r8   r9   r    )r5   r   r   r8   r9   r   )r5   r   r   r$   r9   r8   )r5   r   )r   r   r9   r_   )r   r    r9   r    )r   rK   r9   r$   )r5   r   r9   rw   )
r~   r   r   r   r   r   r   r_   r9   r   )
r   r   r   r   r   r   r   r_   r9   r    )r   r   r   r   context_awareformula_awarer   r*   rI   rP   rN   ri   r`   r^   ru   r   r]   r   r   r   r   r   r   r   r   r   r~   r   r   r   s   @r3   r   r     s         MMK (,+/K K K K K K KJ 	: : : : :~ +/$0 $0 $0 $0 $0R +/jB jB jB jB jBX    42 2 2 22 2 2 2$ $ $ $
 /3# # # # #/ / / / /+ + + +   3. 3. 3. 3.l 7<C C C C CP P P P
	2 	2 	2 	2 999 X9222 X2T T T T*
 
 
 
U U U U U UW W W 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   r3   <module>r      s
   # " " " " " "                                  ! ! ! ! ! ! /                  ;:::::::######........8
$    `
 `
 `
 `
 `
5 `
 `
 `
FNW NW NW NW NWU NW NW NW NW NWr4   