
    Pi+                        d Z ddlmZ ddlZddlZddlmZmZm	Z	 ddl
mZ ddlmZmZmZ ej        r
ddlmZmZm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S )/zRDF4J utility functions.    )annotationsN)DATASET_DEFAULT_GRAPH_IDDatasetGraph)prepareQuery)BNodeIdentifiedNodeURIRef)
ObjectTypePredicateTypeSubjectTypeparamsdict[str, str]
graph_name8IdentifiedNode | t.Iterable[IdentifiedNode] | str | NonereturnNonec                   |@t          |t                    r+|t          k    rd| d<   dS |                                | d<   dS |;t          |t                    r&t          |                                          | d<   dS |@t          |t          j                  r(d                    d |D                       }|| d<   dS dS dS )aJ  Build the RDF4J http context query parameters dictionary.

    !!! Note
        This mutates the params dictionary key `context`.

    Parameters:
        params: The `httpx.Request` parameter dictionary.
        graph_name: The graph name or iterable of graph names.

            This is the `context` query parameter value.
    Nnullcontext,c                6    g | ]}|                                 S  n3.0xs     m/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/rdflib/contrib/rdf4j/util.py
<listcomp>z'build_context_param.<locals>.<listcomp>)   s     ;;;1;;;    )	
isinstancer	   r   r   strr
   tIterablejoin)r   r   graph_namess      r   build_context_paramr(      s     *Z"H"H111 &F9 *F9		Jz3$?$?	":..1133y		Jz1:$F$F	hh;;
;;;<<'y 
 			r!   subjr   predr   objr   c                    ||                                 | d<   ||                                 | d<   ||                                 | d<   dS dS )a  Build the RDF4J http subj, predicate, and object query parameters dictionary.

    !!! Note
        This mutates the params dictionary key `subj`, `pred`, and `obj`.

    Parameters:
        params: The `httpx.Request` parameter dictionary.
        subj: The `subj` query parameter value.
        pred: The `pred` query parameter value.
        obj: The `obj` query parameter value.
    Nr)   r*   r+   r   )r   r)   r*   r+   s       r   build_spo_paramr-   -   sS    " vv
u r!   Tinferboolc                    |sd| d<   dS dS )zBuild the RDF4J http infer query parameters dictionary.

    !!! Note
        This mutates the params dictionary key `infer`.

    Parameters:
        params: The `httpx.Request` parameter dictionary.
        infer: The `infer` query parameter value.
    falser.   Nr   )r   r.   s     r   build_infer_paramr2   F   s#      "!w" "r!   data*str | bytes | t.BinaryIO | Graph | Datasettuple[t.BinaryIO, bool]c                   t          | t                    rd| vrht          |           dk     rU	 t          | d          }d}n# t          t
          f$ r, t          j        |                     d                    }d}Y nw xY wt          j        |                     d                    }d}nt          | t                    rt          j        |           }d}n}t          | t          t          f          r]| j        r*t          j        |                     dd                    }n)t          j        |                     d	d                    }d}n| }d}||fS )
a  Convert an RDF payload into a file-like object.

    Parameters:
        data: The RDF payload.

            This can be a python `str`, `bytes`, `BinaryIO`, or a
            [`Graph`][rdflib.graph.Graph] or [`Dataset`][rdflib.graph.Dataset].

    Returns:
        A tuple containing the file-like object and a boolean indicating whether the
        immediate caller should close the stream.
    
i  rbTzutf-8Fzapplication/n-quads)formatencodingapplication/n-triples)r"   r#   lenopenFileNotFoundErrorOSErrorioBytesIOencodebytesr   r   context_aware	serialize)r3   streamshould_closes      r   rdf_payload_to_streamrH   W   sj     $ tD		C%dD))#%w/ % % %DKK$8$899$% ZG 4 455F LL	D%	 	  D!!	D5'*	+	+  	Z&;gNN FF Z&=PP F  <s   A :A?>A?queryr#   headersc                    t          |           }|j        j        dv rd|d<   dS |j        j        dv rd|d<   dS t          d|j        j                   )zBuild the SPARQL query accept header.

    !!! Note
        This mutates the headers dictionary key `Accept`.

    Parameters:
        query: The SPARQL query.
    )SelectQueryAskQueryzapplication/sparql-results+jsonAccept)ConstructQueryDescribeQueryr;   zUnsupported query type: N)r   algebraname
ValueError)rI   rJ   prepared_querys      r    build_sparql_query_accept_headerrU      sr     "%((N"&AAA=			$(K	K	K3QN4J4OQQRRRr!   (URIRef | t.Iterable[URIRef] | str | Nonec                    t          | t                    s3t          | t          j                  r(t	          d | D                       rt          d          d S d S )Nc              3  @   K   | ]}t          |t                    V  d S N)r"   r   r   s     r   	<genexpr>z&validate_graph_name.<locals>.<genexpr>   s,      99
1e$$999999r!   zGraph name must not be a BNode.)r"   r   r$   r%   anyrS   )r   s    r   validate_graph_namer\      sn    :u%%<j!*--< 99j99999<
 :;;;< < < <r!   c                    t          | t                    s?t          |t                    s*t          |t                    st          |t                    rt          d|  d| d|           t          |           dS )z@Validate that the subject, predicate, and object are not BNodes.z4Subject, predicate, and object must not be a BNode: z, N)r"   r   rS   r\   )r)   r*   r+   r   s       r   validate_no_bnodesr^      s     	4	
dE""	
 c5!!	
 j%((		
 %% %% %"% %
 
 	
 
#####r!   rY   )r   r   r   r   r   r   )NNN)
r   r   r)   r   r*   r   r+   r   r   r   )T)r   r   r.   r/   r   r   )r3   r4   r   r5   )rI   r#   rJ   r   )r   rV   )
r)   r   r*   r   r+   r   r   rV   r   r   )__doc__
__future__r   r@   typingr$   rdflib.graphr   r   r   rdflib.plugins.sparql.processorr   rdflib.termr   r	   r
   TYPE_CHECKINGrdflib.contrib.rdf4j.clientr   r   r   r(   r-   r2   rH   rU   r\   r^   r   r!   r   <module>rg      sj     " " " " " " 				     A A A A A A A A A A 8 8 8 8 8 8 5 5 5 5 5 5 5 5 5 5? SRRRRRRRRRR
 LP( ( ( ( (> 	! ! ! ! !6 " " " " ""0  0  0  0 fS S S S$< < < <$ $ $ $ $ $r!   