forked from RubenVerborgh/SPARQL.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSparqlGenerator.js
386 lines (347 loc) · 13 KB
/
SparqlGenerator.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
var XSD_INTEGER = 'http://www.w3.org/2001/XMLSchema#integer';
function Generator(options, prefixes) {
this._options = options = options || {};
prefixes = prefixes || {};
this._prefixByIri = {};
var prefixIris = [];
for (var prefix in prefixes) {
var iri = prefixes[prefix];
if (isString(iri)) {
this._prefixByIri[iri] = prefix;
prefixIris.push(iri);
}
}
var iriList = prefixIris.join('|').replace(/[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
this._prefixRegex = new RegExp('^(' + iriList + ')([a-zA-Z][\\-_a-zA-Z0-9]*)$');
this._usedPrefixes = {};
this._indent = isString(options.indent) ? options.indent : ' ';
this._newline = isString(options.newline) ? options.newline : '\n';
}
// Converts the parsed query object into a SPARQL query
Generator.prototype.toQuery = function (q) {
var query = '';
if (q.queryType)
query += q.queryType.toUpperCase() + ' ';
if (q.reduced)
query += 'REDUCED ';
if (q.distinct)
query += 'DISTINCT ';
if (q.variables){
query += mapJoin(q.variables, undefined, function (variable) {
return isTerm(variable) ? this.toEntity(variable) :
'(' + this.toExpression(variable.expression) + ' AS ' + variableToString(variable.variable) + ')';
}, this) + ' ';
}
else if (q.template)
query += this.group(q.template, true) + this._newline;
if (q.from)
query += mapJoin(q.from.default || [], '', function (g) { return 'FROM ' + this.toEntity(g) + this._newline; }, this) +
mapJoin(q.from.named || [], '', function (g) { return 'FROM NAMED ' + this.toEntity(g) + this._newline; }, this);
if (q.where)
query += 'WHERE ' + this.group(q.where, true) + this._newline;
if (q.updates)
query += mapJoin(q.updates, ';' + this._newline, this.toUpdate, this);
if (q.group)
query += 'GROUP BY ' + mapJoin(q.group, undefined, function (it) {
var result = isString(it.expression) ? it.expression : '(' + this.toExpression(it.expression) + ')';
return it.variable ? '(' + result + ' AS ' + variableToString(it.variable) + ')' : result;
}, this) + this._newline;
if (q.having)
query += 'HAVING (' + mapJoin(q.having, undefined, this.toExpression, this) + ')' + this._newline;
if (q.order)
query += 'ORDER BY ' + mapJoin(q.order, undefined, function (it) {
var expr = '(' + this.toExpression(it.expression) + ')';
return !it.descending ? expr : 'DESC ' + expr;
}, this) + this._newline;
if (q.offset)
query += 'OFFSET ' + q.offset + this._newline;
if (q.limit)
query += 'LIMIT ' + q.limit + this._newline;
if (q.values)
query += this.values(q);
// stringify prefixes at the end to mark used ones
query = this.baseAndPrefixes(q) + query;
return query.trim();
};
Generator.prototype.baseAndPrefixes = function (q) {
var base = q.base ? ('BASE <' + q.base + '>' + this._newline) : '';
var prefixes = '';
for (var key in q.prefixes) {
if (this._options.allPrefixes || this._usedPrefixes[key])
prefixes += 'PREFIX ' + key + ': <' + q.prefixes[key] + '>' + this._newline;
}
return base + prefixes;
};
// Converts the parsed SPARQL pattern into a SPARQL pattern
Generator.prototype.toPattern = function (pattern) {
var type = pattern.type || (pattern instanceof Array) && 'array' ||
(pattern.subject && pattern.predicate && pattern.object ? 'triple' : '');
if (!(type in this))
throw new Error('Unknown entry type: ' + type);
return this[type](pattern);
};
Generator.prototype.triple = function (t) {
return this.toEntity(t.subject) + ' ' + this.toEntity(t.predicate) + ' ' + this.toEntity(t.object) + '.';
};
Generator.prototype.array = function (items) {
return mapJoin(items, this._newline, this.toPattern, this);
};
Generator.prototype.bgp = function (bgp) {
return this.encodeTriples(bgp.triples);
};
Generator.prototype.encodeTriples = function (triples) {
if (!triples.length)
return '';
var parts = [], subject = '', predicate = '';
for (var i = 0; i < triples.length; i++) {
var triple = triples[i];
// Triple with different subject
if (triple.subject !== subject) {
// Terminate previous triple
if (subject)
parts.push('.' + this._newline);
subject = triple.subject;
predicate = triple.predicate;
parts.push(this.toEntity(subject), ' ', this.toEntity(predicate));
}
// Triple with same subject but different predicate
else if (triple.predicate !== predicate) {
predicate = triple.predicate;
parts.push(';' + this._newline, this._indent, this.toEntity(predicate));
}
// Triple with same subject and predicate
else {
parts.push(',');
}
parts.push(' ', this.toEntity(triple.object));
}
parts.push('.');
return parts.join('');
}
Generator.prototype.graph = function (graph) {
return 'GRAPH ' + this.toEntity(graph.name) + ' ' + this.group(graph);
};
Generator.prototype.group = function (group, inline) {
group = inline !== true ? this.array(group.patterns || group.triples)
: this.toPattern(group.type !== 'group' ? group : group.patterns);
return group.indexOf(this._newline) === -1 ? '{ ' + group + ' }' : '{' + this._newline + this.indent(group) + this._newline + '}';
};
Generator.prototype.query = function (query) {
return this.toQuery(query);
};
Generator.prototype.filter = function (filter) {
return 'FILTER(' + this.toExpression(filter.expression) + ')';
};
Generator.prototype.bind = function (bind) {
return 'BIND(' + this.toExpression(bind.expression) + ' AS ' + variableToString(bind.variable) + ')';
};
Generator.prototype.optional = function (optional) {
return 'OPTIONAL ' + this.group(optional);
};
Generator.prototype.union = function (union) {
return mapJoin(union.patterns, this._newline + 'UNION' + this._newline, function (p) { return this.group(p, true); }, this);
};
Generator.prototype.minus = function (minus) {
return 'MINUS ' + this.group(minus);
};
Generator.prototype.values = function (valuesList) {
// Gather unique keys
var keys = Object.keys(valuesList.values.reduce(function (keyHash, values) {
for (var key in values) keyHash[key] = true;
return keyHash;
}, {}));
// Check whether simple syntax can be used
var lparen, rparen;
if (keys.length === 1) {
lparen = rparen = '';
} else {
lparen = '(';
rparen = ')';
}
// Create value rows
return 'VALUES ' + lparen + keys.join(' ') + rparen + ' {' + this._newline +
mapJoin(valuesList.values, this._newline, function (values) {
return ' ' + lparen + mapJoin(keys, undefined, function (key) {
return values[key] ? this.toEntity(values[key]) : 'UNDEF';
}, this) + rparen;
}, this) + this._newline + '}';
};
Generator.prototype.service = function (service) {
return 'SERVICE ' + (service.silent ? 'SILENT ' : '') + this.toEntity(service.name) + ' ' +
this.group(service);
};
// Converts the parsed expression object into a SPARQL expression
Generator.prototype.toExpression = function (expr) {
if (isTerm(expr)) {
return this.toEntity(expr);
}
switch (expr.type.toLowerCase()) {
case 'aggregate':
return expr.aggregation.toUpperCase() +
'(' + (expr.distinct ? 'DISTINCT ' : '') + this.toExpression(expr.expression) +
(expr.separator ? '; SEPARATOR = ' + '"' + expr.separator.replace(escape, escapeReplacer) + '"' : '') + ')';
case 'functioncall':
return this.toEntity(expr.function) + '(' + mapJoin(expr.args, ', ', this.toExpression, this) + ')';
case 'operation':
var operator = expr.operator.toUpperCase(), args = expr.args || [];
switch (expr.operator.toLowerCase()) {
// Infix operators
case '<':
case '>':
case '>=':
case '<=':
case '&&':
case '||':
case '=':
case '!=':
case '+':
case '-':
case '*':
case '/':
return (isTerm(args[0]) ? this.toEntity(args[0]) : '(' + this.toExpression(args[0]) + ')') +
' ' + operator + ' ' +
(isTerm(args[1]) ? this.toEntity(args[1]) : '(' + this.toExpression(args[1]) + ')');
// Unary operators
case '!':
return '!(' + this.toExpression(args[0]) + ')';
// IN and NOT IN
case 'notin':
operator = 'NOT IN';
case 'in':
return this.toExpression(args[0]) + ' ' + operator +
'(' + (isString(args[1]) ? args[1] : mapJoin(args[1], ', ', this.toExpression, this)) + ')';
// EXISTS and NOT EXISTS
case 'notexists':
operator = 'NOT EXISTS';
case 'exists':
return operator + ' ' + this.group(args[0], true);
// Other expressions
default:
return operator + '(' + mapJoin(args, ', ', this.toExpression, this) + ')';
}
default:
throw new Error('Unknown expression type: ' + expr.type);
}
};
// Converts the parsed entity (or property path) into a SPARQL entity
Generator.prototype.toEntity = function (value) {
if (isTerm(value)) {
switch (value.termType) {
// variable, * selector, or blank node
case 'Wildcard':
return '*';
case 'Variable':
return variableToString(value);
case 'BlankNode':
return '_:' + value.value;
// literal
case 'Literal':
var lexical = value.value || '', language = value.language || '', datatype = value.datatype;
value = '"' + lexical.replace(escape, escapeReplacer) + '"';
if (language){
value += '@' + language;
} else if (datatype) {
if (datatype.value === XSD_INTEGER && /^\d+$/.test(lexical))
// Add space to avoid confusion with decimals in broken parsers
return lexical + ' ';
value += '^^' + this.encodeIRI(datatype.value);
}
return value;
// IRI
default:
return this.encodeIRI(value.value);
}
}
// property path
else {
var items = value.items.map(this.toEntity, this), path = value.pathType;
switch (path) {
// prefix operator
case '^':
case '!':
return path + items[0];
// postfix operator
case '*':
case '+':
case '?':
return '(' + items[0] + path + ')';
// infix operator
default:
return '(' + items.join(path) + ')';
}
}
};
var escape = /["\\\t\n\r\b\f]/g,
escapeReplacer = function (c) { return escapeReplacements[c]; },
escapeReplacements = { '\\': '\\\\', '"': '\\"', '\t': '\\t',
'\n': '\\n', '\r': '\\r', '\b': '\\b', '\f': '\\f' };
// Represent the IRI, as a prefixed name when possible
Generator.prototype.encodeIRI = function (iri) {
var prefixMatch = this._prefixRegex.exec(iri);
if (prefixMatch) {
var prefix = this._prefixByIri[prefixMatch[1]];
this._usedPrefixes[prefix] = true;
return prefix + ':' + prefixMatch[2];
}
return '<' + iri + '>';
};
// Converts the parsed update object into a SPARQL update clause
Generator.prototype.toUpdate = function (update) {
switch (update.type || update.updateType) {
case 'load':
return 'LOAD' + (update.source ? ' ' + this.toEntity(update.source) : '') +
(update.destination ? ' INTO GRAPH ' + this.toEntity(update.destination) : '');
case 'insert':
return 'INSERT DATA ' + this.group(update.insert, true);
case 'delete':
return 'DELETE DATA ' + this.group(update.delete, true);
case 'deletewhere':
return 'DELETE WHERE ' + this.group(update.delete, true);
case 'insertdelete':
return (update.graph ? 'WITH ' + this.toEntity(update.graph) + this._newline : '') +
(update.delete.length ? 'DELETE ' + this.group(update.delete, true) + this._newline : '') +
(update.insert.length ? 'INSERT ' + this.group(update.insert, true) + this._newline : '') +
'WHERE ' + this.group(update.where, true);
case 'add':
case 'copy':
case 'move':
return update.type.toUpperCase() + (update.source.default ? ' DEFAULT ' : ' ') +
'TO ' + this.toEntity(update.destination.name);
case 'create':
case 'clear':
case 'drop':
return update.type.toUpperCase() + (update.silent ? ' SILENT ' : ' ') + (
update.graph.default ? 'DEFAULT' :
update.graph.named ? 'NAMED' :
update.graph.all ? 'ALL' :
('GRAPH ' + this.toEntity(update.graph.name))
);
default:
throw new Error('Unknown update query type: ' + update.type);
}
};
// Indents each line of the string
Generator.prototype.indent = function(text) { return text.replace(/^/gm, this._indent); }
function variableToString(variable){
return '?' + variable.value;
}
// Checks whether the object is a string
function isString(object) { return typeof object === 'string'; }
// Checks whether the object is a Term
function isTerm(object) { return !!object.termType; }
// Maps the array with the given function, and joins the results using the separator
function mapJoin(array, sep, func, self) {
return array.map(func, self).join(isString(sep) ? sep : ' ');
}
/**
* @param options {
* allPrefixes: boolean,
* indentation: string,
* newline: string
* }
*/
module.exports = function SparqlGenerator(options) {
return {
stringify: function (q) { return new Generator(options, q.prefixes).toQuery(q); }
};
};