-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.py
247 lines (219 loc) · 9.62 KB
/
query.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import xml.etree.ElementTree
import fyzz
import random
import collections
from util import decompose_triple, get_connected_group, get_all_connected_groups, var_to_str, custom_prefixes, ConfigSectionMap
from unification import var, Var
from SPARQLWrapper import SPARQLWrapper, JSON
from prefix import Prefix
ENDPOINT=ConfigSectionMap("Endpoint")['url']
GRAPH_URI=ConfigSectionMap("Graph")['uri']+ConfigSectionMap("Graph")['name']+"/"
class Query(object):
"""SPARQL query handling methods"""
def __init__(self, select, where):
self.select = select
self.where = where
self.const_res_set = dict()
def __str__(self):
select_str = "SELECT "
for var in self.select:
select_str += var + " "
return select_str + "WHERE { " + ' '.join(self.where) + " }"
def __eq__(self, other):
return (self.select == other.select) and (self.where == other.where)
def str_count(self):
return "SELECT COUNT (*) WHERE { " + ' '.join(self.where) + " }"
def evaluate(self, graph, prefixes):
"""
Evaluates the SPARQL query for a given Query object.
Return an iterable query result
"""
select_str = "SELECT "
for var in self.select:
select_str += var + " "
select_str += "\n"
return graph.query(Prefix.writePrefixes(custom_prefixes(), "SPARQL") + select_str +
"WHERE { " + '\n'.join(self.where) + "}")
@staticmethod
def parse_gmark_queries(xml_file):
"""Converts an XML Gmark query node to a Query object."""
res = []
e = xml.etree.ElementTree.parse(xml_file).getroot()
for q in e.findall('query'):
query = Query([], [])
if q.find('head') is not None:
for v in q.find('head').findall('var'):
query.select.append(v.text)
if q.find('bodies') is not None:
for c in q.find('bodies').find('body').findall('conjunct'):
s = c.find('disj').find('concat').find('symbol')
if s.get('inverse') == 'true':
query.where.append(c.get('trg') + " " + s.text + " " + c.get('src') + " .")
else:
query.where.append(c.get('src') + " " + s.text + " " + c.get('trg') + " .")
res.append(query)
return res
@staticmethod
def parse_txt_queries(p_pol_size):
"""Parses textual policies files to a set of queries"""
root_path = "./conf/workloads/policies/"+ConfigSectionMap("Graph")['name']+"/"
queries_str = []
for i in range(1,p_pol_size+1):
with open(root_path+'p'+str(i)+'.rq', 'r') as f:
queries_str.append(f.read())
queries = []
for q_str in queries_str:
q = Query([], [])
fyzz_q = fyzz.parse(q_str)
for sel in fyzz_q.selected:
q.select.append('?'+sel.name)
for wh in fyzz_q.where:
wh_str = ""
for wh_part in range(0,3):
print str(wh[wh_part]) + ": " + str(type(wh[wh_part]))
if type(wh[wh_part]) is fyzz.ast.SparqlVar:
wh_str += '?' + wh[wh_part].name + ' '
elif type(wh[wh_part]) is fyzz.ast.SparqlLiteral:
wh_str += wh[wh_part].value + ' '
elif type(wh[wh_part]) is tuple: #for IRIs
if ((wh[wh_part][0] + wh[wh_part][1]) == "a"):
wh_str += "a "
else:
wh_str += '<' + wh[wh_part][0] + wh[wh_part][1] + '> '
elif type(wh[wh_part]) is str:
wh_str += wh[wh_part] + ' '
wh_str += "."
q.where.append(wh_str)
queries.append(q)
return queries
def get_connected_components(self):
graph_dic = collections.OrderedDict()
for t in self.where:
(s,_,o) = decompose_triple(t)
if s not in graph_dic:
graph_dic[s] = set()
if o not in graph_dic:
graph_dic[o] = set()
graph_dic[s].add(o)
graph_dic[o].add(s)
#print graph_dic
components = get_all_connected_groups(graph_dic)
#print components
res_components = []
res_vars = []
res_ind = 0
for c in components:
res_vars.append(set())
res_components.append([])
for t in self.where:
(s,p,o) = decompose_triple(t)
if s in c and o in c:
if var_to_str(s) in self.select:
res_vars[res_ind].add(var_to_str(s))
if var_to_str(p) in self.select:
res_vars[res_ind].add(var_to_str(p))
if var_to_str(o) in self.select:
res_vars[res_ind].add(var_to_str(o))
res_components[res_ind].append(t)
res_ind += 1
for i in range(len(res_vars)):
print "Vars: " + str(res_vars[i]) + " / CC : " + str(res_components[i])
return res_vars, res_components
def get_consts(self, prefixes, nb_mutations):
"""Get possible values for a given query."""
print "Fetching all values for this query's variables..."
sparql = SPARQLWrapper(ENDPOINT)
var_set = set()
for t in self.where:
(s,p,o) = decompose_triple(t)
if (type(s) == Var):
var_set.add(s)
if (type(p) == Var):
var_set.add(p)
if (type(o) == Var):
var_set.add(o)
print var_set
for v in var_set:
v = var_to_str(v)
print "\tRunning query for "+ str(v) + "..."
query_str = "DEFINE sql:log-enable 3 \n" + Prefix.writePrefixes(prefixes, "SPARQL") + " " \
"SELECT "+v+", COUNT(*) AS ?nb " + \
"FROM <"+GRAPH_URI+"> " + \
"WHERE { " + ' '.join(self.where) + "} " + \
"ORDER BY DESC(?nb) " + \
"LIMIT " + str(nb_mutations)
print query_str
sparql.setQuery(query_str)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
# print results
print "\tQuery done. Storing results..."
v = v[1:]
self.const_res_set[v] = []
ind = 0
for result in results["results"]["bindings"]:
if '"' in result[v]['value']:
continue
ind += 1
if (ind % 100 == 0):
print "\t" + str(ind)
self.const_res_set[v].append(result[v]['value'])
print "\tDone! "+str(ind)+" values found for this variable"
# query_str = Prefix.writePrefixes(prefixes, "SPARQL") + \
# " SELECT * FROM <"+GRAPH_URI+"> " + \
# "WHERE { " + ' '.join(self.where) + "}"
# sparql.setQuery(query_str)
# # print query_str
# sparql.setReturnFormat(JSON)
# results = sparql.query().convert()
# # print results
# res = dict()
# for var in self.select:
# var = var[1:]
# res[var] = []
# for result in results["results"]["bindings"]:
# res[var].append(result[var]['value'])
def var_to_const(self):
"""Changes a variable to a constant in the given query."""
print self.where
cand_vars = self.const_res_set.keys()
print "Variables that can be replaced: " + str(cand_vars)
var_to_replace = cand_vars[random.randint(0,len(cand_vars)-1)]
#print res_q.const_res_set
if self.const_res_set[var_to_replace]:
replacement_value = self.const_res_set[var_to_replace].pop()
if "://" in replacement_value:
replacement_value = "<" + replacement_value + ">"
else:
replacement_value = '"' + replacement_value + '"'
for ind_t in range(0,len(self.where)):
self.where[ind_t] = self.where[ind_t].replace('?'+var_to_replace, replacement_value)
#TODO : remplacer aussi dans le select ?
# explored_triples = []
# while (res_q == query):
# t = None
# if set(explored_triples) == set(res_q.where):
# # If we can't find anything, we return the query as is (as it should be identical to the input)
# return res_q
# while ((t is None) or (t in explored_triples)):
# ind = random.randint(0, len(res_q.where)-1)
# t = res_q.where[ind]
# (s,p,o) = decompose_triple(t)
# print "Candidate triple:" + str((s,p,o))
# cand = [v for v in [s,p,o] if (type(v) == Var) ]
# print "\tCandidate variables: " + str(cand)
# if cand:
# res = t
# while (res == t):
# res = generate_const(t, query.const_res_set)
# if res == None:
# print "\tCannot replace anything in this triple."
# # Can't find a variable to be specified in this triple
# explored_triples.append(t)
# res = t
# break
# print "\tNew triple: " + str(res)
# res_q.where[res_q.where.index(t)] = res
# else:
# #Nothing to do with this triple
# explored_triples.append(t)