Skip to content

Commit 19cd8d6

Browse files
committed
sparql query builder separate method for construct queries
1 parent bd67d1b commit 19cd8d6

1 file changed

Lines changed: 42 additions & 38 deletions

File tree

kgforge/core/commons/sparql_query_builder.py

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -184,48 +184,52 @@ def build_resource_from_response(
184184
query: str, response: Dict, context: Context, *args, **params
185185
) -> List[Resource]:
186186
_, q_comp = Query.parseString(query)
187-
if q_comp.name == "ConstructQuery":
188-
subject_triples = {}
189-
for r in response["results"]["bindings"]:
190-
subject = r["subject"]["value"]
191-
s = f"<{r['subject']['value']}>"
192-
p = f"<{r['predicate']['value']}>"
193-
if r["object"]["type"] == "uri":
194-
o = f"<{r['object']['value']}>"
195-
else:
196-
if "datatype" in r["object"]:
197-
o = f"\"{r['object']['value']}\"^^<{r['object']['datatype']}>"
198-
else:
199-
o = f"\"{r['object']['value']}\""
200-
if subject in subject_triples:
201-
subject_triples[subject] += f"\n{s} {p} {o} . "
202-
else:
203-
subject_triples[subject] = f"{s} {p} {o} . "
204-
205-
def triples_to_resource(iri, triples):
206-
graph = Graph().parse(data=triples, format="nt")
207-
data_expanded = json.loads(graph.serialize(format="json-ld"))
208-
data_expanded = json.loads(graph.serialize(format="json-ld"))
209-
frame = {"@id": iri}
210-
data_framed = jsonld.frame(data_expanded, frame)
211-
compacted = jsonld.compact(data_framed, context.document)
212-
resource = from_jsonld(compacted)
213-
resource.context = (
214-
context.iri
215-
if context.is_http_iri()
216-
else context.document["@context"]
217-
)
218-
return resource
187+
bindings = response["results"]["bindings"]
219188

220-
return [triples_to_resource(s, t) for s, t in subject_triples.items()]
189+
if q_comp.name == "ConstructQuery":
190+
return SPARQLQueryBuilder.build_resource_from_construct_query(bindings, context)
221191
else:
222192
# SELECT QUERY
223-
return SPARQLQueryBuilder.build_resource_from_select_query(
224-
response["results"]["bindings"]
193+
return SPARQLQueryBuilder.build_resource_from_select_query(bindings)
194+
195+
@staticmethod
196+
def build_resource_from_construct_query(results: List, context: Context) -> List[Resource]:
197+
198+
subject_triples = {}
199+
200+
for r in results:
201+
subject = r["subject"]["value"]
202+
s = f"<{r['subject']['value']}>"
203+
p = f"<{r['predicate']['value']}>"
204+
if r["object"]["type"] == "uri":
205+
o = f"<{r['object']['value']}>"
206+
else:
207+
if "datatype" in r["object"]:
208+
o = f"\"{r['object']['value']}\"^^<{r['object']['datatype']}>"
209+
else:
210+
o = f"\"{r['object']['value']}\""
211+
if subject in subject_triples:
212+
subject_triples[subject] += f"\n{s} {p} {o} . "
213+
else:
214+
subject_triples[subject] = f"{s} {p} {o} . "
215+
216+
def triples_to_resource(iri, triples):
217+
graph = Graph().parse(data=triples, format="nt")
218+
data_expanded = json.loads(graph.serialize(format="json-ld"))
219+
data_framed = jsonld.frame(data_expanded, {"@id": iri})
220+
compacted = jsonld.compact(data_framed, context.document)
221+
resource = from_jsonld(compacted)
222+
resource.context = (
223+
context.iri
224+
if context.is_http_iri()
225+
else context.document["@context"]
225226
)
227+
return resource
228+
229+
return [triples_to_resource(s, t) for s, t in subject_triples.items()]
226230

227231
@staticmethod
228-
def build_resource_from_select_query(results: List):
232+
def build_resource_from_select_query(results: List) -> List[Resource]:
229233

230234
def process_v(v):
231235
if v['type'] == 'literal' and 'datatype' in v and v['datatype'] == \
@@ -246,7 +250,7 @@ def process_v(v):
246250
for x in results
247251
]
248252

249-
253+
@staticmethod
250254
def rewrite_sparql(query: str, context: Context, metadata_context: Context) -> str:
251255
"""Rewrite local property and type names from Model.template() as IRIs.
252256
@@ -344,7 +348,6 @@ def _replace_in_sparql(
344348

345349
return qr
346350

347-
348351
@staticmethod
349352
def apply_limit_and_offset_to_query(query, limit, default_limit, offset, default_offset):
350353
if limit:
@@ -360,5 +363,6 @@ def apply_limit_and_offset_to_query(query, limit, default_limit, offset, default
360363

361364
return query
362365

366+
363367
def _box_value_as_full_iri(value):
364368
return f"<{value}>" if is_valid_url(value) else value

0 commit comments

Comments
 (0)