@@ -44,7 +44,6 @@ class TripleStoreEndpointABC(ABC):
4444 This class provides an abstraction for a TripleStore.
4545 """
4646
47- @abstractmethod
4847 def with_query (self , sparql_query : str , substitution_variables : dict = None ,
4948 sparql_prefixes : str = "" ) -> 'TripleStoreEndpointABC' :
5049 """
@@ -54,15 +53,32 @@ def with_query(self, sparql_query: str, substitution_variables: dict = None,
5453 :param sparql_prefixes:
5554 :return:
5655 """
56+ if substitution_variables :
57+ template_query = SubstitutionTemplate (sparql_query )
58+ sparql_query = template_query .safe_substitute (substitution_variables )
59+
60+ sparql_query = (sparql_prefixes + " " + sparql_query ).strip ()
61+ self ._set_sparql_query (sparql_query = sparql_query )
62+ return self
5763
58- @abstractmethod
5964 def with_query_from_file (self , sparql_query_file_path : str , substitution_variables : dict = None ,
60- prefixes : str = "" ) -> 'TripleStoreEndpointABC' :
65+ sparql_prefixes : str = "" ) -> 'TripleStoreEndpointABC' :
6166 """
6267 This method will read a query from a file
6368 :param sparql_query_file_path:
6469 :param substitution_variables:
65- :param prefixes:
70+ :param sparql_prefixes:
71+ :return:
72+ """
73+ sparql_query = Path (sparql_query_file_path ).resolve ().read_text (encoding = "utf-8" )
74+ return self .with_query (sparql_query = sparql_query , substitution_variables = substitution_variables ,
75+ sparql_prefixes = sparql_prefixes )
76+
77+ @abstractmethod
78+ def _set_sparql_query (self , sparql_query : str ):
79+ """
80+ This method is used to set sparql query for future query operation.
81+ :param sparql_query:
6682 :return:
6783 """
6884
@@ -94,39 +110,13 @@ class SPARQLTripleStoreEndpoint(TripleStoreEndpointABC):
94110 def __init__ (self , endpoint_url : str ):
95111 self .endpoint = SPARQLClientPool .create_or_reuse_connection (endpoint_url )
96112
97- def with_query (self , sparql_query : str , substitution_variables : dict = None ,
98- sparql_prefixes : str = "" ) -> TripleStoreEndpointABC :
99- """
100- Set the query text and return the reference to self for chaining.
101- :return:
113+ def _set_sparql_query (self , sparql_query : str ):
102114 """
103- if substitution_variables :
104- template_query = SubstitutionTemplate (sparql_query )
105- sparql_query = template_query .safe_substitute (substitution_variables )
106-
107- new_query = (sparql_prefixes + " " + sparql_query ).strip ()
108-
109- self .endpoint .setQuery (new_query )
110- return self
111-
112- def with_query_from_file (self , sparql_query_file_path : str , substitution_variables : dict = None ,
113- prefixes : str = "" ) -> TripleStoreEndpointABC :
114- """
115- Set the query text and return the reference to self for chaining.
115+ This method is used to set sparql query for future query operation.
116+ :param sparql_query:
116117 :return:
117118 """
118-
119- with open (Path (sparql_query_file_path ).resolve (), 'r' ) as file :
120- query_from_file = file .read ()
121-
122- if substitution_variables :
123- template_query = SubstitutionTemplate (query_from_file )
124- query_from_file = template_query .safe_substitute (substitution_variables )
125-
126- new_query = (prefixes + " " + query_from_file ).strip ()
127-
128- self .endpoint .setQuery (new_query )
129- return self
119+ self .endpoint .setQuery (sparql_query )
130120
131121 def fetch_tabular (self ) -> pd .DataFrame :
132122 """
@@ -174,35 +164,13 @@ def __init__(self, rdf_content: str, rdf_content_format: str = DEFAULT_RDF_FILE_
174164 self .graph .parse (data = rdf_content , format = rdf_content_format )
175165 self .sparql_query = None
176166
177- def with_query (self , sparql_query : str , substitution_variables : dict = None ,
178- sparql_prefixes : str = "" ) -> TripleStoreEndpointABC :
179- """
180- Set the query text and return the reference to self for chaining.
181- :return:
182- """
183- if substitution_variables :
184- template_query = SubstitutionTemplate (sparql_query )
185- sparql_query = template_query .safe_substitute (substitution_variables )
186-
187- new_query = (sparql_prefixes + " " + sparql_query ).strip ()
188- self .sparql_query = new_query
189- return self
190-
191- def with_query_from_file (self , sparql_query_file_path : str , substitution_variables : dict = None ,
192- prefixes : str = "" ) -> TripleStoreEndpointABC :
167+ def _set_sparql_query (self , sparql_query : str ):
193168 """
194- Set the query text and return the reference to self for chaining.
169+ This method is used to set sparql query for future query operation.
170+ :param sparql_query:
195171 :return:
196172 """
197- with open (Path (sparql_query_file_path ).resolve (), 'r' ) as file :
198- query_from_file = file .read ()
199-
200- if substitution_variables :
201- template_query = SubstitutionTemplate (query_from_file )
202- query_from_file = template_query .safe_substitute (substitution_variables )
203-
204- self .sparql_query = (prefixes + " " + query_from_file ).strip ()
205- return self
173+ self .sparql_query = sparql_query
206174
207175 def fetch_tabular (self ) -> pd .DataFrame :
208176 """
@@ -220,7 +188,7 @@ def fetch_tree(self) -> dict:
220188 query_result = self .graph .query (query_object = self .sparql_query )
221189 return json .loads (query_result .serialize (format = "json" ))
222190
223- def fetch_rdf (self ) -> rdflib .query . Result :
191+ def fetch_rdf (self ) -> rdflib .Graph :
224192 """
225193 This method will return the result of the SPARQL query in a RDF format,
226194 use this method only for SPARQL queries of type CONSTRUCT or DESCRIBE.
0 commit comments