Ë
    µŒjF  ã                  óP   — d dl mZ d dlZd dlmZmZmZmZ erd dlZ G d„ d«      Z	y)é    )ÚannotationsN)ÚTYPE_CHECKINGÚListÚOptionalÚUnionc                  ó¨   — e Zd ZdZ	 	 	 d	 	 	 	 	 	 	 	 	 dd„Zedd„«       Zdd„Ze	 d	 	 	 	 	 dd„«       Zedd„«       Z	dd„Z
edd	„«       Z	 	 	 	 dd
„Zy)ÚOntotextGraphDBGraphaè  Ontotext GraphDB https://graphdb.ontotext.com/ wrapper for graph operations.

    *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.
    Nc                óÜ  — |r|rt        d«      ‚|s|st        d«      ‚	 ddl}ddlm} | j                  «       }|j                  |¬«      }|j                  |«        |j                  |dd¬	«      | _	        | j                  «        |r| j                  ||«      }	n"| j                  |«       | j                  |«      }	|	j                  d
¬«      | _        y# t        $ r t	        d«      ‚w xY w)a+  
        Set up the GraphDB wrapper

        :param query_endpoint: SPARQL endpoint for queries, read access

        If GraphDB is secured,
        set the environment variables 'GRAPHDB_USERNAME' and 'GRAPHDB_PASSWORD'.

        :param query_ontology: a `CONSTRUCT` query that is executed
        on the SPARQL endpoint and returns the KG schema statements
        Example:
        'CONSTRUCT {?s ?p ?o} FROM <https://example.com/ontology/> WHERE {?s ?p ?o}'
        Currently, DESCRIBE queries like
        'PREFIX onto: <https://example.com/ontology/>
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        DESCRIBE ?term WHERE {
            ?term rdfs:isDefinedBy onto:
        }'
        are not supported, because DESCRIBE returns
        the Symmetric Concise Bounded Description (SCBD),
        i.e. also the incoming class links.
        In case of large graphs with a million of instances, this is not efficient.
        Check https://github.com/eclipse-rdf4j/rdf4j/issues/4857

        :param local_file: a local RDF ontology file.
        Supported RDF formats:
        Turtle, RDF/XML, JSON-LD, N-Triples, Notation-3, Trig, Trix, N-Quads.
        If the rdf format can't be determined from the file extension,
        pass explicitly the rdf format in `local_file_format` param.

        :param local_file_format: Used if the rdf format can't be determined
        from the local file extension.
        One of "json-ld", "xml", "n3", "turtle", "nt", "trig", "nquads", "trix"

        Either `query_ontology` or `local_file` should be passed.
        z2Both file and query provided. Only one is allowed.z1Neither file nor query provided. One is required.r   N)ÚsparqlstorezTCould not import rdflib python package. Please install it with `pip install rdflib`.)ÚauthÚnone)Ú
identifierÚbind_namespacesÚturtle©Úformat)Ú
ValueErrorÚrdflibÚrdflib.plugins.storesr   ÚImportErrorÚ	_get_authÚSPARQLStoreÚopenÚGraphÚgraphÚ_check_connectivityÚ_load_ontology_schema_from_fileÚ_validate_user_queryÚ _load_ontology_schema_with_queryÚ	serializeÚschema)
ÚselfÚquery_endpointÚquery_ontologyÚ
local_fileÚlocal_file_formatr   r   r   ÚstoreÚontology_schema_graphs
             ú{/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langchain_community/graphs/ontotext_graphdb_graph.pyÚ__init__zOntotextGraphDBGraph.__init__   sû   € ñX ™jÜÐQÓRÐRá¡jÜÐPÓQÐQð	ÛÝ9ð �~‰~ÓˆØ×'Ñ'¨TÐ'Ó2ˆØ�
‰
�>Ô"à!�V—\‘\ %°DÈ&ÔQˆŒ
Ø× Ñ Ô"ñ Ø$(×$HÑ$HØØ!ó%Ñ!ð
 ×%Ñ% nÔ5Ø$(×$IÑ$IØó%Ð!ð ,×5Ñ5¸XÐ5ÓFˆ�øô1 ò 	Üð?óð ð	ús    
C ÃC+c                 óª   — t         j                  j                  dd«      } t         j                  j                  dd«      }| r|st        d«      ‚| |fS y)z@
        Returns the basic authentication configuration
        ÚGRAPHDB_USERNAMENÚGRAPHDB_PASSWORDzREnvironment variable 'GRAPHDB_USERNAME' is set, but 'GRAPHDB_PASSWORD' is not set.)ÚosÚenvironÚgetr   )ÚusernameÚpasswords     r)   r   zOntotextGraphDBGraph._get_authm   sV   € ô
 —:‘:—>‘>Ð"4°dÓ;ˆÜ—:‘:—>‘>Ð"4°dÓ;ˆáÙÜ ð9óð ð
   Ð)Ð)Øó    c                ól   — 	 | j                   j                  d«       y# t        $ r t        d«      ‚w xY w)zE
        Executes a simple `ASK` query to check connectivity
        zASK { ?s ?p ?o }züCould not query the provided endpoint. Please, check, if the value of the provided query_endpoint points to the right repository. If GraphDB is secured, please, make sure that the environment variables 'GRAPHDB_USERNAME' and 'GRAPHDB_PASSWORD' are set.N)r   Úqueryr   ©r"   s    r)   r   z(OntotextGraphDBGraph._check_connectivity   s<   € ð
	Ø�J‰J×ÑÐ/Õ0øÜò 	ÜðEóð ð	ús   ‚ ž3c                ó\  — ddl }t        j                  j                  | «      st	        d| › d�«      ‚t        j
                  | t        j                  «      st        d| › d�«      ‚ |j                  «       }	 |j                  | |¬«       |S # t        $ r}t        d| › d	�|«      ‚d}~ww xY w)
zM
        Parse the ontology schema statements from the provided file
        r   NzFile z does not exist.zRead permission for z is restrictedr   zInvalid file format for z : )r   r.   ÚpathÚexistsÚFileNotFoundErrorÚaccessÚR_OKÚPermissionErrorÚConjunctiveGraphÚparseÚ	Exceptionr   )r%   r&   r   r   Úes        r)   r   z4OntotextGraphDBGraph._load_ontology_schema_from_file�   s¬   € ó 	ä�w‰w�~‰~˜jÔ)Ü# e¨J¨<Ð7GÐ$HÓIÐIÜ�y‰y˜¤R§W¡WÔ-Ü!Ð$8¸¸ÀNÐ"SÓTÐTØ'�×'Ñ'Ó)ˆð	LØ�K‰K˜
Ð+<ˆKÔ=ð ˆøô ò 	LÜÐ7¸
°|À3ÐGÈÓKÐKûð	Lús   Á8B Â	B+ÂB&Â&B+c                óÞ   — ddl m} ddlm} t	        | t
        «      st        d«      ‚	  || «      }|j                  j                  dk7  rt        d«      ‚y# |$ r}t        d|«      ‚d}~ww xY w)	zF
        Validate the query is a valid SPARQL CONSTRUCT query
        r   )ÚParseException)ÚprepareQueryz*Ontology query must be provided as string.z+Ontology query is not a valid SPARQL query.NÚConstructQueryz9Invalid query type. Only CONSTRUCT queries are supported.)
Ú	pyparsingrC   Úrdflib.plugins.sparqlrD   Ú
isinstanceÚstrÚ	TypeErrorr   ÚalgebraÚname)r$   rC   rD   Úparsed_queryrA   s        r)   r   z)OntotextGraphDBGraph._validate_user_query£   s~   € õ
 	-Ý6ä˜.¬#Ô.ÜÐHÓIÐIð	OÙ'¨Ó7ˆLð ×Ñ×$Ñ$Ð(8Ò8ÜØKóð ð 9øð ò 	OÜÐJÈAÓNÐNûð	Oús   ©A ÁA,ÁA'Á'A,c                óÂ   — ddl m} 	 | j                  j                  |«      }|j                  st	        d«      ‚|j                  S # |$ r}t	        d|› �«      ‚d}~ww xY w)zQ
        Execute the query for collecting the ontology schema statements
        r   )ÚParserErrorz&Generated SPARQL statement is invalid
NzMissing graph in results.)Úrdflib.exceptionsrO   r   r5   r   )r"   r5   rO   ÚresultsrA   s        r)   r   z5OntotextGraphDBGraph._load_ontology_schema_with_query·   sg   € õ 	2ð	LØ—j‘j×&Ñ& uÓ-ˆGð �}Š}ÜÐ8Ó9Ð9à�}‰}Ðøð ò 	LÜÐFÀqÀcÐJÓKÐKûð	Lús   ˆA ÁAÁAÁAc                ó   — | j                   S )zK
        Returns the schema of the graph database in turtle format
        )r!   r6   s    r)   Ú
get_schemazOntotextGraphDBGraph.get_schemaÇ   s   € ð
 �{‰{Ðr3   c                óˆ   — ddl m} | j                  j                  |«      }|D �cg c]  }t	        ||«      sŒ|‘Œ c}S c c}w )z"
        Query the graph.
        r   )Ú	ResultRow)Úrdflib.queryrU   r   r5   rH   )r"   r5   rU   ÚresÚrs        r)   r5   zOntotextGraphDBGraph.queryÎ   s<   € õ 	+à�j‰j×Ñ˜uÓ%ˆÙÓ;™3�a¤*¨Q°	Õ":’˜3Ñ;Ð;ùÒ;s   ¦?¸?)NNN)
r#   rI   r$   úOptional[str]r%   rY   r&   rY   ÚreturnÚNone)rZ   zUnion[tuple, None])rZ   r[   )N)r%   rI   r&   rY   rZ   z'rdflib.ConjunctiveGraph')r$   rI   rZ   r[   )r5   rI   rZ   z'rdflib.Graph')rZ   rI   )r5   rI   rZ   zList[rdflib.query.ResultRow])Ú__name__Ú
__module__Ú__qualname__Ú__doc__r*   Ústaticmethodr   r   r   r   r   ÚpropertyrS   r5   © r3   r)   r	   r	      sà   „ ñð" )-Ø$(Ø+/ðMGàðMGð &ðMGð "ð	MGð
 )ðMGð 
óMGð^ òó ðó"ð  à<@ðØðØ,9ðà	"òó ðð& òó ðó&ð  òó ðð
<àð
<ð 
&ô
<r3   r	   )
Ú
__future__r   r.   Útypingr   r   r   r   r   r	   rb   r3   r)   Ú<module>re      s)   ðÝ "ã 	÷ó ñ Û÷I<ò I<r3   