Skip to content

Commit

Permalink
sparql query builder separate method for construct queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ssssarah committed Oct 25, 2023
1 parent f58e63f commit 865ed62
Showing 1 changed file with 42 additions and 38 deletions.
80 changes: 42 additions & 38 deletions kgforge/core/commons/sparql_query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,48 +186,52 @@ def build_resource_from_response(
query: str, response: Dict, context: Context, *args, **params
) -> List[Resource]:
_, q_comp = Query.parseString(query)
if q_comp.name == "ConstructQuery":
subject_triples = {}
for r in response["results"]["bindings"]:
subject = r["subject"]["value"]
s = f"<{r['subject']['value']}>"
p = f"<{r['predicate']['value']}>"
if r["object"]["type"] == "uri":
o = f"<{r['object']['value']}>"
else:
if "datatype" in r["object"]:
o = f"\"{r['object']['value']}\"^^<{r['object']['datatype']}>"
else:
o = f"\"{r['object']['value']}\""
if subject in subject_triples:
subject_triples[subject] += f"\n{s} {p} {o} . "
else:
subject_triples[subject] = f"{s} {p} {o} . "

def triples_to_resource(iri, triples):
graph = Graph().parse(data=triples, format="nt")
data_expanded = json.loads(graph.serialize(format="json-ld"))
data_expanded = json.loads(graph.serialize(format="json-ld"))
frame = {"@id": iri}
data_framed = jsonld.frame(data_expanded, frame)
compacted = jsonld.compact(data_framed, context.document)
resource = from_jsonld(compacted)
resource.context = (
context.iri
if context.is_http_iri()
else context.document["@context"]
)
return resource
bindings = response["results"]["bindings"]

return [triples_to_resource(s, t) for s, t in subject_triples.items()]
if q_comp.name == "ConstructQuery":
return SPARQLQueryBuilder.build_resource_from_construct_query(bindings, context)
else:
# SELECT QUERY
return SPARQLQueryBuilder.build_resource_from_select_query(
response["results"]["bindings"]
return SPARQLQueryBuilder.build_resource_from_select_query(bindings)

@staticmethod
def build_resource_from_construct_query(results: List, context: Context) -> List[Resource]:

subject_triples = {}

for r in results:
subject = r["subject"]["value"]
s = f"<{r['subject']['value']}>"
p = f"<{r['predicate']['value']}>"
if r["object"]["type"] == "uri":
o = f"<{r['object']['value']}>"
else:
if "datatype" in r["object"]:
o = f"\"{r['object']['value']}\"^^<{r['object']['datatype']}>"
else:
o = f"\"{r['object']['value']}\""
if subject in subject_triples:
subject_triples[subject] += f"\n{s} {p} {o} . "
else:
subject_triples[subject] = f"{s} {p} {o} . "

def triples_to_resource(iri, triples):
graph = Graph().parse(data=triples, format="nt")
data_expanded = json.loads(graph.serialize(format="json-ld"))
data_framed = jsonld.frame(data_expanded, {"@id": iri})
compacted = jsonld.compact(data_framed, context.document)
resource = from_jsonld(compacted)
resource.context = (
context.iri
if context.is_http_iri()
else context.document["@context"]
)
return resource

return [triples_to_resource(s, t) for s, t in subject_triples.items()]

@staticmethod
def build_resource_from_select_query(results: List):
def build_resource_from_select_query(results: List) -> List[Resource]:

def process_v(v):
if v['type'] == 'literal' and 'datatype' in v and v['datatype'] == \
Expand All @@ -248,7 +252,7 @@ def process_v(v):
for x in results
]


@staticmethod
def rewrite_sparql(query: str, context: Context, metadata_context: Context) -> str:
"""Rewrite local property and type names from Model.template() as IRIs.
Expand Down Expand Up @@ -346,7 +350,6 @@ def _replace_in_sparql(

return qr


@staticmethod
def apply_limit_and_offset_to_query(query, limit, default_limit, offset, default_offset):
if limit:
Expand All @@ -362,5 +365,6 @@ def apply_limit_and_offset_to_query(query, limit, default_limit, offset, default

return query


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

0 comments on commit 865ed62

Please sign in to comment.