rdflib 2.2.21 → 2.2.22
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 +36 -36
- package/src/fetcher.ts +2 -2
- package/src/update-manager.ts +12 -7
- package/changes.txt +0 -59
package/esm/query.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import _createClass from "@babel/runtime/helpers/createClass";
|
|
2
|
-
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
3
1
|
// Matching a formula against another formula
|
|
4
2
|
// Assync as well as Synchronously
|
|
5
3
|
//
|
|
@@ -11,33 +9,32 @@ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
|
11
9
|
// Here we introduce for the first time a subclass of term: variable.
|
|
12
10
|
//
|
|
13
11
|
// SVN ID: $Id: query.js 25116 2008-11-15 16:13:48Z timbl $
|
|
12
|
+
|
|
14
13
|
// Variable
|
|
15
14
|
//
|
|
16
15
|
// Compare with BlankNode. They are similar, but a variable
|
|
17
16
|
// stands for something whose value is to be returned.
|
|
18
17
|
// Also, users name variables and want the same name back when stuff is printed
|
|
18
|
+
/* jsl:option explicit */ // Turn on JavaScriptLint variable declaration checking
|
|
19
19
|
|
|
20
|
-
/* jsl:option explicit */
|
|
21
|
-
// Turn on JavaScriptLint variable declaration checking
|
|
22
20
|
import IndexedFormula from './store';
|
|
23
21
|
import { defaultGraphURI as defaultDocumentURI } from './utils/default-graph-uri';
|
|
24
22
|
import log from './log';
|
|
25
23
|
import { docpart } from './uri';
|
|
24
|
+
|
|
26
25
|
/**
|
|
27
26
|
* Query class, for tracking queries the user has in the UI.
|
|
28
27
|
*/
|
|
28
|
+
export class Query {
|
|
29
|
+
constructor(name, id) {
|
|
30
|
+
this.pat = new IndexedFormula(); // The pattern to search for
|
|
31
|
+
this.vars = []; // Used by UI code but not in query.js
|
|
32
|
+
// this.orderBy = [] // Not used yet
|
|
33
|
+
this.name = name;
|
|
34
|
+
this.id = id;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
29
37
|
|
|
30
|
-
export var Query = /*#__PURE__*/_createClass(function Query(name, id) {
|
|
31
|
-
_classCallCheck(this, Query);
|
|
32
|
-
|
|
33
|
-
this.pat = new IndexedFormula(); // The pattern to search for
|
|
34
|
-
|
|
35
|
-
this.vars = []; // Used by UI code but not in query.js
|
|
36
|
-
// this.orderBy = [] // Not used yet
|
|
37
|
-
|
|
38
|
-
this.name = name;
|
|
39
|
-
this.id = id;
|
|
40
|
-
});
|
|
41
38
|
/**
|
|
42
39
|
* This function will match a pattern to the current Store
|
|
43
40
|
*
|
|
@@ -53,32 +50,26 @@ export var Query = /*#__PURE__*/_createClass(function Query(name, id) {
|
|
|
53
50
|
* which has been created for the quadstore being queried.
|
|
54
51
|
* @param onDone - callback when query finished
|
|
55
52
|
*/
|
|
56
|
-
|
|
57
53
|
export function indexedFormulaQuery(myQuery, callback, fetcher, onDone) {
|
|
58
54
|
/** Debug strings
|
|
59
55
|
*/
|
|
60
56
|
function bindingDebug(b) {
|
|
61
57
|
var str = '';
|
|
62
58
|
var v;
|
|
63
|
-
|
|
64
59
|
for (v in b) {
|
|
65
60
|
if (b.hasOwnProperty(v)) {
|
|
66
61
|
str += ' ' + v + ' -> ' + b[v];
|
|
67
62
|
}
|
|
68
63
|
}
|
|
69
|
-
|
|
70
64
|
return str;
|
|
71
65
|
}
|
|
72
|
-
|
|
73
66
|
function bindingsDebug(nbs) {
|
|
74
67
|
var str = 'Bindings: ';
|
|
75
68
|
var i;
|
|
76
69
|
var n = nbs.length;
|
|
77
|
-
|
|
78
70
|
for (i = 0; i < n; i++) {
|
|
79
71
|
str += bindingDebug(nbs[i][0]) + ';\n\t';
|
|
80
72
|
}
|
|
81
|
-
|
|
82
73
|
return str;
|
|
83
74
|
} // bindingsDebug
|
|
84
75
|
|
|
@@ -89,11 +80,8 @@ export function indexedFormulaQuery(myQuery, callback, fetcher, onDone) {
|
|
|
89
80
|
* @returns {Arrray}- a list of bindings, where a binding is an associative array
|
|
90
81
|
* mapping variuable to value.
|
|
91
82
|
*/
|
|
92
|
-
|
|
93
|
-
|
|
94
83
|
function unifyTerm(self, other, bindings, formula) {
|
|
95
84
|
var actual = bindings[self];
|
|
96
|
-
|
|
97
85
|
if (actual === undefined) {
|
|
98
86
|
// Not mapped
|
|
99
87
|
if (self.isVar) {
|
|
@@ -104,40 +92,32 @@ export function indexedFormulaQuery(myQuery, callback, fetcher, onDone) {
|
|
|
104
92
|
|
|
105
93
|
actual = self;
|
|
106
94
|
}
|
|
107
|
-
|
|
108
95
|
if (!actual.complexType) {
|
|
109
96
|
if (formula.redirections[actual]) {
|
|
110
97
|
actual = formula.redirections[actual];
|
|
111
98
|
}
|
|
112
|
-
|
|
113
99
|
if (formula.redirections[other]) {
|
|
114
100
|
other = formula.redirections[other];
|
|
115
101
|
}
|
|
116
|
-
|
|
117
102
|
if (actual.equals(other) || actual.uri && actual.uri === defaultDocumentURI) {
|
|
118
103
|
// Used to mean 'any graph' in a query
|
|
119
104
|
return [[[], null]];
|
|
120
105
|
}
|
|
121
|
-
|
|
122
106
|
return [];
|
|
123
107
|
}
|
|
124
|
-
|
|
125
108
|
if (self instanceof Array) {
|
|
126
109
|
if (!(other instanceof Array)) {
|
|
127
110
|
return [];
|
|
128
111
|
}
|
|
129
|
-
|
|
130
112
|
return unifyContents(self, other, bindings);
|
|
131
113
|
}
|
|
132
|
-
|
|
133
|
-
|
|
114
|
+
throw new Error('query.js: oops - code not written yet');
|
|
115
|
+
// return undefined; // for lint - no jslint objects to unreachables
|
|
134
116
|
// return actual.unifyContents(other, bindings)
|
|
135
117
|
} // unifyTerm
|
|
136
118
|
|
|
137
|
-
|
|
138
119
|
function unifyContents(self, other, bindings, formula) {
|
|
139
120
|
var nbs2;
|
|
140
|
-
|
|
141
121
|
if (self.length !== other.length) {
|
|
142
122
|
return []; // no way
|
|
143
123
|
}
|
|
@@ -147,11 +127,9 @@ export function indexedFormulaQuery(myQuery, callback, fetcher, onDone) {
|
|
|
147
127
|
}
|
|
148
128
|
|
|
149
129
|
var nbs = unifyTerm(self[0], other[0], bindings, formula);
|
|
150
|
-
|
|
151
130
|
if (nbs.length === 0) {
|
|
152
131
|
return nbs;
|
|
153
132
|
}
|
|
154
|
-
|
|
155
133
|
var res = [];
|
|
156
134
|
var i;
|
|
157
135
|
var n = nbs.length;
|
|
@@ -161,13 +139,10 @@ export function indexedFormulaQuery(myQuery, callback, fetcher, onDone) {
|
|
|
161
139
|
var v;
|
|
162
140
|
var nb2;
|
|
163
141
|
var bindings2;
|
|
164
|
-
|
|
165
142
|
for (i = 0; i < n; i++) {
|
|
166
143
|
// for each possibility from the first term
|
|
167
144
|
nb = nbs[i][0]; // new bindings
|
|
168
|
-
|
|
169
145
|
bindings2 = [];
|
|
170
|
-
|
|
171
146
|
for (v in nb) {
|
|
172
147
|
if (nb.hasOwnProperty(v)) {
|
|
173
148
|
bindings2[v] = nb[v]; // copy
|
|
@@ -182,131 +157,112 @@ export function indexedFormulaQuery(myQuery, callback, fetcher, onDone) {
|
|
|
182
157
|
|
|
183
158
|
nbs2 = unifyContents(self.slice(1), other.slice(1), bindings2, formula);
|
|
184
159
|
m = nbs2.length;
|
|
185
|
-
|
|
186
160
|
for (j = 0; j < m; j++) {
|
|
187
161
|
nb2 = nbs2[j][0]; // @@@@ no idea whether this is used or right
|
|
188
|
-
|
|
189
162
|
for (v in nb) {
|
|
190
163
|
if (nb.hasOwnProperty(v)) {
|
|
191
164
|
nb2[v] = nb[v];
|
|
192
165
|
}
|
|
193
166
|
}
|
|
194
|
-
|
|
195
167
|
res.push([nb2, null]);
|
|
196
168
|
}
|
|
197
169
|
}
|
|
198
|
-
|
|
199
170
|
return res;
|
|
200
171
|
} // unifyContents
|
|
172
|
+
|
|
201
173
|
// Matching
|
|
202
174
|
//
|
|
203
175
|
// Matching finds all bindings such that when the binding is applied
|
|
204
176
|
// to one term it is equal to the other term. We only match formulae.
|
|
205
177
|
|
|
206
178
|
/** if x is not in the bindings array, return the var; otherwise, return the bindings **/
|
|
207
|
-
|
|
208
|
-
|
|
209
179
|
function bind(x, binding) {
|
|
210
180
|
var y = binding[x];
|
|
211
|
-
|
|
212
181
|
if (y === undefined) {
|
|
213
182
|
return x;
|
|
214
183
|
}
|
|
215
|
-
|
|
216
184
|
return y;
|
|
217
|
-
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// When there are OPTIONAL clauses, we must return bindings without them if none of them
|
|
218
188
|
// succeed. However, if any of them do succeed, we should not. (This is what branchCount()
|
|
219
189
|
// tracked. The problem currently is (2011/7) that when several optionals exist, and they
|
|
220
190
|
// all match, multiple sets of bindings are returned, each with one optional filled in.)
|
|
221
191
|
|
|
222
|
-
|
|
223
192
|
function union(a, b) {
|
|
224
193
|
var c = {};
|
|
225
194
|
var x;
|
|
226
|
-
|
|
227
195
|
for (x in a) {
|
|
228
196
|
if (a.hasOwnProperty(x)) {
|
|
229
197
|
c[x] = a[x];
|
|
230
198
|
}
|
|
231
199
|
}
|
|
232
|
-
|
|
233
200
|
for (x in b) {
|
|
234
201
|
if (b.hasOwnProperty(x)) {
|
|
235
202
|
c[x] = b[x];
|
|
236
203
|
}
|
|
237
204
|
}
|
|
238
|
-
|
|
239
205
|
return c;
|
|
240
206
|
}
|
|
241
|
-
|
|
242
207
|
function OptionalBranchJunction(originalCallback, trunkBindings) {
|
|
243
208
|
this.trunkBindings = trunkBindings;
|
|
244
209
|
this.originalCallback = originalCallback;
|
|
245
|
-
this.branches = [];
|
|
210
|
+
this.branches = [];
|
|
211
|
+
// this.results = []; // result[i] is an array of bindings for branch i
|
|
246
212
|
// this.done = {}; // done[i] means all/any results are in for branch i
|
|
247
213
|
// this.count = {}
|
|
248
|
-
|
|
249
214
|
return this;
|
|
250
215
|
}
|
|
251
|
-
|
|
252
216
|
OptionalBranchJunction.prototype.checkAllDone = function () {
|
|
253
217
|
var i;
|
|
254
|
-
|
|
255
218
|
for (i = 0; i < this.branches.length; i++) {
|
|
256
219
|
if (!this.branches[i].done) {
|
|
257
220
|
return;
|
|
258
221
|
}
|
|
259
222
|
}
|
|
260
|
-
|
|
261
223
|
log.debug('OPTIONAL BIDNINGS ALL DONE:');
|
|
262
224
|
this.doCallBacks(this.branches.length - 1, this.trunkBindings);
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
|
|
225
|
+
};
|
|
226
|
+
// Recrursively generate the cross product of the bindings
|
|
266
227
|
OptionalBranchJunction.prototype.doCallBacks = function (b, bindings) {
|
|
267
228
|
var j;
|
|
268
|
-
|
|
269
229
|
if (b < 0) {
|
|
270
230
|
return this.originalCallback(bindings);
|
|
271
231
|
}
|
|
272
|
-
|
|
273
232
|
for (j = 0; j < this.branches[b].results.length; j++) {
|
|
274
233
|
this.doCallBacks(b - 1, union(bindings, this.branches[b].results[j]));
|
|
275
234
|
}
|
|
276
|
-
};
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
// A mandatory branch is the normal one, where callbacks
|
|
277
238
|
// are made immediately and no junction is needed.
|
|
278
239
|
// Might be useful for onFinsihed callback for query API.
|
|
279
|
-
|
|
280
|
-
|
|
281
240
|
function MandatoryBranch(callback, onDone) {
|
|
282
241
|
this.count = 0;
|
|
283
242
|
this.success = false;
|
|
284
|
-
this.done = false;
|
|
285
|
-
|
|
243
|
+
this.done = false;
|
|
244
|
+
// this.results = []
|
|
286
245
|
this.callback = callback;
|
|
287
|
-
this.onDone = onDone;
|
|
246
|
+
this.onDone = onDone;
|
|
247
|
+
// this.junction = junction
|
|
288
248
|
// junction.branches.push(this)
|
|
289
|
-
|
|
290
249
|
return this;
|
|
291
250
|
}
|
|
292
|
-
|
|
293
251
|
MandatoryBranch.prototype.reportMatch = function (bindings) {
|
|
294
252
|
// log.error("@@@@ query.js 1"); // @@
|
|
295
253
|
this.callback(bindings);
|
|
296
254
|
this.success = true;
|
|
297
255
|
};
|
|
298
|
-
|
|
299
256
|
MandatoryBranch.prototype.reportDone = function () {
|
|
300
257
|
this.done = true;
|
|
301
258
|
log.info('Mandatory query branch finished.***');
|
|
302
|
-
|
|
303
259
|
if (this.onDone !== undefined) {
|
|
304
260
|
this.onDone();
|
|
305
261
|
}
|
|
306
|
-
};
|
|
307
|
-
|
|
262
|
+
};
|
|
308
263
|
|
|
309
|
-
|
|
264
|
+
// An optional branch hoards its results.
|
|
265
|
+
var OptionalBranch = function (junction) {
|
|
310
266
|
this.count = 0;
|
|
311
267
|
this.done = false;
|
|
312
268
|
this.results = [];
|
|
@@ -314,45 +270,39 @@ export function indexedFormulaQuery(myQuery, callback, fetcher, onDone) {
|
|
|
314
270
|
junction.branches.push(this);
|
|
315
271
|
return this;
|
|
316
272
|
};
|
|
317
|
-
|
|
318
273
|
OptionalBranch.prototype.reportMatch = function (bindings) {
|
|
319
274
|
this.results.push(bindings);
|
|
320
275
|
};
|
|
321
|
-
|
|
322
276
|
OptionalBranch.prototype.reportDone = function () {
|
|
323
277
|
log.debug('Optional branch finished - results.length = ' + this.results.length);
|
|
324
|
-
|
|
325
278
|
if (this.results.length === 0) {
|
|
326
279
|
// This is what optional means: if no hits,
|
|
327
280
|
this.results.push({}); // mimic success, but with no bindings
|
|
328
|
-
|
|
329
281
|
log.debug("Optional branch FAILED - that's OK.");
|
|
330
282
|
}
|
|
331
|
-
|
|
332
283
|
this.done = true;
|
|
333
284
|
this.junction.checkAllDone();
|
|
334
285
|
};
|
|
286
|
+
|
|
335
287
|
/** prepare -- sets the index of the item to the possible matches
|
|
336
288
|
* @param f - formula
|
|
337
289
|
* @param item - an Statement, possibly w/ vars in it
|
|
338
290
|
* @param bindings - Bindings so far
|
|
339
291
|
* @returns false if the query fails -- there are no items that match
|
|
340
292
|
*/
|
|
341
|
-
|
|
342
|
-
|
|
343
293
|
function prepare(f, item, bindings) {
|
|
344
294
|
var terms, termIndex, i, ind;
|
|
345
295
|
item.nvars = 0;
|
|
346
|
-
item.index = null;
|
|
296
|
+
item.index = null;
|
|
297
|
+
// if (!f.statements) log.warn("@@@ prepare: f is "+f)
|
|
347
298
|
// log.debug("Prepare: f has "+ f.statements.length)
|
|
348
299
|
// log.debug("Prepare: Kb size "+f.statements.length+" Preparing "+item)
|
|
349
300
|
|
|
350
301
|
terms = [item.subject, item.predicate, item.object, item.why];
|
|
351
302
|
ind = [f.subjectIndex, f.predicateIndex, f.objectIndex, f.whyIndex];
|
|
352
|
-
|
|
353
303
|
for (i = 0; i < 4; i++) {
|
|
354
|
-
|
|
355
|
-
|
|
304
|
+
let t = terms[i];
|
|
305
|
+
// console.log(" Prepare (" + t + ") "+(t in bindings))
|
|
356
306
|
if (t.uri && t.uri === defaultDocumentURI) {// chrome:session
|
|
357
307
|
// console.log(' query: Ignoring slot ' + i)
|
|
358
308
|
} else if (t.isVar && !(bindings[t] !== undefined)) {
|
|
@@ -360,13 +310,11 @@ export function indexedFormulaQuery(myQuery, callback, fetcher, onDone) {
|
|
|
360
310
|
} else {
|
|
361
311
|
t = bind(terms[i], bindings); // returns the RDF binding if bound, otherwise itself
|
|
362
312
|
// if (terms[i]!=bind(terms[i],bindings) alert("Term: "+terms[i]+"Binding: "+bind(terms[i], bindings))
|
|
363
|
-
|
|
364
313
|
if (f.redirections[f.id(t)]) {
|
|
365
314
|
t = f.redirections[f.id(t)]; // redirect
|
|
366
315
|
}
|
|
367
316
|
|
|
368
317
|
termIndex = ind[i][f.id(t)];
|
|
369
|
-
|
|
370
318
|
if (!termIndex) {
|
|
371
319
|
item.index = [];
|
|
372
320
|
return false; // Query line cannot match
|
|
@@ -378,30 +326,23 @@ export function indexedFormulaQuery(myQuery, callback, fetcher, onDone) {
|
|
|
378
326
|
}
|
|
379
327
|
}
|
|
380
328
|
}
|
|
381
|
-
|
|
382
329
|
if (item.index === null) {
|
|
383
330
|
// All 4 are variables?
|
|
384
331
|
item.index = f.statements;
|
|
385
332
|
}
|
|
386
|
-
|
|
387
333
|
return true;
|
|
388
334
|
} // prepare
|
|
389
335
|
|
|
390
336
|
/** sorting function -- negative if self is easier **/
|
|
391
337
|
// We always prefer to start with a URI to be able to browse a graph
|
|
392
338
|
// this is why we put off items with more variables till later.
|
|
393
|
-
|
|
394
|
-
|
|
395
339
|
function easiestQuery(self, other) {
|
|
396
340
|
if (self.nvars !== other.nvars) {
|
|
397
341
|
return self.nvars - other.nvars;
|
|
398
342
|
}
|
|
399
|
-
|
|
400
343
|
return self.index.length - other.index.length;
|
|
401
344
|
}
|
|
402
|
-
|
|
403
345
|
var matchIndex = 0; // index
|
|
404
|
-
|
|
405
346
|
/** matches a pattern formula against the knowledge base, e.g. to find matches for table-view
|
|
406
347
|
*
|
|
407
348
|
* @param f - knowledge base formula
|
|
@@ -414,38 +355,31 @@ export function indexedFormulaQuery(myQuery, callback, fetcher, onDone) {
|
|
|
414
355
|
*
|
|
415
356
|
* Will fetch linked data from the web iff the knowledge base an associated source fetcher (f.fetcher)
|
|
416
357
|
***/
|
|
358
|
+
var match = function (f, g, bindingsSoFar, level, fetcher, localCallback, branch) {
|
|
359
|
+
log.debug('Match begins, Branch count now: ' + branch.count + ' for ' + branch.pattern_debug);
|
|
417
360
|
|
|
418
|
-
|
|
419
|
-
log.debug('Match begins, Branch count now: ' + branch.count + ' for ' + branch.pattern_debug); // log.debug("match: f has "+f.statements.length+", g has "+g.statements.length)
|
|
420
|
-
|
|
361
|
+
// log.debug("match: f has "+f.statements.length+", g has "+g.statements.length)
|
|
421
362
|
var pattern = g.statements;
|
|
422
|
-
|
|
423
363
|
if (pattern.length === 0) {
|
|
424
364
|
// when it's satisfied all the pattern triples
|
|
425
365
|
log.debug('FOUND MATCH WITH BINDINGS:' + bindingDebug(bindingsSoFar));
|
|
426
|
-
|
|
427
366
|
if (g.optional.length === 0) {
|
|
428
367
|
branch.reportMatch(bindingsSoFar);
|
|
429
368
|
} else {
|
|
430
369
|
log.debug('OPTIONAL: ' + g.optional);
|
|
431
370
|
var junction = new OptionalBranchJunction(callback, bindingsSoFar); // @@ won't work with nested optionals? nest callbacks
|
|
432
|
-
|
|
433
371
|
var br = [];
|
|
434
372
|
var b;
|
|
435
|
-
|
|
436
373
|
for (b = 0; b < g.optional.length; b++) {
|
|
437
374
|
br[b] = new OptionalBranch(junction); // Allocate branches to prevent premature ending
|
|
438
|
-
|
|
439
375
|
br[b].pattern_debug = g.optional[b]; // for diagnotics only
|
|
440
376
|
}
|
|
441
377
|
|
|
442
378
|
for (b = 0; b < g.optional.length; b++) {
|
|
443
379
|
br[b].count = br[b].count + 1; // Count how many matches we have yet to complete
|
|
444
|
-
|
|
445
380
|
match(f, g.optional[b], bindingsSoFar, '', fetcher, callback, br[b]);
|
|
446
381
|
}
|
|
447
382
|
}
|
|
448
|
-
|
|
449
383
|
branch.count--;
|
|
450
384
|
log.debug('Match ends -- success , Branch count now: ' + branch.count + ' for ' + branch.pattern_debug);
|
|
451
385
|
return; // Success
|
|
@@ -453,28 +387,26 @@ export function indexedFormulaQuery(myQuery, callback, fetcher, onDone) {
|
|
|
453
387
|
|
|
454
388
|
var item;
|
|
455
389
|
var i;
|
|
456
|
-
var n = pattern.length;
|
|
457
|
-
//
|
|
390
|
+
var n = pattern.length;
|
|
391
|
+
// log.debug(level + "Match "+n+" left, bs so far:"+bindingDebug(bindingsSoFar))
|
|
458
392
|
|
|
393
|
+
// Follow links from variables in query
|
|
459
394
|
if (fetcher) {
|
|
460
395
|
// Fetcher is used to fetch URIs, function first term is a URI term, second is the requester
|
|
461
396
|
var id = 'match' + matchIndex++;
|
|
462
|
-
|
|
463
|
-
var fetchResource = function fetchResource(requestedTerm, id) {
|
|
397
|
+
var fetchResource = function (requestedTerm, id) {
|
|
464
398
|
var docuri = requestedTerm.uri.split('#')[0];
|
|
465
399
|
fetcher.nowOrWhenFetched(docuri, undefined, function (ok, body, xhr) {
|
|
466
400
|
if (!ok) {
|
|
467
401
|
console.log('Error following link to <' + requestedTerm.uri + '> in query: ' + body);
|
|
468
402
|
}
|
|
469
|
-
|
|
470
|
-
|
|
403
|
+
match(f, g, bindingsSoFar, level, fetcher,
|
|
404
|
+
// match not match2 to look up any others necessary.
|
|
471
405
|
localCallback, branch);
|
|
472
406
|
});
|
|
473
407
|
};
|
|
474
|
-
|
|
475
408
|
for (i = 0; i < n; i++) {
|
|
476
409
|
item = pattern[i]; // for each of the triples in the query
|
|
477
|
-
|
|
478
410
|
if (bindingsSoFar[item.subject] !== undefined && bindingsSoFar[item.subject].uri && fetcher && fetcher.getState(docpart(bindingsSoFar[item.subject].uri)) === 'unrequested') {
|
|
479
411
|
// fetch the subject info and return to id
|
|
480
412
|
fetchResource(bindingsSoFar[item.subject], id);
|
|
@@ -487,35 +419,28 @@ export function indexedFormulaQuery(myQuery, callback, fetcher, onDone) {
|
|
|
487
419
|
}
|
|
488
420
|
}
|
|
489
421
|
} // if fetcher
|
|
490
|
-
|
|
491
|
-
|
|
492
422
|
match2(f, g, bindingsSoFar, level, fetcher, localCallback, branch);
|
|
493
423
|
}; // match
|
|
494
424
|
|
|
495
|
-
|
|
496
|
-
var constraintsSatisfied = function constraintsSatisfied(bindings, constraints) {
|
|
425
|
+
var constraintsSatisfied = function (bindings, constraints) {
|
|
497
426
|
var res = true;
|
|
498
427
|
var x;
|
|
499
428
|
var test;
|
|
500
|
-
|
|
501
429
|
for (x in bindings) {
|
|
502
430
|
if (bindings.hasOwnProperty(x)) {
|
|
503
431
|
if (constraints[x]) {
|
|
504
432
|
test = constraints[x].test;
|
|
505
|
-
|
|
506
433
|
if (test && !test(bindings[x])) {
|
|
507
434
|
res = false;
|
|
508
435
|
}
|
|
509
436
|
}
|
|
510
437
|
}
|
|
511
438
|
}
|
|
512
|
-
|
|
513
439
|
return res;
|
|
514
440
|
};
|
|
515
|
-
/** match2 -- stuff after the fetch **/
|
|
516
441
|
|
|
517
|
-
|
|
518
|
-
var match2 = function
|
|
442
|
+
/** match2 -- stuff after the fetch **/
|
|
443
|
+
var match2 = function (f, g, bindingsSoFar, level, fetcher, callback, branch) {
|
|
519
444
|
// post fetch
|
|
520
445
|
var pattern = g.statements;
|
|
521
446
|
var n = pattern.length;
|
|
@@ -526,44 +451,41 @@ export function indexedFormulaQuery(myQuery, callback, fetcher, onDone) {
|
|
|
526
451
|
var bindings2;
|
|
527
452
|
var newBindings1;
|
|
528
453
|
var item;
|
|
529
|
-
|
|
530
454
|
for (i = 0; i < n; i++) {
|
|
531
455
|
// For each statement left in the query, run prepare
|
|
532
|
-
item = pattern[i];
|
|
533
|
-
|
|
534
|
-
prepare(f, item, bindingsSoFar);
|
|
456
|
+
item = pattern[i];
|
|
457
|
+
// log.info('match2: item=' + item + ', bindingsSoFar=' + bindingDebug(bindingsSoFar))
|
|
458
|
+
prepare(f, item, bindingsSoFar);
|
|
459
|
+
// if (item.index) console.log(' item.index.length ' + item.index.length)
|
|
535
460
|
}
|
|
536
461
|
|
|
537
462
|
pattern.sort(easiestQuery);
|
|
538
|
-
item = pattern[0];
|
|
539
|
-
|
|
463
|
+
item = pattern[0];
|
|
464
|
+
// log.debug("Sorted pattern:\n"+pattern)
|
|
540
465
|
var rest = f.formula();
|
|
541
466
|
rest.optional = g.optional;
|
|
542
467
|
rest.constraints = g.constraints;
|
|
543
468
|
rest.statements = pattern.slice(1); // No indexes: we will not query g.
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
469
|
+
log.debug(level + 'match2 searching ' + item.index.length + ' for ' + item + '; bindings so far=' + bindingDebug(bindingsSoFar));
|
|
470
|
+
// var results = []
|
|
547
471
|
var c;
|
|
548
472
|
var nc = item.index.length;
|
|
549
473
|
var nbs1;
|
|
550
474
|
var st;
|
|
551
|
-
var onward = 0;
|
|
552
|
-
|
|
475
|
+
var onward = 0;
|
|
476
|
+
// var x
|
|
553
477
|
for (c = 0; c < nc; c++) {
|
|
554
478
|
// For each candidate statement
|
|
555
479
|
st = item.index[c]; // for each statement in the item's index, spawn a new match with that binding
|
|
556
|
-
|
|
557
480
|
nbs1 = unifyContents([item.subject, item.predicate, item.object, item.why], [st.subject, st.predicate, st.object, st.why], bindingsSoFar, f);
|
|
558
481
|
log.info(level + ' From first: ' + nbs1.length + ': ' + bindingsDebug(nbs1));
|
|
559
|
-
nk = nbs1.length;
|
|
482
|
+
nk = nbs1.length;
|
|
483
|
+
// branch.count += nk
|
|
560
484
|
// log.debug("Branch count bumped "+nk+" to: "+branch.count)
|
|
561
|
-
|
|
562
485
|
for (k = 0; k < nk; k++) {
|
|
563
486
|
// For each way that statement binds
|
|
564
487
|
bindings2 = [];
|
|
565
488
|
newBindings1 = nbs1[k][0];
|
|
566
|
-
|
|
567
489
|
if (!constraintsSatisfied(newBindings1, g.constraints)) {
|
|
568
490
|
// branch.count--
|
|
569
491
|
log.debug('Branch count CS: ' + branch.count);
|
|
@@ -581,7 +503,6 @@ export function indexedFormulaQuery(myQuery, callback, fetcher, onDone) {
|
|
|
581
503
|
}
|
|
582
504
|
|
|
583
505
|
branch.count++; // Count how many matches we have yet to complete
|
|
584
|
-
|
|
585
506
|
onward++;
|
|
586
507
|
match(f, rest, bindings2, level + ' ', fetcher, callback, branch); // call match
|
|
587
508
|
}
|
|
@@ -589,26 +510,20 @@ export function indexedFormulaQuery(myQuery, callback, fetcher, onDone) {
|
|
|
589
510
|
}
|
|
590
511
|
|
|
591
512
|
branch.count--;
|
|
592
|
-
|
|
593
513
|
if (onward === 0) {
|
|
594
514
|
log.debug('Match2 fails completely on ' + item);
|
|
595
515
|
}
|
|
596
|
-
|
|
597
516
|
log.debug('Match2 ends, Branch count: ' + branch.count + ' for ' + branch.pattern_debug);
|
|
598
|
-
|
|
599
517
|
if (branch.count === 0) {
|
|
600
518
|
log.debug('Branch finished.');
|
|
601
519
|
branch.reportDone();
|
|
602
520
|
}
|
|
603
521
|
}; // match2
|
|
604
522
|
// ////////////////////////// Body of query() ///////////////////////
|
|
605
|
-
|
|
606
|
-
|
|
607
523
|
var f = this;
|
|
608
524
|
log.debug('Query on ' + this.statements.length);
|
|
609
525
|
var trunck = new MandatoryBranch(callback, onDone);
|
|
610
526
|
trunck.count++; // count one branch to complete at the moment
|
|
611
|
-
|
|
612
527
|
if (myQuery.sync) {
|
|
613
528
|
match(f, myQuery.pat, myQuery.pat.initBindings, '', fetcher, callback, trunck);
|
|
614
529
|
} else {
|
|
@@ -616,6 +531,7 @@ export function indexedFormulaQuery(myQuery, callback, fetcher, onDone) {
|
|
|
616
531
|
setTimeout(function () {
|
|
617
532
|
match(f, myQuery.pat, myQuery.pat.initBindings, '', fetcher, callback, trunck);
|
|
618
533
|
}, 0);
|
|
619
|
-
}
|
|
534
|
+
}
|
|
620
535
|
|
|
536
|
+
// returns nothing; callback does the work
|
|
621
537
|
} // query
|