rdflib 2.2.8 → 2.2.10-4fa7e876

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