-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryHandling.js
408 lines (359 loc) · 14.5 KB
/
queryHandling.js
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import Tokenizer from "./Tokenizer.js";
import { parsePreamble } from "./sparqlEncoding.js";
export function decomposeQuery(queryStr, options = {}) {
const tokenizer = new Tokenizer(queryStr, options);
var tokenizerResult;
var tokenIndex = 0;
const queryPieces = [''];
const constants = [];
while((!options.maxTokens || tokenIndex < options.maxTokens) && (tokenizerResult = tokenizer.next()) !== null) {
tokenIndex++;
if (tokenizerResult.parameterizable) {
constants.push(tokenizerResult.match);
queryPieces.push('');
} else {
queryPieces[queryPieces.length-1] += tokenizerResult.match;
}
}
let preamble;
try {
preamble = parsePreamble(tokenizer.preamble);
} catch(exception) {
// Not a valid preamble, probably query is sintactically incorrect, it falls back to put the "preamble" back in the query.
preamble = {};
queryPieces[0] = tokenizer.preamble + queryPieces[0];
}
return {queryPieces, constants, preamble};
}
export function createGeneralizedQuery(queryStr, options = {}) {
const {queryPieces, constants, preamble} = decomposeQuery(queryStr, options);
return {
generalizedQuery: {
queryPieces,
parameterByPosition: constants.map((constant, index) => index)
},
constants,
preamble
};
}
export function generateParameterLabel(parameterIndex) {
return '$__PARAM_' + parameterIndex;
}
export function toString({queryPieces, parameterByPosition}) {
var queryStr = queryPieces[0];
parameterByPosition.forEach((parameter, position) => {
queryStr += generateParameterLabel(parameter) + queryPieces[position + 1];
});
return queryStr;
}
export function queriesExcept(queries, queriesToSubtract) {
const querySet = new Set(queriesToSubtract.map(toString));
return queries.filter(query => {
const queryStr = toString(query);
if (!querySet.has(queryStr)) {
querySet.add(queryStr);
return true;
} else {
return false;
}
});
}
class QuerySet {
constructor(queries = []) {
this.map = new Map(queries.map(query => [toString(query), query]));
}
addIfNotExisting(queries) {
return queries.filter(query => {
const queryStr = toString(query);
if (!this.map.has(queryStr)) {
this.map.set(queryStr, query);
return true;
} else {
return false;
}
});
}
values() {
return this.map.values();
}
}
export function uniqueQueries(queries) {
const querySet = new Set();
return queries.filter(query => {
const queryStr = toString(query);
if (!querySet.has(queryStr)) {
querySet.add(queryStr);
return true;
} else {
return false;
}
});
}
export function fixParameter({queryPieces, parameterByPosition, preamble}, parameterToFix, value) {
const newQuery = {queryPieces: [queryPieces[0]], parameterByPosition: [], preamble};
parameterByPosition.forEach((parameter, position) => {
const queryPiece = queryPieces[position + 1];
if (parameter === parameterToFix) {
newQuery.queryPieces[newQuery.queryPieces.length - 1] += value + queryPiece;
} else {
newQuery.queryPieces.push(queryPiece);
newQuery.parameterByPosition.push(parameter > parameterToFix ? parameter - 1 : parameter);
}
});
return newQuery;
}
export function pairParameters({queryPieces, parameterByPosition, preamble}, parameter1, parameter2) {
const newQuery = {queryPieces, parameterByPosition: [], preamble};
parameterByPosition.forEach(parameter => {
if (parameter === parameter2) {
newQuery.parameterByPosition.push(parameter1);
} else {
newQuery.parameterByPosition.push(parameter > parameter2 ? parameter - 1 : parameter);
}
});
return newQuery;
}
export function simplifyQueryBasic(parametricQuery) {
let simplifiedParametricQuery = parametricQuery;
const bindingsToParam = new Map();
let outputParameterIndex = 0;
const {preamble, instances} = parametricQuery;
const numOfInputParameters = instances[0].bindings.length;
const paramsToRemove = new Set();
for (let inputParameterIndex = 0; inputParameterIndex < numOfInputParameters; inputParameterIndex++) {
const firstValue = instances[0].bindings[inputParameterIndex];
if (instances.every(({bindings}) => bindings[inputParameterIndex] === firstValue)) {
simplifiedParametricQuery = fixParameter(simplifiedParametricQuery, outputParameterIndex, firstValue);
paramsToRemove.add(inputParameterIndex);
} else {
const bindingsStr = JSON.stringify(instances.map(({bindings}) => bindings[inputParameterIndex]));
if (bindingsToParam.has(bindingsStr)) {
simplifiedParametricQuery = pairParameters(simplifiedParametricQuery, bindingsToParam.get(bindingsStr), outputParameterIndex);
paramsToRemove.add(inputParameterIndex);
} else {
bindingsToParam.set(bindingsStr, outputParameterIndex);
outputParameterIndex++;
}
}
}
return {
...simplifiedParametricQuery,
preamble,
instances: instances.map(({bindings, ...instanceData}) => ({
bindings: bindings.filter((value, parameterIndex) => !(paramsToRemove.has(parameterIndex))),
...instanceData
}))
}
}
function selectByParameterValue({queryPieces, parameterByPosition, preamble, instances}, parameter, value, skipFilter = false) {
return {
...fixParameter({queryPieces, parameterByPosition}, parameter, value),
preamble,
instances:
(skipFilter ? instances : instances.filter(({bindings}) => bindings[parameter] === value))
.map(({bindings, ...instanceData}) => ({
bindings: [...bindings.slice(0, parameter), ...bindings.slice(parameter + 1)],
...instanceData
}))
}
}
function selectByPairedParameters({queryPieces, parameterByPosition, preamble, instances}, parameter1, parameter2, skipFilter = false) {
return {
...pairParameters({queryPieces, parameterByPosition}, parameter1, parameter2),
preamble,
instances:
(skipFilter ? instances : instances.filter(({bindings}) => bindings[parameter1] === bindings[parameter2]))
.map(({bindings, ...instanceData}) => ({
bindings: [...bindings.slice(0, parameter2), ...bindings.slice(parameter2 + 1)],
...instanceData
}))
}
}
function totNumOfExecutions(instances) {
return instances.reduce((totNumOfExecutions, {numOfExecutions}) => totNumOfExecutions + numOfExecutions);
}
export function generateSpecializations(parametricQuery, fromParameter, options = {}) {
if (fromParameter >= parametricQuery.instances[0].bindings.length) {
return [];
}
const minNumOfInstances = options.minNumOfInstancesInSubclass || options.minNumOfInstances || 1;
const minNumOfExecutions = options.minNumOfExecutionsInSubclass || options.minNumOfExecutions || 1;
const instancesByValue = Object.create(null);
const instancesByRepeatedParam = Object.create(null);
parametricQuery.instances.forEach(instance => {
const value = instance.bindings[fromParameter];
if (!(value in instancesByValue)) {
instancesByValue[value] = [instance];
} else {
instancesByValue[value].push(instance);
}
instance.bindings.forEach((otherValue, parameter) => {
if (parameter > fromParameter && otherValue === value) {
if (!(otherValue in instancesByRepeatedParam)) {
instancesByRepeatedParam[parameter] = [instance];
} else {
instancesByRepeatedParam[parameter].push(instance);
}
}
});
});
var valuesAndInstancesForSpecializations = Object.entries(instancesByValue);
if (minNumOfInstances > 1) {
valuesAndInstancesForSpecializations = valuesAndInstancesForSpecializations.filter(
([value, instances]) => instances.length >= minNumOfInstances);
}
if (minNumOfExecutions > 1) {
valuesAndInstancesForSpecializations = valuesAndInstancesForSpecializations.filter(
([value, instances]) => totNumOfExecutions(instances) >= minNumOfExecutions);
}
const fixedValueSpecializations = valuesAndInstancesForSpecializations.map(([value, instances]) =>
simplifyAndGenerateSpecializations(
selectByParameterValue({
...parametricQuery,
instances
}, fromParameter, value, true), fromParameter, options));
var paramAndInstancesForSpecializations = Object.entries(instancesByRepeatedParam);
if (minNumOfInstances > 1) {
paramAndInstancesForSpecializations = paramAndInstancesForSpecializations.filter(
([value, instances]) => instances.length >= minNumOfInstances);
}
if (minNumOfExecutions > 1) {
paramAndInstancesForSpecializations = paramAndInstancesForSpecializations.filter(
([value, instances]) => totNumOfExecutions(instances) >= minNumOfExecutions);
}
const repeatedValueSpecializations = paramAndInstancesForSpecializations.map(([pairedParameter, instances]) =>
simplifyAndGenerateSpecializations(
selectByPairedParameters({
...parametricQuery,
instances
}, fromParameter, pairedParameter, true), fromParameter + 1, options));
const furtherSpecializations = generateSpecializations(parametricQuery, fromParameter + 1, options);
return uniqueQueries([
...fixedValueSpecializations,
...repeatedValueSpecializations,
...furtherSpecializations
]);
}
export function simplifyAndGenerateSpecializations(parametricQuery, fromParameter, options = {}) {
const simplifiedQuery = simplifyQueryBasic(parametricQuery);
if (!options.generalizationTree || fromParameter >= parametricQuery.instances[0].bindings.length) {
return {
...simplifiedQuery,
specializations: []
};
}
return {
...simplifiedQuery,
specializations: generateSpecializations(simplifiedQuery, fromParameter, options)
}
}
function pruneRedundantQueries({specializations, ...queryData}) {
var indirectSpecializations = [];
const directSpecializations = [];
const querySet = new QuerySet();
for (const specialization of specializations) {
const {allSpecializations, ...prunedQuery} = pruneRedundantQueries(specialization);
indirectSpecializations = indirectSpecializations.concat(querySet.addIfNotExisting(allSpecializations));
directSpecializations.push(prunedQuery);
}
const filteredDirectSpecialization = querySet.addIfNotExisting(directSpecializations);
return {
...queryData,
specializations: filteredDirectSpecialization,
allSpecializations: [...querySet.values()]
}
}
export function buildSpecializationTree(queryClass, options = {}) {
const {allSpecializations, ...specializationTree} =
pruneRedundantQueries(simplifyAndGenerateSpecializations(queryClass, 0, options));
return specializationTree;
}
export function mergePreambles(preamble1, preamble2) {
if (!preamble1) {
return preamble2;
}
return {
base: 'base' in preamble1 ? preamble1.base : preamble2.base,
prefixes: 'prefixes' in preamble1 ? 'prefixes' in preamble2 ? {...preamble1.prefixes, ...preamble2.prefixes} : preamble1.prefixes : preamble2.prefixes
};
}
// const inputStr = `
// PREFIX foaf: <http://xmlns.com/foaf/0.1/>
// PREFIX xsd: <http://www.w3.org/2001/XMLSchema#/>
// PREFIX ex: <http://www.example.com/>
// SELECT * {
// ?mickey foaf:name "Pippo"@en, "23"^^<http://www.w3.org/2001/XMLSchema#int>, 42, "12"^^xsd:int
// VALUES ?mickey { <ex:m1> <ex:m2> <ex:m3> }
// # foaf:knows ?other.
// }
// `;
// const {generalizedQuery, constants} = createGeneralizedQuery(inputStr, {sparqlParameters: true});
// console.log(generalizedQuery);
// console.log(constants);
// // console.log(toString(generalizedQuery));
// const queryClass = {
// ...generalizedQuery,
// instances: [
// {bindings: constants, numOfExecutions: 3},
// {bindings: constants, numOfExecutions: 5},
// {bindings: [
// 'foaf:name',
// '"Pippo"',
// // 'foaf:hates'
// ], numOfExecutions: 8},
// // {bindings: ['foaf:name', '"Donald Duck"', 'foaf:hates'], numOfExecutions: 4},
// {bindings: [
// 'foaf:hates', '"Rodolfo"',
// // 'foaf:hates'
// ], numOfExecutions: 6},
// // {bindings: ['foaf:hates', '"Rambo"', 'foaf:hates'], numOfExecutions: 2},
// ]
// }
// const queryClass2 = {
// "queryPieces": [
// "SELECT ?s ?o\nWHERE\n { GRAPH bio2rdf:bioportal_resource:bio2rdf.dataset.bioportal.R3.statistics\n { ?s ",
// " ?o }\n }\nOFFSET ",
// "\nLIMIT ",
// "\n"
// ],
// "parameterByPosition": [
// 0,
// 1,
// 2
// ],
// "instances": [
// {
// "bindings": [
// "rdf:type",
// "5000",
// "5000"
// ],
// "id": "14d6836f1070e73ab44fea4fcef086f0f952658aeaf7313da011e4160db93d22",
// "numOfExecutions": 2,
// "numOfHosts": 1
// },
// {
// "bindings": [
// "rdfs:label",
// "10000",
// "10000"
// ],
// "id": "19dd3bdcef3066c9d30a06c6f713c1f4b7f51de5cad50be1a8be6ead8f3c2498",
// "numOfExecutions": 7,
// "numOfHosts": 1
// },
// {
// "bindings": [
// "rdf:type",
// "120000",
// "5000"
// ],
// "id": "06f2e8e1506f5aba60c8b4ab6d4dc1652cdba9c332049cdc9155cbf3dfa762f8",
// "numOfExecutions": 1,
// "numOfHosts": 1
// }
// ]
// };
// // // console.log(JSON.stringify(simplifyAndGenerateSpecializations(queryClass, 0), null, 2));
// // console.log(JSON.stringify(buildSpecializationTree(queryClass), null, 2));
// console.log(JSON.stringify(simplifyQueryBasic(queryClass2), null, 2));