
    #HJj7                        d dl mZ d dlmZ d dlmZmZmZmZm	Z	m
Z
mZ d dlmZ d dlmZ d dlmZ d dlmZmZmZmZmZmZ d dlmZmZ d d	lmZmZmZm Z  erd d
lm!Z! ddZ"ddZ#ddZ$ G d de      Z%y)    )annotationsdedent)Any	GeneratorIterableIteratorMappingOptionalTuple)Graph)	has_httpx)RepositoryNotFoundError)DATASET_DEFAULT_GRAPH_IDDataset_ContextType	_QuadType_TriplePatternType_TripleType)VALID_STOREStore)BNodeNodeURIRefVariable)RDF4JClientc                    t        t        |j                                     }|s| S dj                  dj                  |D cg c]  \  }}d|d|d c}}      d| g      S c c}}w )N
zPREFIX z: <> )setlistitemsjoin)queryextra_bindingsbindingskvs        e/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/rdflib/plugins/stores/rdf4j.py_inject_prefixesr+      se    4,,./0H99IIhGhdaAq1hGH	
 Gs   A$c                X    t        | t              rt        d      | j                         S )NzaSPARQL-based stores do not support BNodesSee http://www.w3.org/TR/sparql11-query/#BGPsparqlBNodes)
isinstancer   	Exceptionn3)nodes    r*   _node_to_sparqlr1   %   s-    $G
 	
 779    c                     t        d|  d      S )Nzu
        PREFIX config: <tag:rdf4j.org,2023:config/>

        []    a config:Repository ;
            config:rep.id "a  " ;
            config:rep.impl
                [
                    config:rep.type "openrdf:SailRepository" ;
                    config:sail.impl
                        [
                            config:native.tripleIndexers "spoc,posc" ;
                            config:sail.defaultQueryEvaluationMode "STANDARD" ;
                            config:sail.iterationCacheSyncThreshold "10000" ;
                            config:sail.type "openrdf:NativeStore" ;
                        ] ;
                ] ;
        .
    r   )repository_ids    r*   _default_repo_configr5   .   s%     *? +		 r2   c                  2    e Zd ZdZdZdZdZdZ	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 d fdZe	d        Z
e	d        Z	 d	 	 	 	 	 ddZdddZ	 	 d	 	 	 	 	 	 	 dd	Zdd
Z	 d	 	 	 	 	 ddZ	 d	 	 	 	 	 ddZ	 d	 	 	 d dZd!d"dZd#dZd$dZd%dZd&dZd&dZdd'dZ xZS )(
RDF4JStorezAn RDF4J store.TFc                    |t        |      }t        |||fi || _        || _        d | _        | j                  ||       t        |           y N)r5   r   _client_repository_id_repoopensuper__init__)	selfbase_urlr4   configurationauthtimeoutcreatekwargs	__class__s	           r*   r?   zRDF4JStore.__init__M   sR      0?M"8T7EfE+
		-(r2   c                    | j                   S r9   )r:   r@   s    r*   clientzRDF4JStore.client_   s    ||r2   c                    | j                   4| j                  j                  j                  | j                        | _         | j                   S r9   )r<   rJ   repositoriesgetr;   rI   s    r*   repozRDF4JStore.repoc   s8    ::1155d6I6IJDJzzr2   c                6   	 | j                   j                          t        S # t        $ rm |rQ| j                  j                  j                  | j                  |       | j                   j                          Y t        S t        d| j                   d      w xY w)NzRepository z not found.)	rN   healthr   rJ   rL   rE   r;   r.   r   )r@   rB   rE   s      r*   r=   zRDF4JStore.openi   s    	PII  ' 	P((//0C0C]S		  "   +d.A.A-B+ NOO	Ps   " AB>Bc                8    | j                   j                          y r9   )rJ   close)r@   commit_pending_transactions     r*   rR   zRDF4JStore.closex   s    r2   c                   |\  }}}||j                   t        k(  rdn|j                   j                         }|j                          d|j                          d|j                          d| d}| j                  j	                  |       y )Nr     z .
identifierr   r/   rN   upload)	r@   triplecontextquotedspo
graph_name	statements	            r*   addzRDF4JStore.add{   s     1a '"4"48P"P ##&&( 	
 ttvhaxq*R@			#r2   c           	     2   d}|D ]u  \  }}}}||j                   t        k(  rdn|j                   j                         }|j                          d|j                          d|j                          d| d}||z  }w | j                  j	                  |       y )Nr    rU   z .
rV   )	r@   quads
statementsr\   r]   r^   cr_   r`   s	            r*   addNzRDF4JStore.addN   s    
JAq!Q 90H H \\__& 
 446(!ADDF81QTTVHAj\FI)#J   			$r2   c                l    |\  }}}||j                   nd }| j                  j                  ||||       y r9   )rW   rN   delete)r@   rY   rZ   r\   r]   r^   gs          r*   removezRDF4JStore.remove   s9    
 1a")"5G4		Aq!$r2   c              #    K   |\  }}}||j                   nd }| j                  j                  ||||      }t        |t              r+|D ]%  \  }}}}|||ft        t        | |      g      f ' y |D ]  }	|	t        d g      f  y w)NrW   )rW   rN   rM   r-   r   iterr   )
r@   triple_patternrZ   r\   r]   r^   r_   result_graphri   rY   s
             r*   tripleszRDF4JStore.triples   s     
 !1a+2+>W''D
yy}}Q1j9lG,*
1a!QiuTa'@&A!BBB + 'dD6l** 's   BB
c              #  z  K   |/| j                   j                         D ]  }t        | |        y |\  }}}t        |r|n
t	        d            t        |r|n
t	        d            t        |r|n
t	        d            f}d|z  }| j                   j                  |      }|D ]  }	t        | |	d           y w)Nrl   r\   r]   r^   z:SELECT DISTINCT ?graph WHERE { GRAPH ?graph { %s %s %s } }graph)rN   graph_namesr   r1   r   r%   )
r@   rY   r_   r\   r]   r^   paramsr%   resultrows
             r*   contextszRDF4JStore.contexts   s      >"ii335
DZ88 6 GAq!QHSM:QHSM:QHSM:F MvU  YY__U+FDS\:: s   B9B;c                P    | j                   j                  j                  ||       y r9   )rN   
namespacesr!   )r@   prefix	namespaceoverrides       r*   bindzRDF4JStore.bind   s    		  3r2   c                    t        | j                  j                  j                         D cg c]  }|j                  |j
                  f c}      }|j                  t        |            S c c}w r9   )dictrN   ry   r"   r{   rz   rM   str)r@   r{   xnamespace_prefixess       r*   rz   zRDF4JStore.prefix   s^    !.2ii.B.B.G.G.IJ.Iakk188$.IJ
 "%%c)n55 Ks   A,c                j    | j                   j                  j                  |      }|t        |      S d S r9   )rN   ry   rM   r   )r@   rz   ru   s      r*   r{   zRDF4JStore.namespace   s0    %%))&1!'!3vf~==r2   c              #     K   | j                   j                  j                         D ]%  }|j                  t	        |j
                        f ' y wr9   )rN   ry   r"   rz   r   r{   )r@   ru   s     r*   ry   zRDF4JStore.namespaces   s=     ii**//1F--(8(8!999 2s   AAc                    |j                   t        k7  r7| j                  j                  d|j                   j	                                 y y )NzCREATE SILENT GRAPH )rW   r   rN   updater/   r@   rr   s     r*   	add_graphzRDF4JStore.add_graph   s@    77II3E4D4D4G4G4I3JKL 8r2   c                b    | j                   j                  j                  |j                         y r9   )rN   graphsclearrW   r   s     r*   remove_graphzRDF4JStore.remove_graph   s     		u//0r2   c                \    | j                   j                  ||      S |j                        S r9   )rN   sizerW   )r@   rZ   s     r*   __len__zRDF4JStore.__len__   s'    yy~~gQQg>P>PQQr2   )NNg      >@F)rA   r   r4   r   rB   z
str | NonerC   ztuple[str, str] | NonerD   floatrE   bool)F)rB   zstr | tuple[str, str] | NonerE   r   returnz
int | None)rS   r   r   None)NF)rY   r   rZ   _ContextType | Noner[   r   r   r   )rc   zIterable[_QuadType]r   r   r9   )rY   r   rZ   Optional[_ContextType]r   r   )rn   r   rZ   r   r   z>Iterator[Tuple[_TripleType, Iterator[Optional[_ContextType]]]])rY   zOptional[_TripleType]r   z#Generator[_ContextType, None, None])T)rz   r   r{   r   r|   r   r   r   )r{   r   r   zOptional[str])rz   r   r   zOptional[URIRef])r   zIterator[Tuple[str, URIRef]])rr   r   r   r   )rZ   r   r   int)__name__
__module____qualname____doc__context_awareformula_awaretransaction_awaregraph_awarer?   propertyrJ   rN   r=   rR   ra   rf   rj   rp   rw   r}   rz   r{   ry   r   r   r   __classcell__)rG   s   @r*   r7   r7   E   s   MMK %)'+  "	
 %  $     KP9CG	 (,	$$ %$ 	$
 
$
% +/%"% (% 
	% +/+*+ (+ 
H	+$ /3;+;	,;(46>:M
1R Rr2   r7   N)r%   r   r&   zMapping[str, Any]r   r   )r0   r   r   r   )r4   r   r   r   )&
__future__r   textwrapr   typingr   r   r   r	   r
   r   r   rdflibr   rdflib.contrib.rdf4jr   rdflib.contrib.rdf4j.exceptionsr   rdflib.graphr   r   r   r   r   r   rdflib.storer   r   rdflib.termr   r   r   r   r   r+   r1   r5   r7    r2   r*   <module>r      sW    "  O O O  * C  , 5 50
.ZR ZRr2   