o
    minu                     @  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		dBdC fddZ		dDdEddZ	dFdGddZ	dFdHddZdFdIddZdJdKd%d&Z	dLd(d)Z
dMd*d+ZdNd-d.ZdOd0d1ZdP fd<d=ZdQ fd@dAZ  ZS )Rr   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__ V/home/kim/smarthome/.venv/lib/python3.10/site-packages/rdflib/plugins/stores/memory.pyr'   4   s   
zSimpleMemory.__init__Ftripler   contextr   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 )%Add a triple to the store of triples.   N)r(   r)   r*   )r.   r3   r4   r5   subject	predicateobjectspopooposossospsppr1   r1   r2   addN   s>   

zSimpleMemory.addtriple_patternr   Optional[_ContextType]c                 C  sR   t | |D ]\\}}}}| j| | |= | j| | |= | j| | |= qd S r%   )listtriplesr(   r)   r*   )r.   rG   r4   r:   r;   r<   cr1   r1   r2   removez   s
   zSimpleMemory.remove>Iterator[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.   rG   r4   r:   r;   r<   r=   subjectDictionaryr?   rE   r@   predicateDictionaryrB   rC   objectDictionaryr1   r1   r2   rJ      sz   

	zSimpleMemory.triplesintc                 C  s    d}|  dD ]}|d7 }q|S )Nr   NNNr9   )rJ   )r.   r4   ir3   r1   r1   r2   __len__   s   
zSimpleMemory.__len__Tprefixstr	namespacer   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.   rX   rZ   r[   Zbound_namespaceZbound_prefixr1   r1   r2   bind   s$   


zSimpleMemory.bindOptional[URIRef]c                 C     | j |d S r%   )r+   r_   r.   rX   r1   r1   r2   rZ         zSimpleMemory.namespacec                 C  rc   r%   )r,   r_   r.   rZ   r1   r1   r2   rX      re   zSimpleMemory.prefixIterator[Tuple[str, URIRef]]c                 c  $    | j  D ]	\}}||fV  qd S r%   )r+   itemsr.   rX   rZ   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   ).0rK   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kwargsr   r   c                   "   t t| j||||fi | d S r%   )r&   r   rt   r.   rt   rv   rx   rz   r{   r/   r1   r2   rt      
   

zSimpleMemory.queryupdateUnion[Update, str]c                   r|   r%   )r&   r   r   r.   r   rv   rx   rz   r{   r/   r1   r2   r      r~   zSimpleMemory.updateNNr!   r"   r#   r$   Fr3   r   r4   r   r5   r6   r7   r   r%   rG   r   r4   rH   r7   r   )rG   r   r4   rH   r7   rM   r4   rH   r7   rT   TrX   rY   rZ   r   r[   r6   r7   r   rX   rY   r7   rb   rZ   r   r7   r"   r7   rg   )r7   rm   )rt   ru   rv   rw   rx   ry   rz   rY   r{   r   r7   r   )r   r   rv   rw   rx   ry   rz   rY   r{   r   r7   r   )__name__
__module____qualname____doc__r'   rF   rL   rJ   rW   ra   rZ   rX   rk   rO   rt   r   __classcell__r1   r1   r/   r2   r   *   s&    /@



c                      s.  e Zd ZdZdZdZdZ		dYdZ fdd	Z	
d[d\ddZ	d]d^ddZ		d]d_ddZ
d`dad"d#Zdbd%d&Zdcd'd(Zddd*d+Z	d]ded.d/Zd]dfd1d2Zdgd5d6Zdgd7d8Zdhd:d;Z	
d[did>d?ZdjdAdBZdkdCdDZedldEdFZedmdGdFZdndHdFZdodIdJZdp fdSdTZdq fdWdXZ  ZS )rr   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                   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   r   r4   r   r5   r6   r7   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 )r8   )r5   NTr9   F)	r   rF   r   r   LookupErrorKeyError_Memory__add_triple_contextr   r   )r.   r3   r4   r5   r:   r;   object_r=   r>   r?   _triple_existsr@   rA   rB   rC   rD   rE   r1   r1   r2   rF   9  sb   

z
Memory.addrG   r   rH   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)r4   T
skipQuotedr9   r   rU   )_Memory__ctx_to_strrJ   _Memory__get_context_for_triple_Memory__remove_triple_contextlenr   r   r   r   r   r   graph_awarerL   )r.   rG   r4   req_ctxr3   rK   r:   r;   r   ctxctxsr1   r1   r2   rL   u  s6   



zMemory.removeXGenerator[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 rN   )r   r   copy_Memory__contextsr   _Memory__triple_has_contextr   rI   rP   r   r   )r.   rG   r4   r   r:   r;   r   r3   r   r=   rQ   r?   rE   r@   rR   rB   rC   rS   r1   r1   r2   rJ     s   



















zMemory.triplesrX   rY   rZ   r   r[   c                 C  r\   r]   )r   r_   r   r   r`   r1   r1   r2   ra     s$   


zMemory.bindrb   c                 C  rc   r%   )r   r_   rd   r1   r1   r2   rZ   !  re   zMemory.namespacec                 C  rc   r%   )r   r_   rf   r1   r1   r2   rX   $  re   zMemory.prefixrg   c                 c  rh   r%   )r   ri   rj   r1   r1   r2   rk   '  rl   zMemory.namespacesOptional[_TripleType]rm   c                 C  sn   |d u s|dkrdd t | jD S |\}}}z| j| | | }| |W S  ty6   dd dD  Y S w )NrU   c                 s  rn   r%   r1   )ro   r4   r1   r1   r2   rp   /  rq   z"Memory.contexts.<locals>.<genexpr>c                 s  rn   r%   r1   )ro   r   r1   r1   r2   rp   6  rq   r1   )rI   r   r   r   r   )r.   r3   Zsubjpredobjr   r1   r1   r2   contexts+  s   
zMemory.contextsrT   c                 C  s&   |  |}|| jvrdS t| j| S )Nr   )r   r   r   )r.   r4   r   r1   r1   r2   rW   8  s   

zMemory.__len__graphr   c                 C  s&   | j st| | d S | j| d S r%   )r   r   	add_graphr   rF   r.   r   r1   r1   r2   r   >  s   zMemory.add_graphc                 C  sJ   | j st| | d S | d| z	| j| W d S  ty$   Y d S w )NrU   )r   r   remove_graphrL   r   r   r   r1   r1   r2   r   D  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   r6   r   r   r   r   r   rF   r   )r.   r3   r   r4   r5   r   Ztriple_contextr1   r1   r2   Z__add_triple_contextO  s8   



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   )ro   r   r5   r1   r1   r2   
<listcomp>  s    z3Memory.__get_context_for_triple.<locals>.<listcomp>)r   r_   r   rP   ri   )r.   r3   r   r   r1   r1   r2   Z__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   r_   r   )r.   r3   r   r1   r1   r2   Z__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   r_   r   r   r   rL   )r.   r3   r   r   r1   r1   r2   Z__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
isinstancerY   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   r_   )ro   r   rr   r1   r2   rp     s    z$Memory.__contexts.<locals>.<genexpr>Tr   )r   )r.   r3   r1   rr   r2   rs     s   
zMemory.__contextsrt   ru   rv   rw   rx   ry   rz   r   c                   r|   r%   )r&   r   rt   r}   r/   r1   r2   rt        "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   )rG   r   r4   rH   r7   r   r   r   r   r   r   )r3   r   r7   rm   r   )r   r   r7   r   )
r3   r   r   r6   r4   rH   r5   r6   r7   r   )r3   r   r   r6   r7   r   )r3   r   r   r"   r7   r6   )r3   r   )r   r   r7   rY   )r   r   r7   r   )r   rH   r7   r"   )r3   r   r7   rm   )
rt   ru   rv   rw   rx   ry   rz   rY   r7   r   )
r   r   rv   rw   rx   ry   rz   rY   r7   r   )r   r   r   r   Zcontext_awareZformula_awarer   r'   rF   rL   rJ   ra   rZ   rX   rk   r   rW   r   r   r   r   r   r   r   r   r   rt   r   r   r1   r1   r/   r2   r     sH    %?)l





6




)(
__future__r   typingr   r   r   r   r   r   r	   r
   r   r   r   r   Zrdflib.storer   Zrdflib.utilr   Zrdflib.graphr   r   r   r   r   r   r   Zrdflib.plugins.sparql.sparqlr   r   Zrdflib.queryr   Zrdflib.termr   r   __all__r    __annotations__r   r   r1   r1   r1   r2   <module>   s   8$	 d