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