-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryFeatures.js
227 lines (206 loc) · 7.31 KB
/
queryFeatures.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
// import {Parser} from 'sparqljs';
import Sparqljs from 'sparqljs';
// console.log(Sparqljs);
const {Parser, Generator} = Sparqljs;
export function extractFeaturesFromQueryString(queryStr, options = {}) {
const parser = new Parser();
const generator = options.generator || new Generator({}).createGenerator();
// LIMIT\s*\$__PARAM_[0-9]*
queryStr = queryStr.replaceAll(/LIMIT\s*\$__PARAM_[0-9]*/g, 'LIMIT 10');
queryStr = queryStr.replaceAll(/OFFSET\s*\$__PARAM_[0-9]*/g, 'OFFSET 10');
queryStr = queryStr.replaceAll(
/VALUES\s*([^{$]*)\s\$__PARAM_[0-9]*/g,
(match, vars) => `VALUES ${vars} {}`);
console.log(queryStr);
const queryTree = parser.parse(queryStr);
console.log(JSON.stringify(queryTree, null, 2));
// const {clauses, triples} =
return {
queryType: queryTree.queryType,
...extractFeatures(queryTree.where, {
generator, ...options
})
};
// return {
// queryType: queryTree.queryType,
// clauses, triples
// }
}
function mergeDictsIn(dictBase, ...dicts) {
dicts.forEach(dict => Object.entries(dict).forEach(([key, newValues]) => {
if (key in dictBase) {
if (newValues instanceof Array) {
newValues.forEach(newValue => {
if (!(dictBase[key].includes(newValue))) {
dictBase[key].push(newValue);
}
});
} else {
mergeDictsIn(dictBase[key], newValues);
}
} else {
dictBase[key] = newValues;
}
}));
return dictBase;
}
function emptyExtract() {
return {clauses: [], triples: [], termsByType: {}};
}
function extractOpsFromExpression(expr, options = {}) {
if (expr.termType) {
const generator = options.generator || new Generator({}).createGenerator();
return {[generator.toEntity(expr)]: 1};
}
if (['operation','functionCall'].includes(expr.type)) {
expr.args.map(arg => extractOpsFromExpression(arg, options)).reduce((merge, newDict) => ({
// TODO review!!!!!!!!
}))
}
}
export function extractFeatures(queryTree, options = {}) {
// const generator = options.generator || new Generator({}).createGenerator();
if (Array.isArray(queryTree)) {
return queryTree.map(subTree => extractFeatures(subTree, options)).reduce((merge, {clauses, triples, termsByType}) => ({
clauses: [...merge.clauses, ...clauses],
triples: [...merge.triples, ...triples],
termsByType: mergeDictsIn(merge.termsByType, termsByType),
}), emptyExtract());
}
// console.log(queryTree);
if (queryTree.type === 'bgp') {
const generator = options.generator || new Generator({}).createGenerator();
return {
clauses: ['bgp'],//{bgp: 1},
triples: queryTree.triples.map(({subject, predicate, object}) => ({
subject: generator.toEntity(subject),
predicate: generator.toEntity(predicate),
object: generator.toEntity(object)
})),
termsByType: {
subjObj: queryTree.triples.flatMap(({subject, predicate, object}) => [
{[subject.termType]: [generator.toEntity(subject)]},
{[object.termType]: [generator.toEntity(object)]}
]).reduce((dict, keyValue) => mergeDictsIn(dict, keyValue), {}),
pred: queryTree.triples.map(({subject, predicate, object}) => ({
[predicate.termType || predicate.type]: [generator.toEntity(predicate)]
})).reduce((dict, keyValue) => mergeDictsIn(dict, keyValue), {})
}
}
}
if (queryTree.type === 'bind') {
return {
...emptyExtract(),
clauses: ['bind'],
termsByType: {
bindVariables: [queryTree.variable],
expressionOps: []
}
}
}
if (queryTree.patterns) {
const {clauses, ...extract} = extractFeatures(queryTree.patterns);
return {
clauses: [queryTree.type, ...clauses],
...extract
}
}
return {
...emptyExtract(),
clauses: [queryTree.type]
}
// console.log(parsedQuery);
}
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.
?mickey foaf:knows/foaf:knows ex:Gigi.
VALUES ?mickey { <ex:m1> <ex:m2> <ex:m3> }
OPTIONAL {
?mickey foaf:knows ?ciccio.
OPTIONAL { ?ciccio foaf:knows ?pablo }
}
FILTER(xsd:int(?ciccio) + 3 >= str("wow")).
VALUES ?pablo $__PARAM_56.
ex:Gigi foaf:knows/foaf:knows ex:Gigi.
VALUES (?cicio ?pasticcio) $__PARAM_31.
BIND(2 * 20 + 2 AS ?fortytwo).
# foaf:knows ?other.
}
LIMIT $__PARAM_45
OFFSET $__PARAM_22
`;
console.log(extractFeaturesFromQueryString(inputStr));
// 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));