rdflib 2.2.21 → 2.2.22-b51259b5

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