-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_query_executor.py
More file actions
67 lines (57 loc) · 2.35 KB
/
local_query_executor.py
File metadata and controls
67 lines (57 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# basic imports
import pprint
import logging
import logging_config as lc
# graph imports
import rdflib
from rdflib.util import from_n3
from rdflib import RDF, Graph, Namespace, URIRef, Literal
from rdflib.plugins.sparql.parser import parseQuery
# enable logging
logger = logging.getLogger(__name__)
logger.setLevel(lc.LOG_LEVEL)
####################################
# QUERY EXECUTION FUNCTIONS #
####################################
def executeQuery(graph: Graph, query: str) -> dict:
# run the original query on the graph to get the results
logger.debug(f"Query to be executed on local graph is: {query}")
parsed_query = parseQuery(query)
if parsed_query[1].name == "SelectQuery":
result = graph.query(query)
# the result object should contain bindings and vars
logger.debug(f'Result of the SELECT query when executed on the local graph is: {result.bindings}')
# reformat the result into a SPARQL 1.1 JSON result structure
json_result = reformatResultIntoSPARQLJson(result)
if parsed_query[1].name == "AskQuery":
result = graph.query(query)
# the result object should contain an askAnswer field
logger.debug(f"Result of the ASK query when executed on the local graph is: {result.askAnswer}")
json_result = {
"head" : {},
"boolean": result.askAnswer
}
return json_result
def reformatResultIntoSPARQLJson(result:dict) -> dict:
json_result = {
"head" : { "vars": [str(var) for var in result.vars]
},
"results": {
"bindings": []
}
}
if result.bindings != []:
bindings = []
for binding in result.bindings:
b = {}
for key in binding:
if isinstance(binding[key],rdflib.term.Literal):
if binding[key].datatype == None:
b[str(key)] = {"type": "literal", "value": str(binding[key])}
else:
b[str(key)] = {"type": "typed-literal", "datatype": str(binding[key].datatype), "value": str(binding[key])}
if isinstance(binding[key],rdflib.term.URIRef):
b[str(key)] = {"type": "uri", "value": str(binding[key])}
bindings.append(b)
json_result["results"] = {"bindings": bindings}
return json_result