rdflib 2.2.21 → 2.2.22-b51259b5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/rdflib.min.js +1 -1
- package/dist/rdflib.min.js.LICENSE.txt +9 -1
- package/dist/rdflib.min.js.map +1 -1
- package/esm/blank-node.js +61 -114
- package/esm/class-order.js +1 -1
- package/esm/collection.js +70 -128
- package/esm/convert.js +1 -2
- package/esm/default-graph.js +14 -48
- package/esm/empty.js +8 -39
- package/esm/factories/canonical-data-factory.js +33 -65
- package/esm/factories/extended-term-factory.js +18 -25
- package/esm/factories/factory-types.js +3 -2
- package/esm/factories/rdflib-data-factory.js +9 -19
- package/esm/fetcher.js +1341 -1854
- package/esm/formula.js +639 -846
- package/esm/index.js +40 -76
- package/esm/jsonldparser.js +24 -49
- package/esm/jsonparser.js +1 -8
- package/esm/lists.js +47 -110
- package/esm/literal.js +120 -189
- package/esm/log.js +7 -7
- package/esm/n3parser.js +1015 -1412
- package/esm/named-node.js +70 -119
- package/esm/namespace.js +2 -5
- package/esm/node-internal.js +73 -110
- package/esm/node.js +2 -7
- package/esm/parse.js +12 -19
- package/esm/patch-parser.js +10 -30
- package/esm/query-to-sparql.js +0 -18
- package/esm/query.js +63 -147
- package/esm/rdfaparser.js +794 -997
- package/esm/rdfxmlparser.js +347 -461
- package/esm/serialize.js +9 -27
- package/esm/serializer.js +820 -1049
- package/esm/sparql-to-query.js +44 -134
- package/esm/statement.js +54 -85
- package/esm/store.js +830 -1103
- package/esm/types.js +22 -21
- package/esm/update-manager.js +869 -1106
- package/esm/updates-via.js +104 -161
- package/esm/uri.js +9 -53
- package/esm/utils/default-graph-uri.js +3 -2
- package/esm/utils/termValue.js +0 -1
- package/esm/utils/terms.js +19 -21
- package/esm/utils-js.js +20 -61
- package/esm/utils.js +10 -21
- package/esm/variable.js +32 -78
- package/esm/xsd.js +2 -2
- package/lib/blank-node.js +60 -113
- package/lib/class-order.js +1 -2
- package/lib/collection.js +69 -131
- package/lib/convert.js +3 -9
- package/lib/default-graph.js +13 -52
- package/lib/empty.js +8 -43
- package/lib/factories/canonical-data-factory.js +35 -79
- package/lib/factories/extended-term-factory.js +18 -32
- package/lib/factories/factory-types.d.ts +6 -6
- package/lib/factories/factory-types.js +1 -4
- package/lib/factories/rdflib-data-factory.js +9 -23
- package/lib/fetcher.d.ts +6 -6
- package/lib/fetcher.js +1370 -1843
- package/lib/formula.js +640 -855
- package/lib/index.js +66 -152
- package/lib/jsonldparser.js +23 -53
- package/lib/jsonparser.js +1 -10
- package/lib/lists.js +55 -112
- package/lib/literal.js +120 -195
- package/lib/log.d.ts +0 -6
- package/lib/log.js +7 -8
- package/lib/n3parser.js +1030 -1436
- package/lib/named-node.js +69 -126
- package/lib/namespace.js +2 -7
- package/lib/node-internal.js +74 -107
- package/lib/node.js +2 -12
- package/lib/parse.d.ts +1 -1
- package/lib/parse.js +12 -32
- package/lib/patch-parser.js +11 -34
- package/lib/query-to-sparql.js +0 -23
- package/lib/query.js +62 -167
- package/lib/rdfaparser.js +796 -1009
- package/lib/rdfxmlparser.js +349 -466
- package/lib/serialize.js +11 -37
- package/lib/serializer.js +823 -1064
- package/lib/sparql-to-query.js +42 -167
- package/lib/statement.js +55 -91
- package/lib/store.d.ts +1 -1
- package/lib/store.js +850 -1112
- package/lib/tf-types.d.ts +4 -4
- package/lib/types.d.ts +8 -8
- package/lib/types.js +23 -23
- package/lib/update-manager.d.ts +1 -1
- package/lib/update-manager.js +865 -1103
- package/lib/updates-via.js +105 -164
- package/lib/uri.js +8 -61
- package/lib/utils/default-graph-uri.js +3 -5
- package/lib/utils/termValue.js +0 -2
- package/lib/utils/terms.js +19 -40
- package/lib/utils-js.js +23 -88
- package/lib/utils.js +10 -27
- package/lib/variable.js +34 -85
- package/lib/xsd-internal.js +0 -3
- package/lib/xsd.js +2 -6
- package/package.json +35 -35
- package/src/fetcher.ts +2 -2
- package/src/update-manager.ts +12 -7
- package/changes.txt +0 -59
package/esm/sparql-to-query.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
// Converting between SPARQL queries and the $rdf query API
|
|
2
|
-
|
|
3
2
|
/*
|
|
4
3
|
|
|
5
4
|
function SQuery () {
|
|
@@ -9,36 +8,33 @@ function SQuery () {
|
|
|
9
8
|
|
|
10
9
|
STerm.prototype.toString = STerm.val
|
|
11
10
|
SQuery.prototype.add = function (str) {this.terms.push()}*/
|
|
11
|
+
|
|
12
12
|
import log from './log';
|
|
13
13
|
import { Query } from './query';
|
|
14
|
+
|
|
14
15
|
/**
|
|
15
16
|
* @SPARQL: SPARQL text that is converted to a query object which is returned.
|
|
16
17
|
* @testMode: testing flag. Prevents loading of sources.
|
|
17
18
|
*/
|
|
18
|
-
|
|
19
19
|
export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
20
20
|
// AJAR_ClearTable()
|
|
21
21
|
var variableHash = [];
|
|
22
|
-
|
|
23
22
|
function makeVar(name) {
|
|
24
23
|
if (variableHash[name]) {
|
|
25
24
|
return variableHash[name];
|
|
26
25
|
}
|
|
27
|
-
|
|
28
26
|
var newVar = kb.variable(name);
|
|
29
27
|
variableHash[name] = newVar;
|
|
30
28
|
return newVar;
|
|
31
|
-
}
|
|
32
|
-
|
|
29
|
+
}
|
|
33
30
|
|
|
31
|
+
// term type functions
|
|
34
32
|
function isRealText(term) {
|
|
35
33
|
return typeof term === 'string' && term.match(/[^ \n\t]/);
|
|
36
34
|
}
|
|
37
|
-
|
|
38
35
|
function isVar(term) {
|
|
39
36
|
return typeof term === 'string' && term.match(/^[\?\$]/);
|
|
40
37
|
}
|
|
41
|
-
|
|
42
38
|
function fixSymbolBrackets(term) {
|
|
43
39
|
if (typeof term === 'string') {
|
|
44
40
|
return term.replace(/^</, '<').replace(/>$/, '>');
|
|
@@ -46,57 +42,47 @@ export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
|
46
42
|
return term;
|
|
47
43
|
}
|
|
48
44
|
}
|
|
49
|
-
|
|
50
45
|
function isSymbol(term) {
|
|
51
46
|
return typeof term === 'string' && term.match(/^<[^>]*>$/);
|
|
52
47
|
}
|
|
53
|
-
|
|
54
48
|
function isBnode(term) {
|
|
55
49
|
return typeof term === 'string' && (term.match(/^_:/) || term.match(/^$/));
|
|
56
50
|
}
|
|
57
|
-
|
|
58
51
|
function isPrefix(term) {
|
|
59
52
|
return typeof term === 'string' && term.match(/:$/);
|
|
60
53
|
}
|
|
61
|
-
|
|
62
54
|
function isPrefixedSymbol(term) {
|
|
63
55
|
return typeof term === 'string' && term.match(/^:|^[^_][^:]*:/);
|
|
64
56
|
}
|
|
65
|
-
|
|
66
57
|
function getPrefix(term) {
|
|
67
58
|
var a = term.split(':');
|
|
68
59
|
return a[0];
|
|
69
60
|
}
|
|
70
|
-
|
|
71
61
|
function getSuffix(term) {
|
|
72
62
|
var a = term.split(':');
|
|
73
63
|
return a[1];
|
|
74
64
|
}
|
|
75
|
-
|
|
76
65
|
function removeBrackets(term) {
|
|
77
66
|
if (isSymbol(term)) {
|
|
78
67
|
return term.slice(1, term.length - 1);
|
|
79
68
|
} else {
|
|
80
69
|
return term;
|
|
81
70
|
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
71
|
+
}
|
|
72
|
+
// takes a string and returns an array of strings and Literals in the place of literals
|
|
85
73
|
function parseLiterals(str) {
|
|
86
74
|
// var sin = (str.indexOf(/[ \n]\'/)==-1)?null:str.indexOf(/[ \n]\'/), doub = (str.indexOf(/[ \n]\"/)==-1)?null:str.indexOf(/[ \n]\"/)
|
|
87
75
|
var sin = str.indexOf("'") === -1 ? null : str.indexOf("'");
|
|
88
|
-
var doub = str.indexOf('"') === -1 ? null : str.indexOf('"');
|
|
89
|
-
|
|
76
|
+
var doub = str.indexOf('"') === -1 ? null : str.indexOf('"');
|
|
77
|
+
// alert("S: "+sin+" D: "+doub)
|
|
90
78
|
if (!sin && !doub) {
|
|
91
79
|
var a = new Array(1);
|
|
92
80
|
a[0] = str;
|
|
93
81
|
return a;
|
|
94
82
|
}
|
|
95
|
-
|
|
96
83
|
var res = new Array(2);
|
|
97
84
|
var br;
|
|
98
85
|
var ind;
|
|
99
|
-
|
|
100
86
|
if (!sin || doub && doub < sin) {
|
|
101
87
|
br = '"';
|
|
102
88
|
ind = doub;
|
|
@@ -107,29 +93,25 @@ export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
|
107
93
|
log.error('SQARQL QUERY OOPS!');
|
|
108
94
|
return res;
|
|
109
95
|
}
|
|
110
|
-
|
|
111
96
|
res[0] = str.slice(0, ind);
|
|
112
97
|
var end = str.slice(ind + 1).indexOf(br);
|
|
113
|
-
|
|
114
98
|
if (end === -1) {
|
|
115
99
|
log.error('SPARQL parsing error: no matching parentheses in literal ' + str);
|
|
116
100
|
return str;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
|
|
101
|
+
}
|
|
102
|
+
// alert(str.slice(end + ind + 2).match(/^\^\^/))
|
|
120
103
|
var end2;
|
|
121
|
-
|
|
122
104
|
if (str.slice(end + ind + 2).match(/^\^\^/)) {
|
|
123
|
-
end2 = str.slice(end + ind + 2).indexOf(' ');
|
|
124
|
-
|
|
125
|
-
res[1] = kb.literal(str.slice(ind + 1, ind + 1 + end), kb.sym(removeBrackets(str.slice(ind + 4 + end, ind + 2 + end + end2))));
|
|
126
|
-
|
|
105
|
+
end2 = str.slice(end + ind + 2).indexOf(' ');
|
|
106
|
+
// alert(end2)
|
|
107
|
+
res[1] = kb.literal(str.slice(ind + 1, ind + 1 + end), kb.sym(removeBrackets(str.slice(ind + 4 + end, ind + 2 + end + end2))));
|
|
108
|
+
// alert(res[1].datatype.uri)
|
|
127
109
|
res = res.concat(parseLiterals(str.slice(end + ind + 3 + end2)));
|
|
128
110
|
} else if (str.slice(end + ind + 2).match(/^@/)) {
|
|
129
|
-
end2 = str.slice(end + ind + 2).indexOf(' ');
|
|
130
|
-
|
|
131
|
-
res[1] = kb.literal(str.slice(ind + 1, ind + 1 + end), str.slice(ind + 3 + end, ind + 2 + end + end2), null);
|
|
132
|
-
|
|
111
|
+
end2 = str.slice(end + ind + 2).indexOf(' ');
|
|
112
|
+
// alert(end2)
|
|
113
|
+
res[1] = kb.literal(str.slice(ind + 1, ind + 1 + end), str.slice(ind + 3 + end, ind + 2 + end + end2), null);
|
|
114
|
+
// alert(res[1].datatype.uri)
|
|
133
115
|
res = res.concat(parseLiterals(str.slice(end + ind + 2 + end2)));
|
|
134
116
|
} else {
|
|
135
117
|
res[1] = kb.literal(str.slice(ind + 1, ind + 1 + end));
|
|
@@ -139,30 +121,24 @@ export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
|
139
121
|
|
|
140
122
|
return res;
|
|
141
123
|
}
|
|
142
|
-
|
|
143
124
|
function spaceDelimit(str) {
|
|
144
125
|
str = str.replace(/\(/g, ' ( ').replace(/\)/g, ' ) ').replace(/</g, ' <').replace(/>/g, '> ').replace(/{/g, ' { ').replace(/}/g, ' } ').replace(/[\t\n\r]/g, ' ').replace(/; /g, ' ; ').replace(/\. /g, ' . ').replace(/, /g, ' , ');
|
|
145
126
|
log.info('New str into spaceDelimit: \n' + str);
|
|
146
127
|
var res = [];
|
|
147
128
|
var br = str.split(' ');
|
|
148
|
-
|
|
149
129
|
for (var x in br) {
|
|
150
130
|
if (isRealText(br[x])) {
|
|
151
131
|
res = res.concat(br[x]);
|
|
152
132
|
}
|
|
153
133
|
}
|
|
154
|
-
|
|
155
134
|
return res;
|
|
156
135
|
}
|
|
157
|
-
|
|
158
136
|
function replaceKeywords(input) {
|
|
159
137
|
var strarr = input;
|
|
160
|
-
|
|
161
138
|
for (var x = 0; x < strarr.length; x++) {
|
|
162
139
|
if (strarr[x] === 'a') {
|
|
163
140
|
strarr[x] = '<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>';
|
|
164
141
|
}
|
|
165
|
-
|
|
166
142
|
if (strarr[x] === 'is' && strarr[x + 2] === 'of') {
|
|
167
143
|
strarr.splice(x, 1);
|
|
168
144
|
strarr.splice(x + 1, 1);
|
|
@@ -171,21 +147,16 @@ export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
|
171
147
|
strarr[x + 1] = s;
|
|
172
148
|
}
|
|
173
149
|
}
|
|
174
|
-
|
|
175
150
|
return strarr;
|
|
176
151
|
}
|
|
177
|
-
|
|
178
152
|
function toTerms(input) {
|
|
179
153
|
var res = [];
|
|
180
|
-
|
|
181
154
|
for (var x = 0; x < input.length; x++) {
|
|
182
155
|
if (typeof input[x] !== 'string') {
|
|
183
156
|
res[x] = input[x];
|
|
184
157
|
continue;
|
|
185
158
|
}
|
|
186
|
-
|
|
187
159
|
input[x] = fixSymbolBrackets(input[x]);
|
|
188
|
-
|
|
189
160
|
if (isVar(input[x])) {
|
|
190
161
|
res[x] = makeVar(input[x].slice(1));
|
|
191
162
|
} else if (isBnode(input[x])) {
|
|
@@ -196,7 +167,6 @@ export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
|
196
167
|
res[x] = kb.sym(removeBrackets(input[x]));
|
|
197
168
|
} else if (isPrefixedSymbol(input[x])) {
|
|
198
169
|
log.info(input[x] + ' was identified as a prefixed symbol');
|
|
199
|
-
|
|
200
170
|
if (prefixes[getPrefix(input[x])]) {
|
|
201
171
|
res[x] = kb.sym(input[x] = prefixes[getPrefix(input[x])] + getSuffix(input[x]));
|
|
202
172
|
} else {
|
|
@@ -207,14 +177,11 @@ export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
|
207
177
|
res[x] = input[x];
|
|
208
178
|
}
|
|
209
179
|
}
|
|
210
|
-
|
|
211
180
|
return res;
|
|
212
181
|
}
|
|
213
|
-
|
|
214
182
|
function tokenize(str) {
|
|
215
183
|
var token1 = parseLiterals(str);
|
|
216
184
|
var token2 = [];
|
|
217
|
-
|
|
218
185
|
for (var x in token1) {
|
|
219
186
|
if (typeof token1[x] === 'string') {
|
|
220
187
|
token2 = token2.concat(spaceDelimit(token1[x]));
|
|
@@ -222,48 +189,40 @@ export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
|
222
189
|
token2 = token2.concat(token1[x]);
|
|
223
190
|
}
|
|
224
191
|
}
|
|
225
|
-
|
|
226
192
|
token2 = replaceKeywords(token2);
|
|
227
193
|
log.info('SPARQL Tokens: ' + token2);
|
|
228
194
|
return token2;
|
|
229
|
-
}
|
|
230
|
-
|
|
195
|
+
}
|
|
231
196
|
|
|
197
|
+
// CASE-INSENSITIVE
|
|
232
198
|
function arrayIndexOf(str, arr) {
|
|
233
199
|
for (var i = 0; i < arr.length; i++) {
|
|
234
200
|
if (typeof arr[i] !== 'string') {
|
|
235
201
|
continue;
|
|
236
202
|
}
|
|
237
|
-
|
|
238
203
|
if (arr[i].toLowerCase() === str.toLowerCase()) {
|
|
239
204
|
return i;
|
|
240
205
|
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
|
|
206
|
+
}
|
|
207
|
+
// log.warn("No instance of "+str+" in array "+arr)
|
|
244
208
|
return null;
|
|
245
|
-
}
|
|
246
|
-
|
|
209
|
+
}
|
|
247
210
|
|
|
211
|
+
// CASE-INSENSITIVE
|
|
248
212
|
function arrayIndicesOf(str, arr) {
|
|
249
213
|
var ind = [];
|
|
250
|
-
|
|
251
214
|
for (var i = 0; i < arr.length; i++) {
|
|
252
215
|
if (typeof arr[i] !== 'string') {
|
|
253
216
|
continue;
|
|
254
217
|
}
|
|
255
|
-
|
|
256
218
|
if (arr[i].toLowerCase() === str.toLowerCase()) {
|
|
257
219
|
ind.push(i);
|
|
258
220
|
}
|
|
259
221
|
}
|
|
260
|
-
|
|
261
222
|
return ind;
|
|
262
223
|
}
|
|
263
|
-
|
|
264
224
|
function setVars(input, query) {
|
|
265
225
|
log.info('SPARQL vars: ' + input);
|
|
266
|
-
|
|
267
226
|
for (var x in input) {
|
|
268
227
|
if (isVar(input[x])) {
|
|
269
228
|
log.info('Added ' + input[x] + ' to query variables from SPARQL');
|
|
@@ -275,15 +234,12 @@ export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
|
275
234
|
}
|
|
276
235
|
}
|
|
277
236
|
}
|
|
278
|
-
|
|
279
237
|
function getPrefixDeclarations(input) {
|
|
280
238
|
var prefInd = arrayIndicesOf('PREFIX', input);
|
|
281
239
|
var res = [];
|
|
282
|
-
|
|
283
240
|
for (var i in prefInd) {
|
|
284
241
|
var a = input[prefInd[i] + 1];
|
|
285
242
|
var b = input[prefInd[i] + 2];
|
|
286
|
-
|
|
287
243
|
if (!isPrefix(a)) {
|
|
288
244
|
log.error('Invalid SPARQL prefix: ' + a);
|
|
289
245
|
} else if (!isSymbol(b)) {
|
|
@@ -295,37 +251,29 @@ export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
|
295
251
|
res[pref] = symbol;
|
|
296
252
|
}
|
|
297
253
|
}
|
|
298
|
-
|
|
299
254
|
return res;
|
|
300
255
|
}
|
|
301
|
-
|
|
302
256
|
function getMatchingBracket(arr, open, close) {
|
|
303
257
|
log.info('Looking for a close bracket of type ' + close + ' in ' + arr);
|
|
304
258
|
var index = 0;
|
|
305
|
-
|
|
306
259
|
for (var i = 0; i < arr.length; i++) {
|
|
307
260
|
if (arr[i] === open) {
|
|
308
261
|
index++;
|
|
309
262
|
}
|
|
310
|
-
|
|
311
263
|
if (arr[i] === close) {
|
|
312
264
|
index--;
|
|
313
265
|
}
|
|
314
|
-
|
|
315
266
|
if (index < 0) {
|
|
316
267
|
return i;
|
|
317
268
|
}
|
|
318
269
|
}
|
|
319
|
-
|
|
320
270
|
log.error('Statement had no close parenthesis in SPARQL query');
|
|
321
271
|
return 0;
|
|
322
272
|
}
|
|
323
|
-
|
|
324
273
|
function constraintGreaterThan(value) {
|
|
325
274
|
this.describe = function (varstr) {
|
|
326
275
|
return varstr + ' > ' + value.toNT();
|
|
327
276
|
};
|
|
328
|
-
|
|
329
277
|
this.test = function (term) {
|
|
330
278
|
if (term.value.match(/[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?/)) {
|
|
331
279
|
return parseFloat(term.value) > parseFloat(value);
|
|
@@ -333,16 +281,13 @@ export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
|
333
281
|
return term.toNT() > value.toNT();
|
|
334
282
|
}
|
|
335
283
|
};
|
|
336
|
-
|
|
337
284
|
return this;
|
|
338
285
|
}
|
|
339
|
-
|
|
340
286
|
function constraintLessThan(value) {
|
|
341
287
|
// this is not the recommended usage. Should only work on literal, numeric, dateTime
|
|
342
288
|
this.describe = function (varstr) {
|
|
343
289
|
return varstr + ' < ' + value.toNT();
|
|
344
290
|
};
|
|
345
|
-
|
|
346
291
|
this.test = function (term) {
|
|
347
292
|
// this.describe = function (varstr) { return varstr + " < "+value }
|
|
348
293
|
if (term.value.match(/[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?/)) {
|
|
@@ -351,34 +296,28 @@ export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
|
351
296
|
return term.toNT() < value.toNT();
|
|
352
297
|
}
|
|
353
298
|
};
|
|
354
|
-
|
|
355
299
|
return this;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
|
|
300
|
+
}
|
|
301
|
+
// This should only work on literals but doesn't.
|
|
359
302
|
function ConstraintEqualTo(value) {
|
|
360
303
|
this.describe = function (varstr) {
|
|
361
304
|
return varstr + ' = ' + value.toNT();
|
|
362
305
|
};
|
|
363
|
-
|
|
364
306
|
this.test = function (term) {
|
|
365
307
|
return value.equals(term);
|
|
366
308
|
};
|
|
367
|
-
|
|
368
309
|
return this;
|
|
369
|
-
}
|
|
370
|
-
|
|
310
|
+
}
|
|
371
311
|
|
|
312
|
+
// value must be a literal
|
|
372
313
|
function ConstraintRegexp(value) {
|
|
373
314
|
this.describe = function (varstr) {
|
|
374
315
|
return "REGEXP( '" + value + "' , " + varstr + ' )';
|
|
375
316
|
};
|
|
376
|
-
|
|
377
317
|
this.test = function (term) {
|
|
378
|
-
var str = value;
|
|
379
|
-
|
|
318
|
+
var str = value;
|
|
319
|
+
// str = str.replace(/^//,"").replace(//$/,"")
|
|
380
320
|
var rg = new RegExp(str);
|
|
381
|
-
|
|
382
321
|
if (term.value) {
|
|
383
322
|
return rg.test(term.value);
|
|
384
323
|
} else {
|
|
@@ -386,7 +325,6 @@ export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
|
386
325
|
}
|
|
387
326
|
};
|
|
388
327
|
}
|
|
389
|
-
|
|
390
328
|
function setConstraint(input, pat) {
|
|
391
329
|
if (input.length === 3 && input[0].termType === 'Variable' && (input[2].termType === 'NamedNode' || input[2].termType === 'Literal')) {
|
|
392
330
|
if (input[1] === '=') {
|
|
@@ -404,9 +342,9 @@ export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
|
404
342
|
} else if (input.length === 6 && typeof input[0] === 'string' && input[0].toLowerCase() === 'regexp' && input[1] === '(' && input[5] === ')' && input[3] === ',' && input[4].termType === 'Variable' && input[2].termType === 'Literal') {
|
|
405
343
|
log.debug('Constraint added: ' + input);
|
|
406
344
|
pat.constraints[input[4]] = new ConstraintRegexp(input[2].value);
|
|
407
|
-
}
|
|
345
|
+
}
|
|
346
|
+
// log.warn("I don't know how to handle the constraint: "+input)
|
|
408
347
|
// alert("length: "+input.length+" input 0 type: "+input[0].termType+" input 1: "+input[1]+" input[2] type: "+input[2].termType)
|
|
409
|
-
|
|
410
348
|
}
|
|
411
349
|
|
|
412
350
|
function setOptional(terms, pat) {
|
|
@@ -415,45 +353,36 @@ export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
|
415
353
|
setWhere(terms, opt);
|
|
416
354
|
pat.optional.push(opt);
|
|
417
355
|
}
|
|
418
|
-
|
|
419
356
|
function setWhere(input, pat) {
|
|
420
357
|
var terms = toTerms(input);
|
|
421
358
|
var end;
|
|
422
359
|
log.debug('WHERE: ' + terms);
|
|
423
|
-
var opt;
|
|
424
|
-
|
|
360
|
+
var opt;
|
|
361
|
+
// var opt = arrayIndicesOf("OPTIONAL",terms)
|
|
425
362
|
while (arrayIndexOf('OPTIONAL', terms)) {
|
|
426
363
|
opt = arrayIndexOf('OPTIONAL', terms);
|
|
427
364
|
log.debug('OPT: ' + opt + ' ' + terms[opt] + ' in ' + terms);
|
|
428
|
-
|
|
429
365
|
if (terms[opt + 1] !== '{') {
|
|
430
366
|
log.warn('Bad optional opening bracket in word ' + opt);
|
|
431
367
|
}
|
|
432
|
-
|
|
433
368
|
end = getMatchingBracket(terms.slice(opt + 2), '{', '}');
|
|
434
|
-
|
|
435
369
|
if (end === -1) {
|
|
436
370
|
log.error('No matching bracket in word ' + opt);
|
|
437
371
|
} else {
|
|
438
|
-
setOptional(terms.slice(opt + 2, opt + 2 + end), pat);
|
|
439
|
-
|
|
372
|
+
setOptional(terms.slice(opt + 2, opt + 2 + end), pat);
|
|
373
|
+
// alert(pat.statements[0].toNT())
|
|
440
374
|
opt = arrayIndexOf('OPTIONAL', terms);
|
|
441
375
|
end = getMatchingBracket(terms.slice(opt + 2), '{', '}');
|
|
442
376
|
terms.splice(opt, end + 3);
|
|
443
377
|
}
|
|
444
378
|
}
|
|
445
|
-
|
|
446
379
|
log.debug('WHERE after optionals: ' + terms);
|
|
447
|
-
|
|
448
380
|
while (arrayIndexOf('FILTER', terms)) {
|
|
449
381
|
var filt = arrayIndexOf('FILTER', terms);
|
|
450
|
-
|
|
451
382
|
if (terms[filt + 1] !== '(') {
|
|
452
383
|
log.warn('Bad filter opening bracket in word ' + filt);
|
|
453
384
|
}
|
|
454
|
-
|
|
455
385
|
end = getMatchingBracket(terms.slice(filt + 2), '(', ')');
|
|
456
|
-
|
|
457
386
|
if (end === -1) {
|
|
458
387
|
log.error('No matching bracket in word ' + filt);
|
|
459
388
|
} else {
|
|
@@ -463,23 +392,18 @@ export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
|
463
392
|
terms.splice(filt, end + 3);
|
|
464
393
|
}
|
|
465
394
|
}
|
|
466
|
-
|
|
467
395
|
log.debug('WHERE after filters and optionals: ' + terms);
|
|
468
396
|
extractStatements(terms, pat);
|
|
469
397
|
}
|
|
470
|
-
|
|
471
398
|
function extractStatements(terms, formula) {
|
|
472
399
|
var arrayZero = new Array(1);
|
|
473
400
|
arrayZero[0] = -1; // this is just to add the beginning of the where to the periods index.
|
|
474
|
-
|
|
475
401
|
var per = arrayZero.concat(arrayIndicesOf('.', terms));
|
|
476
402
|
var stat = [];
|
|
477
|
-
|
|
478
403
|
for (var x = 0; x < per.length - 1; x++) {
|
|
479
404
|
stat[x] = terms.slice(per[x] + 1, per[x + 1]);
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
|
|
405
|
+
}
|
|
406
|
+
// Now it's in an array of statements
|
|
483
407
|
for (x in stat) {
|
|
484
408
|
// THIS MUST BE CHANGED FOR COMMA, SEMICOLON
|
|
485
409
|
log.info('s+p+o ' + x + ' = ' + stat[x]);
|
|
@@ -488,11 +412,9 @@ export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
|
488
412
|
var sem = arrayZero.concat(arrayIndicesOf(';', stat[x]));
|
|
489
413
|
sem.push(stat[x].length);
|
|
490
414
|
var stat2 = [];
|
|
491
|
-
|
|
492
415
|
for (var y = 0; y < sem.length - 1; y++) {
|
|
493
416
|
stat2[y] = stat[x].slice(sem[y] + 1, sem[y + 1]);
|
|
494
417
|
}
|
|
495
|
-
|
|
496
418
|
for (x in stat2) {
|
|
497
419
|
log.info('p+o ' + x + ' = ' + stat[x]);
|
|
498
420
|
var pred = stat2[x][0];
|
|
@@ -500,11 +422,9 @@ export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
|
500
422
|
var com = arrayZero.concat(arrayIndicesOf(',', stat2[x]));
|
|
501
423
|
com.push(stat2[x].length);
|
|
502
424
|
var stat3 = [];
|
|
503
|
-
|
|
504
425
|
for (y = 0; y < com.length - 1; y++) {
|
|
505
426
|
stat3[y] = stat2[x].slice(com[y] + 1, com[y + 1]);
|
|
506
427
|
}
|
|
507
|
-
|
|
508
428
|
for (x in stat3) {
|
|
509
429
|
var obj = stat3[x][0];
|
|
510
430
|
log.info('Subj=' + subj + ' Pred=' + pred + ' Obj=' + obj);
|
|
@@ -512,55 +432,45 @@ export default function SPARQLToQuery(SPARQL, testMode, kb) {
|
|
|
512
432
|
}
|
|
513
433
|
}
|
|
514
434
|
}
|
|
515
|
-
}
|
|
516
|
-
|
|
435
|
+
}
|
|
517
436
|
|
|
437
|
+
// ******************************* Body of SPARQLToQuery ***************************//
|
|
518
438
|
log.info('SPARQL input: \n' + SPARQL);
|
|
519
439
|
var q = new Query();
|
|
520
440
|
var sp = tokenize(SPARQL); // first tokenize everything
|
|
521
|
-
|
|
522
441
|
var prefixes = getPrefixDeclarations(sp);
|
|
523
|
-
|
|
524
442
|
if (!prefixes.rdf) {
|
|
525
443
|
prefixes.rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
|
|
526
444
|
}
|
|
527
|
-
|
|
528
445
|
if (!prefixes.rdfs) {
|
|
529
446
|
prefixes.rdfs = 'http://www.w3.org/2000/01/rdf-schema#';
|
|
530
447
|
}
|
|
531
|
-
|
|
532
448
|
var selectLoc = arrayIndexOf('SELECT', sp);
|
|
533
449
|
var whereLoc = arrayIndexOf('WHERE', sp);
|
|
534
|
-
|
|
535
450
|
if (selectLoc < 0 || whereLoc < 0 || selectLoc > whereLoc) {
|
|
536
451
|
log.error('Invalid or nonexistent SELECT and WHERE tags in SPARQL query');
|
|
537
452
|
return false;
|
|
538
453
|
}
|
|
539
|
-
|
|
540
454
|
setVars(sp.slice(selectLoc + 1, whereLoc), q);
|
|
541
455
|
setWhere(sp.slice(whereLoc + 2, sp.length - 1), q.pat);
|
|
542
|
-
|
|
543
456
|
if (testMode) {
|
|
544
457
|
return q;
|
|
545
458
|
}
|
|
546
|
-
|
|
547
459
|
for (var x in q.pat.statements) {
|
|
548
460
|
var st = q.pat.statements[x];
|
|
549
|
-
|
|
550
461
|
if (st.subject.termType === 'NamedNode') {
|
|
551
462
|
if (kb.fetcher) {
|
|
552
463
|
kb.fetcher.lookUpThing(st.subject, 'sparql:' + st.subject);
|
|
553
464
|
}
|
|
554
465
|
}
|
|
555
|
-
|
|
556
466
|
if (st.object.termType === 'NamedNode') {
|
|
557
467
|
if (kb.fetcher) {
|
|
558
468
|
kb.fetcher.lookUpThing(st.object, 'sparql:' + st.object);
|
|
559
469
|
}
|
|
560
470
|
}
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
471
|
+
}
|
|
472
|
+
// alert(q.pat)
|
|
473
|
+
return q;
|
|
474
|
+
// checkVars()
|
|
565
475
|
// *******************************************************************//
|
|
566
476
|
}
|