@_linked/rdf-mem-store 0.0.1

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 (137) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +285 -0
  3. package/lib/cjs/Datafactory.d.ts +29 -0
  4. package/lib/cjs/Datafactory.js +101 -0
  5. package/lib/cjs/Datafactory.js.map +1 -0
  6. package/lib/cjs/InMemoryStore.d.ts +35 -0
  7. package/lib/cjs/InMemoryStore.js +98 -0
  8. package/lib/cjs/InMemoryStore.js.map +1 -0
  9. package/lib/cjs/collections/NodeMap.d.ts +35 -0
  10. package/lib/cjs/collections/NodeMap.js +244 -0
  11. package/lib/cjs/collections/NodeMap.js.map +1 -0
  12. package/lib/cjs/collections/NodeSet.d.ts +55 -0
  13. package/lib/cjs/collections/NodeSet.js +296 -0
  14. package/lib/cjs/collections/NodeSet.js.map +1 -0
  15. package/lib/cjs/collections/NodeURIMappings.d.ts +20 -0
  16. package/lib/cjs/collections/NodeURIMappings.js +65 -0
  17. package/lib/cjs/collections/NodeURIMappings.js.map +1 -0
  18. package/lib/cjs/collections/NodeValuesSet.d.ts +63 -0
  19. package/lib/cjs/collections/NodeValuesSet.js +100 -0
  20. package/lib/cjs/collections/NodeValuesSet.js.map +1 -0
  21. package/lib/cjs/collections/QuadArray.d.ts +17 -0
  22. package/lib/cjs/collections/QuadArray.js +67 -0
  23. package/lib/cjs/collections/QuadArray.js.map +1 -0
  24. package/lib/cjs/collections/QuadMap.d.ts +64 -0
  25. package/lib/cjs/collections/QuadMap.js +155 -0
  26. package/lib/cjs/collections/QuadMap.js.map +1 -0
  27. package/lib/cjs/collections/QuadSet.d.ts +22 -0
  28. package/lib/cjs/collections/QuadSet.js +106 -0
  29. package/lib/cjs/collections/QuadSet.js.map +1 -0
  30. package/lib/cjs/collections/SearchMap.d.ts +5 -0
  31. package/lib/cjs/collections/SearchMap.js +13 -0
  32. package/lib/cjs/collections/SearchMap.js.map +1 -0
  33. package/lib/cjs/events/EventBatcher.d.ts +20 -0
  34. package/lib/cjs/events/EventBatcher.js +97 -0
  35. package/lib/cjs/events/EventBatcher.js.map +1 -0
  36. package/lib/cjs/events/EventEmitter.d.ts +15 -0
  37. package/lib/cjs/events/EventEmitter.js +102 -0
  38. package/lib/cjs/events/EventEmitter.js.map +1 -0
  39. package/lib/cjs/index.d.ts +14 -0
  40. package/lib/cjs/index.js +48 -0
  41. package/lib/cjs/index.js.map +1 -0
  42. package/lib/cjs/interfaces/IGraphObject.d.ts +29 -0
  43. package/lib/cjs/interfaces/IGraphObject.js +3 -0
  44. package/lib/cjs/interfaces/IGraphObject.js.map +1 -0
  45. package/lib/cjs/interfaces/IGraphObjectSet.d.ts +24 -0
  46. package/lib/cjs/interfaces/IGraphObjectSet.js +3 -0
  47. package/lib/cjs/interfaces/IGraphObjectSet.js.map +1 -0
  48. package/lib/cjs/interfaces/IShape.d.ts +22 -0
  49. package/lib/cjs/interfaces/IShape.js +3 -0
  50. package/lib/cjs/interfaces/IShape.js.map +1 -0
  51. package/lib/cjs/interfaces/ISingleGraphObject.d.ts +3 -0
  52. package/lib/cjs/interfaces/ISingleGraphObject.js +3 -0
  53. package/lib/cjs/interfaces/ISingleGraphObject.js.map +1 -0
  54. package/lib/cjs/models.d.ts +1167 -0
  55. package/lib/cjs/models.js +2668 -0
  56. package/lib/cjs/models.js.map +1 -0
  57. package/lib/cjs/package.json +3 -0
  58. package/lib/cjs/utils/Debug.d.ts +3 -0
  59. package/lib/cjs/utils/Debug.js +46 -0
  60. package/lib/cjs/utils/Debug.js.map +1 -0
  61. package/lib/cjs/utils/LocalQueryResolver.d.ts +21 -0
  62. package/lib/cjs/utils/LocalQueryResolver.js +1442 -0
  63. package/lib/cjs/utils/LocalQueryResolver.js.map +1 -0
  64. package/lib/cjs/utils/URI.d.ts +18 -0
  65. package/lib/cjs/utils/URI.js +42 -0
  66. package/lib/cjs/utils/URI.js.map +1 -0
  67. package/lib/cjs/utils/toNamedNode.d.ts +8 -0
  68. package/lib/cjs/utils/toNamedNode.js +15 -0
  69. package/lib/cjs/utils/toNamedNode.js.map +1 -0
  70. package/lib/esm/Datafactory.d.ts +29 -0
  71. package/lib/esm/Datafactory.js +97 -0
  72. package/lib/esm/Datafactory.js.map +1 -0
  73. package/lib/esm/InMemoryStore.d.ts +35 -0
  74. package/lib/esm/InMemoryStore.js +94 -0
  75. package/lib/esm/InMemoryStore.js.map +1 -0
  76. package/lib/esm/collections/NodeMap.d.ts +35 -0
  77. package/lib/esm/collections/NodeMap.js +240 -0
  78. package/lib/esm/collections/NodeMap.js.map +1 -0
  79. package/lib/esm/collections/NodeSet.d.ts +55 -0
  80. package/lib/esm/collections/NodeSet.js +292 -0
  81. package/lib/esm/collections/NodeSet.js.map +1 -0
  82. package/lib/esm/collections/NodeURIMappings.d.ts +20 -0
  83. package/lib/esm/collections/NodeURIMappings.js +61 -0
  84. package/lib/esm/collections/NodeURIMappings.js.map +1 -0
  85. package/lib/esm/collections/NodeValuesSet.d.ts +63 -0
  86. package/lib/esm/collections/NodeValuesSet.js +96 -0
  87. package/lib/esm/collections/NodeValuesSet.js.map +1 -0
  88. package/lib/esm/collections/QuadArray.d.ts +17 -0
  89. package/lib/esm/collections/QuadArray.js +63 -0
  90. package/lib/esm/collections/QuadArray.js.map +1 -0
  91. package/lib/esm/collections/QuadMap.d.ts +64 -0
  92. package/lib/esm/collections/QuadMap.js +151 -0
  93. package/lib/esm/collections/QuadMap.js.map +1 -0
  94. package/lib/esm/collections/QuadSet.d.ts +22 -0
  95. package/lib/esm/collections/QuadSet.js +102 -0
  96. package/lib/esm/collections/QuadSet.js.map +1 -0
  97. package/lib/esm/collections/SearchMap.d.ts +5 -0
  98. package/lib/esm/collections/SearchMap.js +9 -0
  99. package/lib/esm/collections/SearchMap.js.map +1 -0
  100. package/lib/esm/events/EventBatcher.d.ts +20 -0
  101. package/lib/esm/events/EventBatcher.js +90 -0
  102. package/lib/esm/events/EventBatcher.js.map +1 -0
  103. package/lib/esm/events/EventEmitter.d.ts +15 -0
  104. package/lib/esm/events/EventEmitter.js +98 -0
  105. package/lib/esm/events/EventEmitter.js.map +1 -0
  106. package/lib/esm/index.d.ts +14 -0
  107. package/lib/esm/index.js +22 -0
  108. package/lib/esm/index.js.map +1 -0
  109. package/lib/esm/interfaces/IGraphObject.d.ts +29 -0
  110. package/lib/esm/interfaces/IGraphObject.js +2 -0
  111. package/lib/esm/interfaces/IGraphObject.js.map +1 -0
  112. package/lib/esm/interfaces/IGraphObjectSet.d.ts +24 -0
  113. package/lib/esm/interfaces/IGraphObjectSet.js +2 -0
  114. package/lib/esm/interfaces/IGraphObjectSet.js.map +1 -0
  115. package/lib/esm/interfaces/IShape.d.ts +22 -0
  116. package/lib/esm/interfaces/IShape.js +2 -0
  117. package/lib/esm/interfaces/IShape.js.map +1 -0
  118. package/lib/esm/interfaces/ISingleGraphObject.d.ts +3 -0
  119. package/lib/esm/interfaces/ISingleGraphObject.js +2 -0
  120. package/lib/esm/interfaces/ISingleGraphObject.js.map +1 -0
  121. package/lib/esm/models.d.ts +1167 -0
  122. package/lib/esm/models.js +2659 -0
  123. package/lib/esm/models.js.map +1 -0
  124. package/lib/esm/package.json +3 -0
  125. package/lib/esm/utils/Debug.d.ts +3 -0
  126. package/lib/esm/utils/Debug.js +42 -0
  127. package/lib/esm/utils/Debug.js.map +1 -0
  128. package/lib/esm/utils/LocalQueryResolver.d.ts +21 -0
  129. package/lib/esm/utils/LocalQueryResolver.js +1434 -0
  130. package/lib/esm/utils/LocalQueryResolver.js.map +1 -0
  131. package/lib/esm/utils/URI.d.ts +18 -0
  132. package/lib/esm/utils/URI.js +38 -0
  133. package/lib/esm/utils/URI.js.map +1 -0
  134. package/lib/esm/utils/toNamedNode.d.ts +8 -0
  135. package/lib/esm/utils/toNamedNode.js +12 -0
  136. package/lib/esm/utils/toNamedNode.js.map +1 -0
  137. package/package.json +57 -0
@@ -0,0 +1,2659 @@
1
+ import { DefaultGraphTermType } from 'rdflib/lib/types.js';
2
+ import { defaultGraphURI } from 'rdflib/lib/utils/default-graph-uri.js';
3
+ import { QuadSet } from './collections/QuadSet.js';
4
+ import { CoreMap } from '@_linked/core/collections/CoreMap';
5
+ import { QuadMap } from './collections/QuadMap.js';
6
+ import { QuadArray } from './collections/QuadArray.js';
7
+ import { NodeSet } from './collections/NodeSet.js';
8
+ import { NodeValuesSet } from './collections/NodeValuesSet.js';
9
+ import { eventBatcher } from './events/EventBatcher.js';
10
+ import { EventEmitter } from './events/EventEmitter.js';
11
+ import { NodeMap } from './collections/NodeMap.js';
12
+ import { NodeURIMappings } from './collections/NodeURIMappings.js';
13
+ import { CoreSet } from '@_linked/core/collections/CoreSet';
14
+ import { Prefix } from '@_linked/core/utils/Prefix';
15
+ export class Node extends EventEmitter {
16
+ constructor(_value) {
17
+ super();
18
+ this._value = _value;
19
+ }
20
+ get value() {
21
+ return this._value;
22
+ }
23
+ set value(val) {
24
+ this._value = val;
25
+ }
26
+ /**
27
+ * Create an instance of the given class (or one of its subclasses) as a presentation of this node.
28
+ * NOTE: this node MUST have the static.type of the given class as its rdf:type property
29
+ * @param type - a class that extends Shape and thus who's instances represent a node as an instance of one specific type.
30
+ */
31
+ getAs(type) {
32
+ return type.getOf(this);
33
+ }
34
+ /**
35
+ * Create an instance of the given class as a presentation of this node.
36
+ * Other than getAs this 'strict' message will ONLY return an exact instance of the given class, not one of its subclasses
37
+ * rdf.type properties of the node are IGNORED. This method can therefore also come in handy in circumstances when you don't have the node it's rdf.type properties at hand.
38
+ * Do not misuse this method though, the main use case is if you don't want to allow any subclass instances. If that's not neccecarily the case and it would make also sense to have the properties loaded, make sure to load them and use getAs.
39
+ * OR use getAsAsync automatically ensures the data of the node is fully loaded before creating an instance.
40
+ * @param type - a class that extends Shape and thus who's instances represent a node as an instance of one specific type.
41
+ */
42
+ getStrictlyAs(type) {
43
+ return type.getStrictlyOf(this);
44
+ }
45
+ /**
46
+ * Compares whether the two nodes are equal
47
+ * @param other The other node
48
+ */
49
+ equals(other) {
50
+ if (!other) {
51
+ return false;
52
+ }
53
+ return this.termType === other.termType && this.value === other.value;
54
+ }
55
+ set(property, value) {
56
+ return false;
57
+ }
58
+ setValue(property, value) {
59
+ return false;
60
+ }
61
+ has(property, value) {
62
+ return false;
63
+ }
64
+ hasValue(property, value) {
65
+ return false;
66
+ }
67
+ hasExplicit(property, value) {
68
+ return false;
69
+ }
70
+ hasExact(property, value) {
71
+ return false;
72
+ }
73
+ hasProperty(property) {
74
+ return false;
75
+ }
76
+ hasInverseProperty(property) {
77
+ return false;
78
+ }
79
+ hasInverse(property, value) {
80
+ return false;
81
+ }
82
+ mset(property, values) {
83
+ return false;
84
+ }
85
+ getProperties(includeFromIncomingArcs = false) {
86
+ return new NodeSet();
87
+ }
88
+ getInverseProperties() {
89
+ return new NodeSet();
90
+ }
91
+ getOne(property) {
92
+ return undefined;
93
+ }
94
+ getAll(property) {
95
+ return new NodeValuesSet(this, property);
96
+ }
97
+ getValue(property) {
98
+ return undefined;
99
+ }
100
+ getDeep(property, maxDepth = -1, partialResult = new NodeSet()) {
101
+ return partialResult;
102
+ }
103
+ getOneInverse(property) {
104
+ return undefined;
105
+ }
106
+ getOneWhere(property, filterProperty, filterValue) {
107
+ return undefined;
108
+ }
109
+ getOneWhereEquivalent(property, filterProperty, filterValue, caseSensitive) {
110
+ return undefined;
111
+ }
112
+ getAllExplicit(property) {
113
+ return undefined;
114
+ }
115
+ getAllInverse(property) {
116
+ return undefined;
117
+ }
118
+ getMultiple(properties) {
119
+ return new NodeSet();
120
+ }
121
+ hasPath(properties) {
122
+ return false;
123
+ }
124
+ hasPathTo(properties, value) {
125
+ return false;
126
+ }
127
+ hasPathToSomeInSet(properties, endPoints) {
128
+ return false;
129
+ }
130
+ getOneFromPath(...properties) {
131
+ return undefined;
132
+ }
133
+ getAllFromPath(...properties) {
134
+ return new NodeSet();
135
+ }
136
+ getQuads(property, value) {
137
+ return new QuadSet();
138
+ }
139
+ getInverseQuad(property, subject) {
140
+ return undefined;
141
+ }
142
+ getInverseQuads(property) {
143
+ return new QuadSet();
144
+ }
145
+ getAllInverseQuads(includeImplicit) {
146
+ return new QuadArray();
147
+ }
148
+ getAllQuads(includeAsObject = false, includeImplicit = false) {
149
+ return null;
150
+ }
151
+ overwrite(property, value) {
152
+ return false;
153
+ }
154
+ moverwrite(property, value) {
155
+ return false;
156
+ }
157
+ unset(property, value) {
158
+ return false;
159
+ }
160
+ unsetAll(property) {
161
+ return false;
162
+ }
163
+ isLoaded(includingIncomingProperties) {
164
+ return false;
165
+ }
166
+ promiseLoaded(loadInverseProperties) {
167
+ return null;
168
+ }
169
+ getMultipleInverse(properties) {
170
+ return new NodeSet();
171
+ }
172
+ /**
173
+ * @internal
174
+ * @param quad
175
+ */
176
+ unregisterInverseProperty(quad, alteration, emitEvents) { }
177
+ /**
178
+ * registers the use of a quad. Since a quad can only be used in 1 quad
179
+ * this method makes a clone of the Literal if it's used a second time,
180
+ * and returns that new Literal so it will be used by the quad
181
+ * @internal
182
+ * @param quad
183
+ */
184
+ registerInverseProperty(quad, alteration, emitEvents) {
185
+ return null;
186
+ }
187
+ clone() {
188
+ return null;
189
+ }
190
+ print() {
191
+ return '';
192
+ }
193
+ }
194
+ /**
195
+ * A Named Node in the graph is a node that has outgoing edges to other nodes.
196
+ *
197
+ * In RDF specifications, a Named Node is a URI Node.
198
+ * You can manage this by setting and getting 'properties' of this node, which will reflect in which nodes this node is connected with.
199
+ * A Named Node is one of the two types of nodes in a graph in the semantic web / RDF.
200
+ * The other one being Literal
201
+ * @see https://www.w3.org/TR/rdf-concepts/#section-Graph-URIref
202
+ *
203
+ * @example
204
+ *
205
+ * Use NamedNode.getOrCreate() if you have a URI
206
+ * Use NamedNode.create() to create a new NamedNode without specifying a URI
207
+ * Do NOT use the constructor
208
+ *
209
+ * ```
210
+ * let node = NamedNode.create();
211
+ * let node = NamedNode.getOrCreate("http://url.of.some/node")
212
+ * ```
213
+ */
214
+ export class NamedNode extends Node {
215
+ /**
216
+ * WARNING: Do not directly create a Node, instead use NamedNode.getOrCreate(uri)
217
+ * This ensures the same node is used for the same uri system wide
218
+ * @param uri - the URI (more generic form of a URL) of the NamedNode
219
+ * @param _isTemporaryNode - set to true if this node is only temporarily available in the local environment
220
+ */
221
+ constructor(uri = '', _isTemporaryNode = false) {
222
+ super(uri);
223
+ this._isTemporaryNode = _isTemporaryNode;
224
+ /**
225
+ * map of QuadMaps indexed by property (where this node occurs as subject)
226
+ * NOTE: 'properties' serves only to increase lookup speed but also costs memory
227
+ * since reverse lookup (where this node occurs as object) will be much less frequent
228
+ * the inverse of 'properties' is not kept, so all results for reverse lookup will be created from 'asObject'
229
+ * @internal
230
+ */
231
+ this.properties = new CoreMap();
232
+ // private static termType: string = 'NamedNode';
233
+ this.termType = 'NamedNode';
234
+ /**
235
+ * map of QuadMaps indexed by property where this node occurs as subject
236
+ * NOTE: we use QuadMap here because in ES5 a quadMap is much faster than a quadSet, because we can check by key with uri directly if the quad exists instead of having to look in an array with indexOf (ES5 does not support objects as keys)
237
+ * @internal
238
+ */
239
+ this.asSubject = new Map();
240
+ if (this._isTemporaryNode) {
241
+ //created locally, so we know everything about it there is to know
242
+ // this.allPropertiesLoaded = {promise: Promise.resolve(this), done: true};
243
+ }
244
+ }
245
+ get isStoring() {
246
+ return this._isStoring && true;
247
+ }
248
+ set isStoring(storing) {
249
+ //when storing
250
+ if (storing) {
251
+ //create a deferred promise and store it as _isStoring
252
+ this._isStoring = {};
253
+ this._isStoring.promise = new Promise((resolve, reject) => {
254
+ this._isStoring.resolve = resolve;
255
+ this._isStoring.reject = reject;
256
+ });
257
+ }
258
+ else {
259
+ //when done storing, resolve the promise
260
+ this._isStoring.resolve();
261
+ delete this._isStoring;
262
+ }
263
+ }
264
+ /**
265
+ * JSLib.js documentation states: "Alias for value, favored by Tim" ... LINCD author René agrees with Tim
266
+ * @see https://github.com/linkeddata/rdflib.js/blob/bbf456390afe7743020e0c8c4db20b10cfb808c7/src/named-node.ts#L88
267
+ */
268
+ get uri() {
269
+ return this._value;
270
+ }
271
+ set uri(uri) {
272
+ this.value = uri;
273
+ }
274
+ /**
275
+ * Alias for uri, so NamedNode satisfies NodeReferenceValue ({id: string}) from core.
276
+ */
277
+ get id() {
278
+ return this._value;
279
+ }
280
+ /**
281
+ * Returns true if this node has a temporary URI and only exists in the local environment.
282
+ * e.g. this is usually true if you create a new NamedNode without having specified a URI yet
283
+ */
284
+ get isTemporaryNode() {
285
+ return this._isTemporaryNode;
286
+ }
287
+ set isTemporaryNode(val) {
288
+ this._isTemporaryNode = val;
289
+ }
290
+ get value() {
291
+ return this._value;
292
+ }
293
+ set value(newUri) {
294
+ if (NamedNode.namedNodes.has(newUri)) {
295
+ throw new Error('Cannot update URI. A node with this URI already exists: ' +
296
+ newUri +
297
+ '. You tried to update the URI of ' +
298
+ this._value);
299
+ }
300
+ var oldUri = this._value;
301
+ NamedNode.namedNodes.delete(this._value);
302
+ this._value = newUri;
303
+ NamedNode.namedNodes.set(this._value, this);
304
+ // //if this node had a temporary URI
305
+ // if (this._isTemporaryNode) {
306
+ // //it now has an explicit URI, so it's no longer temporary
307
+ // this._isTemporaryNode = false;
308
+ // }
309
+ this.emit(NamedNode.URI_UPDATED, this, oldUri, newUri);
310
+ eventBatcher.register(NamedNode);
311
+ NamedNode.nodesURIUpdated.set(this, [oldUri, newUri]);
312
+ }
313
+ /**
314
+ * Emits the batched (property) events of the NamedNode CLASS (meaning events that relate to all nodes)
315
+ * Used internally by the framework to batch and emit change events
316
+ * @internal
317
+ */
318
+ static emitBatchedEvents(resolve, reject) {
319
+ if (this.nodesToRemove.size) {
320
+ this.emitter.emit(NamedNode.REMOVE_NODES, this.nodesToRemove);
321
+ this.nodesToRemove = new CoreSet();
322
+ }
323
+ if (this.nodesToSave.size) {
324
+ this.emitter.emit(NamedNode.STORE_NODES, this.nodesToSave);
325
+ this.nodesToSave = new NodeSet();
326
+ }
327
+ if (this.nodesToLoad.size || this.nodesToLoadFully.size) {
328
+ this.emitter.emit(NamedNode.LOAD_NODES, this.nodesToLoad, this.nodesToLoadFully);
329
+ this.nodesToLoad = new NodeSet();
330
+ this.nodesToLoadFully = new NodeSet();
331
+ }
332
+ if (this.nodesURIUpdated.size) {
333
+ this.emitter.emit(NamedNode.URI_UPDATED, this.nodesURIUpdated);
334
+ this.nodesURIUpdated = new CoreMap();
335
+ }
336
+ if (this.clearedProperties.size) {
337
+ this.emitter.emit(NamedNode.CLEARED_PROPERTIES, this.clearedProperties);
338
+ this.clearedProperties = new CoreMap();
339
+ }
340
+ }
341
+ /**
342
+ * Returns true if this node has any batched events waiting to be emitted
343
+ * Used internally by the framework to batch and emit change events
344
+ * @internal
345
+ */
346
+ static hasBatchedEvents() {
347
+ return (this.nodesToRemove.size > 0 ||
348
+ this.nodesToSave.size > 0 ||
349
+ this.nodesToLoad.size ||
350
+ this.nodesToLoadFully.size > 0 ||
351
+ this.nodesURIUpdated.size > 0 ||
352
+ this.clearedProperties.size > 0);
353
+ }
354
+ /**
355
+ * Converts the string '<http://some.uri>' into a NamedNode
356
+ * @param uriString the string representation of a NamedNode, consisting of its URI surrounded by brackets: '<' URI '>'
357
+ */
358
+ static fromString(uriString) {
359
+ var firstChar = uriString.substr(0, 1);
360
+ if (firstChar == '<') {
361
+ return this.getOrCreate(uriString.substr(1, uriString.length - 2));
362
+ }
363
+ else {
364
+ throw new Error('fromString expects a URI wrapped in brackets, like <http://www.example.com>');
365
+ }
366
+ }
367
+ /**
368
+ * Resets the map of nodes that is known in this local environment
369
+ * Mostly used for test functionality
370
+ */
371
+ static reset() {
372
+ this.tempCounter = 0;
373
+ this.namedNodes = new NodeMap();
374
+ }
375
+ /**
376
+ * Create a new local NamedNode. A temporary URI will be generated for its URI.
377
+ * This node will not exist in the graph database (persistent storage) until you call `node.save()`
378
+ * Until saved, `node.isTemporaryNode()` will return true.
379
+ */
380
+ static create() {
381
+ let tmpURI = this.createNewTempUri();
382
+ while (this.getNamedNode(tmpURI)) {
383
+ this.tempCounter++;
384
+ tmpURI = this.createNewTempUri();
385
+ }
386
+ return this._create(tmpURI, true);
387
+ }
388
+ /**
389
+ * Registers a NamedNode to the locally known list of nodes
390
+ * @internal
391
+ * @param node
392
+ */
393
+ static register(node) {
394
+ if (this.namedNodes.has(node.uri)) {
395
+ throw new Error('A node with this URI already exists: "' +
396
+ node.uri +
397
+ '". You should probably use NamedNode.getOrCreate instead of NamedNode.create (' +
398
+ node.uri +
399
+ ')');
400
+ }
401
+ this.namedNodes.set(node.uri, node);
402
+ }
403
+ /**
404
+ * Unregisters a NamedNode from the locally known list of nodes
405
+ * @internal
406
+ * @param node
407
+ */
408
+ static unregister(node) {
409
+ if (!this.namedNodes.has(node.uri)) {
410
+ throw new Error('This node has already been removed from the registry: ' + node.uri);
411
+ }
412
+ this.namedNodes.delete(node.uri);
413
+ }
414
+ /**
415
+ * Returns a map of all locally known nodes.
416
+ * The map will have URI's as keys and NamedNodes as values
417
+ * @param node
418
+ */
419
+ static getAllNamedNodes() {
420
+ return this.namedNodes;
421
+ }
422
+ /**
423
+ * Returns a map of all locally known nodes.
424
+ * The map will have URI's as keys and NamedNodes as values
425
+ * @param node
426
+ */
427
+ static createNewTempUri() {
428
+ return this.TEMP_URI_BASE + this.tempCounter++; //+'/';+Date.now()+Math.random();
429
+ }
430
+ static getCounter() {
431
+ return this.tempCounter;
432
+ }
433
+ /**
434
+ * ##########################################################################
435
+ * ############# PUBLIC METHODS FOR REGULAR USE #############
436
+ * ##########################################################################
437
+ */
438
+ /**
439
+ * The proper way to obtain a node from a URI.
440
+ * If requested before, this returns the existing NamedNode for the given URI.
441
+ * Or, if this is the first request for this URI, it creates a new NamedNode first, and returns that
442
+ * Using this method over `new NamedNode()` makes sure all nodes are registered, and no duplicates will exist.
443
+ * `new NamedNode()` should therefore never be used.
444
+ * @param uri
445
+ */
446
+ static getOrCreate(uri, isTemporaryNode = false) {
447
+ return this.getNamedNode(uri) || this._create(uri, isTemporaryNode);
448
+ }
449
+ /**
450
+ * Returns the NamedNode with the given URI, IF it exists.
451
+ * DOES NOT create a new NamedNode if it didn't exist yet, instead it returns undefined.
452
+ * You can therefore use this method to see if a NamedNode already exists locally.
453
+ * Use `getOrCreate()` if you want to simply get a NamedNode for a certain URI
454
+ * @param uri
455
+ */
456
+ static getNamedNode(uri) {
457
+ return this.namedNodes.get(uri);
458
+ }
459
+ static emitClearedProperty(node, property) {
460
+ //if not a local node we will emit events for storage controllers to be picked up
461
+ if (!node.isTemporaryNode) {
462
+ //regardless of how many values are known 'locally', we want to emit this event so that the source of data can eventually properly clear all values
463
+ if (!NamedNode.clearedProperties.has(node)) {
464
+ NamedNode.clearedProperties.set(node, []);
465
+ eventBatcher.register(NamedNode);
466
+ }
467
+ //we save the property that was cleared AND the quads that were cleared
468
+ NamedNode.clearedProperties
469
+ .get(node)
470
+ .push([
471
+ property,
472
+ node.asSubject.has(property)
473
+ ? new QuadArray(...node.asSubject.get(property).getQuadSet())
474
+ : null,
475
+ ]);
476
+ }
477
+ }
478
+ static _create(uri, isLocalNode = false) {
479
+ var node = new NamedNode(uri, isLocalNode);
480
+ this.register(node);
481
+ return node;
482
+ }
483
+ /**
484
+ * Used by Quads to signal their subject about a new property
485
+ * @internal
486
+ * @param quad
487
+ * @param alteration
488
+ * @param emitEvents
489
+ */
490
+ registerProperty(quad, alteration = false, emitEvents = true) {
491
+ var predicate = quad.predicate;
492
+ //first make sure we have a QuadMap value for key=predicate
493
+ if (!this.asSubject.has(predicate)) {
494
+ this.asSubject.set(predicate, new QuadMap());
495
+ this.properties.set(predicate, new NodeValuesSet(this, predicate));
496
+ }
497
+ //Add the quad to the QuadMap (see implementation for more details)
498
+ let quadMap = this.asSubject.get(predicate);
499
+ //make sure we have a QuadSet ready for the object of this quad
500
+ quadMap.__set(quad.object, quad);
501
+ //Now for the property index (which gives direct access to the object values of a certain predicate)
502
+ //Because multiple graphs can hold the same subj-pred-obj triple, we want to avoid adding literal values
503
+ //that have the exact same literal value, so we need to test for equality here before adding it
504
+ if (!this.properties
505
+ .get(predicate)
506
+ .some((object) => object.equals(quad.object))) {
507
+ this.properties.get(predicate).__add(quad.object);
508
+ }
509
+ //add this quad to the map of events to send on the next tick
510
+ if (emitEvents) {
511
+ if (!this.changedProperties)
512
+ this.changedProperties = new CoreMap();
513
+ if (alteration && !this.alteredProperties)
514
+ this.alteredProperties = new CoreMap();
515
+ //register this change as alteration (user input) or as normal (automatic, data based) property change
516
+ this.registerPropertyChange(quad, alteration
517
+ ? [this.changedProperties, this.alteredProperties]
518
+ : [this.changedProperties]);
519
+ }
520
+ }
521
+ /**
522
+ * Inverse property can be thought of as "this node is the value (object) of another nodes' property"
523
+ * This method is used by the class Quad to communicate its existence to the quads object
524
+ * @internal
525
+ * @param quad
526
+ * @param alteration
527
+ * @param emitEvents
528
+ */
529
+ registerInverseProperty(quad, alteration = false, emitEvents = true) {
530
+ //asObject is not always initialised - to save some memory on nodes without incoming properties (only used as subject)
531
+ if (!this.asObject) {
532
+ this.asObject = new CoreMap();
533
+ }
534
+ var index = quad.predicate;
535
+ if (!this.asObject.has(index)) {
536
+ this.asObject.set(index, new QuadMap());
537
+ }
538
+ this.asObject.get(index).__set(quad.subject, quad);
539
+ //add this quad to the map of events to send on the next tick
540
+ if (emitEvents) {
541
+ if (!this.changedInverseProperties)
542
+ this.changedInverseProperties = new CoreMap();
543
+ if (alteration && !this.alteredInverseProperties)
544
+ this.alteredInverseProperties = new CoreMap();
545
+ this.registerPropertyChange(quad, alteration
546
+ ? [this.changedInverseProperties, this.alteredInverseProperties]
547
+ : [this.changedInverseProperties]);
548
+ }
549
+ //need to return this, see Literal
550
+ return this;
551
+ }
552
+ /**
553
+ * Called when this node occurs as predicate in a quad
554
+ * @internal
555
+ */
556
+ // registerAsPredicate(
557
+ // quad: Quad,
558
+ // alteration: boolean = false,
559
+ // emitEvents: boolean = true,
560
+ // ) {
561
+ // //asPredicate is not always initialised because only properties can occur as predicate
562
+ // if (!this.asPredicate) {
563
+ // this.asPredicate = new QuadArray();
564
+ // }
565
+ // this.asPredicate.push(quad);
566
+ //
567
+ // if (emitEvents) {
568
+ // this.registerPredicateChange(quad, alteration);
569
+ // }
570
+ // }
571
+ /**
572
+ * This method is used by the class Quad to communicate with its nodes
573
+ * @internal
574
+ * @param quad
575
+ * @param alteration
576
+ */
577
+ registerValueChange(quad, alteration = false) {
578
+ if (!this.changedProperties)
579
+ this.changedProperties = new CoreMap();
580
+ if (alteration) {
581
+ if (!this.alteredProperties)
582
+ this.alteredProperties = new CoreMap();
583
+ }
584
+ this.registerPropertyChange(quad, alteration
585
+ ? [this.changedProperties, this.alteredProperties]
586
+ : [this.changedProperties]);
587
+ }
588
+ /**
589
+ * This method is used by the class Quad to communicate with its nodes
590
+ * @internal
591
+ */
592
+ unregisterProperty(quad, alteration = false, emitEvents = true) {
593
+ var predicate = quad.predicate;
594
+ //start by looking through the QuadMap (it is more complete than the quick & easy properties index, as it accounts for multiple quads per object value in different graphs)
595
+ var quadMap = this.asSubject.get(predicate);
596
+ if (quadMap) {
597
+ let valueQuads = quadMap.get(quad.object);
598
+ if (!valueQuads)
599
+ return;
600
+ valueQuads.delete(quad);
601
+ //if we no longer hold any quads for this object
602
+ if (valueQuads.size == 0) {
603
+ //remove the key
604
+ quadMap.__delete(quad.object);
605
+ //for this.properties we just keep ONE value for identical literals (in case multiple graphs hold the same subject-pred-obj triple)
606
+ //so here we check if any other object that is still registered equals the current object
607
+ if (![...quadMap.keys()].some((object) => quad.object.equals(object))) {
608
+ //if that's not the case, then also remove this object from the propertySet index (the index should exist)
609
+ //Note: in some cases, for example when a quad moved between graphs, the object registered here as property value might not be the same as the as the object of the quad that is still registered,
610
+ // therefor we also check & remove equivalent values in case regular removal didnt work
611
+ //TODO: we could improve this by making sure that this.properties stays up to date with the actual quads
612
+ this.properties.get(predicate).__delete(quad.object) ||
613
+ this.properties
614
+ .get(predicate)
615
+ .__delete(this.properties
616
+ .get(predicate)
617
+ .find((object) => object.equals(quad.object)));
618
+ }
619
+ //if we now also no longer hold any values for this predicate
620
+ //NOTE: this was turned off because NodeValuesSets are reused, recreating a new one when a new value
621
+ // is added then does not add the value to the old one.
622
+ // if (quadMap.size === 0) {
623
+ // //delete both indices for this predicate
624
+ // this.asSubject.delete(predicate);
625
+ // this.properties.delete(predicate);
626
+ // }
627
+ }
628
+ }
629
+ //NOTE: also when removing property values (therefore unregistering the property), we add the removed quad to the SAME list of changed properties
630
+ //event listeners will have to filter out which quad was added or removed
631
+ if (emitEvents) {
632
+ if (!this.changedProperties)
633
+ this.changedProperties = new CoreMap();
634
+ if (alteration && !this.alteredProperties)
635
+ this.alteredProperties = new CoreMap();
636
+ this.registerPropertyChange(quad, alteration
637
+ ? [this.changedProperties, this.alteredProperties]
638
+ : [this.changedProperties]);
639
+ }
640
+ }
641
+ /**
642
+ * This method is used by the class Quad to communicate with its nodes
643
+ * @internal
644
+ */
645
+ // unregisterAsPredicate(
646
+ // quad: Quad,
647
+ // alteration: boolean = false,
648
+ // emitEvents: boolean = true,
649
+ // ) {
650
+ // this.asPredicate.splice(this.asPredicate.indexOf(quad), 1);
651
+ //
652
+ // if (emitEvents) {
653
+ // this.registerPredicateChange(quad, alteration);
654
+ // }
655
+ // }
656
+ //
657
+ // /**
658
+ // * Returns a list of quads in which this node is now used as predicate
659
+ // * BEFORE these changes are sent as events in the normal event flow
660
+ // * Currently used by Reasoner to allow for immediate application of reasoning
661
+ // */
662
+ // getPendingPredicateChanges(): QuadArray {
663
+ // return this.changedAsPredicate;
664
+ // }
665
+ /**
666
+ * This method is used by the class Quad to communicate with its nodes
667
+ * @internal
668
+ */
669
+ unregisterInverseProperty(quad, alteration = false, emitEvents = true) {
670
+ //start by looking through the QuadMap (it is more complete than the quick & easy properties index, as it accounts for identical sub-pred-obj triples that occur in different graphs)
671
+ //here we get a map of all the quads for the given predicate, grouped by subject (each map contains identical triples, but with different graphs)
672
+ var quadMap = this.asObject.get(quad.predicate);
673
+ if (quadMap) {
674
+ let quadSet = quadMap.get(quad.subject);
675
+ if (!quadSet)
676
+ return;
677
+ //remove this quad
678
+ quadSet.delete(quad);
679
+ //if we no longer hold any quads for this subject
680
+ if (quadSet.size === 0) {
681
+ quadMap.__delete(quad.subject);
682
+ }
683
+ if (quadMap.size == 0) {
684
+ this.asObject.delete(quad.predicate);
685
+ }
686
+ }
687
+ //also when removing the property do wee add the removed quad to the list of changed properties
688
+ //event listeners will have to filter out which quad was added or removed
689
+ if (emitEvents) {
690
+ if (!this.changedInverseProperties)
691
+ this.changedInverseProperties = new CoreMap();
692
+ if (alteration && !this.alteredInverseProperties)
693
+ this.alteredInverseProperties = new CoreMap();
694
+ this.registerPropertyChange(quad, alteration
695
+ ? [this.changedInverseProperties, this.alteredInverseProperties]
696
+ : [this.changedInverseProperties]);
697
+ }
698
+ }
699
+ /**
700
+ * Returns a list of quads in which this node is now used as object
701
+ * BEFORE these changes are sent as events in the normal event flow
702
+ * Currently used by Reasoner to allow for immediate application of reasoning
703
+ */
704
+ getPendingInverseChanges(property) {
705
+ return this.changedInverseProperties
706
+ ? this.changedInverseProperties.get(property)
707
+ : new QuadSet();
708
+ }
709
+ /**
710
+ * Returns a list of quads in which this node is now used as subject
711
+ * BEFORE these changes are sent as events in the normal event flow
712
+ * Currently used by Reasoner to allow for immediate application of reasoning
713
+ */
714
+ getPendingChanges(property) {
715
+ return this.changedProperties.get(property);
716
+ }
717
+ /**
718
+ * Set the a single property value
719
+ * Creates a single connection between two nodes in the graph: from this node, to the node given as value, with the property as the connecting 'edge' between them
720
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
721
+ * @param value - the node that this new graph-edge points to. The object of the quad to be created.
722
+ */
723
+ set(property, value) {
724
+ if (!value) {
725
+ throw Error('No value provided to set!');
726
+ }
727
+ //if there is already a quad with exactly this prop-value pair
728
+ if (this.has(property, value)) {
729
+ //make all quads with this pair explicit if they were not yet
730
+ this.getQuads(property, value).makeExplicit();
731
+ //yet return false because nothing was changes in the propreties
732
+ return false;
733
+ }
734
+ //if this pair didn't exist yet, create a new quad (the graph is undefined for now, Storage will pick this up and place it in the right graph)
735
+ //note that the sixth parameter is true, this indicates that this is an alteration (as in new data that triggers change events instead of a quad created for already existing data)
736
+ new Quad(this, property, value, undefined, false, true);
737
+ return true;
738
+ }
739
+ /**
740
+ * Same as set() except this method allows you to pass a string as value and converts it to a Literal for you
741
+ * @param property
742
+ * @param value
743
+ */
744
+ setValue(property, value) {
745
+ return this.set(property, new Literal(value));
746
+ }
747
+ /**
748
+ * Set multiple values at once for a single property.
749
+ * You can use this for example to state that this node (a person) has a 'hasFriend' connection to multiple people (friends) in 1 statement
750
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
751
+ * @param values - an array or set of nodes. Can be NamedNodes or Literals
752
+ */
753
+ mset(property, values) {
754
+ //if(save) dacore.system.storageQueueStart(this);
755
+ var res = false;
756
+ for (var value of values) {
757
+ res = this.set(property, value) || res;
758
+ }
759
+ return res;
760
+ }
761
+ /**
762
+ * Returns true if this node has the given value as the value of the given property
763
+ * NOTE: returns true when a literal node is provided that is EQUIVALENT to any of the values that this node has for this property (whilst not neccecarilly being the exact same object in memory)
764
+ * See also: Literal.equals
765
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
766
+ * @param value - a single node. Can be a NamedNode or Literal
767
+ */
768
+ has(property, value) {
769
+ if (!value) {
770
+ throw new Error('No value provided to NamedNode.has(). Did you mean `hasProperty`?');
771
+ }
772
+ var properties = this.properties.get(property);
773
+ return (properties &&
774
+ (properties.has(value) ||
775
+ properties.some((object) => object.equals(value))));
776
+ }
777
+ /**
778
+ * Returns true if this node has the given value for the given property in an EXPLICIT quad.
779
+ * That is, this property-value has been explicitly set, and is NOT generated by the Reasoner.
780
+ * See the documentation for more information about implicit vs explicit
781
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
782
+ * @param value - a single node. Can be a NamedNode or Literal
783
+ */
784
+ hasExplicit(property, value) {
785
+ if (!this.asSubject.has(property))
786
+ return false;
787
+ return this.getQuadsByValue(property, value).some((quad) => !quad.implicit);
788
+ }
789
+ /**
790
+ * Returns true if this node has ANY explicit quad with the given property
791
+ * See the documentation for more information about implicit vs explicit
792
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
793
+ */
794
+ hasExplicitProperty(property) {
795
+ if (!this.asSubject.has(property))
796
+ return false;
797
+ return this.getQuads(property).some((quad) => !quad.implicit);
798
+ }
799
+ /**
800
+ * Returns true if this node has a Literal as value of the given property who's literal-value (a string) matches the given value
801
+ * So works the same as `has()` except you can provide a string as value, and will obviously not match any NamedNode values
802
+ * And unlike has() this method will NOT check for the Literal its datatype. Instead only checking the literal-value
803
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
804
+ * @param value - the string value we want to check for
805
+ */
806
+ hasValue(property, value) {
807
+ var properties = this.properties.get(property);
808
+ return (properties &&
809
+ properties.some((object) => 'value' in object && object['value'] === value));
810
+ }
811
+ /**
812
+ * Returns true if this node has the given value as the value of the given property with an EXACT match (meaning the same object in memory)
813
+ * So works the same as has() except for Literals this only returns true if the value of the property is exactly the same object as the given value
814
+ * UNLIKE `has()` which checks if the literal value, datatype and language tag of two literal nodes are equivalent
815
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
816
+ * @param value - a single node. Can be a NamedNode or Literal
817
+ */
818
+ hasExact(property, value) {
819
+ var properties = this.properties.get(property);
820
+ return properties && properties.has(value);
821
+ }
822
+ /**
823
+ * Returns true if this node has ANY value set for the given property.
824
+ * That is, if any quad exists that has this node as the subject and the given property as predicate
825
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
826
+ */
827
+ hasProperty(property) {
828
+ //properties can be empty sets, so we need to check if there are any values in the set
829
+ return (this.properties.has(property) && this.properties.get(property).size > 0);
830
+ }
831
+ /**
832
+ * Returns true if the given end point can be reached by following the given properties in order
833
+ * Example: hasPathTo([foaf.hasFriend,rdf.type],foaf.Person) will return true if any of the friends of this node (this person in this example) is of the type foaf:Person
834
+ * @param properties an array of NamedNodes
835
+ * @param endPoint the node to reach, a Literal or a NamedNode
836
+ */
837
+ hasPathTo(properties, endPoint) {
838
+ //we just need to find one matching path, so we do a depth-first algorithm which will be more performant, so:
839
+ //take first property
840
+ var property = properties.shift();
841
+ //if more properties left
842
+ if (properties.length > 0) {
843
+ var res;
844
+ //check if any of the values of that property for this node
845
+ //has a path to the rest of the properties, and if so return the found value
846
+ for (var value of this.getAll(property)) {
847
+ if ((res = value.hasPathTo([...properties], endPoint))) {
848
+ return res;
849
+ }
850
+ }
851
+ return false;
852
+ }
853
+ else {
854
+ //if last property
855
+ //see if we can reach the value if a value was given
856
+ //else: see if any value (any path) exists
857
+ if (endPoint) {
858
+ return this.has(property, endPoint);
859
+ }
860
+ else {
861
+ return this.hasProperty(property);
862
+ }
863
+ }
864
+ }
865
+ getAs(type) {
866
+ return type.getOf(this);
867
+ }
868
+ /**
869
+ * returns true if ANY of the given end points can be reached by following the given properties in the given order
870
+ * Example: hasPathTo([foaf.hasFriend,foaf.hasFriend],[mike,jenny]) will return true if this node (person) has a friend that has mike or jenny as a friend
871
+ * @param properties an array of NamedNodes
872
+ * @param endPoint the node to reach, a Literal or a NamedNode
873
+ */
874
+ hasPathToSomeInSet(properties, endPoints) {
875
+ //we just need to find one matching path, so we do a depth-first algorithm which will be more performant, so:
876
+ //take first property
877
+ var property = properties.shift();
878
+ //if more properties left
879
+ if (properties.length > 0) {
880
+ var res;
881
+ //check if any of the values of that property for this node
882
+ //has a path to the rest of the properties, and if so return the found value
883
+ for (var value of this.getAll(property)) {
884
+ if ((res = value.hasPathToSomeInSet([...properties], endPoints))) {
885
+ return res;
886
+ }
887
+ }
888
+ return false;
889
+ }
890
+ else {
891
+ //if last property
892
+ //see if we can reach the value if a value was given
893
+ //else: see if any value (any path) exists
894
+ return endPoints.some((endPoint) => this.has(property, endPoint));
895
+ }
896
+ }
897
+ /**
898
+ * returns true if ANY end point (node) can be reached by following the given properties in order
899
+ * @param properties an array of NamedNodes
900
+ */
901
+ hasPath(properties) {
902
+ //we just need to find one matching path, so we do a depth-first algorithm which will be more performant, so:
903
+ //take first property
904
+ var property = properties.shift();
905
+ //if more properties left
906
+ if (properties.length > 0) {
907
+ var res;
908
+ //check if any of the values of that property for this node
909
+ //has a path to the rest of the properties, and if so return the found value
910
+ for (var value of this.getAll(property)) {
911
+ if ((res = value.hasPath([...properties]))) {
912
+ return res;
913
+ }
914
+ }
915
+ return false;
916
+ }
917
+ else {
918
+ //if last property
919
+ //see if we can reach the value if a value was given
920
+ //else: see if any value (any path) exists
921
+ return this.hasProperty(property);
922
+ }
923
+ }
924
+ /**
925
+ * Returns a set of all the properties this node has.
926
+ * That is, all unique predicates of quads where this node is the subject
927
+ * @param includeFromIncomingArcs if true, also includes predicates (properties) of quads where this node is the VALUE of another nodes' property. Default: false
928
+ */
929
+ getProperties(includeFromIncomingArcs = false) {
930
+ if (includeFromIncomingArcs) {
931
+ return new NodeSet((this.asObject
932
+ ? [...this.asSubject.keys(), ...this.asObject.keys()]
933
+ : this.asSubject.keys()));
934
+ }
935
+ else {
936
+ return new NodeSet(this.asSubject.keys());
937
+ }
938
+ }
939
+ /**
940
+ * Returns a set of all the properties used by this node in EXPLICIT facts (quads)
941
+ * See the documentation for more information about implicit vs explicit facts
942
+ * @param includeFromIncomingArcs if true, also includes predicates (properties) of quads where this node is the VALUE of another nodes' property. Default: false
943
+ */
944
+ getExplicitProperties(includeFromIncomingArcs = false) {
945
+ return new NodeSet([
946
+ ...this.getAllQuads(includeFromIncomingArcs)
947
+ .filter((t) => !t.implicit)
948
+ .map((t) => t.predicate),
949
+ ]);
950
+ }
951
+ /**
952
+ * Returns a set of all the properties used by other nodes where this node is the VALUE of that property
953
+ * For example if this node is Jenny and the following is true: Mike foaf:hasFriend Jenny, calling this method on Jenny will return hasFriend
954
+ */
955
+ getInverseProperties() {
956
+ return this.asObject
957
+ ? new NodeSet(this.asObject.keys())
958
+ : new NodeSet();
959
+ }
960
+ /**
961
+ * If this node has values for the given property, the first value is returned
962
+ * NOTE: the order of multiple values CANNOT be guaranteed. Therefore use this value if it DOESN'T matter to you which of multiple possible values for this property you'll get OR if you're certain there will be only 1 value.
963
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
964
+ */
965
+ getOne(property) {
966
+ return this.properties.has(property)
967
+ ? this.properties.get(property).first()
968
+ : undefined;
969
+ }
970
+ /**
971
+ * If this node has EXPLICIT values for the given property, the first value is returned
972
+ * Same as `getOne()` except only explicit quads / facts are considered
973
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
974
+ */
975
+ getOneExplicit(property) {
976
+ for (let quad of this.getQuads(property)) {
977
+ if (!quad.implicit) {
978
+ return quad.object;
979
+ }
980
+ }
981
+ }
982
+ /**
983
+ * Returns all values this node has for the given property
984
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
985
+ */
986
+ getAll(property) {
987
+ //we usually just index all the existing properties
988
+ //but to have consistent behaviour with PropertyValueSets, when you request a property that has no values
989
+ //we need to create an index for the empty result set
990
+ if (!this.properties.has(property)) {
991
+ this.asSubject.set(property, new QuadMap());
992
+ this.properties.set(property, new NodeValuesSet(this, property));
993
+ }
994
+ return this.properties.get(property);
995
+ // return this.properties.has(property)
996
+ // ? this.properties.get(property)
997
+ // : new PropertyValueSet(this,property);
998
+ }
999
+ /**
1000
+ * Returns all values this node EXPLICITLY has for the given property
1001
+ * So, same as `getAll()` except only explicit facts are considered.
1002
+ * See the documentation for more information about implicit vs explicit
1003
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1004
+ */
1005
+ getAllExplicit(property) {
1006
+ return this.getExplicitQuads(property).getObjects();
1007
+ }
1008
+ /**
1009
+ * Returns the literal value of the first Literal value for the given property
1010
+ * Only returns a results in the disired language if specified.
1011
+ * For example if `this rdfs:label "my name" then this.getValue(rdfs.label) will return "my name".
1012
+ * So, works the same as getOne() except it will return the literal (string) value of the first found Literal
1013
+ * NOTE: the order of multiple values CANNOT be guaranteed. Therefore use this value if it DOESN'T matter to you which of multiple possible values for this property you'll get OR if you're certain there will be only one value.
1014
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1015
+ */
1016
+ //NOTE: we have to overload getValue without parameters here to be compatible with Literal
1017
+ getValue(property, language) {
1018
+ //going over all property values
1019
+ for (var valueObject of this.getAll(property)) {
1020
+ //see if its a Literal
1021
+ //we do this by checking if value exists in the valueObject.
1022
+ //And we do that like this because we dont want to explicitly import Literal here.
1023
+ //@TODO: Possibly create an interface to avoid this 'hacky' workaround
1024
+ if (valueObject['value'] &&
1025
+ (!language || valueObject['isOfLanguage'](language))) {
1026
+ return valueObject.value;
1027
+ }
1028
+ }
1029
+ }
1030
+ /**
1031
+ * Returns the literal values (strings) of all Literals this this node has a value for the given property
1032
+ * For example if `this rdfs:label "my name" and `this rdfs:label "my other name" it will return ["my name","my other name"].
1033
+ * So, works the same as getAll() except it will return an array of strings
1034
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1035
+ */
1036
+ getValues(property) {
1037
+ var valueObjects = this.getAll(property);
1038
+ var res = [];
1039
+ valueObjects.forEach((valueObject) => {
1040
+ if ('value' in valueObject) {
1041
+ res.push(valueObject['value']);
1042
+ }
1043
+ });
1044
+ return res;
1045
+ }
1046
+ /**
1047
+ * Returns any value (node, node) that is connected to this node with one or more connections of the given property.
1048
+ * For example getDeep(hasFriend) will return all the people that are my friends or friends of friends
1049
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1050
+ * @param maxDepth - the maximum number of connections that resulting nodes are removed from this node. In the example above maxDepth=2 would return only friends and friends of friends
1051
+ */
1052
+ getDeep(property, maxDepth = Infinity) {
1053
+ var result = new NodeSet();
1054
+ var stack = new NodeSet([this]);
1055
+ while (stack.size > 0 && maxDepth > 0) {
1056
+ var nextLevelStack = new NodeSet();
1057
+ for (let node of stack) {
1058
+ for (var value of node.getAll(property)) {
1059
+ if (!result.has(value)) {
1060
+ result.add(value);
1061
+ nextLevelStack.add(value);
1062
+ }
1063
+ }
1064
+ }
1065
+ stack = nextLevelStack;
1066
+ maxDepth--;
1067
+ }
1068
+ return result;
1069
+ }
1070
+ /**
1071
+ * Returns the first found value following the given properties in the given order.
1072
+ * For example: getOneFromPath([hasFriend,hasFather]) would return the first found father out of the set 'fathers of my friends'
1073
+ * @param properties - an array of NamedNodes. Which are nodes with rdf:type rdf:Property, the edges in the graph, the predicates of quads.
1074
+ */
1075
+ getOneFromPath(...properties) {
1076
+ //we just need one, so we do a depth-first algorithm which will be more performant, so:
1077
+ //take first property
1078
+ var property = properties.shift();
1079
+ //if more properties left
1080
+ if (properties.length > 0) {
1081
+ var res;
1082
+ //check if any of the values of that property for this node
1083
+ //has a path to the rest of the properties, and if so return the found value
1084
+ for (var value of this.getAll(property)) {
1085
+ if ((res = value.getOneFromPath(...properties))) {
1086
+ return res;
1087
+ }
1088
+ }
1089
+ }
1090
+ else {
1091
+ //return the first value possible
1092
+ return this.getOne(property);
1093
+ }
1094
+ }
1095
+ /**
1096
+ * Returns all values that can be reached by following the given properties in order.
1097
+ * For example getAllFromPath([hasFriend,hasFather]) will return all fathers of all my (direct) friends
1098
+ * @param properties - an array of NamedNodes. Which are nodes with rdf:type rdf:Property, the edges in the graph, the predicates of quads.
1099
+ */
1100
+ getAllFromPath(...properties) {
1101
+ //we just need all paths, so we can do a breadth first implementation
1102
+ //take first property
1103
+ var property = properties.shift();
1104
+ if (properties.length > 0) {
1105
+ //and ask the whole set of values to return all values of the rest of the path
1106
+ return this.getAll(property).getAllFromPath(...properties);
1107
+ }
1108
+ else {
1109
+ return this.getAll(property);
1110
+ }
1111
+ }
1112
+ /**
1113
+ * Same as getDeep() but for inverse properties.
1114
+ * Best understood with an example: if this is a person. this.getInverseDeep(hasChild) would return all this persons ancestors (which had children that eventually had this person as their child)
1115
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1116
+ * @param maxDepth - the maximum number of connections that resulting nodes are removed from this node. In the example above maxDepth=2 would return only the parents and grand parents
1117
+ */
1118
+ getInverseDeep(property, maxDepth = Infinity) {
1119
+ var result = new NodeSet();
1120
+ var stack = new NodeSet([this]);
1121
+ while (stack.size > 0 && maxDepth > 0) {
1122
+ var nextLevelStack = new NodeSet();
1123
+ for (let node of stack) {
1124
+ for (var value of node.getAllInverse(property)) {
1125
+ if (!result.has(value)) {
1126
+ result.add(value);
1127
+ nextLevelStack.add(value);
1128
+ }
1129
+ }
1130
+ }
1131
+ stack = nextLevelStack;
1132
+ maxDepth--;
1133
+ }
1134
+ return result;
1135
+ }
1136
+ /**
1137
+ * Returns true if the given value can be reached with one or more connections of the given property
1138
+ * Example: if this is a person. this.hasDeep(hasFriend,Mike) returns true if this person has Mike as a friend, or if any this persons friends or friends of friends have Mike as a friend.
1139
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1140
+ * @param maxDepth - the maximum number of connections that resulting nodes are removed from this node. In the example above maxDepth=2 would return true only if Mike is the persons friend, or friend of a friend
1141
+ */
1142
+ hasDeep(property, value, maxDepth = Infinity) {
1143
+ var checked = new NodeSet();
1144
+ var stack = new NodeSet([this]);
1145
+ while (stack.size > 0 && maxDepth > 0) {
1146
+ var nextLevelStack = new NodeSet();
1147
+ for (let node of stack) {
1148
+ for (var propertyValue of node.getAll(property)) {
1149
+ if (propertyValue === value) {
1150
+ return true;
1151
+ }
1152
+ if (!checked.has(propertyValue)) {
1153
+ checked.add(propertyValue);
1154
+ nextLevelStack.add(propertyValue);
1155
+ }
1156
+ }
1157
+ }
1158
+ stack = nextLevelStack;
1159
+ maxDepth--;
1160
+ }
1161
+ return false;
1162
+ }
1163
+ /**
1164
+ * Returns the first node that has this node as the valu eof the given property.
1165
+ * Same as getOne() but for 'inverse properties'. Meaning nodes that have this node as their value.
1166
+ * Example: if this is a person. this.getOneInverse(hasChild) returns one of the persons parents
1167
+ * NOTE: the order of multiple (inverse) values CANNOT be guaranteed. Therefore use this value if it DOESN'T matter to you which of multiple possible inverse values for this property you'll get OR if you're certain there will be only one value.
1168
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1169
+ */
1170
+ getOneInverse(property) {
1171
+ if (!this.asObject)
1172
+ return undefined;
1173
+ var quads = this.asObject.get(property);
1174
+ return quads ? quads.keys().next().value : undefined;
1175
+ }
1176
+ /**
1177
+ * Returns all the nodes that have this node as the value of the given property.
1178
+ * Same as getAll() but for 'inverse properties'. Meaning nodes that have this node as their value.
1179
+ * Example: if this is a person. this.getAllInverse(hasChild) returns all the persons parents
1180
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1181
+ */
1182
+ getAllInverse(property) {
1183
+ if (!this.asObject || !this.asObject.has(property))
1184
+ return new NodeSet();
1185
+ return this.asObject.get(property).getSubjects();
1186
+ }
1187
+ /**
1188
+ * Returns all the nodes that have this node as the EXPLICIT value of the given property.
1189
+ * Same as getAll() but only considers explicit facts (excluding implicit facts generated by the reasoner)
1190
+ * Example: if this is a person. this.getAllInverse(hasChild) returns all the persons parents, as long as the fact that these are this persons parents is explicitly stated
1191
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1192
+ */
1193
+ getAllInverseExplicit(property) {
1194
+ return this.getExplicitInverseQuads(property).getSubjects();
1195
+ }
1196
+ /**
1197
+ * Get all the values of multiple properties at once
1198
+ * Same as getAll() but for multiple properties at once
1199
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1200
+ */
1201
+ getMultiple(properties) {
1202
+ var res = new NodeSet();
1203
+ for (var property of properties) {
1204
+ res = res.concat(this.getAll(property));
1205
+ }
1206
+ return res;
1207
+ }
1208
+ /**
1209
+ * Get all the nodes that have this node as their value for any of the given properties
1210
+ * Same as getMultiple() but for the opposite direction
1211
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1212
+ */
1213
+ getMultipleInverse(properties) {
1214
+ var res = new NodeSet();
1215
+ for (var property of properties) {
1216
+ res = res.concat(this.getAllInverse(property));
1217
+ }
1218
+ return res;
1219
+ }
1220
+ /**
1221
+ * Get the quad that represent the connection from this node to the given value, connected by the given property
1222
+ * NOTE: accessing quads is a very low level functionality required for the framework itself
1223
+ * and SHOULD GENERALLY NOT BE USED. Use methods to get/set properties instead
1224
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1225
+ * @param value - the node that this new graph-edge points to. The object of the quad to be created.
1226
+ */
1227
+ getQuads(property, value) {
1228
+ if (!this.asSubject.has(property))
1229
+ return new QuadSet();
1230
+ if (value) {
1231
+ return this.getQuadsByValue(property, value);
1232
+ }
1233
+ else {
1234
+ return this.asSubject.get(property).getQuadSet();
1235
+ }
1236
+ }
1237
+ /**
1238
+ * Get all the quads that represent EXPLICIT connections from this node to another node, connected by the given property
1239
+ * NOTE: accessing quads is a very low level functionality required for the framework itself
1240
+ * and SHOULD GENERALLY NOT BE USED. Use methods to get/set properties instead
1241
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1242
+ */
1243
+ getExplicitQuads(property) {
1244
+ return this.getQuads(property).filter((quad) => !quad.implicit);
1245
+ }
1246
+ /**
1247
+ * Get all the quads that represent EXPLICIT connections from another node that has this node as its value for the given property
1248
+ * NOTE: accessing quads is a very low level functionality required for the framework itself
1249
+ * and SHOULD GENERALLY NOT BE USED. Use methods to get/set properties instead
1250
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1251
+ */
1252
+ getExplicitInverseQuads(property) {
1253
+ return this.getInverseQuads(property).filter((quad) => !quad.implicit);
1254
+ }
1255
+ /**
1256
+ * Get all quads that represent connections from another node that has this node as its value for the given property
1257
+ * NOTE: accessing quads is a very low level functionality required for the framework itself
1258
+ * and SHOULD GENERALLY NOT BE USED. Use methods to get/set properties instead
1259
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1260
+ */
1261
+ getInverseQuads(property) {
1262
+ if (!this.asObject.has(property))
1263
+ return undefined;
1264
+ return this.asObject.get(property).getQuadSet();
1265
+ // return this.asObject && this.asObject.has(property)
1266
+ // ? this.asObject.get(property)
1267
+ // : new QuadMap();
1268
+ }
1269
+ /**
1270
+ * Get all (by default explicit) quads that represent connections from another node that has this node as its value for ANY property
1271
+ * NOTE: accessing quads is a very low level functionality required for the framework itself
1272
+ * and SHOULD GENERALLY NOT BE USED. Use methods to get/set properties instead
1273
+ * @param includeImplicit if true, includes both implicit and explicit quads. By default false, so will only return explicit quads
1274
+ */
1275
+ getAllInverseQuads(includeImplicit = false) {
1276
+ var res = new QuadArray();
1277
+ if (this.asObject) {
1278
+ this.asObject.forEach((quadSet) => {
1279
+ quadSet.forEach((quad) => {
1280
+ if (!includeImplicit && quad.implicit)
1281
+ return;
1282
+ res.push(quad);
1283
+ });
1284
+ });
1285
+ }
1286
+ return res;
1287
+ }
1288
+ /**
1289
+ * Get all (by default explicit) quads that represent connections for all values of this node (so quads where this node is the subject)
1290
+ * NOTE: accessing quads is a very low level functionality required for the framework itself
1291
+ * and SHOULD GENERALLY NOT BE USED. Use methods to get/set properties instead
1292
+ * @param includeAsObject if true, includes quads from both directions (so also inverse properties where this node is the object of the quad)
1293
+ * @param includeImplicit if true, includes both implicit and explicit quads. By default false, so will only return explicit quads
1294
+ */
1295
+ getAllQuads(includeAsObject = false, includeImplicit = false) {
1296
+ var res = new QuadArray();
1297
+ this.asSubject.forEach((quadMap) => {
1298
+ quadMap.forEach((quad) => {
1299
+ if (!includeImplicit && quad.implicit)
1300
+ return;
1301
+ res.push(quad);
1302
+ });
1303
+ });
1304
+ if (includeAsObject && this.asObject) {
1305
+ this.asObject.forEach((quadMap) => {
1306
+ quadMap.forEach((quad) => {
1307
+ //we manually filter duplicates from the result set here so that we can keep using QuadArray, which is much faster in ES5
1308
+ //and the only duplicates will be a node with itself as subject AND object, so we filter the second occurrence here
1309
+ if (!includeImplicit && quad.implicit)
1310
+ return;
1311
+ if (quad.subject === this)
1312
+ return;
1313
+ res.push(quad);
1314
+ });
1315
+ });
1316
+ }
1317
+ return res;
1318
+ }
1319
+ /**
1320
+ * #######################################################################
1321
+ * ######################## EVENT METHODS / LISTENERS ####################
1322
+ * #######################################################################
1323
+ **/
1324
+ /**
1325
+ * Update a certain property so that only the given value is a value of this property.
1326
+ * Overwrites (and thus removes) any previously set values
1327
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1328
+ * @param value - a single node. Can be a NamedNode or Literal
1329
+ */
1330
+ overwrite(property, value) {
1331
+ //don't do anything if the current value is already equivalent to the new value
1332
+ if (this.getAll(property).size == 1 && value && this.has(property, value))
1333
+ return false;
1334
+ //clear all values and set new value
1335
+ this.unsetAll(property);
1336
+ if (value) {
1337
+ return this.set(property, value);
1338
+ }
1339
+ }
1340
+ /**
1341
+ * Update a certain property so that only the given values are the values of this property.
1342
+ * Overwrites (and thus removes) any previously set values
1343
+ * Same as overwrite() except this allows you to replace the previous values with MULTIPLE new values
1344
+ * @param property
1345
+ * @param values
1346
+ */
1347
+ moverwrite(property, values) {
1348
+ //don't update if the new set of values is the same (or equivalent) as the old set of values
1349
+ if (this.getAll(property).size === (values.size || values.length) &&
1350
+ values.every((value) => this.has(property, value))) {
1351
+ return false;
1352
+ }
1353
+ this.unsetAll(property);
1354
+ return this.mset(property, values);
1355
+ }
1356
+ /**
1357
+ * Removes this node from the graph, locally and remotely, in all connected QuadStores.
1358
+ * All properties will be unset, both where this node is the subject or the object.
1359
+ * Emits an event from the node itself and from the NamedNode class
1360
+ */
1361
+ remove() {
1362
+ //collect all the quads that are about to be removed
1363
+ let removedQuads = this.getAllQuads(true);
1364
+ //remove all quads locally
1365
+ this.asSubject.forEach((quads) => quads.removeAll(true));
1366
+ if (this.asObject)
1367
+ this.asObject.forEach((quads) => quads.removeAll(true));
1368
+ //emit event from this node itself, with all the quads that were removed
1369
+ this.emit(NamedNode.NODE_REMOVED, this, removedQuads);
1370
+ //clean up anything connected to this node
1371
+ this.removeAllListeners();
1372
+ //remove form list
1373
+ NamedNode.unregister(this);
1374
+ //make sure a global event is emitted that nodes are moved (picked up by storage)
1375
+ eventBatcher.register(NamedNode);
1376
+ NamedNode.nodesToRemove.add([this, removedQuads]);
1377
+ }
1378
+ /**
1379
+ * UNSET (remove) a single property value connection.
1380
+ * Remove a single connection between two nodes in the graph: from this node, to the node given as value, with the property as the connecting 'edge' between them
1381
+ * In the graph, this will remove the edge between two nodes.
1382
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1383
+ * @param value - the node that this new graph-edge points to. The object of the quad to be created.
1384
+ */
1385
+ unset(property, value) {
1386
+ if (this.has(property, value)) {
1387
+ this.getQuads(property, value).removeAll(true);
1388
+ return true;
1389
+ }
1390
+ return false;
1391
+ }
1392
+ /**
1393
+ * unset (remove) all values of a certain property.
1394
+ * Removes all connections (edges) in the graph between this node and other nodes, where the given property is used as the connecting 'edge' between them
1395
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1396
+ */
1397
+ unsetAll(property) {
1398
+ NamedNode.emitClearedProperty(this, property);
1399
+ if (this.hasProperty(property)) {
1400
+ //false as parameter because we don't need alteration events for each single quad, but rather manage this with clearedProperties events
1401
+ this.asSubject.get(property).removeAll(false);
1402
+ return true;
1403
+ }
1404
+ return false;
1405
+ }
1406
+ /**
1407
+ * returns true if ANY node has this node as the value of the given property
1408
+ * Example: if 'this' is a person, this.hasInverseProperty(hasChild) returns true if any facts stating `someParent hasChild thisPerson` are known
1409
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1410
+ */
1411
+ hasInverseProperty(property) {
1412
+ return this.asObject && this.asObject.has(property);
1413
+ }
1414
+ /**
1415
+ * returns true if the given inverse 'value' has this node as the (real) value of the given property
1416
+ * Example: if 'this' is a person, this.hasInverseProperty(hasChild,someParent) returns true if someParent indeed has this person as a child
1417
+ * @param property - a NamedNode with rdf:type rdf:Property, the edge in the graph, the predicate of a quad
1418
+ * @param value - a single node. Can be a NamedNode or Literal
1419
+ */
1420
+ hasInverse(property, value) {
1421
+ var _a;
1422
+ return (this.asObject &&
1423
+ ((_a = this.asObject.get(property)) === null || _a === void 0 ? void 0 : _a.some((quad) => quad.subject.equals(value))));
1424
+ }
1425
+ /**
1426
+ * returns true if this node is equivaluent to the given node.
1427
+ * For NamedNodes it simply returns true if this === the given object. So if its the same object in memory.
1428
+ * It exists mainly for comparing Literals, where two different objects can still be equivalent
1429
+ * @param other another node
1430
+ */
1431
+ equals(other) {
1432
+ return other === this;
1433
+ }
1434
+ /**
1435
+ * Save this node into the graph database.
1436
+ * Newly created nodes will exist only in local memory until you call this function
1437
+ * @returns a promise that resolves when the node has received a permanent URI
1438
+ */
1439
+ save() {
1440
+ if (this.isTemporaryNode) {
1441
+ if (!this._isStoring) {
1442
+ //this creates a promise that will resolve when the node is stored
1443
+ this.isStoring = true;
1444
+ NamedNode.nodesToSave.add(this);
1445
+ eventBatcher.register(NamedNode);
1446
+ }
1447
+ //always return a promise
1448
+ return this._isStoring.promise;
1449
+ }
1450
+ }
1451
+ /**
1452
+ * Create a new URI Node with the same properties as the current node
1453
+ * NOTE: does NOT clone the inverse properties (where this node is the value of another node its properties)
1454
+ */
1455
+ clone() {
1456
+ var node = NamedNode.create();
1457
+ this.getAllQuads().forEach((quad) => {
1458
+ node.set(quad.predicate, quad.object);
1459
+ });
1460
+ return node;
1461
+ }
1462
+ /**
1463
+ * Returns a string representation of this node.
1464
+ * Returns the URI for a NamedNode
1465
+ */
1466
+ toString() {
1467
+ return this.uri;
1468
+ }
1469
+ print(includeIncomingProperties = true) {
1470
+ var print = '';
1471
+ this.getAsSubjectQuads().forEach((quadMap) => {
1472
+ BlankNode.includeBlankNodes(quadMap.getQuadSet()).forEach((quad) => (print += '\t' + quad.print() + '.\n'));
1473
+ });
1474
+ if (this.asObject && includeIncomingProperties) {
1475
+ print += '<----\n';
1476
+ this.asObject.forEach((quadMap) => {
1477
+ BlankNode.includeBlankNodes(quadMap.getQuadSet(), false, true).forEach((quad) => (print += '\t' + quad.print() + '.\n'));
1478
+ });
1479
+ }
1480
+ return print;
1481
+ }
1482
+ /**
1483
+ * Fires the given call back when ANY property of this node changes.
1484
+ * @param callback the method to be called when the change happens. The quads that have changed + the property that was updated are supplied as parameters
1485
+ * @param context give a context to make sure you can easily unset / clear event listeners. Usually you would provide 'this' as context
1486
+ */
1487
+ onChangeAny(callback, context) {
1488
+ this.on(NamedNode.PROPERTY_CHANGED, callback, context);
1489
+ }
1490
+ /**
1491
+ * Fires the given call back when this node become the value or is no longer the value of another node
1492
+ * @param callback the method to be called when the change happens. The quads that have changed + the property that was updated are supplied as parameters
1493
+ * @param context give a context to make sure you can easily unset / clear event listeners. Usually you would provide 'this' as context
1494
+ */
1495
+ onChangeAnyInverse(callback, context) {
1496
+ this.on(NamedNode.INVERSE_PROPERTY_CHANGED, callback, context);
1497
+ }
1498
+ /**
1499
+ * Fires the given call back when this node changes the values of the given property
1500
+ * @param callback the method to be called when the change happens. The quads that have changed + the property that was updated are supplied as parameters
1501
+ * @param context give a context to make sure you can easily unset / clear event listeners. Usually you would provide 'this' as context
1502
+ */
1503
+ onChange(property, callback, context) {
1504
+ this.on(NamedNode.PROPERTY_CHANGED + property.uri, callback, context);
1505
+ }
1506
+ /**
1507
+ * Fires the given callback when this node become the value or is no longer the value of the given property of another node
1508
+ * Example: if someGroup hasParticipant thisResource, and the group removes this node from its participants, it will trigger onChangeInverse for this node
1509
+ * @param callback the method to be called when the change happens. The quads that have changed + the property that was updated are supplied as parameters
1510
+ * @param context give a context to make sure you can easily unset / clear event listeners. Usually you would provide 'this' as context
1511
+ */
1512
+ onChangeInverse(property, callback, context) {
1513
+ this.on(NamedNode.INVERSE_PROPERTY_CHANGED + property.uri, callback, context);
1514
+ }
1515
+ /**
1516
+ * Call this when you want to stop listening for onChangeAny events. Make sure to provide the exact same BOUND instance of a method to properly clear the listener. OR make sure to provide a context both when setting and clearing the listener.
1517
+ * @param callback the exact same method you supplied to onChangeAny
1518
+ * @param context the same context you supplied to onChangeAny
1519
+ */
1520
+ removeOnChangeAny(callback, context) {
1521
+ this.off(NamedNode.PROPERTY_CHANGED, callback, context);
1522
+ }
1523
+ /**
1524
+ * Call this when you want to stop listening for onChangeAnyInverse events. Make sure to provide the exact same BOUND instance of a method to properly clear the listener. OR make sure to provide a context both when setting and clearing the listener.
1525
+ * @param callback the exact same method you supplied to onChangeAnyInverse
1526
+ * @param context the same context you supplied to onChangeAnyInverse
1527
+ */
1528
+ removeOnChangeAnyInverse(callback, context) {
1529
+ this.off(NamedNode.INVERSE_PROPERTY_CHANGED, callback, context);
1530
+ }
1531
+ /**
1532
+ * Call this when you want to stop listening for onChange events. Make sure to provide the exact same BOUND instance of a method as callback to properly clear the listener. OR make sure to provide a context both when setting and clearing the listener.
1533
+ * @param callback the exact same method you supplied to onChange
1534
+ * @param context the same context you supplied to onChange
1535
+ */
1536
+ removeOnChange(property, callback, context) {
1537
+ this.off(NamedNode.PROPERTY_CHANGED + property.uri, callback, context);
1538
+ }
1539
+ /* #######################################################################
1540
+ * ######################### STATIC METHODS ##############################
1541
+ * #######################################################################
1542
+ */
1543
+ /**
1544
+ * Call this when you want to stop listening for onChangeInverse events. Make sure to provide the exact same BOUND instance of a method as callback to properly clear the listener. OR make sure to provide a context both when setting and clearing the listener.
1545
+ * @param callback the exact same method you supplied to onChangeInverse
1546
+ * @param context the same context you supplied to onChangeInverse
1547
+ */
1548
+ removeOnChangeInverse(property, callback, context) {
1549
+ this.off(NamedNode.INVERSE_PROPERTY_CHANGED + property.uri, callback, context);
1550
+ }
1551
+ /**
1552
+ * Call this when you want to stop listening for onChangeAny events. Other then removeOnChangeAny you only have to supply the context.
1553
+ * Use this if you no longer have access to the same bound listener function or you're otherwise unable to clear with removeOnChangeAny
1554
+ * @param context the same context you supplied to onChangeAny
1555
+ */
1556
+ clearOnChangeAny(context) {
1557
+ this.removeListenerByContext(NamedNode.PROPERTY_CHANGED, context);
1558
+ }
1559
+ /**
1560
+ * Call this when you want to stop listening for onChangeAnyInverse events. Other then removeOnChangeAnyInverse you only have to supply the context.
1561
+ * Use this if you no longer have access to the same bound listener function or you're otherwise unable to clear with removeOnChangeAnyInverse
1562
+ * @param context the same context you supplied to onChangeAnyInverse
1563
+ */
1564
+ clearOnChangeAnyInverse(context) {
1565
+ this.removeListenerByContext(NamedNode.INVERSE_PROPERTY_CHANGED, context);
1566
+ }
1567
+ /**
1568
+ * Call this when you want to stop listening for onChange events. Other then removeOnChange you only have to supply the context.
1569
+ * Use this if you no longer have access to the same bound listener function or you're otherwise unable to clear with removeOnChange
1570
+ * @param context the same context you supplied to onChange
1571
+ */
1572
+ clearOnChange(property, context) {
1573
+ this.removeListenerByContext(NamedNode.PROPERTY_CHANGED + property.uri, context);
1574
+ }
1575
+ /**
1576
+ * Call this when you want to stop listening for onChangeInverse events. Other then removeOnChangeInverse you only have to supply the context.
1577
+ * Use this if you no longer have access to the same bound listener function or you're otherwise unable to clear with removeOnChangeInverse
1578
+ * @param context the same context you supplied to onChangeAny
1579
+ */
1580
+ clearOnChangeInverse(property, context) {
1581
+ this.removeListenerByContext(NamedNode.INVERSE_PROPERTY_CHANGED + property.uri, context);
1582
+ }
1583
+ /**
1584
+ * Call this when you want to stop listening for onPredicateChange events
1585
+ * @param context the same context you supplied to onPredicateChange
1586
+ */
1587
+ clearOnPredicateChange(context) {
1588
+ this.removeListenerByContext(NamedNode.AS_PREDICATE_CHANGED, context);
1589
+ }
1590
+ /**
1591
+ * Emits the batched (property) events of a NamedNode INSTANCE (meaning for this specific node)
1592
+ * Used internally by the framework to manage emitting change events
1593
+ * @internal
1594
+ */
1595
+ emitBatchedEvents() {
1596
+ //for each type of property change (and the map of batched changes for that type of change)
1597
+ [
1598
+ [this.changedProperties, NamedNode.PROPERTY_CHANGED],
1599
+ [this.changedInverseProperties, NamedNode.INVERSE_PROPERTY_CHANGED],
1600
+ [this.alteredProperties, NamedNode.PROPERTY_ALTERED],
1601
+ [this.alteredInverseProperties, NamedNode.INVERSE_PROPERTY_ALTERED],
1602
+ ].forEach(([map, event]) => {
1603
+ if (!map)
1604
+ return;
1605
+ //for each individual change that was made
1606
+ map.forEach((quads, property) => {
1607
+ //emit the specific event that THIS property has changed
1608
+ this.emit(event + property.uri, quads, property);
1609
+ });
1610
+ if (map.size > 0) {
1611
+ //emit the general event that A property has changed/altered
1612
+ this.emit(event, map);
1613
+ }
1614
+ map.clear();
1615
+ });
1616
+ // if (this.changedAsPredicate) {
1617
+ // this.emit(NamedNode.AS_PREDICATE_CHANGED, this.changedAsPredicate, this);
1618
+ // this.changedAsPredicate = null;
1619
+ // }
1620
+ // if (this.alteredAsPredicate) {
1621
+ // this.emit(NamedNode.AS_PREDICATE_ALTERED, this.alteredAsPredicate, this);
1622
+ // this.alteredAsPredicate = null;
1623
+ // }
1624
+ }
1625
+ getAsSubjectQuads() {
1626
+ return this.asSubject;
1627
+ }
1628
+ getAsObjectQuads() {
1629
+ return this.asObject;
1630
+ }
1631
+ // getAsPredicateQuads() {
1632
+ // return this.asPredicate;
1633
+ // }
1634
+ createPromise() {
1635
+ var resolve, reject;
1636
+ var promise = new Promise((res, rej) => {
1637
+ resolve = res;
1638
+ reject = rej;
1639
+ });
1640
+ return { promise, resolve, reject };
1641
+ }
1642
+ /**
1643
+ * Adds the quad to all given maps
1644
+ * @param quad
1645
+ * @param maps
1646
+ * @private
1647
+ */
1648
+ registerPropertyChange(quad, maps) {
1649
+ //register that this class has some events to emit
1650
+ eventBatcher.register(this);
1651
+ //for each given map
1652
+ maps.forEach((map) => {
1653
+ //add this quad under the predicate as key
1654
+ if (!map.has(quad.predicate)) {
1655
+ map.set(quad.predicate, new QuadSet());
1656
+ }
1657
+ map.get(quad.predicate).add(quad);
1658
+ });
1659
+ }
1660
+ // private registerPredicateChange(quad: Quad, alteration: boolean) {
1661
+ // eventBatcher.register(this);
1662
+ // if (!this.changedAsPredicate) {
1663
+ // this.changedAsPredicate = new QuadArray();
1664
+ // }
1665
+ // this.changedAsPredicate.push(quad);
1666
+ // if (alteration) {
1667
+ // if (!this.alteredAsPredicate) {
1668
+ // this.alteredAsPredicate = new QuadArray();
1669
+ // }
1670
+ // this.alteredAsPredicate.push(quad);
1671
+ // }
1672
+ // }
1673
+ getQuadsByValue(property, value) {
1674
+ return value instanceof NamedNode
1675
+ ? this.asSubject.get(property).has(value)
1676
+ ? this.asSubject.get(property).get(value)
1677
+ : new QuadSet()
1678
+ : this.asSubject
1679
+ .get(property)
1680
+ .filter((quad) => quad.object.equals(value), QuadSet);
1681
+ }
1682
+ }
1683
+ /**
1684
+ * The base of temporary URI's
1685
+ * @internal
1686
+ */
1687
+ NamedNode.TEMP_URI_BASE = 'lin://tmp/';
1688
+ /**
1689
+ * Emitter used by the class itself by static methods emitting events.
1690
+ * Anyone wanting to listen to that should therefore add a listener with NamedNode.emitter.on(...)
1691
+ * @internal
1692
+ */
1693
+ NamedNode.emitter = new EventEmitter();
1694
+ /**
1695
+ * event emitted when nodes need to be stored
1696
+ * @internal
1697
+ */
1698
+ NamedNode.STORE_NODES = 'STORE_NODES';
1699
+ /**
1700
+ * Event emitted when previous values have been overwritten (for example with update or moverwrite)
1701
+ * NOTE: Locally we may not know all the properties, but the intend of update / moverwrite is to overwrite ANY existing properties. So event handlers listening to this event should clear any previous property values they can find.
1702
+ * @internal
1703
+ */
1704
+ NamedNode.CLEARED_PROPERTIES = 'CLEARED_PROPERTIES';
1705
+ /**
1706
+ * event emitted when nodes need to be removed
1707
+ * @internal
1708
+ */
1709
+ NamedNode.REMOVE_NODES = 'REMOVE_NODES';
1710
+ /**
1711
+ * event emitted when the URI of a node has been updated
1712
+ */
1713
+ NamedNode.URI_UPDATED = 'URI_UPDATED';
1714
+ /**
1715
+ * event emitted when nodes need to be loaded
1716
+ * @internal
1717
+ */
1718
+ NamedNode.LOAD_NODES = 'LOAD_NODES';
1719
+ /**
1720
+ * event emitted by a single node when its properties have changed
1721
+ * @internal
1722
+ */
1723
+ NamedNode.PROPERTY_CHANGED = 'PROPERTY_CHANGED';
1724
+ /**
1725
+ * event emitted by a single node when its properties have been altered
1726
+ * NOTE: we use 'altered' for changes made by user interaction vs 'changed' for changes that
1727
+ * could for example be due to new data being loaded
1728
+ * @internal
1729
+ */
1730
+ NamedNode.PROPERTY_ALTERED = 'PROPERTY_ALTERED';
1731
+ /**
1732
+ * event emitted by a single node when its inverse properties have been changed
1733
+ * @internal
1734
+ */
1735
+ NamedNode.INVERSE_PROPERTY_CHANGED = 'INVERSE_PROPERTY_CHANGED';
1736
+ //### event types ###
1737
+ /**
1738
+ * event emitted by a single node when its inverse properties have been altered
1739
+ * NOTE: we use 'altered' for changes made by user interaction vs 'changed' for changes that
1740
+ * could for example be due to new data being loaded
1741
+ * @internal
1742
+ */
1743
+ NamedNode.INVERSE_PROPERTY_ALTERED = 'INVERSE_PROPERTY_ALTERED';
1744
+ /**
1745
+ * event emitted by a single node when its used or not used anymore as a predicate
1746
+ * @internal
1747
+ */
1748
+ NamedNode.AS_PREDICATE_CHANGED = 'AS_PREDICATE_CHANGED';
1749
+ /**
1750
+ * event emitted by a single node when its used or not used anymore as a predicate due to user requested alterations
1751
+ * @internal
1752
+ */
1753
+ NamedNode.AS_PREDICATE_ALTERED = 'AS_PREDICATE_ALTERED';
1754
+ /**
1755
+ * event emitted by a single node when it's been removed
1756
+ */
1757
+ NamedNode.NODE_REMOVED = 'NODE_REMOVED';
1758
+ NamedNode.namedNodes = new NodeMap();
1759
+ NamedNode.tempCounter = 0;
1760
+ NamedNode.nodesToSave = new NodeSet();
1761
+ NamedNode.nodesToLoad = new NodeSet();
1762
+ NamedNode.nodesToLoadFully = new NodeSet();
1763
+ NamedNode.nodesToRemove = new CoreSet();
1764
+ NamedNode.nodesURIUpdated = new CoreMap();
1765
+ NamedNode.clearedProperties = new CoreMap();
1766
+ //cannot import from xsd ontology here without creating circular dependencies
1767
+ var rdfLangString = NamedNode.getOrCreate('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString');
1768
+ var xsdString = NamedNode.getOrCreate('http://www.w3.org/2001/XMLSchema#string');
1769
+ export class BlankNode extends NamedNode {
1770
+ constructor(uri, isTemporaryNode = false) {
1771
+ super(uri || BlankNode.createUri(), isTemporaryNode);
1772
+ this.termType = 'BlankNode';
1773
+ NamedNode.register(this);
1774
+ }
1775
+ get uri() {
1776
+ return this._value;
1777
+ }
1778
+ set uri(uri) {
1779
+ throw new Error('You should not set the URI of a BlankNode. Make sure the node is created as a NamedNode. BlankNode data:\n' +
1780
+ BlankNode.includeBlankNodes(this.getAllQuads()).toString());
1781
+ }
1782
+ static create(isTemporaryNode = false) {
1783
+ return new BlankNode(null, isTemporaryNode);
1784
+ }
1785
+ static createUri() {
1786
+ return '_:' + this.counter++;
1787
+ }
1788
+ static includeBlankNodes(quads, includeObjectBlankNodes = true, includeSubjectBlankNodes = false, blankNodes = new NodeURIMappings()) {
1789
+ let add = quads instanceof Set ? quads.add.bind(quads) : quads.push.bind(quads);
1790
+ quads.forEach((quad) => {
1791
+ if (includeObjectBlankNodes && quad.object instanceof BlankNode) {
1792
+ this.addBlankNodeQuads(quad.object, add, blankNodes);
1793
+ }
1794
+ if (includeSubjectBlankNodes && quad.subject instanceof BlankNode) {
1795
+ //also add quads of subjects that are blank nodes, iteratively, but don't include the blank node objects of those quads
1796
+ this.addBlankNodeQuads(quad.subject, add, blankNodes, true);
1797
+ }
1798
+ });
1799
+ return quads;
1800
+ }
1801
+ static addBlankNodeQuads(blankNode, add, blankNodes, inverseIteration = false) {
1802
+ // console.log('adding quads of ' + blankNode.uri);
1803
+ blankNodes.set(blankNode.uri, blankNode);
1804
+ blankNode.getAllQuads().forEach((quad) => {
1805
+ if (!(quad instanceof Quad)) {
1806
+ throw new Error('Not a quad');
1807
+ }
1808
+ add(quad);
1809
+ //also, iteratively include quads of blank-node values of blank-nodes
1810
+ if (!inverseIteration && quad.object instanceof BlankNode) {
1811
+ //if we've not seen this blank node yet during this collection (avoiding loops from circular references between blank nodes)
1812
+ if (!blankNodes.has(quad.object.uri)) {
1813
+ this.addBlankNodeQuads(quad.object, add, blankNodes);
1814
+ }
1815
+ }
1816
+ if (inverseIteration && quad.subject instanceof BlankNode) {
1817
+ //if we've not seen this blank node yet during this collection (avoiding loops from circular references between blank nodes)
1818
+ if (!blankNodes.has(quad.subject.uri)) {
1819
+ this.addBlankNodeQuads(quad.subject, add, blankNodes, true);
1820
+ }
1821
+ }
1822
+ });
1823
+ }
1824
+ }
1825
+ BlankNode.counter = 0;
1826
+ /**
1827
+ * One of the two main classes of nodes (nodes) in the graph.
1828
+ * Literals are endpoints. They do NOT have outgoing connections (edges) to other nodes in the graph.
1829
+ * Though a NamedNode can point to a Literal.
1830
+ * Each literal node has a literal value, like a string.
1831
+ * Besides that is can also have a language tag or a data type.
1832
+ * Literals are often saved as a single string, for example '"yes"@en' (yes in english) or '"true"^^xsd:boolean (the value true with datatype english)
1833
+ * This class represents those properties.
1834
+ * See also: https://www.w3.org/TR/rdf-concepts/#section-Graph-Literal
1835
+ */
1836
+ export class Literal extends Node {
1837
+ /**
1838
+ * Other than with NamedNodes, its fine to do `new Literal("my string value")`
1839
+ * Datatype and language tags are optional
1840
+ * @param value
1841
+ * @param datatype
1842
+ * @param language
1843
+ */
1844
+ constructor(value, _datatype = null, _language = '') {
1845
+ super(value);
1846
+ this._datatype = _datatype;
1847
+ this._language = _language;
1848
+ this.termType = 'Literal';
1849
+ if (typeof value !== 'string') {
1850
+ throw new Error('Literal value must be a string. Given value was a ' +
1851
+ typeof value +
1852
+ ' (' +
1853
+ value +
1854
+ ')');
1855
+ }
1856
+ }
1857
+ /**
1858
+ * get the language tag of this literal which states which language this literal is written in
1859
+ * See also: http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
1860
+ */
1861
+ get language() {
1862
+ //list of language tags: http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
1863
+ return this._language;
1864
+ }
1865
+ /**
1866
+ * update the language tag of this literal
1867
+ */
1868
+ set language(lang) {
1869
+ this._language = lang;
1870
+ //the datatype of any literal with a language tag is rdf:langString
1871
+ this._datatype = rdfLangString;
1872
+ }
1873
+ /**
1874
+ * returns the datatype of this literal
1875
+ * Note that datatypes are NamedNodes themselves, who always have rdf:type rdf:Datatype
1876
+ * If no datatype is set, the default datatype xsd:string will be returned
1877
+ * If a language tag is set, the returned datatype will be rdf:langString
1878
+ */
1879
+ get datatype() {
1880
+ if (this._datatype) {
1881
+ return this._datatype;
1882
+ }
1883
+ //default datatype is xsd:string, if language is set this.datatype should be langString already
1884
+ return xsdString;
1885
+ }
1886
+ /**
1887
+ * Update the datatype of this literal
1888
+ * @param datatype
1889
+ */
1890
+ set datatype(datatype) {
1891
+ this._datatype = datatype;
1892
+ }
1893
+ /**
1894
+ * Return the value of this literal
1895
+ * @param datatype
1896
+ */
1897
+ get value() {
1898
+ return this._value;
1899
+ }
1900
+ /**
1901
+ * update the literal value of this literal
1902
+ * @param datatype
1903
+ */
1904
+ set value(value) {
1905
+ let previousValue;
1906
+ //if this literal is being used in a quad
1907
+ if (this.referenceQuad) {
1908
+ //remember the previous value for the events below
1909
+ previousValue = this.clone();
1910
+ }
1911
+ //update the value
1912
+ this._value = value;
1913
+ if (this.referenceQuad) {
1914
+ //register change for subject node (for node.onChange(prop) listeners)
1915
+ this.referenceQuad.subject.registerValueChange(this.referenceQuad, true);
1916
+ //notify the graph of a change (will mimic a removed and added quad)
1917
+ // this.referenceQuad.graph.registerQuadValueChange(previousValue,this.referenceQuad);
1918
+ //notify the quad that the value of it's object has changed (will mimic a removed and added quad)
1919
+ this.referenceQuad.onValueChanged(previousValue);
1920
+ }
1921
+ }
1922
+ /**
1923
+ * Returns the literal value of the first Literal that occurs as object for the given subject and property and optionally also matches the given language
1924
+ * @param subject
1925
+ * @param property
1926
+ * @param language
1927
+ * @deprecated
1928
+ * @returns {string|undefined}
1929
+ */
1930
+ static getValue(subject, property, language = '') {
1931
+ for (var value of subject.getAll(property)) {
1932
+ if (value instanceof Literal &&
1933
+ (!language || value.isOfLanguage(language))) {
1934
+ return value.value;
1935
+ }
1936
+ }
1937
+ return undefined;
1938
+ }
1939
+ /**
1940
+ * Returns all literal values of the Literals that occur as object for the given subject and property and optionally also match the given language
1941
+ * @param subject
1942
+ * @param property
1943
+ * @param language
1944
+ * @returns {string[]}
1945
+ */
1946
+ static getValues(subject, property, language = '') {
1947
+ var res = [];
1948
+ for (var value of subject.getAll(property)) {
1949
+ if (value instanceof Literal &&
1950
+ (!language || value.isOfLanguage(language))) {
1951
+ res.push(value.value);
1952
+ }
1953
+ }
1954
+ return res;
1955
+ }
1956
+ static isLiteralString(literalString) {
1957
+ var regex = new RegExp('(\\"[^\\"^\\n]*\\")(@[a-z]{1,3}|\\^\\^[a-zA-Z]+\\:[a-zA-Z0-9_-]+|\\<https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)\\>)?');
1958
+ return regex.test(literalString);
1959
+ }
1960
+ static fromString(literalString) {
1961
+ //self made regex thatL
1962
+ // match anything between quotes or quad quotes (the quotes are group 1 and 3)
1963
+ // except escaped quotes (2)
1964
+ //and everything behind it (4) for language or datatype
1965
+ //..with a little help on the escaped quotes from here
1966
+ //https://stackoverflow.com/questions/38563414/javascript-regex-to-select-quoted-string-but-not-escape-quotes
1967
+ let match = literalString.match(/("|""")([^"\\]*(?:\\.[^"\\]*)*)("|""")(.*)/);
1968
+ //NOTE: if \n replacement turns out to be not correct here it should at least be moved to JSONLDParser, see https://github.com/digitalbazaar/jsonld.js/issues/242
1969
+ let literal = (match[2] ? match[2] : '')
1970
+ .replace(/\\"/g, '"')
1971
+ .replace(/\\n/g, '\n');
1972
+ let suffix = match[4];
1973
+ if (!suffix) {
1974
+ return new Literal(literal);
1975
+ }
1976
+ if (suffix[0] == '@') {
1977
+ return new Literal(literal, null, suffix.substr(1));
1978
+ }
1979
+ else if (suffix[0] == '^') {
1980
+ var datatype = NamedNode.fromString(suffix.substr(2));
1981
+ return new Literal(literal, datatype);
1982
+ }
1983
+ else {
1984
+ throw new Error('Invalid literal string format: ' + literalString);
1985
+ }
1986
+ }
1987
+ getAs(type) {
1988
+ return type.getOf(this);
1989
+ }
1990
+ /**
1991
+ * @internal
1992
+ * @param quad
1993
+ */
1994
+ registerProperty(quad) {
1995
+ throw new Error('Literal nodes should not be used as subjects');
1996
+ }
1997
+ /**
1998
+ * registers the use of a quad. Since a quad can only be used in 1 quad
1999
+ * this method makes a clone of the Literal if it's used a second time,
2000
+ * and returns that new Literal so it will be used by the quad
2001
+ * @internal
2002
+ * @param quad
2003
+ */
2004
+ registerInverseProperty(quad) {
2005
+ //if this Literal is already being used in another quad
2006
+ if (this.referenceQuad) {
2007
+ //then return a clone
2008
+ //(this allows things like a.set(label,b.getOne(label)))
2009
+ return this.clone().registerInverseProperty(quad);
2010
+ }
2011
+ this.referenceQuad = quad;
2012
+ return this;
2013
+ }
2014
+ /**
2015
+ * @internal
2016
+ * @param quad
2017
+ */
2018
+ unregisterProperty(quad) {
2019
+ throw new Error('Literal nodes should not be used as subjects');
2020
+ }
2021
+ /**
2022
+ * @internal
2023
+ * @param quad
2024
+ */
2025
+ unregisterInverseProperty(quad) {
2026
+ this.referenceQuad = null;
2027
+ }
2028
+ /**
2029
+ * returns true if this literal node has a language tag
2030
+ */
2031
+ hasLanguage() {
2032
+ return this._language != '';
2033
+ }
2034
+ /**
2035
+ * returns true if the language tag of this literal matches the given language
2036
+ */
2037
+ isOfLanguage(language) {
2038
+ return this._language === language;
2039
+ }
2040
+ /**
2041
+ * returns true if this literal has a datatype
2042
+ */
2043
+ hasDatatype() {
2044
+ //checks for null and undefined
2045
+ return this._datatype != null;
2046
+ }
2047
+ /**
2048
+ * Returns true if both are literal nodes, with equal literal values, equal language tags and equal data types
2049
+ * Other than NamedNodes, two different literal node instances can be deemed equivalent if all their properties are the same
2050
+ * @param other
2051
+ * @param caseSensitive
2052
+ */
2053
+ equals(other) {
2054
+ return this._equals(other);
2055
+ }
2056
+ /**
2057
+ * Returns true if both are literal nodes, with equal literal values (CASE INSENSITIVE CHECK), equal language tags and equal data types
2058
+ * Other than NamedNodes, two different literal node instances can be deemed equivalent if all their properties are the same
2059
+ * @param other
2060
+ */
2061
+ equalsCaseInsensitive(other) {
2062
+ return this._equals(other, false);
2063
+ }
2064
+ /**
2065
+ * Creates a new Literal with exact the same properties (value,datatype and language)
2066
+ */
2067
+ clone() {
2068
+ return new Literal(this._value, this.datatype, this.language);
2069
+ }
2070
+ getReferenceQuad() {
2071
+ return this.referenceQuad;
2072
+ }
2073
+ hasInverseProperty(property) {
2074
+ return this.referenceQuad && this.referenceQuad.predicate === property;
2075
+ }
2076
+ hasInverse(property, value) {
2077
+ return (this.referenceQuad &&
2078
+ this.referenceQuad.predicate === property &&
2079
+ this.referenceQuad.subject === value);
2080
+ }
2081
+ getOneInverse(property) {
2082
+ return this.referenceQuad && this.referenceQuad.predicate === property
2083
+ ? this.referenceQuad.subject
2084
+ : undefined;
2085
+ }
2086
+ getMultipleInverse(properties) {
2087
+ if (properties.find((p) => p === this.referenceQuad.predicate)) {
2088
+ return new NodeSet([this.referenceQuad.subject]);
2089
+ }
2090
+ return new NodeSet();
2091
+ }
2092
+ getAllInverseQuads(includeImplicit) {
2093
+ return !includeImplicit || !this.referenceQuad.implicit
2094
+ ? new QuadArray(this.referenceQuad)
2095
+ : new QuadArray();
2096
+ }
2097
+ getAllQuads(includeAsObject = false, includeImplicit = false) {
2098
+ return includeAsObject && (!includeImplicit || !this.referenceQuad.implicit)
2099
+ ? new QuadArray(this.referenceQuad)
2100
+ : new QuadArray();
2101
+ }
2102
+ promiseLoaded(loadInverseProperties = false) {
2103
+ return Promise.resolve(true);
2104
+ }
2105
+ isLoaded(includingInverseProperties = false) {
2106
+ return true;
2107
+ }
2108
+ toString() {
2109
+ //quotes are needed to differentiate the literal "http://test.com" from the URI http://test.com, so the literal value is always surrounded by quotes
2110
+ //quad quotes are needed in case of newlines
2111
+ // let quotes = this._value.indexOf("\n") != -1 ? '"""' : '"';
2112
+ let quotes = '"';
2113
+ let suffix = '';
2114
+ if (this.hasLanguage()) {
2115
+ suffix = '@' + this.language;
2116
+ }
2117
+ else if (this.hasDatatype()) {
2118
+ suffix = '^^<' + this.datatype.uri + '>';
2119
+ }
2120
+ //quotes in the value need to be escaped
2121
+ return (quotes +
2122
+ this._value.replace(/\"/g, '\\"').replace(/\n/g, '\\n') +
2123
+ quotes +
2124
+ suffix);
2125
+ }
2126
+ print(includeIncomingProperties = true) {
2127
+ return this.toString();
2128
+ }
2129
+ _equals(other, caseSensitive = true) {
2130
+ if (other === this)
2131
+ return true;
2132
+ var valueToMatch;
2133
+ var languageToMatch;
2134
+ var datatypeToMatch;
2135
+ if (other instanceof Literal) {
2136
+ valueToMatch = other.value;
2137
+ languageToMatch = other.language;
2138
+ datatypeToMatch = other.datatype; //direct access to avoid default, alternatively build a boolean parameter 'returnDefault=true' into getDataType()
2139
+ }
2140
+ else {
2141
+ var type = typeof other;
2142
+ if (type == 'string' || type == 'number' || type == 'boolean') {
2143
+ //if you don't specify a datatype we accept all
2144
+ valueToMatch = other.toString();
2145
+ languageToMatch = '';
2146
+ datatypeToMatch = null;
2147
+ }
2148
+ else {
2149
+ return false;
2150
+ }
2151
+ }
2152
+ //do the actual matching
2153
+ var valueMatch;
2154
+ if (caseSensitive) {
2155
+ valueMatch = this._value === valueToMatch;
2156
+ }
2157
+ else {
2158
+ valueMatch =
2159
+ this._value.toLocaleLowerCase() == valueToMatch.toLocaleLowerCase();
2160
+ }
2161
+ //if values match
2162
+ if (valueMatch) {
2163
+ //if there is a language
2164
+ if (this.hasLanguage()) {
2165
+ //then only the languages need to match
2166
+ return this.language == languageToMatch;
2167
+ }
2168
+ else {
2169
+ //no language = datatypes need to match
2170
+ //we check with this.datatype, not this.datatype which can return the default xsd:String
2171
+ //a literal without datatypespecified is however considered different from a a literal with a explicit xsd:String datatype
2172
+ //that is, like some SPARQL quad stores, you should be able to create two otherwise identical (sub&pred) quads for those two literals
2173
+ return this.datatype === datatypeToMatch;
2174
+ }
2175
+ }
2176
+ return false;
2177
+ }
2178
+ }
2179
+ export class Graph {
2180
+ constructor(value, quads) {
2181
+ this.value = value;
2182
+ // private static addedQuads: Map<Graph,QuadArray> = new Map();
2183
+ // private static removedQuads: Map<Graph,QuadArray> = new Map();
2184
+ // private static addedQuadsAlterations: Map<Graph,QuadArray> = new Map();
2185
+ this.termType = 'NamedNode';
2186
+ // super();
2187
+ this._node = NamedNode.getOrCreate(value);
2188
+ this.quads = quads ? quads : new QuadSet();
2189
+ }
2190
+ get node() {
2191
+ return this._node;
2192
+ }
2193
+ //Static methods
2194
+ /**
2195
+ * Resets the map of nodes that is known in this environment
2196
+ */
2197
+ static reset() {
2198
+ this.graphs = new CoreMap();
2199
+ }
2200
+ static create(quads) {
2201
+ var uri = NamedNode.createNewTempUri();
2202
+ return this._create(uri, quads);
2203
+ }
2204
+ /**
2205
+ * @internal
2206
+ * @param graph
2207
+ */
2208
+ static register(graph) {
2209
+ if (this.graphs.has(graph.node.uri)) {
2210
+ throw new Error('A graph with this URI already exists. You should probably use Graph.getOrCreate instead of Graph.create (' +
2211
+ graph.node.uri +
2212
+ ')');
2213
+ }
2214
+ this.graphs.set(graph.node.uri, graph);
2215
+ // super.register(graph);
2216
+ }
2217
+ /**
2218
+ * @internal
2219
+ * @param graph
2220
+ */
2221
+ static unregister(graph) {
2222
+ if (!this.graphs.has(graph.node.uri)) {
2223
+ throw new Error('This node has already been removed from the registry: ' +
2224
+ graph.node.uri);
2225
+ }
2226
+ this.graphs.delete(graph.node.uri);
2227
+ }
2228
+ /**
2229
+ * Adds the quad to all given maps
2230
+ * @param quad
2231
+ * @param maps
2232
+ * @private
2233
+ */
2234
+ /*private static registerGraphEvent(graph: Graph, quad:Quad, maps: Map<Graph, QuadArray>[]) {
2235
+ //register that this class has some events to emit
2236
+ eventBatcher.register(Graph);
2237
+ //for each given map
2238
+ maps.forEach((map) => {
2239
+ //add this quad under the predicate as key
2240
+ if (!map.has(graph)) {
2241
+ map.set(graph, new QuadArray());
2242
+ }
2243
+ map.get(graph).push(quad);
2244
+ });
2245
+ }*/
2246
+ /*static emitBatchedEvents(resolve?: any, reject?: any) {
2247
+ if(this.addedQuads.size > 0 || this.removedQuads.size > 0)
2248
+ {
2249
+ this.emitter.emit(Graph.CONTENTS_CHANGED,this.addedQuads,this.removedQuads);
2250
+ this.addedQuads = new Map();
2251
+ this.removedQuads = new Map()
2252
+ }
2253
+ if(this.addedQuadsAlterations.size > 0 || this.removedQuadsAlterations.size > 0)
2254
+ {
2255
+ this.emitter.emit(Graph.CONTENTS_ALTERED,this.addedQuadsAlterations,this.removedQuadsAlterations);
2256
+ this.addedQuadsAlterations = new Map();
2257
+ this.removedQuadsAlterations = new Map()
2258
+ }
2259
+ }*/
2260
+ static getOrCreate(uri) {
2261
+ return this.getGraph(uri) || this._create(uri);
2262
+ }
2263
+ static getGraph(uri, mustExist = false) {
2264
+ //look it up in known full uri node map
2265
+ if (this.graphs.has(uri)) {
2266
+ return this.graphs.get(uri);
2267
+ }
2268
+ if (mustExist) {
2269
+ throw Error('Could not find graph for: ' + uri);
2270
+ }
2271
+ return null;
2272
+ }
2273
+ static updateUri(graph, uri) {
2274
+ graph.node.uri = uri;
2275
+ }
2276
+ static getAll() {
2277
+ return this.graphs;
2278
+ }
2279
+ static _create(uri, quads) {
2280
+ var graph = new Graph(uri, quads);
2281
+ this.register(graph);
2282
+ return graph;
2283
+ }
2284
+ equals(other) {
2285
+ return other === this;
2286
+ }
2287
+ /**
2288
+ * @internal
2289
+ * @param quad
2290
+ */
2291
+ registerQuad(quad, alteration = false, emitEvents = true) {
2292
+ this.quads.add(quad);
2293
+ // if(emitEvents)
2294
+ // {
2295
+ // Graph.registerGraphEvent(this,quad,alteration ? [Graph.addedQuads] : [Graph.addedQuads,Graph.addedQuadsAlterations]);
2296
+ // }
2297
+ }
2298
+ /**
2299
+ * @internal
2300
+ * @param quad
2301
+ */
2302
+ unregisterQuad(quad, alteration = false, emitEvents = true) {
2303
+ this.quads.delete(quad);
2304
+ // if(emitEvents)
2305
+ // {
2306
+ // Graph.registerGraphEvent(this,quad,alteration ? [Graph.removedQuads] : [Graph.removedQuads,Graph.removedQuadsAlterations])
2307
+ // }
2308
+ }
2309
+ hasQuad(quad) {
2310
+ return this.quads.has(quad);
2311
+ }
2312
+ //Note: cannot name this getQuads, because NamedNode already uses that for getting all quads of all its properties
2313
+ getContents() {
2314
+ return this.quads;
2315
+ }
2316
+ setContents(quads) {
2317
+ this.quads = quads;
2318
+ }
2319
+ toString() {
2320
+ return ('Graph: [' +
2321
+ this.node.uri.toString() +
2322
+ ' - ' +
2323
+ this.quads.size +
2324
+ ' quads]');
2325
+ }
2326
+ }
2327
+ // private static removedQuadsAlterations: Map<Graph,QuadArray> = new Map();
2328
+ /**
2329
+ * Emitted when changes have been made to this graph. Only emitted when data has actually changed, not just when data is loaded
2330
+ */
2331
+ Graph.CONTENTS_ALTERED = 'CONTENTS_ALTERED';
2332
+ /**
2333
+ * Emitted when the contents of this graph have changed. Can also be due to loading data
2334
+ */
2335
+ Graph.CONTENTS_CHANGED = 'CONTENTS_ALTERED';
2336
+ Graph.graphs = new CoreMap();
2337
+ class DefaultGraph extends Graph {
2338
+ constructor() {
2339
+ //empty string for default graph URI (part of the standard)
2340
+ //https://rdf.js.org/data-model-spec/#defaultgraph-interface
2341
+ super('');
2342
+ this.value = '';
2343
+ this.termType = DefaultGraphTermType;
2344
+ this.uri = defaultGraphURI;
2345
+ }
2346
+ toString() {
2347
+ return 'DefaultGraph';
2348
+ }
2349
+ }
2350
+ export const defaultGraph = new DefaultGraph();
2351
+ export class Quad extends EventEmitter {
2352
+ /**
2353
+ * Creates the quad
2354
+ * @param subject - the subject of the quad
2355
+ * @param predicate
2356
+ * @param object
2357
+ */
2358
+ constructor(subject, predicate, object, _graph = defaultGraph, implicit = false, alteration = false, emitEvents = true) {
2359
+ super();
2360
+ this.subject = subject;
2361
+ this.predicate = predicate;
2362
+ this.object = object;
2363
+ this._graph = _graph;
2364
+ this.implicit = implicit;
2365
+ this.setup(alteration, emitEvents);
2366
+ }
2367
+ get graph() {
2368
+ return this._graph;
2369
+ }
2370
+ /**
2371
+ * Returns true if this quad still exists as an object in memory, but is no longer actively used in the graph
2372
+ */
2373
+ get isRemoved() {
2374
+ return this._removed;
2375
+ }
2376
+ /**
2377
+ * @internal
2378
+ * Returns true if events of newly created quads or removed quads are currently batched and waiting to be emitted
2379
+ */
2380
+ static hasBatchedEvents() {
2381
+ return this.createdQuads.size > 0 || this.removedQuads.size > 0;
2382
+ }
2383
+ /*set graph(newGraph: Graph) {
2384
+ if(newGraph !== this._graph)
2385
+ {
2386
+ //NOTE: we could have gone a different way with Quad.moveToGraph(quad,newGraph) / quad.moveto(newGraph), which removes the old one and returns a new quad
2387
+ //if there is any issues with this implementation, go that way.
2388
+ //for now, this implementation keeps the same Quad object but mimics the adding / removing of quads
2389
+
2390
+ //create a clone of this quad as it is now, without sending alteration events
2391
+ let oldQuad = new Quad(this.subject,this.predicate,this.object,this._graph,this.implicit,false,false);
2392
+
2393
+ //make sure this cloned quad is not even registered
2394
+ oldQuad.turnOff();
2395
+
2396
+ //remove this quad from the old graph
2397
+ this._graph.unregisterQuad(this,true);
2398
+
2399
+ //update the graph
2400
+ this._graph = newGraph;
2401
+
2402
+ //register this quad in the new graph
2403
+ this._graph.registerQuad(this,true);
2404
+
2405
+ this.mimicEventsOnUpdate(oldQuad);
2406
+ }
2407
+ }*/
2408
+ /**
2409
+ * @internal
2410
+ */
2411
+ static emitBatchedEvents() {
2412
+ if (this.createdQuads.size > 0 && this.removedQuads.size > 0) {
2413
+ //if both created and removed quads are batched then we remove the quad from both sets
2414
+ this.createdQuads.forEach((quad) => {
2415
+ if (this.removedQuads.has(quad)) {
2416
+ this.createdQuads.delete(quad);
2417
+ this.removedQuads.delete(quad);
2418
+ }
2419
+ });
2420
+ }
2421
+ if (this.createdQuads.size > 0) {
2422
+ this.emitter.emit(Quad.QUADS_CREATED, this.createdQuads);
2423
+ this.createdQuads = new QuadSet();
2424
+ }
2425
+ if (this.removedQuads.size > 0) {
2426
+ this.emitter.emit(Quad.QUADS_REMOVED, this.removedQuads);
2427
+ this.removedQuads = new QuadSet();
2428
+ }
2429
+ if (this.createdQuadsAltered.size > 0 ||
2430
+ this.removedQuadsAltered.size > 0) {
2431
+ this.emitter.emit(Quad.QUADS_ALTERED, this.createdQuadsAltered, this.removedQuadsAltered);
2432
+ this.createdQuadsAltered = new QuadSet();
2433
+ this.removedQuadsAltered = new QuadSet();
2434
+ }
2435
+ }
2436
+ /**
2437
+ * Get the existing quad for the given subject,predicate and object, or create it if it didn't exists yet.
2438
+ * @param subject
2439
+ * @param predicate
2440
+ * @param object
2441
+ * @param implicit
2442
+ * @param alteration - states whether this quad has been created by a user interaction (true) or simply because of updated data has been loaded
2443
+ */
2444
+ static getOrCreate(subject, predicate, object, graph = defaultGraph, implicit = false, alteration = false, emitEvents = true) {
2445
+ return (this.get(subject, predicate, object, graph) ||
2446
+ new Quad(subject, predicate, object, graph, implicit, alteration, emitEvents));
2447
+ }
2448
+ /**
2449
+ * Gets the existing quad for the given subject,predicate and object.
2450
+ * Will return any quad with an equivalent object. See Literal.isEquivalentTo() and NamedNode.isEquivalentTo() for more information.
2451
+ * @param subject
2452
+ * @param predicate
2453
+ * @param object
2454
+ */
2455
+ static get(subject, predicate, object, graph) {
2456
+ if (!subject || !predicate || !object)
2457
+ return null;
2458
+ return subject.getQuads(predicate, object).find((q) => q._graph === graph);
2459
+ }
2460
+ static moveQuadsToGraph(quads, graph, alteration = false) {
2461
+ let result = new (Object.getPrototypeOf(quads).constructor)();
2462
+ quads.forEach((quad) => {
2463
+ result.push(quad.moveToGraph(graph, alteration));
2464
+ });
2465
+ return result;
2466
+ }
2467
+ static emitRemovedQuad(quad, alteration = false) {
2468
+ //removed quad events are batched together and emitted on the next tick
2469
+ //so here we make sure the Quad class will emit its batched events on the next tick
2470
+ eventBatcher.register(Quad);
2471
+ //and here we save this quad to a set of removedQuads which is a static property of the Quad class
2472
+ Quad.removedQuads.add(quad);
2473
+ if (alteration && !quad.implicit) {
2474
+ Quad.removedQuadsAltered.add(quad);
2475
+ }
2476
+ //we need to let this quad emit this event straight away because for example the reasoner needs to listen to this exact quad to retract
2477
+ quad.emit(Quad.QUAD_REMOVED);
2478
+ }
2479
+ static emitCreatedQuad(quad, alteration = false) {
2480
+ //new quad events are batched together and emitted on the next tick
2481
+ //so here we make sure the Quad class will emit its batched events on the next tick
2482
+ eventBatcher.register(Quad);
2483
+ //and here we save this quad to a set of newQuads which is a static property of the Quad class
2484
+ Quad.createdQuads.add(quad);
2485
+ //only if it's an alteration AND it's relevant to storage controllers do we emit the QUADS_ALTERED event for this quad
2486
+ if (alteration && !quad.implicit) {
2487
+ Quad.createdQuadsAltered.add(quad);
2488
+ }
2489
+ }
2490
+ /**
2491
+ * Removes this quad and creates a new quad with the same subject,predicate,object, but a new graph.
2492
+ * Returns the new quad
2493
+ * @param newGraph
2494
+ */
2495
+ moveToGraph(newGraph, alteration = true) {
2496
+ if (newGraph === this._graph) {
2497
+ return this;
2498
+ }
2499
+ let newQuad = Quad.getOrCreate(this.subject, this.predicate, this.object, newGraph, this.implicit, alteration);
2500
+ this.remove(alteration);
2501
+ return newQuad;
2502
+ }
2503
+ /**
2504
+ * Turns off a quad. Meaning it will no longer be active in the graph.
2505
+ * Comes in handy in very specific use cases when for example quads have already been created, but you want to check what the state was before these quads were created
2506
+ */
2507
+ turnOff() {
2508
+ this.subject.unregisterProperty(this, false, false);
2509
+ // this.predicate.unregisterAsPredicate(this, false, false);
2510
+ this.object.unregisterInverseProperty(this, false, false);
2511
+ this.graph.unregisterQuad(this, false, false);
2512
+ }
2513
+ /**
2514
+ * Turns on a quad. Meaning it will be active (again) in the graph.
2515
+ * Only use this if you've had to turn quads off first.
2516
+ */
2517
+ turnOn() {
2518
+ this.subject.registerProperty(this, false, false);
2519
+ // this.predicate.registerAsPredicate(this, false, false);
2520
+ this.object.registerInverseProperty(this, false, false);
2521
+ this.graph.registerQuad(this, false, false);
2522
+ }
2523
+ /**
2524
+ * Turn an implicit quad into an explicit quad (because an explicit user action generated it as an independent explicit fact now)
2525
+ */
2526
+ makeExplicit() {
2527
+ if (this.implicit) {
2528
+ //unregister and make explicit
2529
+ this.turnOff();
2530
+ this.implicit = false;
2531
+ //re-register and make it an 'alteration' so it will be picked up by the storage
2532
+ this.setup(true);
2533
+ }
2534
+ }
2535
+ /**
2536
+ * Remove this quad from the graph
2537
+ * Will be removed both locally and from the graph database
2538
+ * @param alteration
2539
+ */
2540
+ remove(alteration = false, emitEvents = true) {
2541
+ if (this._removed)
2542
+ return;
2543
+ //first set removed is true so event handlers can detect the difference between added or removed values
2544
+ this._removed = true;
2545
+ this.subject.unregisterProperty(this);
2546
+ // this.predicate.unregisterAsPredicate(this);
2547
+ this.object.unregisterInverseProperty(this);
2548
+ this.graph.unregisterQuad(this, alteration);
2549
+ if (emitEvents) {
2550
+ Quad.emitRemovedQuad(this, alteration);
2551
+ }
2552
+ Quad.globalNumQuads--;
2553
+ }
2554
+ /**
2555
+ * Cancel the removal of a quad
2556
+ */
2557
+ undoRemoval() {
2558
+ this.setup();
2559
+ this._removed = false;
2560
+ }
2561
+ onValueChanged(oldValue) {
2562
+ let oldQuad = new Quad(this.subject, this.predicate, oldValue, this.graph, this.implicit, false, false);
2563
+ this.mimicEventsOnUpdate(oldQuad);
2564
+ }
2565
+ print() {
2566
+ return (Prefix.toPrefixedIfPossible(this.subject.uri) +
2567
+ ' ' +
2568
+ Prefix.toPrefixedIfPossible(this.predicate.uri) +
2569
+ ' ' +
2570
+ (this.object instanceof NamedNode
2571
+ ? Prefix.toPrefixedIfPossible(this.object.uri)
2572
+ : this.object.toString()));
2573
+ }
2574
+ /**
2575
+ * Print this quad as a string
2576
+ */
2577
+ toString() {
2578
+ return (this.subject.toString() +
2579
+ ' ' +
2580
+ this.predicate.toString() +
2581
+ ' ' +
2582
+ this.object.toString() +
2583
+ ' ' +
2584
+ this.graph.toString() +
2585
+ (this.isRemoved ? ' (removed)' : ''));
2586
+ }
2587
+ setup(alteration = false, emitEvents = true) {
2588
+ // if(this.predicate.uri == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" && this.object['uri'] == "http://data.dacore.org/ontologies/core/Editor")
2589
+ // {
2590
+ // debugger;
2591
+ // }
2592
+ //let nodes take note of this quad in which they occur
2593
+ //first, we overwrite the property this.object with the result of register because a Literal may return a clone
2594
+ this.object = this.object.registerInverseProperty(this, alteration);
2595
+ this.subject.registerProperty(this, alteration);
2596
+ // this.predicate.registerAsPredicate(this, alteration);
2597
+ this._graph.registerQuad(this, alteration);
2598
+ if (emitEvents) {
2599
+ Quad.emitCreatedQuad(this, alteration);
2600
+ }
2601
+ Quad.globalNumQuads++;
2602
+ }
2603
+ mimicEventsOnUpdate(oldQuad) {
2604
+ //manually mimic the fact the old quad was removed and the new quad was added (storage requires this to add/remove those quads to/from the right quad stores)
2605
+ eventBatcher.register(Quad);
2606
+ Quad.removedQuads.add(oldQuad);
2607
+ Quad.removedQuadsAltered.add(oldQuad);
2608
+ Quad.createdQuads.add(this);
2609
+ Quad.createdQuadsAltered.add(this);
2610
+ }
2611
+ }
2612
+ /**
2613
+ * Emitter used by the class itself by static methods emitting events.
2614
+ * Anyone wanting to listen to that should therefore add a listener with Quad.emitter.on(...)
2615
+ * @internal
2616
+ */
2617
+ Quad.emitter = new EventEmitter();
2618
+ /**
2619
+ * The number of quads active in this system
2620
+ */
2621
+ Quad.globalNumQuads = 0;
2622
+ /**
2623
+ * @internal
2624
+ * emitted when new quads have been created
2625
+ * TODO: possibly we can remove this, it may never be used. Only alterations are of interest?
2626
+ */
2627
+ Quad.QUADS_CREATED = 'QUADS_CREATED';
2628
+ /**
2629
+ * @internal
2630
+ * emitted by the Quad class itself when quads have been removed
2631
+ * TODO: possibly we can remove this, it may never be used. Only alterations are of interest?
2632
+ */
2633
+ Quad.QUADS_REMOVED = 'QUADS_REMOVED';
2634
+ /**
2635
+ * emitted by a quad when that quad is being removed
2636
+ * TODO: possibly we can remove this, it may never be used. Only alterations are of interest?
2637
+ */
2638
+ Quad.QUAD_REMOVED = 'QUAD_REMOVED';
2639
+ /**
2640
+ * emitted when quads have been altered by user interaction
2641
+ * @internal
2642
+ */
2643
+ Quad.QUADS_ALTERED = 'QUADS_ALTERED';
2644
+ //TODO: possibly we can remove these first two, they may never be used. Only alterations are of interest?
2645
+ Quad.createdQuads = new QuadSet();
2646
+ Quad.removedQuads = new QuadSet();
2647
+ Quad.removedQuadsAltered = new QuadSet();
2648
+ Quad.createdQuadsAltered = new QuadSet();
2649
+ //for debugging purposes
2650
+ let getNode = function (uri) {
2651
+ return NamedNode.getOrCreate(uri);
2652
+ };
2653
+ if (typeof window !== 'undefined') {
2654
+ window['getNode'] = getNode;
2655
+ }
2656
+ else if (typeof global !== 'undefined') {
2657
+ global['getNode'] = getNode;
2658
+ }
2659
+ //# sourceMappingURL=models.js.map