
    NgQ)                    .   d dl mZ d dlmZmZmZmZ erd dlZdddddZed	         d
z   Z	ed	         dz   Z
ed	         dz   Zed	         dz   Zed         ed	         z   dz   Zed	         ed         z   dz   Zed	         ed         z   dz   Z G d d          ZdS )    )annotations)TYPE_CHECKINGDictListOptionalNz-PREFIX owl: <http://www.w3.org/2002/07/owl#>
z:PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
z5PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
z0PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
)owlrdfrdfsxsdr
   zeSELECT DISTINCT ?cls ?com
WHERE { 
    ?instance a ?cls . 
    OPTIONAL { ?cls rdfs:comment ?com } 
}zvSELECT DISTINCT ?cls ?com
WHERE { 
    ?instance a/rdfs:subClassOf* ?cls . 
    OPTIONAL { ?cls rdfs:comment ?com } 
}zSELECT DISTINCT ?cls ?com
WHERE { 
    ?instance a/rdfs:subClassOf* ?cls . 
    FILTER (isIRI(?cls)) . 
    OPTIONAL { ?cls rdfs:comment ?com } 
}zdSELECT DISTINCT ?rel ?com
WHERE { 
    ?subj ?rel ?obj . 
    OPTIONAL { ?rel rdfs:comment ?com } 
}r	   z|SELECT DISTINCT ?rel ?com
WHERE { 
    ?rel a/rdfs:subPropertyOf* rdf:Property . 
    OPTIONAL { ?rel rdfs:comment ?com } 
}r   zSELECT DISTINCT ?op ?com
WHERE { 
    ?op a/rdfs:subPropertyOf* owl:ObjectProperty . 
    OPTIONAL { ?op rdfs:comment ?com } 
}zSELECT DISTINCT ?dp ?com
WHERE { 
    ?dp a/rdfs:subPropertyOf* owl:DatatypeProperty . 
    OPTIONAL { ?dp rdfs:comment ?com } 
}c                  |    e Zd ZdZ	 	 	 	 	 	 	 	 dd dZed!d            Zd"dZd#dZe	d$d            Z
d%dZd&dZdS )'RdfGrapha  RDFlib wrapper for graph operations.

    Modes:
    * local: Local file - can be queried and changed
    * online: Online file - can only be queried, changes can be stored locally
    * store: Triple store - can be queried and changed if update_endpoint available
    Together with a source file, the serialization should be specified.

    *Security note*: Make sure that the database connection uses credentials
        that are narrowly-scoped to only include necessary permissions.
        Failure to do so may result in data corruption or loss, since the calling
        code may attempt commands that would result in deletion, mutation
        of data if appropriately prompted or reading sensitive data if such
        data is present in the database.
        The best way to guard against such negative outcomes is to (as appropriate)
        limit the permissions granted to the credentials used with this tool.

        See https://python.langchain.com/docs/security for more information.
    Nttlr	   source_fileOptional[str]serializationquery_endpointupdate_endpointstandard
local_copygraph_kwargsOptional[Dict]store_kwargsreturnNonec	                ~   || _         || _        || _        || _        || _        || _        	 ddl}	ddlm}
 n# t          $ r t          d          w xY w| j        dx}vrt          d| d          |s|r|r|s|rt          d          |rl|                    d	          rd
| _        nd| _        | j        | j         | _         |	j                    | _        | j                            || j                   |r|pi }d| _        |s- |
j        di || _        | j                            |           n. |
j        di || _        | j                            ||f           |pi } |	j        | j        fi || _        t)          | j                  st+          d          d| _        |                                  dS )a  
        Set up the RDFlib graph

        :param source_file: either a path for a local file or a URL
        :param serialization: serialization of the input
        :param query_endpoint: SPARQL endpoint for queries, read access
        :param update_endpoint: SPARQL endpoint for UPDATE queries, write access
        :param standard: RDF, RDFS, or OWL
        :param local_copy: new local copy for storing changes
        :param graph_kwargs: Additional rdflib.Graph specific kwargs
        that will be used to initialize it,
        if query_endpoint is provided.
        :param store_kwargs: Additional sparqlstore.SPARQLStore specific kwargs
        that will be used to initialize it,
        if query_endpoint is provided.
        r   N)sparqlstorezTCould not import rdflib python package. Please install it with `pip install rdflib`.)r	   r
   r   z+Invalid standard. Supported standards are: .zCould not unambiguously initialize the graph wrapper. Specify either a file (local or online) via the source_file or a triple store via the endpoints.httponlinelocal)formatstorezThe graph is empty.  )r   r   r   r   r   r   rdflibrdflib.plugins.storesr   ImportError
ValueError
startswithmodeGraphgraphparseSPARQLStore_storeopenSPARQLUpdateStorelenAssertionErrorschemaload_schema)selfr   r   r   r   r   r   r   r   r%   r   supported_standardss               `/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/graphs/rdf_graph.py__init__zRdfGraph.__init__o   sk   6 '*,. $	MMM9999999 	 	 	?  	
 =8N!N!4OOT>QTTT  
 
	"
	 
	  	
	 $3	
	 7    	E%%f-- 7$		#	?*&*&6DO%DJJ[1CDDD 
	C'-2LDI" D5k5EEEE  0000;k;KKlKK  ./!BCCC'-2L%dkBB\BBDJ 4: 	8 !6777 s	   
7 Astrc                    | j         S )z;
        Returns the schema of the graph database.
        )r4   )r6   s    r8   
get_schemazRdfGraph.get_schema   s    
 {    queryList[rdflib.query.ResultRow]c                    ddl m} ddlm 	 | j                            |          }n# |$ r}t          d|           d}~ww xY wfd|D             S )z"
        Query the graph.
        r   ParserError)	ResultRow&Generated SPARQL statement is invalid
Nc                4    g | ]}t          |          |S r$   )
isinstance).0rrC   s     r8   
<listcomp>z"RdfGraph.query.<locals>.<listcomp>   s(    ;;;a*Q	":":;;;;r=   )rdflib.exceptionsrB   rdflib.queryrC   r,   r>   r(   )r6   r>   rB   reserC   s        @r8   r>   zRdfGraph.query   s     	211111******	O*""5))CC 	O 	O 	OM!MMNNN	O;;;;3;;;;s   * AAAc                2   ddl m} 	 | j                            |           n# |$ r}t	          d|           d}~ww xY w| j        rA| j                            | j        | j                            d          d                    dS t	          d          )	z#
        Update the graph.
        r   rA   rD   Nr   )destinationr!   z5No target file specified for saving the updated file.)rJ   rB   r,   updater(   r   	serializesplit)r6   r>   rB   rM   s       r8   rQ   zRdfGraph.update   s     	211111	OJe$$$$ 	O 	O 	OM!MMNNN	O? 	VJ   ODO4I4I#4N4Nr4R !      TUUUs   # ?:?iric                    d| v r|                      d          d         }n3d| v r|                      d          d         }nt          d|  d          |S )N#rO   /zUnexpected IRI 'z ', contains neither '#' nor '/'.)rS   r(   )rT   
local_names     r8   _get_local_namezRdfGraph._get_local_name   s`    #::3+JJCZZ3+JJUUUUVVVr=   rL   rdflib.query.ResultRowvarc                    dt          ||                   z   dz   |                     ||                   z   dz   t          |d                   z   dz   S )N<z> (, com))r:   rY   )r6   rL   r[   s      r8   _res_to_strzRdfGraph._res_to_str   sh    #c(mm ""3s8,,- 	
 #e*oo 	
r=   c           	         d fd} j         dk    rG                     t                    }                     t                    } |||           _        dS  j         dk    rG                     t
                    }                     t                    } |||           _        dS  j         d	k    r                     t                    }                     t                    }                     t                    }d
d
                     fd|D                        dd
                     fd|D                        dd
                     fd|D                        d _        dS t          d j          d          )z4
        Load the graph schema information.
        classesr?   relationshipsr   r:   c                    dd                     fd| D                        dd                     fd|D                        dS )NzIn the following, each IRI is followed by the local name and optionally its description in parentheses. 
The RDF graph supports the following node types:
r^   c                <    g | ]}                     |d           S clsra   rG   rH   r6   s     r8   rI   z?RdfGraph.load_schema.<locals>._rdf_s_schema.<locals>.<listcomp>  s)    JJJQd..q%88JJJr=   z5
The RDF graph supports the following relationships:
c                <    g | ]}                     |d           S )relri   rj   s     r8   rI   z?RdfGraph.load_schema.<locals>._rdf_s_schema.<locals>.<listcomp>  s)    PPPQd..q%88PPPr=   
)join)rc   rd   r6   s     r8   _rdf_s_schemaz+RdfGraph.load_schema.<locals>._rdf_s_schema  sx    
V 99JJJJ'JJJKKV V
 99PPPP-PPPQQV V Vr=   r	   r
   r   zIn the following, each IRI is followed by the local name and optionally its description in parentheses. 
The OWL graph supports the following node types:
r^   c                <    g | ]}                     |d           S rg   ri   rj   s     r8   rI   z(RdfGraph.load_schema.<locals>.<listcomp>*  s)    GGGQd..q%88GGGr=   z^
The OWL graph supports the following object properties, i.e., relationships between objects:
c                <    g | ]}                     |d           S )opri   rj   s     r8   rI   z(RdfGraph.load_schema.<locals>.<listcomp>-  )    EEEAd..q$77EEEr=   zi
The OWL graph supports the following data properties, i.e., relationships between objects and literals:
c                <    g | ]}                     |d           S )dpri   rj   s     r8   rI   z(RdfGraph.load_schema.<locals>.<listcomp>0  rs   r=   rm   zMode 'z' is currently not supported.N)rc   r?   rd   r?   r   r:   )r   r>   cls_query_rdfrel_query_rdfr4   cls_query_rdfsrel_query_rdfscls_query_owlop_query_owldp_query_owlrn   r(   )r6   ro   clssrelsopsdpss   `     r8   r5   zRdfGraph.load_schema  s   
	 	 	 	 	 	 =E!!::m,,D::m,,D'-d33DKKK]f$$::n--D::n--D'-d33DKKK]e##::m,,D**\**C**\**C	K 99GGGG$GGGHH	K 	K 99EEEEEEEFF	K 	K 99EEEEEEEFF	K 	K 	K KKK RdmRRRSSSr=   )Nr   NNr	   NNN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r:   )r>   r:   r   r?   )r>   r:   r   r   )rT   r:   r   r:   )rL   rZ   r[   r:   r   r:   )r   r   )__name__
__module____qualname____doc__r9   propertyr<   r>   rQ   staticmethodrY   ra   r5   r$   r=   r8   r   r   Z   s         , &*',(,)-"'$('+'+W W W W Wr    X< < < < V V V V(    \	
 	
 	
 	
+T +T +T +T +T +Tr=   r   )
__future__r   typingr   r   r   r   r%   prefixesrv   rx   rz   rw   ry   r{   r|   r   r$   r=   r8   <module>r      s   " " " " " "             MMM @LHB	    &!     UOv	  Vuo	  Vuo	 YT YT YT YT YT YT YT YT YT YTr=   