Skip to content

Commit cb3af37

Browse files
Output RDF/JS terms (#80)
Output RDF/JS terms
2 parents 5f07a7a + fa2f7a7 commit cb3af37

File tree

181 files changed

+10792
-3179
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

181 files changed

+10792
-3179
lines changed

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
language: node_js
22
node_js:
3-
- "4"
43
- "6"
54
- "8"
65
- "10"
6+
- "12"
7+
- "lts/*"
78
- "node"
89

910
before_script:

lib/SparqlGenerator.js

+36-27
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ Generator.prototype.toQuery = function (q) {
3131
if (q.distinct)
3232
query += 'DISTINCT ';
3333

34-
if (q.variables)
34+
if (q.variables){
3535
query += mapJoin(q.variables, undefined, function (variable) {
36-
return isString(variable) ? this.toEntity(variable) :
37-
'(' + this.toExpression(variable.expression) + ' AS ' + variable.variable + ')';
36+
return isTerm(variable) ? this.toEntity(variable) :
37+
'(' + this.toExpression(variable.expression) + ' AS ' + variableToString(variable.variable) + ')';
3838
}, this) + ' ';
39+
}
3940
else if (q.template)
4041
query += this.group(q.template, true) + this._newline;
4142

@@ -51,7 +52,7 @@ Generator.prototype.toQuery = function (q) {
5152
if (q.group)
5253
query += 'GROUP BY ' + mapJoin(q.group, undefined, function (it) {
5354
var result = isString(it.expression) ? it.expression : '(' + this.toExpression(it.expression) + ')';
54-
return it.variable ? '(' + result + ' AS ' + it.variable + ')' : result;
55+
return it.variable ? '(' + result + ' AS ' + variableToString(it.variable) + ')' : result;
5556
}, this) + this._newline;
5657
if (q.having)
5758
query += 'HAVING (' + mapJoin(q.having, undefined, this.toExpression, this) + ')' + this._newline;
@@ -156,7 +157,7 @@ Generator.prototype.filter = function (filter) {
156157
};
157158

158159
Generator.prototype.bind = function (bind) {
159-
return 'BIND(' + this.toExpression(bind.expression) + ' AS ' + bind.variable + ')';
160+
return 'BIND(' + this.toExpression(bind.expression) + ' AS ' + variableToString(bind.variable) + ')';
160161
};
161162

162163
Generator.prototype.optional = function (optional) {
@@ -189,7 +190,7 @@ Generator.prototype.values = function (valuesList) {
189190
return 'VALUES ' + lparen + keys.join(' ') + rparen + ' {' + this._newline +
190191
mapJoin(valuesList.values, this._newline, function (values) {
191192
return ' ' + lparen + mapJoin(keys, undefined, function (key) {
192-
return values[key] !== undefined ? this.toEntity(values[key]) : 'UNDEF';
193+
return values[key] ? this.toEntity(values[key]) : 'UNDEF';
193194
}, this) + rparen;
194195
}, this) + this._newline + '}';
195196
};
@@ -201,14 +202,14 @@ Generator.prototype.service = function (service) {
201202

202203
// Converts the parsed expression object into a SPARQL expression
203204
Generator.prototype.toExpression = function (expr) {
204-
if (isString(expr))
205+
if (isTerm(expr)) {
205206
return this.toEntity(expr);
206-
207+
}
207208
switch (expr.type.toLowerCase()) {
208209
case 'aggregate':
209210
return expr.aggregation.toUpperCase() +
210211
'(' + (expr.distinct ? 'DISTINCT ' : '') + this.toExpression(expr.expression) +
211-
(expr.separator ? '; SEPARATOR = ' + this.toEntity('"' + expr.separator + '"') : '') + ')';
212+
(expr.separator ? '; SEPARATOR = ' + '"' + expr.separator.replace(escape, escapeReplacer) + '"' : '') + ')';
212213
case 'functioncall':
213214
return this.toEntity(expr.function) + '(' + mapJoin(expr.args, ', ', this.toExpression, this) + ')';
214215
case 'operation':
@@ -227,9 +228,9 @@ Generator.prototype.toExpression = function (expr) {
227228
case '-':
228229
case '*':
229230
case '/':
230-
return (isString(args[0]) ? this.toEntity(args[0]) : '(' + this.toExpression(args[0]) + ')') +
231+
return (isTerm(args[0]) ? this.toEntity(args[0]) : '(' + this.toExpression(args[0]) + ')') +
231232
' ' + operator + ' ' +
232-
(isString(args[1]) ? this.toEntity(args[1]) : '(' + this.toExpression(args[1]) + ')');
233+
(isTerm(args[1]) ? this.toEntity(args[1]) : '(' + this.toExpression(args[1]) + ')');
233234
// Unary operators
234235
case '!':
235236
return '!(' + this.toExpression(args[0]) + ')';
@@ -255,30 +256,31 @@ Generator.prototype.toExpression = function (expr) {
255256

256257
// Converts the parsed entity (or property path) into a SPARQL entity
257258
Generator.prototype.toEntity = function (value) {
258-
// regular entity
259-
if (isString(value)) {
260-
switch (value[0]) {
259+
if (isTerm(value)) {
260+
switch (value.termType) {
261261
// variable, * selector, or blank node
262-
case '?':
263-
case '$':
264-
case '*':
265-
case '_':
266-
return value;
262+
case 'Wildcard':
263+
return '*';
264+
case 'Variable':
265+
return variableToString(value);
266+
case 'BlankNode':
267+
return '_:' + value.value;
267268
// literal
268-
case '"':
269-
var match = value.match(/^"([^]*)"(?:(@.+)|\^\^(.+))?$/) || {},
270-
lexical = match[1] || '', language = match[2] || '', datatype = match[3];
271-
value = '"' + lexical.replace(escape, escapeReplacer) + '"' + language;
272-
if (datatype) {
273-
if (datatype === XSD_INTEGER && /^\d+$/.test(lexical))
269+
case 'Literal':
270+
var lexical = value.value || '', language = value.language || '', datatype = value.datatype;
271+
value = '"' + lexical.replace(escape, escapeReplacer) + '"';
272+
if (language){
273+
value += '@' + language;
274+
} else if (datatype) {
275+
if (datatype.value === XSD_INTEGER && /^\d+$/.test(lexical))
274276
// Add space to avoid confusion with decimals in broken parsers
275277
return lexical + ' ';
276-
value += '^^' + this.encodeIRI(datatype);
278+
value += '^^' + this.encodeIRI(datatype.value);
277279
}
278280
return value;
279281
// IRI
280282
default:
281-
return this.encodeIRI(value);
283+
return this.encodeIRI(value.value);
282284
}
283285
}
284286
// property path
@@ -355,9 +357,16 @@ Generator.prototype.toUpdate = function (update) {
355357
// Indents each line of the string
356358
Generator.prototype.indent = function(text) { return text.replace(/^/gm, this._indent); }
357359

360+
function variableToString(variable){
361+
return '?' + variable.value;
362+
}
363+
358364
// Checks whether the object is a string
359365
function isString(object) { return typeof object === 'string'; }
360366

367+
// Checks whether the object is a Term
368+
function isTerm(object) { return !!object.termType; }
369+
361370
// Maps the array with the given function, and joins the results using the separator
362371
function mapJoin(array, sep, func, self) {
363372
return array.map(func, self).join(isString(sep) ? sep : ' ');

lib/Wildcard.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var Term = require('n3').DataFactory.internal.Term;
2+
3+
// Wildcard constructor
4+
class Wildcard extends Term {
5+
constructor() {
6+
super('');
7+
return WILDCARD || this;
8+
}
9+
10+
equals(other) {
11+
return other && (this.termType === other.termType);
12+
}
13+
}
14+
15+
Object.defineProperty(Wildcard.prototype, 'value', {
16+
enumerable: true,
17+
value: '*',
18+
});
19+
20+
Object.defineProperty(Wildcard.prototype, 'termType', {
21+
enumerable: true,
22+
value: 'Wildcard',
23+
});
24+
25+
26+
// Wildcard singleton
27+
var WILDCARD = new Wildcard();
28+
29+
module.exports.Wildcard = Wildcard;

0 commit comments

Comments
 (0)