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/store.js
CHANGED
|
@@ -1,25 +1,4 @@
|
|
|
1
|
-
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
2
|
-
import _createClass from "@babel/runtime/helpers/createClass";
|
|
3
|
-
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
|
|
4
|
-
import _inherits from "@babel/runtime/helpers/inherits";
|
|
5
|
-
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
|
|
6
|
-
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
|
|
7
1
|
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
8
|
-
|
|
9
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
10
|
-
|
|
11
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
12
|
-
|
|
13
|
-
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
14
|
-
|
|
15
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
16
|
-
|
|
17
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
18
|
-
|
|
19
|
-
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
20
|
-
|
|
21
|
-
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
22
|
-
|
|
23
2
|
/* Identity management and indexing for RDF
|
|
24
3
|
*
|
|
25
4
|
* This file provides IndexedFormula a formula (set of triples) which
|
|
@@ -38,13 +17,16 @@ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Re
|
|
|
38
17
|
*/
|
|
39
18
|
|
|
40
19
|
/** @module store */
|
|
20
|
+
|
|
41
21
|
import ClassOrder from './class-order';
|
|
42
22
|
import { defaultGraphURI } from './factories/canonical-data-factory';
|
|
43
23
|
import Formula from './formula';
|
|
44
24
|
import { ArrayIndexOf } from './utils';
|
|
45
25
|
import { RDFArrayRemove } from './utils-js';
|
|
46
|
-
import { isRDFlibSubject, isRDFlibPredicate, isRDFlibObject, isStore, isGraph,
|
|
47
|
-
|
|
26
|
+
import { isRDFlibSubject, isRDFlibPredicate, isRDFlibObject, isStore, isGraph,
|
|
27
|
+
// isPredicate,
|
|
28
|
+
isQuad
|
|
29
|
+
// isSubject
|
|
48
30
|
} from './utils/terms';
|
|
49
31
|
import Node from './node';
|
|
50
32
|
import Variable from './variable';
|
|
@@ -52,70 +34,56 @@ import { Query, indexedFormulaQuery } from './query';
|
|
|
52
34
|
import { BlankNodeTermType, CollectionTermType, DefaultGraphTermType, EmptyTermType, GraphTermType, LiteralTermType, NamedNodeTermType, VariableTermType } from './types';
|
|
53
35
|
import NamedNode from './named-node';
|
|
54
36
|
import { namedNode } from './index';
|
|
55
|
-
import
|
|
37
|
+
import serialize from "./serialize";
|
|
56
38
|
import BlankNode from './blank-node';
|
|
57
39
|
import DefaultGraph from './default-graph';
|
|
58
40
|
import Literal from './literal';
|
|
59
|
-
|
|
60
|
-
export { defaultGraphURI };
|
|
61
|
-
//
|
|
41
|
+
const owlNamespaceURI = 'http://www.w3.org/2002/07/owl#';
|
|
42
|
+
export { defaultGraphURI };
|
|
43
|
+
// var link_ns = 'http://www.w3.org/2007/ont/link#'
|
|
62
44
|
|
|
45
|
+
// Handle Functional Property
|
|
63
46
|
function handleFP(formula, subj, pred, obj) {
|
|
64
47
|
var o1 = formula.any(subj, pred, undefined);
|
|
65
|
-
|
|
66
48
|
if (!o1) {
|
|
67
49
|
return false; // First time with this value
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
50
|
+
}
|
|
51
|
+
// log.warn("Equating "+o1.uri+" and "+obj.uri + " because FP "+pred.uri); //@@
|
|
71
52
|
formula.equate(o1, obj);
|
|
72
53
|
return true;
|
|
73
54
|
} // handleFP
|
|
74
|
-
// Handle Inverse Functional Property
|
|
75
|
-
|
|
76
55
|
|
|
56
|
+
// Handle Inverse Functional Property
|
|
77
57
|
function handleIFP(formula, subj, pred, obj) {
|
|
78
58
|
var s1 = formula.any(undefined, pred, obj);
|
|
79
|
-
|
|
80
59
|
if (!s1) {
|
|
81
60
|
return false; // First time with this value
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
61
|
+
}
|
|
62
|
+
// log.warn("Equating "+s1.uri+" and "+subj.uri + " because IFP "+pred.uri); //@@
|
|
85
63
|
formula.equate(s1, subj);
|
|
86
64
|
return true;
|
|
87
65
|
} // handleIFP
|
|
88
66
|
|
|
89
|
-
|
|
90
67
|
function handleRDFType(formula, subj, pred, obj, why) {
|
|
91
68
|
//@ts-ignore this method does not seem to exist in this library
|
|
92
69
|
if (formula.typeCallback) {
|
|
93
70
|
formula.typeCallback(formula, obj, why);
|
|
94
71
|
}
|
|
95
|
-
|
|
96
72
|
var x = formula.classActions[formula.id(obj)];
|
|
97
73
|
var done = false;
|
|
98
|
-
|
|
99
74
|
if (x) {
|
|
100
75
|
for (var i = 0; i < x.length; i++) {
|
|
101
76
|
done = done || x[i](formula, subj, pred, obj, why);
|
|
102
77
|
}
|
|
103
78
|
}
|
|
104
|
-
|
|
105
79
|
return done; // statement given is not needed if true
|
|
106
80
|
}
|
|
81
|
+
|
|
107
82
|
/**
|
|
108
83
|
* Indexed Formula aka Store
|
|
109
84
|
*/
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
var IndexedFormula = /*#__PURE__*/function (_Formula) {
|
|
113
|
-
_inherits(IndexedFormula, _Formula);
|
|
114
|
-
|
|
115
|
-
var _super = _createSuper(IndexedFormula);
|
|
116
|
-
|
|
85
|
+
export default class IndexedFormula extends Formula {
|
|
117
86
|
// IN future - allow pass array of statements to constructor
|
|
118
|
-
|
|
119
87
|
/**
|
|
120
88
|
* An UpdateManager initialised to this store
|
|
121
89
|
*/
|
|
@@ -155,1178 +123,937 @@ var IndexedFormula = /*#__PURE__*/function (_Formula) {
|
|
|
155
123
|
* @param [opts.rdfArrayRemove] - Function which removes statements from the store
|
|
156
124
|
* @param [opts.dataCallback] - Callback when a statement is added to the store, will not trigger when adding duplicates
|
|
157
125
|
*/
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
_defineProperty(
|
|
168
|
-
|
|
169
|
-
_defineProperty(
|
|
170
|
-
|
|
171
|
-
_defineProperty(
|
|
172
|
-
|
|
173
|
-
_defineProperty(
|
|
174
|
-
|
|
175
|
-
_defineProperty(
|
|
176
|
-
|
|
177
|
-
_defineProperty(
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
_defineProperty(_assertThisInitialized(_this), "features", void 0);
|
|
192
|
-
|
|
193
|
-
_defineProperty(_assertThisInitialized(_this), "_universalVariables", void 0);
|
|
194
|
-
|
|
195
|
-
_defineProperty(_assertThisInitialized(_this), "_existentialVariables", void 0);
|
|
196
|
-
|
|
197
|
-
_defineProperty(_assertThisInitialized(_this), "rdfArrayRemove", void 0);
|
|
198
|
-
|
|
199
|
-
_defineProperty(_assertThisInitialized(_this), "dataCallbacks", void 0);
|
|
200
|
-
|
|
201
|
-
_this.propertyActions = {};
|
|
202
|
-
_this.classActions = {};
|
|
203
|
-
_this.redirections = [];
|
|
204
|
-
_this.aliases = [];
|
|
205
|
-
_this.HTTPRedirects = [];
|
|
206
|
-
_this.subjectIndex = [];
|
|
207
|
-
_this.predicateIndex = [];
|
|
208
|
-
_this.objectIndex = [];
|
|
209
|
-
_this.whyIndex = [];
|
|
210
|
-
_this.index = [_this.subjectIndex, _this.predicateIndex, _this.objectIndex, _this.whyIndex];
|
|
211
|
-
_this.namespaces = {}; // Dictionary of namespace prefixes
|
|
212
|
-
|
|
213
|
-
_this.features = features || [// By default, devs do not expect these features.
|
|
126
|
+
constructor(features) {
|
|
127
|
+
let opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
128
|
+
super(undefined, undefined, undefined, undefined, opts);
|
|
129
|
+
_defineProperty(this, "updater", void 0);
|
|
130
|
+
_defineProperty(this, "namespaces", void 0);
|
|
131
|
+
_defineProperty(this, "classActions", void 0);
|
|
132
|
+
_defineProperty(this, "propertyActions", void 0);
|
|
133
|
+
_defineProperty(this, "redirections", void 0);
|
|
134
|
+
_defineProperty(this, "aliases", void 0);
|
|
135
|
+
_defineProperty(this, "HTTPRedirects", void 0);
|
|
136
|
+
_defineProperty(this, "subjectIndex", void 0);
|
|
137
|
+
_defineProperty(this, "predicateIndex", void 0);
|
|
138
|
+
_defineProperty(this, "objectIndex", void 0);
|
|
139
|
+
_defineProperty(this, "whyIndex", void 0);
|
|
140
|
+
_defineProperty(this, "index", void 0);
|
|
141
|
+
_defineProperty(this, "features", void 0);
|
|
142
|
+
_defineProperty(this, "_universalVariables", void 0);
|
|
143
|
+
_defineProperty(this, "_existentialVariables", void 0);
|
|
144
|
+
_defineProperty(this, "rdfArrayRemove", void 0);
|
|
145
|
+
_defineProperty(this, "dataCallbacks", void 0);
|
|
146
|
+
this.propertyActions = {};
|
|
147
|
+
this.classActions = {};
|
|
148
|
+
this.redirections = [];
|
|
149
|
+
this.aliases = [];
|
|
150
|
+
this.HTTPRedirects = [];
|
|
151
|
+
this.subjectIndex = [];
|
|
152
|
+
this.predicateIndex = [];
|
|
153
|
+
this.objectIndex = [];
|
|
154
|
+
this.whyIndex = [];
|
|
155
|
+
this.index = [this.subjectIndex, this.predicateIndex, this.objectIndex, this.whyIndex];
|
|
156
|
+
this.namespaces = {}; // Dictionary of namespace prefixes
|
|
157
|
+
this.features = features || [// By default, devs do not expect these features.
|
|
214
158
|
// See https://github.com/linkeddata/rdflib.js/issues/458
|
|
215
159
|
// 'sameAs',
|
|
216
160
|
// 'InverseFunctionalProperty',
|
|
217
161
|
// 'FunctionalProperty',
|
|
218
162
|
];
|
|
219
|
-
|
|
220
|
-
|
|
163
|
+
this.rdfArrayRemove = opts.rdfArrayRemove || RDFArrayRemove;
|
|
221
164
|
if (opts.dataCallback) {
|
|
222
|
-
|
|
165
|
+
this.dataCallbacks = [opts.dataCallback];
|
|
223
166
|
}
|
|
224
|
-
|
|
225
|
-
_this.initPropertyActions(_this.features);
|
|
226
|
-
|
|
227
|
-
return _this;
|
|
167
|
+
this.initPropertyActions(this.features);
|
|
228
168
|
}
|
|
169
|
+
|
|
229
170
|
/**
|
|
230
171
|
* Gets the URI of the default graph
|
|
231
172
|
*/
|
|
173
|
+
static get defaultGraphURI() {
|
|
174
|
+
return defaultGraphURI;
|
|
175
|
+
}
|
|
232
176
|
|
|
177
|
+
/**
|
|
178
|
+
* Gets this graph with the bindings substituted
|
|
179
|
+
* @param bindings The bindings
|
|
180
|
+
*/
|
|
181
|
+
substitute(bindings) {
|
|
182
|
+
var statementsCopy = this.statements.map(function (ea) {
|
|
183
|
+
return ea.substitute(bindings);
|
|
184
|
+
});
|
|
185
|
+
var y = new IndexedFormula();
|
|
186
|
+
y.add(statementsCopy);
|
|
187
|
+
return y;
|
|
188
|
+
}
|
|
233
189
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
function substitute(bindings) {
|
|
242
|
-
var statementsCopy = this.statements.map(function (ea) {
|
|
243
|
-
return ea.substitute(bindings);
|
|
244
|
-
});
|
|
245
|
-
var y = new IndexedFormula();
|
|
246
|
-
y.add(statementsCopy);
|
|
247
|
-
return y;
|
|
248
|
-
}
|
|
249
|
-
/**
|
|
250
|
-
* Add a callback which will be triggered after a statement has been added to the store.
|
|
251
|
-
* @param cb
|
|
252
|
-
*/
|
|
253
|
-
|
|
254
|
-
}, {
|
|
255
|
-
key: "addDataCallback",
|
|
256
|
-
value: function addDataCallback(cb) {
|
|
257
|
-
if (!this.dataCallbacks) {
|
|
258
|
-
this.dataCallbacks = [];
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
this.dataCallbacks.push(cb);
|
|
190
|
+
/**
|
|
191
|
+
* Add a callback which will be triggered after a statement has been added to the store.
|
|
192
|
+
* @param cb
|
|
193
|
+
*/
|
|
194
|
+
addDataCallback(cb) {
|
|
195
|
+
if (!this.dataCallbacks) {
|
|
196
|
+
this.dataCallbacks = [];
|
|
262
197
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
*
|
|
266
|
-
* @param patch - The set of statements to be deleted and to be inserted
|
|
267
|
-
* @param target - The name of the document to patch
|
|
268
|
-
* @param patchCallback - Callback to be called when patching is complete
|
|
269
|
-
*/
|
|
270
|
-
|
|
271
|
-
}, {
|
|
272
|
-
key: "applyPatch",
|
|
273
|
-
value: function applyPatch(patch, target, patchCallback) {
|
|
274
|
-
var targetKB = this;
|
|
275
|
-
var ds;
|
|
276
|
-
var binding = null;
|
|
277
|
-
|
|
278
|
-
function doPatch(onDonePatch) {
|
|
279
|
-
if (patch['delete']) {
|
|
280
|
-
ds = patch['delete']; // console.log(bindingDebug(binding))
|
|
281
|
-
// console.log('ds before substitute: ' + ds)
|
|
282
|
-
|
|
283
|
-
if (binding) ds = ds.substitute(binding); // console.log('applyPatch: delete: ' + ds)
|
|
284
|
-
|
|
285
|
-
ds = ds.statements;
|
|
286
|
-
var bad = [];
|
|
287
|
-
var ds2 = ds.map(function (st) {
|
|
288
|
-
// Find the actual statements in the store
|
|
289
|
-
var sts = targetKB.statementsMatching(st.subject, st.predicate, st.object, target);
|
|
290
|
-
|
|
291
|
-
if (sts.length === 0) {
|
|
292
|
-
// log.info("NOT FOUND deletable " + st)
|
|
293
|
-
bad.push(st);
|
|
294
|
-
return null;
|
|
295
|
-
} else {
|
|
296
|
-
// log.info("Found deletable " + st)
|
|
297
|
-
return sts[0];
|
|
298
|
-
}
|
|
299
|
-
});
|
|
198
|
+
this.dataCallbacks.push(cb);
|
|
199
|
+
}
|
|
300
200
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
201
|
+
/**
|
|
202
|
+
* Apply a set of statements to be deleted and to be inserted
|
|
203
|
+
*
|
|
204
|
+
* @param patch - The set of statements to be deleted and to be inserted
|
|
205
|
+
* @param target - The name of the document to patch
|
|
206
|
+
* @param patchCallback - Callback to be called when patching is complete
|
|
207
|
+
*/
|
|
208
|
+
applyPatch(patch, target, patchCallback) {
|
|
209
|
+
var targetKB = this;
|
|
210
|
+
var ds;
|
|
211
|
+
var binding = null;
|
|
212
|
+
function doPatch(onDonePatch) {
|
|
213
|
+
if (patch['delete']) {
|
|
214
|
+
ds = patch['delete'];
|
|
215
|
+
// console.log(bindingDebug(binding))
|
|
216
|
+
// console.log('ds before substitute: ' + ds)
|
|
217
|
+
if (binding) ds = ds.substitute(binding);
|
|
218
|
+
// console.log('applyPatch: delete: ' + ds)
|
|
219
|
+
ds = ds.statements;
|
|
220
|
+
var bad = [];
|
|
221
|
+
var ds2 = ds.map(function (st) {
|
|
222
|
+
// Find the actual statements in the store
|
|
223
|
+
var sts = targetKB.statementsMatching(st.subject, st.predicate, st.object, target);
|
|
224
|
+
if (sts.length === 0) {
|
|
225
|
+
// log.info("NOT FOUND deletable " + st)
|
|
226
|
+
bad.push(st);
|
|
227
|
+
return null;
|
|
228
|
+
} else {
|
|
229
|
+
// log.info("Found deletable " + st)
|
|
230
|
+
return sts[0];
|
|
305
231
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
if (patch['insert']) {
|
|
313
|
-
// log.info("doPatch insert "+patch['insert'])
|
|
314
|
-
ds = patch['insert'];
|
|
315
|
-
if (binding) ds = ds.substitute(binding);
|
|
316
|
-
ds = ds.statements;
|
|
317
|
-
ds.map(function (st) {
|
|
318
|
-
st.graph = target;
|
|
319
|
-
targetKB.add(st.subject, st.predicate, st.object, st.graph);
|
|
320
|
-
});
|
|
232
|
+
});
|
|
233
|
+
if (bad.length) {
|
|
234
|
+
// console.log('Could not find to delete ' + bad.length + 'statements')
|
|
235
|
+
// console.log('despite ' + targetKB.statementsMatching(bad[0].subject, bad[0].predicate)[0])
|
|
236
|
+
return patchCallback('Could not find to delete: ' + bad.join('\n or '));
|
|
321
237
|
}
|
|
322
|
-
|
|
323
|
-
|
|
238
|
+
ds2.map(function (st) {
|
|
239
|
+
targetKB.remove(st);
|
|
240
|
+
});
|
|
324
241
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
st.graph =
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
query.sync = true;
|
|
335
|
-
var bindingsFound = [];
|
|
336
|
-
targetKB.query(query, function onBinding(binding) {
|
|
337
|
-
bindingsFound.push(binding); // console.log(' got a binding: ' + bindingDebug(binding))
|
|
338
|
-
}, targetKB.fetcher, function onDone() {
|
|
339
|
-
if (bindingsFound.length === 0) {
|
|
340
|
-
return patchCallback('No match found to be patched:' + patch.where);
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
if (bindingsFound.length > 1) {
|
|
344
|
-
return patchCallback('Patch ambiguous. No patch done.');
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
binding = bindingsFound[0];
|
|
348
|
-
doPatch(patchCallback);
|
|
242
|
+
if (patch['insert']) {
|
|
243
|
+
// log.info("doPatch insert "+patch['insert'])
|
|
244
|
+
ds = patch['insert'];
|
|
245
|
+
if (binding) ds = ds.substitute(binding);
|
|
246
|
+
ds = ds.statements;
|
|
247
|
+
ds.map(function (st) {
|
|
248
|
+
st.graph = target;
|
|
249
|
+
targetKB.add(st.subject, st.predicate, st.object, st.graph);
|
|
349
250
|
});
|
|
350
|
-
} else {
|
|
351
|
-
doPatch(patchCallback);
|
|
352
251
|
}
|
|
252
|
+
onDonePatch();
|
|
353
253
|
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
254
|
+
if (patch.where) {
|
|
255
|
+
// log.info("Processing WHERE: " + patch.where + '\n')
|
|
256
|
+
var query = new Query('patch');
|
|
257
|
+
query.pat = patch.where;
|
|
258
|
+
query.pat.statements.map(function (st) {
|
|
259
|
+
st.graph = namedNode(target.value);
|
|
260
|
+
});
|
|
261
|
+
//@ts-ignore TODO: add sync property to Query when converting Query to typescript
|
|
262
|
+
query.sync = true;
|
|
263
|
+
var bindingsFound = [];
|
|
264
|
+
targetKB.query(query, function onBinding(binding) {
|
|
265
|
+
bindingsFound.push(binding);
|
|
266
|
+
// console.log(' got a binding: ' + bindingDebug(binding))
|
|
267
|
+
}, targetKB.fetcher, function onDone() {
|
|
268
|
+
if (bindingsFound.length === 0) {
|
|
269
|
+
return patchCallback('No match found to be patched:' + patch.where);
|
|
270
|
+
}
|
|
271
|
+
if (bindingsFound.length > 1) {
|
|
272
|
+
return patchCallback('Patch ambiguous. No patch done.');
|
|
273
|
+
}
|
|
274
|
+
binding = bindingsFound[0];
|
|
275
|
+
doPatch(patchCallback);
|
|
276
|
+
});
|
|
277
|
+
} else {
|
|
278
|
+
doPatch(patchCallback);
|
|
368
279
|
}
|
|
369
|
-
|
|
370
|
-
* @param features
|
|
371
|
-
*/
|
|
372
|
-
|
|
373
|
-
}, {
|
|
374
|
-
key: "initPropertyActions",
|
|
375
|
-
value: function initPropertyActions(features) {
|
|
376
|
-
// If the predicate is #type, use handleRDFType to create a typeCallback on the object
|
|
377
|
-
this.propertyActions[this.rdfFactory.id(this.rdfFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'))] = [handleRDFType]; // Assumption: these terms are not redirected @@fixme
|
|
280
|
+
}
|
|
378
281
|
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
282
|
+
/**
|
|
283
|
+
* N3 allows for declaring blank nodes, this function enables that support
|
|
284
|
+
*
|
|
285
|
+
* @param x The blank node to be declared, supported in N3
|
|
286
|
+
*/
|
|
287
|
+
declareExistential(x) {
|
|
288
|
+
if (!this._existentialVariables) this._existentialVariables = [];
|
|
289
|
+
this._existentialVariables.push(x);
|
|
290
|
+
return x;
|
|
291
|
+
}
|
|
386
292
|
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
293
|
+
/**
|
|
294
|
+
* @param features
|
|
295
|
+
*/
|
|
296
|
+
initPropertyActions(features) {
|
|
297
|
+
// If the predicate is #type, use handleRDFType to create a typeCallback on the object
|
|
298
|
+
this.propertyActions[this.rdfFactory.id(this.rdfFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'))] = [handleRDFType];
|
|
299
|
+
|
|
300
|
+
// Assumption: these terms are not redirected @@fixme
|
|
301
|
+
if (ArrayIndexOf(features, 'sameAs') >= 0) {
|
|
302
|
+
this.propertyActions[this.rdfFactory.id(this.rdfFactory.namedNode(`${owlNamespaceURI}sameAs`))] = [function (formula, subj, pred, obj, why) {
|
|
303
|
+
// log.warn("Equating "+subj.uri+" sameAs "+obj.uri); //@@
|
|
304
|
+
formula.equate(subj, obj);
|
|
305
|
+
return true; // true if statement given is NOT needed in the store
|
|
306
|
+
}]; // sameAs -> equate & don't add to index
|
|
307
|
+
}
|
|
393
308
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
}
|
|
309
|
+
if (ArrayIndexOf(features, 'InverseFunctionalProperty') >= 0) {
|
|
310
|
+
this.classActions[this.rdfFactory.id(this.rdfFactory.namedNode(`${owlNamespaceURI}InverseFunctionalProperty`))] = [function (formula, subj, pred, obj, addFn) {
|
|
311
|
+
// yes subj not pred!
|
|
312
|
+
return formula.newPropertyAction(subj, handleIFP);
|
|
313
|
+
}]; // IFP -> handleIFP, do add to index
|
|
399
314
|
}
|
|
400
|
-
/** @deprecated Use {add} instead */
|
|
401
315
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
return this.statements.length;
|
|
316
|
+
if (ArrayIndexOf(features, 'FunctionalProperty') >= 0) {
|
|
317
|
+
this.classActions[this.rdfFactory.id(this.rdfFactory.namedNode(`${owlNamespaceURI}FunctionalProperty`))] = [function (formula, subj, proj, obj, addFn) {
|
|
318
|
+
return formula.newPropertyAction(subj, handleFP);
|
|
319
|
+
}]; // FP => handleFP, do add to index
|
|
407
320
|
}
|
|
408
|
-
|
|
409
|
-
* Adds a triple (quad) to the store.
|
|
410
|
-
*
|
|
411
|
-
* @param subj - The thing about which the fact a relationship is asserted.
|
|
412
|
-
* Also accepts a statement or an array of Statements.
|
|
413
|
-
* @param pred - The relationship which is asserted
|
|
414
|
-
* @param obj - The object of the relationship, e.g. another thing or a value. If passed a string, this will become a literal.
|
|
415
|
-
* @param why - The document in which the triple (S,P,O) was or will be stored on the web
|
|
416
|
-
* @returns The statement added to the store, or the store
|
|
417
|
-
*/
|
|
321
|
+
}
|
|
418
322
|
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
323
|
+
/** @deprecated Use {add} instead */
|
|
324
|
+
addStatement(st) {
|
|
325
|
+
this.add(st.subject, st.predicate, st.object, st.graph);
|
|
326
|
+
return this.statements.length;
|
|
327
|
+
}
|
|
423
328
|
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
329
|
+
/**
|
|
330
|
+
* Adds a triple (quad) to the store.
|
|
331
|
+
*
|
|
332
|
+
* @param subj - The thing about which the fact a relationship is asserted.
|
|
333
|
+
* Also accepts a statement or an array of Statements.
|
|
334
|
+
* @param pred - The relationship which is asserted
|
|
335
|
+
* @param obj - The object of the relationship, e.g. another thing or a value. If passed a string, this will become a literal.
|
|
336
|
+
* @param why - The document in which the triple (S,P,O) was or will be stored on the web
|
|
337
|
+
* @returns The statement added to the store, or the store
|
|
338
|
+
*/
|
|
339
|
+
add(subj, pred, obj, why) {
|
|
340
|
+
var i;
|
|
341
|
+
if (arguments.length === 1) {
|
|
342
|
+
if (subj instanceof Array) {
|
|
343
|
+
for (i = 0; i < subj.length; i++) {
|
|
344
|
+
this.add(subj[i]);
|
|
433
345
|
}
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
var actions;
|
|
439
|
-
var st;
|
|
440
|
-
|
|
441
|
-
if (!why) {
|
|
442
|
-
// system generated
|
|
443
|
-
why = this.fetcher ? this.fetcher.appNode : this.rdfFactory.defaultGraph();
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
if (typeof subj == 'string') {
|
|
447
|
-
subj = this.rdfFactory.namedNode(subj);
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
pred = Node.fromValue(pred);
|
|
451
|
-
var objNode = Node.fromValue(obj);
|
|
452
|
-
why = Node.fromValue(why);
|
|
453
|
-
|
|
454
|
-
if (!isRDFlibSubject(subj)) {
|
|
455
|
-
throw new Error('Subject is not a subject type');
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
if (!isRDFlibPredicate(pred)) {
|
|
459
|
-
throw new Error("Predicate ".concat(pred, " is not a predicate type"));
|
|
346
|
+
} else if (isQuad(subj)) {
|
|
347
|
+
this.add(subj.subject, subj.predicate, subj.object, subj.graph);
|
|
348
|
+
} else if (isStore(subj)) {
|
|
349
|
+
this.add(subj.statements);
|
|
460
350
|
}
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
351
|
+
return this;
|
|
352
|
+
}
|
|
353
|
+
var actions;
|
|
354
|
+
var st;
|
|
355
|
+
if (!why) {
|
|
356
|
+
// system generated
|
|
357
|
+
why = this.fetcher ? this.fetcher.appNode : this.rdfFactory.defaultGraph();
|
|
358
|
+
}
|
|
359
|
+
if (typeof subj == 'string') {
|
|
360
|
+
subj = this.rdfFactory.namedNode(subj);
|
|
361
|
+
}
|
|
362
|
+
pred = Node.fromValue(pred);
|
|
363
|
+
const objNode = Node.fromValue(obj);
|
|
364
|
+
why = Node.fromValue(why);
|
|
365
|
+
if (!isRDFlibSubject(subj)) {
|
|
366
|
+
throw new Error('Subject is not a subject type');
|
|
367
|
+
}
|
|
368
|
+
if (!isRDFlibPredicate(pred)) {
|
|
369
|
+
throw new Error(`Predicate ${pred} is not a predicate type`);
|
|
370
|
+
}
|
|
371
|
+
if (!isRDFlibObject(objNode)) {
|
|
372
|
+
throw new Error(`Object ${objNode} is not an object type`);
|
|
373
|
+
}
|
|
374
|
+
if (!isGraph(why)) {
|
|
375
|
+
throw new Error("Why is not a graph type");
|
|
376
|
+
}
|
|
377
|
+
//@ts-ignore This is not used internally
|
|
378
|
+
if (this.predicateCallback) {
|
|
379
|
+
//@ts-ignore This is not used internally
|
|
380
|
+
this.predicateCallback(this, pred, why);
|
|
381
|
+
}
|
|
382
|
+
// Action return true if the statement does not need to be added
|
|
383
|
+
var predHash = this.id(this.canon(pred));
|
|
384
|
+
actions = this.propertyActions[predHash]; // Predicate hash
|
|
385
|
+
var done = false;
|
|
386
|
+
if (actions) {
|
|
387
|
+
// alert('type: '+typeof actions +' @@ actions='+actions)
|
|
388
|
+
for (i = 0; i < actions.length; i++) {
|
|
389
|
+
done = done || actions[i](this, subj, pred, objNode, why);
|
|
464
390
|
}
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
var
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
// alert('type: '+typeof actions +' @@ actions='+actions)
|
|
484
|
-
for (i = 0; i < actions.length; i++) {
|
|
485
|
-
done = done || actions[i](this, subj, pred, objNode, why);
|
|
486
|
-
}
|
|
391
|
+
}
|
|
392
|
+
if (this.holds(subj, pred, objNode, why)) {
|
|
393
|
+
// Takes time but saves duplicates
|
|
394
|
+
// console.log('rdflib: Ignoring dup! {' + subj + ' ' + pred + ' ' + obj + ' ' + why + '}')
|
|
395
|
+
return null; // @@better to return self in all cases?
|
|
396
|
+
}
|
|
397
|
+
// If we are tracking provenance, every thing should be loaded into the store
|
|
398
|
+
// if (done) return this.rdfFactory.quad(subj, pred, obj, why)
|
|
399
|
+
// Don't put it in the store
|
|
400
|
+
// still return this statement for owl:sameAs input
|
|
401
|
+
var hash = [this.id(this.canon(subj)), predHash, this.id(this.canon(objNode)), this.id(this.canon(why))];
|
|
402
|
+
// @ts-ignore this will fail if you pass a collection and the factory does not allow Collections
|
|
403
|
+
st = this.rdfFactory.quad(subj, pred, objNode, why);
|
|
404
|
+
for (i = 0; i < 4; i++) {
|
|
405
|
+
var ix = this.index[i];
|
|
406
|
+
var h = hash[i];
|
|
407
|
+
if (!ix[h]) {
|
|
408
|
+
ix[h] = [];
|
|
487
409
|
}
|
|
410
|
+
ix[h].push(st); // Set of things with this as subject, etc
|
|
411
|
+
}
|
|
488
412
|
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
// if (done) return this.rdfFactory.quad(subj, pred, obj, why)
|
|
495
|
-
// Don't put it in the store
|
|
496
|
-
// still return this statement for owl:sameAs input
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
var hash = [this.id(this.canon(subj)), predHash, this.id(this.canon(objNode)), this.id(this.canon(why))]; // @ts-ignore this will fail if you pass a collection and the factory does not allow Collections
|
|
500
|
-
|
|
501
|
-
st = this.rdfFactory.quad(subj, pred, objNode, why);
|
|
502
|
-
|
|
503
|
-
for (i = 0; i < 4; i++) {
|
|
504
|
-
var ix = this.index[i];
|
|
505
|
-
var h = hash[i];
|
|
506
|
-
|
|
507
|
-
if (!ix[h]) {
|
|
508
|
-
ix[h] = [];
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
ix[h].push(st); // Set of things with this as subject, etc
|
|
512
|
-
} // log.debug("ADDING {"+subj+" "+pred+" "+objNode+"} "+why)
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
this.statements.push(st);
|
|
516
|
-
|
|
517
|
-
if (this.dataCallbacks) {
|
|
518
|
-
var _iterator = _createForOfIteratorHelper(this.dataCallbacks),
|
|
519
|
-
_step;
|
|
520
|
-
|
|
521
|
-
try {
|
|
522
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
523
|
-
var callback = _step.value;
|
|
524
|
-
callback(st);
|
|
525
|
-
}
|
|
526
|
-
} catch (err) {
|
|
527
|
-
_iterator.e(err);
|
|
528
|
-
} finally {
|
|
529
|
-
_iterator.f();
|
|
530
|
-
}
|
|
413
|
+
// log.debug("ADDING {"+subj+" "+pred+" "+objNode+"} "+why)
|
|
414
|
+
this.statements.push(st);
|
|
415
|
+
if (this.dataCallbacks) {
|
|
416
|
+
for (const callback of this.dataCallbacks) {
|
|
417
|
+
callback(st);
|
|
531
418
|
}
|
|
532
|
-
|
|
533
|
-
return st;
|
|
534
419
|
}
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
* @param term - An RDF node
|
|
538
|
-
*/
|
|
420
|
+
return st;
|
|
421
|
+
}
|
|
539
422
|
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
423
|
+
/**
|
|
424
|
+
* Returns the symbol with canonical URI as smushed
|
|
425
|
+
* @param term - An RDF node
|
|
426
|
+
*/
|
|
427
|
+
canon(term) {
|
|
428
|
+
if (!term) {
|
|
429
|
+
// @@ TODO Should improve this to return proper value - doing this to keep it backward compatible
|
|
430
|
+
return term;
|
|
431
|
+
}
|
|
432
|
+
const y = this.redirections[this.id(term)];
|
|
433
|
+
if (y) {
|
|
434
|
+
return y;
|
|
435
|
+
}
|
|
436
|
+
switch (term.termType) {
|
|
437
|
+
case BlankNodeTermType:
|
|
438
|
+
return new BlankNode(term.value);
|
|
439
|
+
case CollectionTermType:
|
|
545
440
|
return term;
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
if (y) {
|
|
551
|
-
return y;
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
switch (term.termType) {
|
|
555
|
-
case BlankNodeTermType:
|
|
556
|
-
return new BlankNode(term.value);
|
|
557
|
-
|
|
558
|
-
case CollectionTermType:
|
|
559
|
-
return term;
|
|
441
|
+
// non-RDF/JS type, should just need to cast
|
|
442
|
+
case DefaultGraphTermType:
|
|
443
|
+
return new DefaultGraph();
|
|
444
|
+
case EmptyTermType:
|
|
560
445
|
// non-RDF/JS type, should just need to cast
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
case LiteralTermType:
|
|
574
|
-
return new Literal(term.value, term.language, term.datatype);
|
|
575
|
-
|
|
576
|
-
case NamedNodeTermType:
|
|
577
|
-
return new NamedNode(term.value);
|
|
578
|
-
|
|
579
|
-
case VariableTermType:
|
|
580
|
-
return new Variable(term.value);
|
|
581
|
-
|
|
582
|
-
default:
|
|
583
|
-
throw new Error("Term Type not recognized for canonization: ".concat(term.termType));
|
|
584
|
-
}
|
|
446
|
+
return term;
|
|
447
|
+
case GraphTermType:
|
|
448
|
+
// non-RDF/JS type, should just need to cast
|
|
449
|
+
return term;
|
|
450
|
+
case LiteralTermType:
|
|
451
|
+
return new Literal(term.value, term.language, term.datatype);
|
|
452
|
+
case NamedNodeTermType:
|
|
453
|
+
return new NamedNode(term.value);
|
|
454
|
+
case VariableTermType:
|
|
455
|
+
return new Variable(term.value);
|
|
456
|
+
default:
|
|
457
|
+
throw new Error(`Term Type not recognized for canonization: ${term.termType}`);
|
|
585
458
|
}
|
|
586
|
-
|
|
587
|
-
* Checks this formula for consistency
|
|
588
|
-
*/
|
|
589
|
-
|
|
590
|
-
}, {
|
|
591
|
-
key: "check",
|
|
592
|
-
value: function check() {
|
|
593
|
-
this.checkStatementList(this.statements);
|
|
594
|
-
|
|
595
|
-
for (var p = 0; p < 4; p++) {
|
|
596
|
-
var ix = this.index[p];
|
|
459
|
+
}
|
|
597
460
|
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
461
|
+
/**
|
|
462
|
+
* Checks this formula for consistency
|
|
463
|
+
*/
|
|
464
|
+
check() {
|
|
465
|
+
this.checkStatementList(this.statements);
|
|
466
|
+
for (var p = 0; p < 4; p++) {
|
|
467
|
+
var ix = this.index[p];
|
|
468
|
+
for (var key in ix) {
|
|
469
|
+
if (ix.hasOwnProperty(key)) {
|
|
470
|
+
// @ts-ignore should this pass an array or a single statement? checkStateMentsList expects an array.
|
|
471
|
+
this.checkStatementList(ix[key], p);
|
|
603
472
|
}
|
|
604
473
|
}
|
|
605
474
|
}
|
|
606
|
-
|
|
607
|
-
* Checks a list of statements for consistency
|
|
608
|
-
* @param sts - The list of statements to check
|
|
609
|
-
* @param from - An index with the array ['subject', 'predicate', 'object', 'why']
|
|
610
|
-
*/
|
|
611
|
-
|
|
612
|
-
}, {
|
|
613
|
-
key: "checkStatementList",
|
|
614
|
-
value: function checkStatementList(sts, from) {
|
|
615
|
-
if (from === undefined) {
|
|
616
|
-
from = 0;
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
var names = ['subject', 'predicate', 'object', 'why'];
|
|
620
|
-
var origin = ' found in ' + names[from] + ' index.';
|
|
621
|
-
var st;
|
|
622
|
-
|
|
623
|
-
for (var j = 0; j < sts.length; j++) {
|
|
624
|
-
st = sts[j];
|
|
625
|
-
var term = [st.subject, st.predicate, st.object, st.graph];
|
|
626
|
-
|
|
627
|
-
var arrayContains = function arrayContains(a, x) {
|
|
628
|
-
for (var i = 0; i < a.length; i++) {
|
|
629
|
-
if (a[i].subject.equals(x.subject) && a[i].predicate.equals(x.predicate) && a[i].object.equals(x.object) && a[i].why.equals(x.graph)) {
|
|
630
|
-
return true;
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
};
|
|
634
|
-
|
|
635
|
-
for (var p = 0; p < 4; p++) {
|
|
636
|
-
var c = this.canon(term[p]);
|
|
637
|
-
var h = this.id(c);
|
|
475
|
+
}
|
|
638
476
|
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
477
|
+
/**
|
|
478
|
+
* Checks a list of statements for consistency
|
|
479
|
+
* @param sts - The list of statements to check
|
|
480
|
+
* @param from - An index with the array ['subject', 'predicate', 'object', 'why']
|
|
481
|
+
*/
|
|
482
|
+
checkStatementList(sts, from) {
|
|
483
|
+
if (from === undefined) {
|
|
484
|
+
from = 0;
|
|
485
|
+
}
|
|
486
|
+
var names = ['subject', 'predicate', 'object', 'why'];
|
|
487
|
+
var origin = ' found in ' + names[from] + ' index.';
|
|
488
|
+
var st;
|
|
489
|
+
for (var j = 0; j < sts.length; j++) {
|
|
490
|
+
st = sts[j];
|
|
491
|
+
var term = [st.subject, st.predicate, st.object, st.graph];
|
|
492
|
+
var arrayContains = function (a, x) {
|
|
493
|
+
for (var i = 0; i < a.length; i++) {
|
|
494
|
+
if (a[i].subject.equals(x.subject) && a[i].predicate.equals(x.predicate) && a[i].object.equals(x.object) && a[i].why.equals(x.graph)) {
|
|
495
|
+
return true;
|
|
643
496
|
}
|
|
644
497
|
}
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
498
|
+
};
|
|
499
|
+
for (var p = 0; p < 4; p++) {
|
|
500
|
+
var c = this.canon(term[p]);
|
|
501
|
+
var h = this.id(c);
|
|
502
|
+
if (!this.index[p][h]) {
|
|
503
|
+
// throw new Error('No ' + name[p] + ' index for statement ' + st + '@' + st.why + origin)
|
|
504
|
+
} else {
|
|
505
|
+
if (!arrayContains(this.index[p][h], st)) {
|
|
506
|
+
// throw new Error('Index for ' + name[p] + ' does not have statement ' + st + '@' + st.why + origin)
|
|
507
|
+
}
|
|
648
508
|
}
|
|
649
509
|
}
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
* Closes this formula (and return it)
|
|
653
|
-
*/
|
|
654
|
-
|
|
655
|
-
}, {
|
|
656
|
-
key: "close",
|
|
657
|
-
value: function close() {
|
|
658
|
-
return this;
|
|
659
|
-
}
|
|
660
|
-
}, {
|
|
661
|
-
key: "compareTerms",
|
|
662
|
-
value: function compareTerms(u1, u2) {
|
|
663
|
-
// Keep compatibility with downstream classOrder changes
|
|
664
|
-
if (Object.prototype.hasOwnProperty.call(u1, "compareTerm")) {
|
|
665
|
-
return u1.compareTerm(u2);
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
if (ClassOrder[u1.termType] < ClassOrder[u2.termType]) {
|
|
669
|
-
return -1;
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
if (ClassOrder[u1.termType] > ClassOrder[u2.termType]) {
|
|
673
|
-
return +1;
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
if (u1.value < u2.value) {
|
|
677
|
-
return -1;
|
|
510
|
+
if (!arrayContains(this.statements, st)) {
|
|
511
|
+
throw new Error('Statement list does not statement ' + st + '@' + st.graph + origin);
|
|
678
512
|
}
|
|
679
|
-
|
|
680
|
-
if (u1.value > u2.value) {
|
|
681
|
-
return +1;
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
return 0;
|
|
685
513
|
}
|
|
686
|
-
|
|
687
|
-
* replaces @template with @target and add appropriate triples
|
|
688
|
-
* removes no triples by default and is a one-direction replication
|
|
689
|
-
* @param template node to copy
|
|
690
|
-
* @param target node to copy to
|
|
691
|
-
* @param flags Whether or not to do a two-directional copy and/or delete triples
|
|
692
|
-
*/
|
|
693
|
-
|
|
694
|
-
}, {
|
|
695
|
-
key: "copyTo",
|
|
696
|
-
value: function copyTo(template, target, flags) {
|
|
697
|
-
if (!flags) flags = [];
|
|
698
|
-
var statList = this.statementsMatching(template);
|
|
699
|
-
|
|
700
|
-
if (ArrayIndexOf(flags, 'two-direction') !== -1) {
|
|
701
|
-
statList.concat(this.statementsMatching(undefined, undefined, template));
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
for (var i = 0; i < statList.length; i++) {
|
|
705
|
-
var st = statList[i];
|
|
706
|
-
|
|
707
|
-
switch (st.object.termType) {
|
|
708
|
-
case 'NamedNode':
|
|
709
|
-
this.add(target, st.predicate, st.object);
|
|
710
|
-
break;
|
|
711
|
-
|
|
712
|
-
case 'Literal':
|
|
713
|
-
case 'BlankNode': // @ts-ignore Collections can appear here
|
|
714
|
-
|
|
715
|
-
case 'Collection':
|
|
716
|
-
// @ts-ignore Possible bug: copy is not available on Collections
|
|
717
|
-
this.add(target, st.predicate, st.object.copy(this));
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
if (ArrayIndexOf(flags, 'delete') !== -1) {
|
|
721
|
-
this.remove(st);
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
/**
|
|
726
|
-
* Simplify graph in store when we realize two identifiers are equivalent
|
|
727
|
-
* We replace the bigger with the smaller.
|
|
728
|
-
* @param u1in The first node
|
|
729
|
-
* @param u2in The second node
|
|
730
|
-
*/
|
|
731
|
-
|
|
732
|
-
}, {
|
|
733
|
-
key: "equate",
|
|
734
|
-
value: function equate(u1in, u2in) {
|
|
735
|
-
// log.warn("Equating "+u1+" and "+u2); // @@
|
|
736
|
-
// @@JAMBO Must canonicalize the uris to prevent errors from a=b=c
|
|
737
|
-
// 03-21-2010
|
|
738
|
-
var u1 = this.canon(u1in);
|
|
739
|
-
var u2 = this.canon(u2in);
|
|
740
|
-
var d = this.compareTerms(u1, u2);
|
|
741
|
-
|
|
742
|
-
if (!d) {
|
|
743
|
-
return true; // No information in {a = a}
|
|
744
|
-
} // var big
|
|
745
|
-
// var small
|
|
746
|
-
|
|
514
|
+
}
|
|
747
515
|
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
516
|
+
/**
|
|
517
|
+
* Closes this formula (and return it)
|
|
518
|
+
*/
|
|
519
|
+
close() {
|
|
520
|
+
return this;
|
|
521
|
+
}
|
|
522
|
+
compareTerms(u1, u2) {
|
|
523
|
+
// Keep compatibility with downstream classOrder changes
|
|
524
|
+
if (Object.prototype.hasOwnProperty.call(u1, "compareTerm")) {
|
|
525
|
+
return u1.compareTerm(u2);
|
|
754
526
|
}
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
* Only applicable for IndexedFormula, but TypeScript won't allow a subclass to override a property
|
|
758
|
-
* @param features The list of features
|
|
759
|
-
*/
|
|
760
|
-
|
|
761
|
-
}, {
|
|
762
|
-
key: "formula",
|
|
763
|
-
value: function formula(features) {
|
|
764
|
-
return new IndexedFormula(features);
|
|
527
|
+
if (ClassOrder[u1.termType] < ClassOrder[u2.termType]) {
|
|
528
|
+
return -1;
|
|
765
529
|
}
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
* (Getter proxy to this.statements).
|
|
769
|
-
* Usage:
|
|
770
|
-
* ```
|
|
771
|
-
* var kb = rdf.graph()
|
|
772
|
-
* kb.length // -> 0
|
|
773
|
-
* ```
|
|
774
|
-
* @returns {Number}
|
|
775
|
-
*/
|
|
776
|
-
|
|
777
|
-
}, {
|
|
778
|
-
key: "length",
|
|
779
|
-
get: function get() {
|
|
780
|
-
return this.statements.length;
|
|
530
|
+
if (ClassOrder[u1.termType] > ClassOrder[u2.termType]) {
|
|
531
|
+
return +1;
|
|
781
532
|
}
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
* Standard RDFJS spec method for Source objects, implemented as an
|
|
785
|
-
* alias to `statementsMatching()`
|
|
786
|
-
* @param subject The subject
|
|
787
|
-
* @param predicate The predicate
|
|
788
|
-
* @param object The object
|
|
789
|
-
* @param graph The graph that contains the statement
|
|
790
|
-
*/
|
|
791
|
-
|
|
792
|
-
}, {
|
|
793
|
-
key: "match",
|
|
794
|
-
value: function match(subject, predicate, object, graph) {
|
|
795
|
-
return this.statementsMatching(Node.fromValue(subject), Node.fromValue(predicate), Node.fromValue(object), Node.fromValue(graph));
|
|
533
|
+
if (u1.value < u2.value) {
|
|
534
|
+
return -1;
|
|
796
535
|
}
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
* @param uri The URI to look for
|
|
800
|
-
*/
|
|
801
|
-
|
|
802
|
-
}, {
|
|
803
|
-
key: "mentionsURI",
|
|
804
|
-
value: function mentionsURI(uri) {
|
|
805
|
-
var hash = '<' + uri + '>';
|
|
806
|
-
return !!this.subjectIndex[hash] || !!this.objectIndex[hash] || !!this.predicateIndex[hash];
|
|
536
|
+
if (u1.value > u2.value) {
|
|
537
|
+
return +1;
|
|
807
538
|
}
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
* @param uri An URI
|
|
811
|
-
*/
|
|
812
|
-
|
|
813
|
-
}, {
|
|
814
|
-
key: "newExistential",
|
|
815
|
-
value: function newExistential(uri) {
|
|
816
|
-
if (!uri) return this.bnode();
|
|
817
|
-
var x = this.sym(uri); // @ts-ignore x should be blanknode, but is namedNode.
|
|
539
|
+
return 0;
|
|
540
|
+
}
|
|
818
541
|
|
|
819
|
-
|
|
542
|
+
/**
|
|
543
|
+
* replaces @template with @target and add appropriate triples
|
|
544
|
+
* removes no triples by default and is a one-direction replication
|
|
545
|
+
* @param template node to copy
|
|
546
|
+
* @param target node to copy to
|
|
547
|
+
* @param flags Whether or not to do a two-directional copy and/or delete triples
|
|
548
|
+
*/
|
|
549
|
+
copyTo(template, target, flags) {
|
|
550
|
+
if (!flags) flags = [];
|
|
551
|
+
var statList = this.statementsMatching(template);
|
|
552
|
+
if (ArrayIndexOf(flags, 'two-direction') !== -1) {
|
|
553
|
+
statList.concat(this.statementsMatching(undefined, undefined, template));
|
|
820
554
|
}
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
if (!this.propertyActions[hash]) {
|
|
834
|
-
this.propertyActions[hash] = [];
|
|
555
|
+
for (var i = 0; i < statList.length; i++) {
|
|
556
|
+
var st = statList[i];
|
|
557
|
+
switch (st.object.termType) {
|
|
558
|
+
case 'NamedNode':
|
|
559
|
+
this.add(target, st.predicate, st.object);
|
|
560
|
+
break;
|
|
561
|
+
case 'Literal':
|
|
562
|
+
case 'BlankNode':
|
|
563
|
+
// @ts-ignore Collections can appear here
|
|
564
|
+
case 'Collection':
|
|
565
|
+
// @ts-ignore Possible bug: copy is not available on Collections
|
|
566
|
+
this.add(target, st.predicate, st.object.copy(this));
|
|
835
567
|
}
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
var toBeFixed = this.statementsMatching(undefined, pred, undefined);
|
|
840
|
-
var done = false;
|
|
841
|
-
|
|
842
|
-
for (var i = 0; i < toBeFixed.length; i++) {
|
|
843
|
-
// NOT optimized - sort toBeFixed etc
|
|
844
|
-
done = done || action(this, toBeFixed[i].subject, pred, toBeFixed[i].object);
|
|
568
|
+
if (ArrayIndexOf(flags, 'delete') !== -1) {
|
|
569
|
+
this.remove(st);
|
|
845
570
|
}
|
|
846
|
-
|
|
847
|
-
return done;
|
|
848
571
|
}
|
|
849
|
-
|
|
850
|
-
* Creates a new universal node
|
|
851
|
-
* Universals are Variables
|
|
852
|
-
* @param uri An URI
|
|
853
|
-
*/
|
|
854
|
-
|
|
855
|
-
}, {
|
|
856
|
-
key: "newUniversal",
|
|
857
|
-
value: function newUniversal(uri) {
|
|
858
|
-
var x = this.sym(uri);
|
|
859
|
-
if (!this._universalVariables) this._universalVariables = [];
|
|
860
|
-
|
|
861
|
-
this._universalVariables.push(x);
|
|
862
|
-
|
|
863
|
-
return x;
|
|
864
|
-
} // convenience function used by N3 parser
|
|
865
|
-
|
|
866
|
-
}, {
|
|
867
|
-
key: "variable",
|
|
868
|
-
value: function variable(name) {
|
|
869
|
-
return new Variable(name);
|
|
870
|
-
}
|
|
871
|
-
/**
|
|
872
|
-
* Find an unused id for a file being edited: return a symbol
|
|
873
|
-
* (Note: Slow iff a lot of them -- could be O(log(k)) )
|
|
874
|
-
* @param doc A document named node
|
|
875
|
-
*/
|
|
572
|
+
}
|
|
876
573
|
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
574
|
+
/**
|
|
575
|
+
* Simplify graph in store when we realize two identifiers are equivalent
|
|
576
|
+
* We replace the bigger with the smaller.
|
|
577
|
+
* @param u1in The first node
|
|
578
|
+
* @param u2in The second node
|
|
579
|
+
*/
|
|
580
|
+
equate(u1in, u2in) {
|
|
581
|
+
// log.warn("Equating "+u1+" and "+u2); // @@
|
|
582
|
+
// @@JAMBO Must canonicalize the uris to prevent errors from a=b=c
|
|
583
|
+
// 03-21-2010
|
|
584
|
+
const u1 = this.canon(u1in);
|
|
585
|
+
const u2 = this.canon(u2in);
|
|
586
|
+
var d = this.compareTerms(u1, u2);
|
|
587
|
+
if (!d) {
|
|
588
|
+
return true; // No information in {a = a}
|
|
884
589
|
}
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
*/
|
|
893
|
-
|
|
894
|
-
}, {
|
|
895
|
-
key: "query",
|
|
896
|
-
value: function query(myQuery, callback, fetcher, onDone) {
|
|
897
|
-
return indexedFormulaQuery.call(this, myQuery, callback, fetcher, onDone);
|
|
590
|
+
// var big
|
|
591
|
+
// var small
|
|
592
|
+
if (d < 0) {
|
|
593
|
+
// u1 less than u2
|
|
594
|
+
return this.replaceWith(u2, u1);
|
|
595
|
+
} else {
|
|
596
|
+
return this.replaceWith(u1, u2);
|
|
898
597
|
}
|
|
899
|
-
|
|
900
|
-
* Query this store synchronously and return bindings
|
|
901
|
-
*
|
|
902
|
-
* @param myQuery The query to be run
|
|
903
|
-
*/
|
|
904
|
-
|
|
905
|
-
}, {
|
|
906
|
-
key: "querySync",
|
|
907
|
-
value: function querySync(myQuery) {
|
|
908
|
-
var results = [];
|
|
598
|
+
}
|
|
909
599
|
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
600
|
+
/**
|
|
601
|
+
* Creates a new empty indexed formula
|
|
602
|
+
* Only applicable for IndexedFormula, but TypeScript won't allow a subclass to override a property
|
|
603
|
+
* @param features The list of features
|
|
604
|
+
*/
|
|
605
|
+
formula(features) {
|
|
606
|
+
return new IndexedFormula(features);
|
|
607
|
+
}
|
|
913
608
|
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
609
|
+
/**
|
|
610
|
+
* Returns the number of statements contained in this IndexedFormula.
|
|
611
|
+
* (Getter proxy to this.statements).
|
|
612
|
+
* Usage:
|
|
613
|
+
* ```
|
|
614
|
+
* var kb = rdf.graph()
|
|
615
|
+
* kb.length // -> 0
|
|
616
|
+
* ```
|
|
617
|
+
* @returns {Number}
|
|
618
|
+
*/
|
|
619
|
+
get length() {
|
|
620
|
+
return this.statements.length;
|
|
621
|
+
}
|
|
917
622
|
|
|
918
|
-
|
|
623
|
+
/**
|
|
624
|
+
* Returns any quads matching the given arguments.
|
|
625
|
+
* Standard RDFJS spec method for Source objects, implemented as an
|
|
626
|
+
* alias to `statementsMatching()`
|
|
627
|
+
* @param subject The subject
|
|
628
|
+
* @param predicate The predicate
|
|
629
|
+
* @param object The object
|
|
630
|
+
* @param graph The graph that contains the statement
|
|
631
|
+
*/
|
|
632
|
+
match(subject, predicate, object, graph) {
|
|
633
|
+
return this.statementsMatching(Node.fromValue(subject), Node.fromValue(predicate), Node.fromValue(object), Node.fromValue(graph));
|
|
634
|
+
}
|
|
919
635
|
|
|
920
|
-
|
|
921
|
-
|
|
636
|
+
/**
|
|
637
|
+
* Find out whether a given URI is used as symbol in the formula
|
|
638
|
+
* @param uri The URI to look for
|
|
639
|
+
*/
|
|
640
|
+
mentionsURI(uri) {
|
|
641
|
+
var hash = '<' + uri + '>';
|
|
642
|
+
return !!this.subjectIndex[hash] || !!this.objectIndex[hash] || !!this.predicateIndex[hash];
|
|
643
|
+
}
|
|
922
644
|
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
645
|
+
/**
|
|
646
|
+
* Existentials are BNodes - something exists without naming
|
|
647
|
+
* @param uri An URI
|
|
648
|
+
*/
|
|
649
|
+
newExistential(uri) {
|
|
650
|
+
if (!uri) return this.bnode();
|
|
651
|
+
var x = this.sym(uri);
|
|
652
|
+
// @ts-ignore x should be blanknode, but is namedNode.
|
|
653
|
+
return this.declareExistential(x);
|
|
654
|
+
}
|
|
926
655
|
|
|
927
|
-
|
|
656
|
+
/**
|
|
657
|
+
* Adds a new property action
|
|
658
|
+
* @param pred the predicate that the function should be triggered on
|
|
659
|
+
* @param action the function that should trigger
|
|
660
|
+
*/
|
|
661
|
+
newPropertyAction(pred, action) {
|
|
662
|
+
// log.debug("newPropertyAction: "+pred)
|
|
663
|
+
var hash = this.id(pred);
|
|
664
|
+
if (!this.propertyActions[hash]) {
|
|
665
|
+
this.propertyActions[hash] = [];
|
|
928
666
|
}
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
this.remove(st[i]);
|
|
940
|
-
}
|
|
667
|
+
this.propertyActions[hash].push(action);
|
|
668
|
+
// Now apply the function to to statements already in the store
|
|
669
|
+
var toBeFixed = this.statementsMatching(undefined, pred, undefined);
|
|
670
|
+
var done = false;
|
|
671
|
+
for (var i = 0; i < toBeFixed.length; i++) {
|
|
672
|
+
// NOT optimized - sort toBeFixed etc
|
|
673
|
+
done = done || action(this, toBeFixed[i].subject, pred, toBeFixed[i].object);
|
|
674
|
+
}
|
|
675
|
+
return done;
|
|
676
|
+
}
|
|
941
677
|
|
|
942
|
-
|
|
943
|
-
|
|
678
|
+
/**
|
|
679
|
+
* Creates a new universal node
|
|
680
|
+
* Universals are Variables
|
|
681
|
+
* @param uri An URI
|
|
682
|
+
*/
|
|
683
|
+
newUniversal(uri) {
|
|
684
|
+
var x = this.sym(uri);
|
|
685
|
+
if (!this._universalVariables) this._universalVariables = [];
|
|
686
|
+
this._universalVariables.push(x);
|
|
687
|
+
return x;
|
|
688
|
+
}
|
|
944
689
|
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
690
|
+
// convenience function used by N3 parser
|
|
691
|
+
variable(name) {
|
|
692
|
+
return new Variable(name);
|
|
693
|
+
}
|
|
948
694
|
|
|
949
|
-
|
|
695
|
+
/**
|
|
696
|
+
* Find an unused id for a file being edited: return a symbol
|
|
697
|
+
* (Note: Slow iff a lot of them -- could be O(log(k)) )
|
|
698
|
+
* @param doc A document named node
|
|
699
|
+
*/
|
|
700
|
+
nextSymbol(doc) {
|
|
701
|
+
for (var i = 0;; i++) {
|
|
702
|
+
var uri = doc.value + '#n' + i;
|
|
703
|
+
if (!this.mentionsURI(uri)) return this.sym(uri);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
950
706
|
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
707
|
+
/**
|
|
708
|
+
* Query this store asynchronously, return bindings in callback
|
|
709
|
+
*
|
|
710
|
+
* @param myQuery The query to be run
|
|
711
|
+
* @param callback Function to call when bindings
|
|
712
|
+
* @param Fetcher | null If you want the query to do link following
|
|
713
|
+
* @param onDone OBSOLETE - do not use this // @@ Why not ?? Called when query complete
|
|
714
|
+
*/
|
|
715
|
+
query(myQuery, callback, fetcher, onDone) {
|
|
716
|
+
return indexedFormulaQuery.call(this, myQuery, callback, fetcher, onDone);
|
|
717
|
+
}
|
|
954
718
|
|
|
955
|
-
|
|
956
|
-
|
|
719
|
+
/**
|
|
720
|
+
* Query this store synchronously and return bindings
|
|
721
|
+
*
|
|
722
|
+
* @param myQuery The query to be run
|
|
723
|
+
*/
|
|
724
|
+
querySync(myQuery) {
|
|
725
|
+
var results = [];
|
|
726
|
+
function saveBinginds(bindings) {
|
|
727
|
+
results.push(bindings);
|
|
957
728
|
}
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
729
|
+
function onDone() {
|
|
730
|
+
done = true;
|
|
731
|
+
}
|
|
732
|
+
var done = false;
|
|
733
|
+
// @ts-ignore TODO: Add .sync to Query
|
|
734
|
+
myQuery.sync = true;
|
|
735
|
+
indexedFormulaQuery.call(this, myQuery, saveBinginds, null, onDone);
|
|
736
|
+
if (!done) {
|
|
737
|
+
throw new Error('Sync query should have called done function');
|
|
738
|
+
}
|
|
739
|
+
return results;
|
|
740
|
+
}
|
|
967
741
|
|
|
968
|
-
|
|
969
|
-
|
|
742
|
+
/**
|
|
743
|
+
* Removes one or multiple statement(s) from this formula
|
|
744
|
+
* @param st - A Statement or array of Statements to remove
|
|
745
|
+
*/
|
|
746
|
+
remove(st) {
|
|
747
|
+
if (st instanceof Array) {
|
|
748
|
+
for (var i = 0; i < st.length; i++) {
|
|
749
|
+
this.remove(st[i]);
|
|
970
750
|
}
|
|
971
|
-
|
|
972
751
|
return this;
|
|
973
752
|
}
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
* @param subj The subject
|
|
977
|
-
* @param pred The predicate
|
|
978
|
-
* @param obj The object
|
|
979
|
-
* @param why The graph that contains the statement
|
|
980
|
-
* @param limit The number of statements to remove
|
|
981
|
-
*/
|
|
982
|
-
|
|
983
|
-
}, {
|
|
984
|
-
key: "removeMany",
|
|
985
|
-
value: function removeMany(subj, pred, obj, why, limit) {
|
|
986
|
-
// log.debug("entering removeMany w/ subj,pred,obj,why,limit = " + subj +", "+ pred+", " + obj+", " + why+", " + limit)
|
|
987
|
-
var sts = this.statementsMatching(subj, pred, obj, why, false); // This is a subtle bug that occurred in updateCenter.js too.
|
|
988
|
-
// The fact is, this.statementsMatching returns this.whyIndex instead of a copy of it
|
|
989
|
-
// but for perfromance consideration, it's better to just do that
|
|
990
|
-
// so make a copy here.
|
|
991
|
-
|
|
992
|
-
var statements = [];
|
|
993
|
-
|
|
994
|
-
for (var i = 0; i < sts.length; i++) {
|
|
995
|
-
statements.push(sts[i]);
|
|
996
|
-
}
|
|
997
|
-
|
|
998
|
-
if (limit) statements = statements.slice(0, limit);
|
|
999
|
-
|
|
1000
|
-
for (i = 0; i < statements.length; i++) {
|
|
1001
|
-
this.remove(statements[i]);
|
|
1002
|
-
}
|
|
753
|
+
if (isStore(st)) {
|
|
754
|
+
return this.remove(st.statements);
|
|
1003
755
|
}
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
756
|
+
var sts = this.statementsMatching(st.subject, st.predicate, st.object, st.graph);
|
|
757
|
+
if (!sts.length) {
|
|
758
|
+
throw new Error('Statement to be removed is not on store: ' + st);
|
|
759
|
+
}
|
|
760
|
+
this.removeStatement(sts[0]);
|
|
761
|
+
return this;
|
|
762
|
+
}
|
|
1011
763
|
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
764
|
+
/**
|
|
765
|
+
* Removes all statements in a doc
|
|
766
|
+
* @param doc - The document / graph
|
|
767
|
+
*/
|
|
768
|
+
removeDocument(doc) {
|
|
769
|
+
var sts = this.statementsMatching(undefined, undefined, undefined, doc).slice(); // Take a copy as this is the actual index
|
|
770
|
+
for (var i = 0; i < sts.length; i++) {
|
|
771
|
+
this.removeStatement(sts[i]);
|
|
1017
772
|
}
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
*
|
|
1021
|
-
* @param st - a statement which is already in the store and indexed.
|
|
1022
|
-
* Make sure you only use this for these.
|
|
1023
|
-
* Otherwise, you should use remove() above.
|
|
1024
|
-
*/
|
|
773
|
+
return this;
|
|
774
|
+
}
|
|
1025
775
|
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
776
|
+
/**
|
|
777
|
+
* Remove all statements matching args (within limit) *
|
|
778
|
+
* @param subj The subject
|
|
779
|
+
* @param pred The predicate
|
|
780
|
+
* @param obj The object
|
|
781
|
+
* @param why The graph that contains the statement
|
|
782
|
+
* @param limit The number of statements to remove
|
|
783
|
+
*/
|
|
784
|
+
removeMany(subj, pred, obj, why, limit) {
|
|
785
|
+
// log.debug("entering removeMany w/ subj,pred,obj,why,limit = " + subj +", "+ pred+", " + obj+", " + why+", " + limit)
|
|
786
|
+
var sts = this.statementsMatching(subj, pred, obj, why, false);
|
|
787
|
+
// This is a subtle bug that occurred in updateCenter.js too.
|
|
788
|
+
// The fact is, this.statementsMatching returns this.whyIndex instead of a copy of it
|
|
789
|
+
// but for perfromance consideration, it's better to just do that
|
|
790
|
+
// so make a copy here.
|
|
791
|
+
var statements = [];
|
|
792
|
+
for (var i = 0; i < sts.length; i++) statements.push(sts[i]);
|
|
793
|
+
if (limit) statements = statements.slice(0, limit);
|
|
794
|
+
for (i = 0; i < statements.length; i++) this.remove(statements[i]);
|
|
795
|
+
}
|
|
1031
796
|
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
797
|
+
/**
|
|
798
|
+
* Remove all matching statements
|
|
799
|
+
* @param subject The subject
|
|
800
|
+
* @param predicate The predicate
|
|
801
|
+
* @param object The object
|
|
802
|
+
* @param graph The graph that contains the statement
|
|
803
|
+
*/
|
|
804
|
+
removeMatches(subject, predicate, object, graph) {
|
|
805
|
+
this.removeMany(subject, predicate, object, graph);
|
|
806
|
+
return this;
|
|
807
|
+
}
|
|
1035
808
|
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
809
|
+
/**
|
|
810
|
+
* Remove a particular statement object from the store
|
|
811
|
+
*
|
|
812
|
+
* @param st - a statement which is already in the store and indexed.
|
|
813
|
+
* Make sure you only use this for these.
|
|
814
|
+
* Otherwise, you should use remove() above.
|
|
815
|
+
*/
|
|
816
|
+
removeStatement(st) {
|
|
817
|
+
// log.debug("entering remove w/ st=" + st)
|
|
818
|
+
var term = [st.subject, st.predicate, st.object, st.graph];
|
|
819
|
+
for (var p = 0; p < 4; p++) {
|
|
820
|
+
var c = this.canon(term[p]);
|
|
821
|
+
var h = this.id(c);
|
|
822
|
+
if (!this.index[p][h]) {
|
|
823
|
+
// log.warn ("Statement removal: no index '+p+': "+st)
|
|
824
|
+
} else {
|
|
825
|
+
this.rdfArrayRemove(this.index[p][h], st);
|
|
1040
826
|
}
|
|
827
|
+
}
|
|
828
|
+
this.rdfArrayRemove(this.statements, st);
|
|
829
|
+
return this;
|
|
830
|
+
}
|
|
1041
831
|
|
|
1042
|
-
|
|
1043
|
-
|
|
832
|
+
/**
|
|
833
|
+
* Removes statements
|
|
834
|
+
* @param sts The statements to remove
|
|
835
|
+
*/
|
|
836
|
+
removeStatements(sts) {
|
|
837
|
+
for (var i = 0; i < sts.length; i++) {
|
|
838
|
+
this.remove(sts[i]);
|
|
1044
839
|
}
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
* @param sts The statements to remove
|
|
1048
|
-
*/
|
|
840
|
+
return this;
|
|
841
|
+
}
|
|
1049
842
|
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
843
|
+
/**
|
|
844
|
+
* Replace big with small, obsoleted with obsoleting.
|
|
845
|
+
*/
|
|
846
|
+
replaceWith(big, small) {
|
|
847
|
+
// log.debug("Replacing "+big+" with "+small) // this.id(@@
|
|
848
|
+
var oldhash = this.id(big);
|
|
849
|
+
var newhash = this.id(small);
|
|
850
|
+
var moveIndex = function (ix) {
|
|
851
|
+
var oldlist = ix[oldhash];
|
|
852
|
+
if (!oldlist) {
|
|
853
|
+
return; // none to move
|
|
1055
854
|
}
|
|
1056
855
|
|
|
1057
|
-
|
|
856
|
+
var newlist = ix[newhash];
|
|
857
|
+
if (!newlist) {
|
|
858
|
+
ix[newhash] = oldlist;
|
|
859
|
+
} else {
|
|
860
|
+
ix[newhash] = oldlist.concat(newlist);
|
|
861
|
+
}
|
|
862
|
+
delete ix[oldhash];
|
|
863
|
+
};
|
|
864
|
+
// the canonical one carries all the indexes
|
|
865
|
+
for (var i = 0; i < 4; i++) {
|
|
866
|
+
moveIndex(this.index[i]);
|
|
1058
867
|
}
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
key: "replaceWith",
|
|
1065
|
-
value: function replaceWith(big, small) {
|
|
1066
|
-
// log.debug("Replacing "+big+" with "+small) // this.id(@@
|
|
1067
|
-
var oldhash = this.id(big);
|
|
1068
|
-
var newhash = this.id(small);
|
|
1069
|
-
|
|
1070
|
-
var moveIndex = function moveIndex(ix) {
|
|
1071
|
-
var oldlist = ix[oldhash];
|
|
1072
|
-
|
|
1073
|
-
if (!oldlist) {
|
|
1074
|
-
return; // none to move
|
|
1075
|
-
}
|
|
1076
|
-
|
|
1077
|
-
var newlist = ix[newhash];
|
|
1078
|
-
|
|
1079
|
-
if (!newlist) {
|
|
1080
|
-
ix[newhash] = oldlist;
|
|
1081
|
-
} else {
|
|
1082
|
-
ix[newhash] = oldlist.concat(newlist);
|
|
1083
|
-
}
|
|
1084
|
-
|
|
1085
|
-
delete ix[oldhash];
|
|
1086
|
-
}; // the canonical one carries all the indexes
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
for (var i = 0; i < 4; i++) {
|
|
1090
|
-
moveIndex(this.index[i]);
|
|
868
|
+
this.redirections[oldhash] = small;
|
|
869
|
+
if (big.value) {
|
|
870
|
+
// @@JAMBO: must update redirections,aliases from sub-items, too.
|
|
871
|
+
if (!this.aliases[newhash]) {
|
|
872
|
+
this.aliases[newhash] = [];
|
|
1091
873
|
}
|
|
1092
|
-
|
|
1093
|
-
this.
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
if (!this.aliases[newhash]) {
|
|
1098
|
-
this.aliases[newhash] = [];
|
|
1099
|
-
}
|
|
1100
|
-
|
|
1101
|
-
this.aliases[newhash].push(big); // Back link
|
|
1102
|
-
|
|
1103
|
-
if (this.aliases[oldhash]) {
|
|
1104
|
-
for (i = 0; i < this.aliases[oldhash].length; i++) {
|
|
1105
|
-
this.redirections[this.id(this.aliases[oldhash][i])] = small;
|
|
1106
|
-
this.aliases[newhash].push(this.aliases[oldhash][i]);
|
|
1107
|
-
}
|
|
1108
|
-
}
|
|
1109
|
-
|
|
1110
|
-
this.add(small, this.sym('http://www.w3.org/2007/ont/link#uri'), big); // If two things are equal, and one is requested, we should request the other.
|
|
1111
|
-
|
|
1112
|
-
if (this.fetcher) {
|
|
1113
|
-
this.fetcher.nowKnownAs(big, small);
|
|
874
|
+
this.aliases[newhash].push(big); // Back link
|
|
875
|
+
if (this.aliases[oldhash]) {
|
|
876
|
+
for (i = 0; i < this.aliases[oldhash].length; i++) {
|
|
877
|
+
this.redirections[this.id(this.aliases[oldhash][i])] = small;
|
|
878
|
+
this.aliases[newhash].push(this.aliases[oldhash][i]);
|
|
1114
879
|
}
|
|
1115
880
|
}
|
|
881
|
+
this.add(small, this.sym('http://www.w3.org/2007/ont/link#uri'), big);
|
|
882
|
+
// If two things are equal, and one is requested, we should request the other.
|
|
883
|
+
if (this.fetcher) {
|
|
884
|
+
this.fetcher.nowKnownAs(big, small);
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
moveIndex(this.classActions);
|
|
888
|
+
moveIndex(this.propertyActions);
|
|
889
|
+
// log.debug("Equate done. "+big+" to be known as "+small)
|
|
890
|
+
return true; // true means the statement does not need to be put in
|
|
891
|
+
}
|
|
1116
892
|
|
|
1117
|
-
|
|
1118
|
-
|
|
893
|
+
/**
|
|
894
|
+
* Return all equivalent URIs by which this is known
|
|
895
|
+
* @param x A named node
|
|
896
|
+
*/
|
|
897
|
+
allAliases(x) {
|
|
898
|
+
var a = this.aliases[this.id(this.canon(x))] || [];
|
|
899
|
+
a.push(this.canon(x));
|
|
900
|
+
return a;
|
|
901
|
+
}
|
|
1119
902
|
|
|
1120
|
-
|
|
903
|
+
/**
|
|
904
|
+
* Compare by canonical URI as smushed
|
|
905
|
+
* @param x A named node
|
|
906
|
+
* @param y Another named node
|
|
907
|
+
*/
|
|
908
|
+
sameThings(x, y) {
|
|
909
|
+
if (x.equals(y)) {
|
|
910
|
+
return true;
|
|
1121
911
|
}
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
912
|
+
var x1 = this.canon(x);
|
|
913
|
+
// alert('x1='+x1)
|
|
914
|
+
if (!x1) return false;
|
|
915
|
+
var y1 = this.canon(y);
|
|
916
|
+
// alert('y1='+y1); //@@
|
|
917
|
+
if (!y1) return false;
|
|
918
|
+
return x1.value === y1.value;
|
|
919
|
+
}
|
|
920
|
+
setPrefixForURI(prefix, nsuri) {
|
|
921
|
+
// TODO: This is a hack for our own issues, which ought to be fixed
|
|
922
|
+
// post-release
|
|
923
|
+
// See http://dig.csail.mit.edu/cgi-bin/roundup.cgi/$rdf/issue227
|
|
924
|
+
if (prefix === 'tab' && this.namespaces['tab']) {
|
|
925
|
+
return;
|
|
926
|
+
} // There are files around with long badly generated prefixes like this
|
|
927
|
+
if (prefix.slice(0, 2) === 'ns' || prefix.slice(0, 7) === 'default') {
|
|
928
|
+
return;
|
|
1133
929
|
}
|
|
1134
|
-
/**
|
|
1135
|
-
* Compare by canonical URI as smushed
|
|
1136
|
-
* @param x A named node
|
|
1137
|
-
* @param y Another named node
|
|
1138
|
-
*/
|
|
1139
|
-
|
|
1140
|
-
}, {
|
|
1141
|
-
key: "sameThings",
|
|
1142
|
-
value: function sameThings(x, y) {
|
|
1143
|
-
if (x.equals(y)) {
|
|
1144
|
-
return true;
|
|
1145
|
-
}
|
|
1146
|
-
|
|
1147
|
-
var x1 = this.canon(x); // alert('x1='+x1)
|
|
1148
|
-
|
|
1149
|
-
if (!x1) return false;
|
|
1150
|
-
var y1 = this.canon(y); // alert('y1='+y1); //@@
|
|
1151
930
|
|
|
1152
|
-
|
|
1153
|
-
|
|
931
|
+
// remove any prefix that currently targets nsuri
|
|
932
|
+
for (let existingPrefix in this.namespaces) {
|
|
933
|
+
if (this.namespaces[existingPrefix] == nsuri) delete this.namespaces[existingPrefix];
|
|
1154
934
|
}
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
value: function setPrefixForURI(prefix, nsuri) {
|
|
1158
|
-
// TODO: This is a hack for our own issues, which ought to be fixed
|
|
1159
|
-
// post-release
|
|
1160
|
-
// See http://dig.csail.mit.edu/cgi-bin/roundup.cgi/$rdf/issue227
|
|
1161
|
-
if (prefix === 'tab' && this.namespaces['tab']) {
|
|
1162
|
-
return;
|
|
1163
|
-
} // There are files around with long badly generated prefixes like this
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
if (prefix.slice(0, 2) === 'ns' || prefix.slice(0, 7) === 'default') {
|
|
1167
|
-
return;
|
|
1168
|
-
} // remove any prefix that currently targets nsuri
|
|
1169
|
-
|
|
935
|
+
this.namespaces[prefix] = nsuri;
|
|
936
|
+
}
|
|
1170
937
|
|
|
1171
|
-
|
|
1172
|
-
|
|
938
|
+
/** Search the Store
|
|
939
|
+
*
|
|
940
|
+
* ALL CONVENIENCE LOOKUP FUNCTIONS RELY ON THIS!
|
|
941
|
+
* @param subj - A node to search for as subject, or if null, a wildcard
|
|
942
|
+
* @param pred - A node to search for as predicate, or if null, a wildcard
|
|
943
|
+
* @param obj - A node to search for as object, or if null, a wildcard
|
|
944
|
+
* @param why - A node to search for as graph, or if null, a wildcard
|
|
945
|
+
* @param justOne - flag - stop when found one rather than get all of them?
|
|
946
|
+
* @returns An array of nodes which match the wildcard position
|
|
947
|
+
*/
|
|
948
|
+
statementsMatching(subj, pred, obj, why, justOne) {
|
|
949
|
+
// log.debug("Matching {"+subj+" "+pred+" "+obj+"}")
|
|
950
|
+
var pat = [subj, pred, obj, why];
|
|
951
|
+
var pattern = [];
|
|
952
|
+
var hash = [];
|
|
953
|
+
var wild = []; // wildcards
|
|
954
|
+
var given = []; // Not wild
|
|
955
|
+
var p;
|
|
956
|
+
var list;
|
|
957
|
+
for (p = 0; p < 4; p++) {
|
|
958
|
+
pattern[p] = this.canon(Node.fromValue(pat[p]));
|
|
959
|
+
if (!pattern[p]) {
|
|
960
|
+
wild.push(p);
|
|
961
|
+
} else {
|
|
962
|
+
given.push(p);
|
|
963
|
+
hash[p] = this.id(pattern[p]);
|
|
1173
964
|
}
|
|
1174
|
-
|
|
1175
|
-
this.namespaces[prefix] = nsuri;
|
|
1176
965
|
}
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
}, {
|
|
1189
|
-
key: "statementsMatching",
|
|
1190
|
-
value: function statementsMatching(subj, pred, obj, why, justOne) {
|
|
1191
|
-
// log.debug("Matching {"+subj+" "+pred+" "+obj+"}")
|
|
1192
|
-
var pat = [subj, pred, obj, why];
|
|
1193
|
-
var pattern = [];
|
|
1194
|
-
var hash = [];
|
|
1195
|
-
var wild = []; // wildcards
|
|
1196
|
-
|
|
1197
|
-
var given = []; // Not wild
|
|
1198
|
-
|
|
1199
|
-
var p;
|
|
1200
|
-
var list;
|
|
1201
|
-
|
|
1202
|
-
for (p = 0; p < 4; p++) {
|
|
1203
|
-
pattern[p] = this.canon(Node.fromValue(pat[p]));
|
|
1204
|
-
|
|
1205
|
-
if (!pattern[p]) {
|
|
1206
|
-
wild.push(p);
|
|
1207
|
-
} else {
|
|
1208
|
-
given.push(p);
|
|
1209
|
-
hash[p] = this.id(pattern[p]);
|
|
966
|
+
if (given.length === 0) {
|
|
967
|
+
return this.statements;
|
|
968
|
+
}
|
|
969
|
+
if (given.length === 1) {
|
|
970
|
+
// Easy too, we have an index for that
|
|
971
|
+
p = given[0];
|
|
972
|
+
list = this.index[p][hash[p]];
|
|
973
|
+
if (list && justOne) {
|
|
974
|
+
if (list.length > 1) {
|
|
975
|
+
list = list.slice(0, 1);
|
|
1210
976
|
}
|
|
1211
977
|
}
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
978
|
+
list = list || [];
|
|
979
|
+
return list;
|
|
980
|
+
}
|
|
981
|
+
// Now given.length is 2, 3 or 4.
|
|
982
|
+
// We hope that the scale-free nature of the data will mean we tend to get
|
|
983
|
+
// a short index in there somewhere!
|
|
984
|
+
var best = 1e10; // really bad
|
|
985
|
+
var iBest;
|
|
986
|
+
var i;
|
|
987
|
+
for (i = 0; i < given.length; i++) {
|
|
988
|
+
p = given[i]; // Which part we are dealing with
|
|
989
|
+
list = this.index[p][hash[p]];
|
|
990
|
+
if (!list) {
|
|
991
|
+
return []; // No occurrences
|
|
1215
992
|
}
|
|
1216
993
|
|
|
1217
|
-
if (
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
list = this.index[p][hash[p]];
|
|
1221
|
-
|
|
1222
|
-
if (list && justOne) {
|
|
1223
|
-
if (list.length > 1) {
|
|
1224
|
-
list = list.slice(0, 1);
|
|
1225
|
-
}
|
|
1226
|
-
}
|
|
1227
|
-
|
|
1228
|
-
list = list || [];
|
|
1229
|
-
return list;
|
|
1230
|
-
} // Now given.length is 2, 3 or 4.
|
|
1231
|
-
// We hope that the scale-free nature of the data will mean we tend to get
|
|
1232
|
-
// a short index in there somewhere!
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
var best = 1e10; // really bad
|
|
1236
|
-
|
|
1237
|
-
var iBest;
|
|
1238
|
-
var i;
|
|
1239
|
-
|
|
1240
|
-
for (i = 0; i < given.length; i++) {
|
|
1241
|
-
p = given[i]; // Which part we are dealing with
|
|
1242
|
-
|
|
1243
|
-
list = this.index[p][hash[p]];
|
|
1244
|
-
|
|
1245
|
-
if (!list) {
|
|
1246
|
-
return []; // No occurrences
|
|
1247
|
-
}
|
|
1248
|
-
|
|
1249
|
-
if (list.length < best) {
|
|
1250
|
-
best = list.length;
|
|
1251
|
-
iBest = i; // (not p!)
|
|
1252
|
-
}
|
|
1253
|
-
} // Ok, we have picked the shortest index but now we have to filter it
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
var pBest = given[iBest];
|
|
1257
|
-
var possibles = this.index[pBest][hash[pBest]];
|
|
1258
|
-
var check = given.slice(0, iBest).concat(given.slice(iBest + 1)); // remove iBest
|
|
1259
|
-
|
|
1260
|
-
var results = [];
|
|
1261
|
-
var parts = ['subject', 'predicate', 'object', 'why'];
|
|
1262
|
-
|
|
1263
|
-
for (var j = 0; j < possibles.length; j++) {
|
|
1264
|
-
var st = possibles[j];
|
|
1265
|
-
|
|
1266
|
-
for (i = 0; i < check.length; i++) {
|
|
1267
|
-
// for each position to be checked
|
|
1268
|
-
p = check[i];
|
|
1269
|
-
|
|
1270
|
-
if (!this.canon(st[parts[p]]).equals(pattern[p])) {
|
|
1271
|
-
st = null;
|
|
1272
|
-
break;
|
|
1273
|
-
}
|
|
1274
|
-
}
|
|
1275
|
-
|
|
1276
|
-
if (st != null) {
|
|
1277
|
-
results.push(st);
|
|
1278
|
-
if (justOne) break;
|
|
1279
|
-
}
|
|
994
|
+
if (list.length < best) {
|
|
995
|
+
best = list.length;
|
|
996
|
+
iBest = i; // (not p!)
|
|
1280
997
|
}
|
|
1281
|
-
|
|
1282
|
-
return results;
|
|
1283
998
|
}
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
for (var i = 0; i < terms.length; i++) {
|
|
1299
|
-
res.push(terms[i].uri);
|
|
999
|
+
// Ok, we have picked the shortest index but now we have to filter it
|
|
1000
|
+
var pBest = given[iBest];
|
|
1001
|
+
var possibles = this.index[pBest][hash[pBest]];
|
|
1002
|
+
var check = given.slice(0, iBest).concat(given.slice(iBest + 1)); // remove iBest
|
|
1003
|
+
var results = [];
|
|
1004
|
+
var parts = ['subject', 'predicate', 'object', 'why'];
|
|
1005
|
+
for (var j = 0; j < possibles.length; j++) {
|
|
1006
|
+
var st = possibles[j];
|
|
1007
|
+
for (i = 0; i < check.length; i++) {
|
|
1008
|
+
// for each position to be checked
|
|
1009
|
+
p = check[i];
|
|
1010
|
+
if (!this.canon(st[parts[p]]).equals(pattern[p])) {
|
|
1011
|
+
st = null;
|
|
1012
|
+
break;
|
|
1300
1013
|
}
|
|
1301
1014
|
}
|
|
1302
|
-
|
|
1303
|
-
|
|
1015
|
+
if (st != null) {
|
|
1016
|
+
results.push(st);
|
|
1017
|
+
if (justOne) break;
|
|
1018
|
+
}
|
|
1304
1019
|
}
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
value: function serialize(base, contentType, provenance, options) {
|
|
1308
|
-
var _options;
|
|
1020
|
+
return results;
|
|
1021
|
+
}
|
|
1309
1022
|
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1023
|
+
/**
|
|
1024
|
+
* A list of all the URIs by which this thing is known
|
|
1025
|
+
* @param term
|
|
1026
|
+
*/
|
|
1027
|
+
uris(term) {
|
|
1028
|
+
var cterm = this.canon(term);
|
|
1029
|
+
var terms = this.aliases[this.id(cterm)];
|
|
1030
|
+
if (!cterm.value) return [];
|
|
1031
|
+
var res = [cterm.value];
|
|
1032
|
+
if (terms) {
|
|
1033
|
+
for (var i = 0; i < terms.length; i++) {
|
|
1034
|
+
res.push(terms[i].uri);
|
|
1035
|
+
}
|
|
1323
1036
|
}
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1037
|
+
return res;
|
|
1038
|
+
}
|
|
1039
|
+
serialize(base, contentType, provenance, options) {
|
|
1040
|
+
var _options;
|
|
1041
|
+
// override Formula.serialize to force the serializer namespace prefixes
|
|
1042
|
+
// to those of this IndexedFormula
|
|
1043
|
+
|
|
1044
|
+
// if namespaces are explicitly passed in options, let them override the existing namespaces in this formula
|
|
1045
|
+
const namespaces = (_options = options) !== null && _options !== void 0 && _options.namespaces ? {
|
|
1046
|
+
...this.namespaces,
|
|
1047
|
+
...options.namespaces
|
|
1048
|
+
} : {
|
|
1049
|
+
...this.namespaces
|
|
1050
|
+
};
|
|
1051
|
+
options = {
|
|
1052
|
+
...(options || {}),
|
|
1053
|
+
namespaces
|
|
1054
|
+
};
|
|
1055
|
+
return serialize(provenance, this, base, contentType, undefined, options);
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1329
1058
|
_defineProperty(IndexedFormula, "handleRDFType", void 0);
|
|
1330
|
-
|
|
1331
|
-
export { IndexedFormula as default };
|
|
1332
1059
|
IndexedFormula.handleRDFType = handleRDFType;
|