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