o
    i6v                     @  s   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rYd 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	 d	eZ&G d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                      s   e Zd ZdZ		dCdD fddZ		dEdFddZ	dGdHddZ	dGdIddZdGdJddZdKdLd%d&Z	dMd(d)Z
dNd*d+ZdOd-d.ZdPd0d1ZdQ fd=d>ZdR fdAdBZ  ZS )Sr   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                   s8   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__ W/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/rdflib/plugins/stores/memory.pyr'   5   s   
zSimpleMemory.__init__Ftriple'_TripleType'context'_ContextType'quotedboolreturnr   c                 C  s  |\}}}| j }z|| }W n
   i  }||< Y z|| }	W n
   i  }	||< Y d|	|< | j}
z|
| }W n
   i  }|
|< Y z|| }W n
   i  }||< Y d||< | j}z|| }W n
   i  }||< Y z|| }W n
   i  }||< Y d||< dS )6        Add a triple to the store of triples.
           N)r(   r)   r*   )r.   r3   r5   r7   subject	predicateobjectspopooposossospsppr1   r1   r2   addO   s>   
zSimpleMemory.addtriple_pattern'_TriplePatternType'Optional['_ContextType']c                 C  sR   t | |D ]\\}}}}| j| | |= | j| | |= | j| | |= qd S r%   )listtriplesr(   r)   r*   )r.   rI   r5   r<   r=   r>   cr1   r1   r2   remove}   s
   zSimpleMemory.removeBIterator[Tuple['_TripleType', Iterator[Optional['_ContextType']]]]c                 c  sV   |\}}}|t kr| j}||v r|| }|t krN||v rK|t kr6||| v r3|||f|  fV  dS 	 dS ||  D ]}|||f|  fV  q<dS 	 dS | D ],}	|t krk|||	 v ri||	|f|  fV  qR	 qR||	  D ]}||	|f|  fV  qqqRdS 	 dS |t kr| j}
||
v r|
| }|t kr||v r||  D ]}|||f|  fV  qdS 	 dS | D ]}||  D ]}|||f|  fV  qqdS dS |t kr| j}||v r|| }| D ]}||  D ]}	||	|f|  fV  qqdS dS | j}| D ]#}|| }| D ]}	||	  D ]}||	|f|  fV  qqqdS z)A generator over all the triples matchingN)r    r(   _SimpleMemory__contextskeysr)   r*   )r.   rI   r5   r<   r=   r>   r?   subjectDictionaryrA   rG   rB   predicateDictionaryrD   rE   objectDictionaryr1   r1   r2   rM      sz   

	zSimpleMemory.triplesintc                 C  s    d}|  dD ]}|d7 }q|S )Nr   NNNr;   )rM   )r.   r5   ir3   r1   r1   r2   __len__   s   
zSimpleMemory.__len__Tprefixstr	namespace'URIRef'overridec                 C     | j |}t| j|| j|}|r1|d ur| j |= |d ur%| j|= || j|< || j |< d S t||d| jt||< t||d| j t||< d S N)default)r+   getr   r,   r.   r[   r]   r_   bound_namespacebound_prefixr1   r1   r2   bind   s$   


zSimpleMemory.bindOptional['URIRef']c                 C     | j |d S r%   )r+   rc   r.   r[   r1   r1   r2   r]         zSimpleMemory.namespacec                 C  ri   r%   )r,   rc   r.   r]   r1   r1   r2   r[      rk   zSimpleMemory.prefixIterator[Tuple[str, 'URIRef']]c                 c  $    | j  D ]	\}}||fV  qd S r%   )r+   itemsr.   r[   r]   r1   r1   r2   
namespaces      zSimpleMemory.namespaces%Generator['_ContextType', None, None]c                 C  s   dd dD S )Nc                 s      | ]}|V  qd S r%   r1   ).0rN   r1   r1   r2   	<genexpr>       z*SimpleMemory.__contexts.<locals>.<genexpr>r1   r1   r.   r1   r1   r2   
__contexts   s   zSimpleMemory.__contextsqueryUnion['Query', str]initNsMapping[str, Any]initBindingsMapping['str', 'Identifier']
queryGraph'str'kwargsr   'Result'c                   "   t t| j||||fi | d S r%   )r&   r   rz   r.   rz   r|   r~   r   r   r/   r1   r2   rz      
   

zSimpleMemory.queryupdateUnion['Update', str]c                   r   r%   )r&   r   r   r.   r   r|   r~   r   r   r/   r1   r2   r     r   zSimpleMemory.updateNNr!   r"   r#   r$   Fr3   r4   r5   r6   r7   r8   r9   r   r%   rI   rJ   r5   rK   r9   r   )rI   rJ   r5   rK   r9   rP   r5   rK   r9   rW   Tr[   r\   r]   r^   r_   r8   r9   r   r[   r\   r9   rh   r]   r^   r9   r"   r9   rm   )r9   rs   )rz   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'   rH   rO   rM   rZ   rg   r]   r[   rq   rR   rz   r   __classcell__r1   r1   r/   r2   r   *   s&    1@



c                      s.  e Zd ZdZdZdZdZ		dZd[ fdd	Z	
d\d]ddZ	d^d_ddZ		d^d`ddZ
dadbd"d#Zdcd%d&Zddd'd(Zded*d+Z	d^dfd.d/Zd^dgd1d2Zdhd5d6Zdhd7d8Zdid:d;Z	
d\djd>d?ZdkdAdBZdldCdDZedmdEdFZedndGdFZdodHdFZdpdIdJZdq fdTdUZdr fdXdYZ  ZS )sr   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
    TNr!   r"   r#   r$   c                   s^   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__defaultContextsr-   r/   r1   r2   r'     s   
zMemory.__init__Fr3   r4   r5   r6   r7   r8   r9   r   c                 C  s  t j| |||d |dur| j| |\}}}| j}z|| }W n ty0   i  }||< Y nw z|| }	W n tyF   i  }	||< Y nw z|	| }
d}W n ty^   d|	|< d}Y nw | |||| |rkdS | j}z|| }W n ty   i  }||< Y nw z|| }W n ty   i  }||< Y nw d||< | j}z|| }W n ty   i  }||< Y nw z|| }W n ty   i  }||< Y nw d||< dS )r:   )r7   NTr;   F)	r   rH   r   r   LookupErrorKeyError_Memory__add_triple_contextr   r   )r.   r3   r5   r7   r<   r=   object_r?   r@   rA   _triple_existsrB   rC   rD   rE   rF   rG   r1   r1   r2   rH   =  sb   
z
Memory.addrI   rJ   rK   c                 C  s:  |  |}| j||dD ]`\}}|\}}}| |D ]}	|d ur%||	kr%q| ||	 q| j|dd}
d |
v rG|d u sAt|
dkrG| |d  t| |dkrl| j| | |= | j| | |= | j| | |= | j|= q|d ur|| j	v rt| j	| dkr| j	|= |dkr|| j
v r| js| j
| d S d S d S d S )N)r5   T
skipQuotedr;   r   rX   )_Memory__ctx_to_strrM   _Memory__get_context_for_triple_Memory__remove_triple_contextlenr   r   r   r   r   r   graph_awarerO   )r.   rI   r5   req_ctxr3   rN   r<   r=   r   ctxctxsr1   r1   r2   rO   {  s6   



zMemory.remove\Generator[Tuple['_TripleType', Generator[Optional['_ContextType'], None, None]], None, None]c                 c  s   |  |}|\}}}|du r2|du r2|du r2|| jvrdS | j|  D ]
}|| |fV  q%dS |durh|durh|durh|}z| j| | | }| ||r[|| |fV  W dS W dS  tyg   Y dS w |dur| j}	||	v r|	| }
|dur||
v r|dur||
| v r|||f}| ||r|| |fV  dS dS 	 dS t|
|  D ]}|||f}| ||r|| |fV  qdS 	 dS t|
 D ]A}|dur||
| v r|||f}| ||r|| |fV  q	 qt|
|  D ]}|||f}| ||r|| |fV  qqdS 	 dS |dur| j	}||v r|| }|durU||v rRt||  D ]}|||f}| ||rN|| |fV  q8dS 	 dS t| D ]%}t||  D ]}|||f}| ||r{|| |fV  qeq[dS dS |dur| j
}||v r|| }t| D ]%}t||  D ]}|||f}| ||r|| |fV  qqdS dS | j}	t|	 D ]1}|	| }
t|
 D ]#}t|
|  D ]}|||f}| ||r|| |fV  qqאqdS rQ   )r   r   copy_Memory__contextsr   _Memory__triple_has_contextr   rL   rS   r   r   )r.   rI   r5   r   r<   r=   r   r3   r   r?   rT   rA   rG   rB   rU   rD   rE   rV   r1   r1   r2   rM     s   



















zMemory.triplesr[   r\   r]   r^   r_   c                 C  r`   ra   )r   rc   r   r   rd   r1   r1   r2   rg     s$   


zMemory.bindrh   c                 C  ri   r%   )r   rc   rj   r1   r1   r2   r]   '  rk   zMemory.namespacec                 C  ri   r%   )r   rc   rl   r1   r1   r2   r[   *  rk   zMemory.prefixrm   c                 c  rn   r%   )r   ro   rp   r1   r1   r2   rq   -  rr   zMemory.namespacesOptional['_TripleType']rs   c                 C  sj   |d u s|dkrdd | j D S |\}}}z| j| | | }| |W S  ty4   dd dD  Y S w )NrX   c                 s  rt   r%   r1   )ru   r5   r1   r1   r2   rv   5  rw   z"Memory.contexts.<locals>.<genexpr>c                 s  rt   r%   r1   )ru   r   r1   r1   r2   rv   <  rw   r1   )r   r   r   r   )r.   r3   subjpredobjr   r1   r1   r2   contexts1  s   
zMemory.contextsrW   c                 C  s&   |  |}|| jvrdS t| j| S )Nr   )r   r   r   )r.   r5   r   r1   r1   r2   rZ   >  s   

zMemory.__len__graph'Graph'c                 C  s&   | j st| | d S | j| d S r%   )r   r   	add_graphr   rH   r.   r   r1   r1   r2   r   D  s   zMemory.add_graphc                 C  sJ   | j st| | d S | d| z	| j| W d S  ty$   Y d S w )NrX   )r   r   remove_graphrO   r   r   r   r1   r1   r2   r   J  s   zMemory.remove_graphr   c                 C  s   |  |}t|}|r1z| j| }W n ty%   | j  }| j|< Y nw |||< |s0||d< n|r=||i }| j|< n||d|i }| j|< |sR| jd | || jvr]t | j|< | j| | | jdu rm|| _|| jkrx| 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   rH   r   )r.   r3   r   r5   r7   r   triple_contextr1   r1   r2   __add_triple_contextU  s<   



zMemory.__add_triple_contextr   Collection[Optional[str]]c                 C  s.   | j || j}|s| S dd | D S )zdreturn a list of contexts (str) for the triple, skipping
        quoted contexts if skipQuoted==Truec                 S  s   g | ]\}}|s|qS r1   r1   )ru   r   r7   r1   r1   r2   
<listcomp>  s    z3Memory.__get_context_for_triple.<locals>.<listcomp>)r   rc   r   rS   ro   )r.   r3   r   r   r1   r1   r2   __get_context_for_triple  s   zMemory.__get_context_for_tripler   c                 C  s   || j || jv S )z5return True if the triple exists in the given context)r   rc   r   )r.   r3   r   r1   r1   r2   __triple_has_context  s   zMemory.__triple_has_contextc                 C  sL   | j || j }||= || jkr| j |= n|| j |< | j| | dS )z"remove the context from the tripleN)r   rc   r   r   r   rO   )r.   r3   r   r   r1   r1   r2   __remove_triple_context  s   


zMemory.__remove_triple_contextc                 C     d S r%   r1   r.   r   r1   r1   r2   __ctx_to_str     zMemory.__ctx_to_strc                 C  r   r%   r1   r   r1   r1   r2   r     r   c                 C  s   |d u rd S zd |jjj|j}|| j|< |W S  tyB   t|tr>d |jj|}|| jv r5| Y S || j|< | Y S tdw )Nz{}:{}z1Cannot use that type of object as a Graph context)	formatr#   r0   r   r   AttributeError
isinstancer\   RuntimeError)r.   r   ctx_strr1   r1   r2   r     s   



c                   s    fdd j |ddD S )zgreturn a generator for all the non-quoted contexts
        (dereferenced) the encoded triple appears inc                 3  s&    | ]}|d ur j ||V  qd S r%   )r   rc   )ru   r   rx   r1   r2   rv     s    z$Memory.__contexts.<locals>.<genexpr>Tr   )r   )r.   r3   r1   rx   r2   ry     s   
zMemory.__contextsrz   r{   r|   r}   r~   r   r   r   r   c                   r   r%   )r&   r   rz   r   r/   r1   r2   rz        "zMemory.queryr   Union['Update', Any]c                   r   r%   )r&   r   r   r   r/   r1   r2   r     r   zMemory.updater   r   r   r   r%   r   )rI   rJ   r5   rK   r9   r   r   r   r   r   r   )r3   r   r9   rs   r   )r   r   r9   r   )
r3   r4   r   r8   r5   rK   r7   r8   r9   r   )r3   r4   r   r8   r9   r   )r3   r4   r   r"   r9   r8   )r3   r4   )r   r6   r9   r\   )r   r   r9   r   )r   rK   r9   r"   )r3   r4   r9   rs   )
rz   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'   rH   rO   rM   rg   r]   r[   rq   r   rZ   r   r   r   r   r   r   r   r   r   rz   r   r   r1   r1   r/   r2   r     sH    %A)l





6




)(
__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   r1   r1   r1   r2   <module>   s   8$	 g