
    NgK(                     j    d dl Z d dlmZ d dlmZmZmZmZ d dlZdZ	dZ
dde	e
dZ G d d          ZdS )	    N)SimpleNamespace)AnyDictOptionalSequencezD
SELECT DISTINCT ?elem 
WHERE { 
 ?elem a owl:DatatypeProperty . 
}
zB
SELECT DISTINCT ?elem 
WHERE { 
 ?elem a owl:ObjectProperty . 
}
)classesrelsdtpropsopropsc                   4   e Zd ZdZ	 	 	 	 	 	 	 	 ddeded	ed
ededee         dee         dededdfdZ	e
defd            Ze
deeef         fd            Zdeeef         fdZdedeeef         fdZdeeef         ddfdZdedee         fdZddZdS )NeptuneRdfGrapha  Neptune wrapper for RDF graph operations.

    Args:
        host: endpoint for the database instance
        port: port number for the database instance, default is 8182
        use_iam_auth: boolean indicating IAM auth is enabled in Neptune cluster
        use_https: whether to use secure connection, default is True
        client: optional boto3 Neptune client
        credentials_profile_name: optional AWS profile name
        region_name: optional AWS region, e.g., us-west-2
        service: optional service name, default is neptunedata
        sign: optional, whether to sign the request payload, default is True

    Example:
        .. code-block:: python

        graph = NeptuneRdfGraph(
            host='<SPARQL host'>,
            port=<SPARQL port>
        )
        schema = graph.get_schema()

        OR
        graph = NeptuneRdfGraph(
            host='<SPARQL host'>,
            port=<SPARQL port>
        )
        schema_elem = graph.get_schema_elements()
        #... change schema_elements ...
        graph.load_schema(schema_elem)

    *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.
      TFNneptunedatahostport	use_httpsuse_iam_authclientcredentials_profile_nameregion_nameservicesignreturnc
                    || _         || _        d| d| d| _        	 ||| _        ndd l}
||
                    |          | _        n|
                                | _        i }|r||d<   |rdnd}| d	| d| |d
<   |	r | j        j        |fi || _        n2ddlm} ddl	m
}  | j        j        |fi |d ||          i| _        nb# t          $ r t          d          t          $ r=}t          |          j        dk    rt          d          |t          d          |d }~ww xY wd| _        i | _        |                                  d S )Nzhttps://:z/sparqlr   )profile_namer   httpshttpz://endpoint_url)UNSIGNED)Configconfig)signature_versionzRCould not import boto3 python package. Please install it with `pip install boto3`.UnknownServiceErrorzgNeptuneGraph requires a boto3 version 1.28.38 or greater.Please install it with `pip install -U boto3`.zCould not load credentials to authenticate with AWS client. Please check that credentials in the specified profile name are valid. )r   r   query_endpointr   boto3Sessionsessionbotocorer    botocore.configr!   ImportError	Exceptiontype__name__
ValueErrorschemaschema_elements_refresh_schema)selfr   r   r   r   r   r   r   r   r   r'   client_paramsprotocolr    r!   es                   h/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/graphs/neptune_rdf_graph.py__init__zNeptuneRdfGraph.__init__K   s    )&======0	!$+7#(==>V=#W#WDLL $)==??DL " ?3>M-0&/;77V3;0M0M0M0Mt0M0Mn- 
"5$,"5g"O"O"O"ODKK111111666666"5$,"5# #'# #  &vAAA# # #DK  	 	 	>    	 	 	Aww#888!E  
 !.  		 /1s   B.C "D,/8D''D,c                     | j         S )z;
        Returns the schema of the graph database.
        )r1   r4   s    r8   
get_schemazNeptuneRdfGraph.get_schema   s    
 {    c                     | j         S )N)r2   r;   s    r8   get_schema_elementsz#NeptuneRdfGraph.get_schema_elements   s    ##r=   c                 8    | j                             d          S )z\
        Obtain Neptune statistical summary of classes and predicates in the graph.
        detailed)mode)r   get_rdf_graph_summaryr;   s    r8   get_summaryzNeptuneRdfGraph.get_summary   s     {00j0AAAr=   queryc                    d|i}|}d}| j         r| j                                        }|                                }|j        }|j        }d}|j        }	d}
t          |||	| j                  }ddl	m
}  |d| j        ||
          }dd	lm}  |||| j                                      |           d
|j        d<   |j        }ni }d
|d<   t!          j        d| j        ||          }t%          j        |j                  }|S )z$
        Run Neptune query.
        rE   Nz
neptune-db)
access_key
secret_keytokenregionr   )
AWSRequestPOST)methodurldataparams)	SigV4Authz!application/x-www-form-urlencodedzContent-Type)rM   rN   headersrO   )r   r)   get_credentialsget_frozen_credentialsrG   rH   rI   r   r   botocore.awsrequestrK   r&   botocore.authrQ   add_authrR   requestsrequestjsonloadstext)r4   rE   request_datarO   request_hdrcredentialsrG   rH   r   session_tokenrP   credsrK   rY   rQ   queryres	json_resps                    r8   rE   zNeptuneRdfGraph.query   s\     ' 	N,6688K%<<>>K$/J$/J"G'-MF#%%#'	  E 766666 j4#6T&  G 0/////IeWd&677@@III.QGON+!/KKK*MK'#t2Kd
 
 
 Jx}--	r=   r2   c           	         i }t           D ]U}g }||         D ]0}|d         }|d         }d| d| d}|                    |           1d                    |          ||<   Vd|d          d	|d
          d|d          d|d          | _        dS )z
        Generates and sets schema from schema_elements. Helpful in
        cases where introspected schema needs pruning.
        urilocal<z> ()z, zIn the following, each IRI is followed by the local name and optionally its description in parentheses. 
The graph supports the following node types:
r   z1
The graph supports the following relationships:
r	   z9
The graph supports the following OWL object properties:
r
   z7
The graph supports the following OWL data properties:
r   N)
ELEM_TYPESappendjoinr1   )	r4   r2   elem_strelemres_listelem_recre   rf   res_strs	            r8   load_schemazNeptuneRdfGraph.load_schema   s      	1 	1DH+D1 ) )uo ).c..e...((((!YYx00HTNN	$ 	"	$ 	$
 	$ 	$ 	"	$ 	$ !	$ 	$ 	r=   iric                 "   d|v r(|                     d          }|d          d|d         gS d|v rM|                     d          }d                    |dt          |          dz
                      d|d         gS t          d| d          )z1
        Split IRI into prefix and local
        #r   /   zUnexpected IRI 'z ', contains neither '#' nor '/'.)splitrk   lenr0   )r4   rr   tokenss      r8   _get_local_namezNeptuneRdfGraph._get_local_name   s     #::YYs^^FayOOOVBZ00CZZYYs^^FxxqVQ 788;;;VBZHHUUUUVVVr=   c                 "   i | j         d<   |                                 }g }|d         d         d         D ]d}|}|                     |          }||d         d}|                    |           |d         | j         d         vrd| j         d         |d         <   e|| j         d<   g }|d         d         d	         D ]i}|D ]d}|}|                     |          }||d         d}|                    |           |d         | j         d         vrd| j         d         |d         <   ej|| j         d
<   t          D ]}	t                              |	          }
|
s|                     |
          }g }|d         d         D ]p}|d         d         }|                     |          }||d         d}|                    |           |d         | j         d         vrd| j         d         |d         <   q|| j         |	<   |                     | j                    dS )z5
        Query Neptune to introspect schema.
        distinct_prefixespayloadgraphSummaryr   rw   )re   rf   r   y
predicatesr	   resultsbindingsrm   valueN)r2   rD   r{   rj   ri   getrE   rq   )r4   summaryreslistcre   rz   elem_recordrprm   qitemss               r8   r3   zNeptuneRdfGraph._refresh_schema   su    5701 ""$$#N3I> 	K 	KAC))#..F"%q	::KNN;'''ay 45H IIIGJ$%89&)D*1Y'#N3LA 	O 	OA O O--c22&)F1I>>{+++!9D$89L$MMMKND()<=fQiHO (/V$  	1 	1Dt$$A JJqMMEG9%j1 O Oi(--c22&)F1I>>{+++!9D$89L$MMMKND()<=fQiH)0D &&-.....r=   )r   TFNNNr   T)r   N)r/   
__module____qualname____doc__strintboolr   r   r9   propertyr<   r   r?   rD   rE   rq   r   r{   r3    r=   r8   r   r      s       ) )\ "26%)$E EE E 	E
 E E #+3-E c]E E E 
E E E EN C    X $T#s(^ $ $ $ X$BT#s(^ B B B B++ 
c3h+ + + +Z
4S> 
d 
 
 
 
:W3 W8C= W W W W./ ./ ./ ./ ./ ./r=   r   )rZ   typesr   typingr   r   r   r   rX   DTPROP_QUERYOPROP_QUERYri   r   r   r=   r8   <module>r      s     ! ! ! ! ! ! 0 0 0 0 0 0 0 0 0 0 0 0  	 
I/ I/ I/ I/ I/ I/ I/ I/ I/ I/r=   