rdflib 2.2.10 → 2.2.12-5f141ca2

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