n3 1.15.0 → 1.16.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -5
- package/browser/n3.min.js +1 -1
- package/lib/N3Parser.js +17 -15
- package/lib/N3Store.js +45 -88
- package/lib/N3Util.js +1 -1
- package/package.json +1 -1
- package/src/N3Parser.js +17 -11
- package/src/N3Store.js +42 -100
- package/src/N3Util.js +1 -1
package/src/N3Store.js
CHANGED
|
@@ -84,48 +84,34 @@ export default class N3Store {
|
|
|
84
84
|
// `name0`, `name1`, and `name2` are the names of the keys at each level,
|
|
85
85
|
// used when reconstructing the resulting quad
|
|
86
86
|
// (for instance: _subject_, _predicate_, and _object_).
|
|
87
|
-
// Finally, `
|
|
88
|
-
|
|
89
|
-
// and iteration halts when it returns truthy for any quad.
|
|
90
|
-
// If instead `array` is given, each result is added to the array.
|
|
91
|
-
_findInIndex(index0, key0, key1, key2, name0, name1, name2, graph, callback, array) {
|
|
87
|
+
// Finally, `graphId` will be the graph of the created quads.
|
|
88
|
+
*_findInIndex(index0, key0, key1, key2, name0, name1, name2, graphId) {
|
|
92
89
|
let tmp, index1, index2;
|
|
93
|
-
|
|
94
|
-
const
|
|
95
|
-
|
|
90
|
+
const entityKeys = this._entities;
|
|
91
|
+
const graph = termFromId(graphId, this._factory);
|
|
92
|
+
const parts = { subject: null, predicate: null, object: null };
|
|
96
93
|
|
|
97
94
|
// If a key is specified, use only that part of index 0.
|
|
98
95
|
if (key0) (tmp = index0, index0 = {})[key0] = tmp[key0];
|
|
99
96
|
for (const value0 in index0) {
|
|
100
|
-
const entity0 = entityKeys[value0];
|
|
101
|
-
|
|
102
97
|
if (index1 = index0[value0]) {
|
|
98
|
+
parts[name0] = termFromId(entityKeys[value0], this._factory);
|
|
103
99
|
// If a key is specified, use only that part of index 1.
|
|
104
100
|
if (key1) (tmp = index1, index1 = {})[key1] = tmp[key1];
|
|
105
101
|
for (const value1 in index1) {
|
|
106
|
-
const entity1 = entityKeys[value1];
|
|
107
|
-
|
|
108
102
|
if (index2 = index1[value1]) {
|
|
103
|
+
parts[name1] = termFromId(entityKeys[value1], this._factory);
|
|
109
104
|
// If a key is specified, use only that part of index 2, if it exists.
|
|
110
105
|
const values = key2 ? (key2 in index2 ? [key2] : []) : Object.keys(index2);
|
|
111
106
|
// Create quads for all items found in index 2.
|
|
112
107
|
for (let l = 0; l < values.length; l++) {
|
|
113
|
-
const parts = { subject: null, predicate: null, object: null };
|
|
114
|
-
parts[name0] = termFromId(entity0, this._factory);
|
|
115
|
-
parts[name1] = termFromId(entity1, this._factory);
|
|
116
108
|
parts[name2] = termFromId(entityKeys[values[l]], this._factory);
|
|
117
|
-
|
|
118
|
-
parts.subject, parts.predicate, parts.object, termFromId(graph, this._factory));
|
|
119
|
-
if (array)
|
|
120
|
-
array.push(quad);
|
|
121
|
-
else if (callback(quad))
|
|
122
|
-
return true;
|
|
109
|
+
yield this._factory.quad(parts.subject, parts.predicate, parts.object, graph);
|
|
123
110
|
}
|
|
124
111
|
}
|
|
125
112
|
}
|
|
126
113
|
}
|
|
127
114
|
}
|
|
128
|
-
return array;
|
|
129
115
|
}
|
|
130
116
|
|
|
131
117
|
// ### `_loop` executes the callback on all keys of index 0
|
|
@@ -274,11 +260,11 @@ export default class N3Store {
|
|
|
274
260
|
return this;
|
|
275
261
|
}
|
|
276
262
|
|
|
277
|
-
// ### `has` determines whether a dataset includes a certain quad.
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
return
|
|
263
|
+
// ### `has` determines whether a dataset includes a certain quad or quad pattern.
|
|
264
|
+
has(subjectOrQuad, predicate, object, graph) {
|
|
265
|
+
if (subjectOrQuad && subjectOrQuad.subject)
|
|
266
|
+
({ subject: subjectOrQuad, predicate, object, graph } = subjectOrQuad);
|
|
267
|
+
return !this.readQuads(subjectOrQuad, predicate, object, graph).next().done;
|
|
282
268
|
}
|
|
283
269
|
|
|
284
270
|
// ### `import` adds a stream of quads to the store
|
|
@@ -341,7 +327,7 @@ export default class N3Store {
|
|
|
341
327
|
const stream = new Readable({ objectMode: true });
|
|
342
328
|
|
|
343
329
|
stream._read = () => {
|
|
344
|
-
for (const quad of this.
|
|
330
|
+
for (const quad of this.readQuads(subject, predicate, object, graph))
|
|
345
331
|
stream.push(quad);
|
|
346
332
|
stream.push(null);
|
|
347
333
|
};
|
|
@@ -357,20 +343,26 @@ export default class N3Store {
|
|
|
357
343
|
// ### `getQuads` returns an array of quads matching a pattern.
|
|
358
344
|
// Setting any field to `undefined` or `null` indicates a wildcard.
|
|
359
345
|
getQuads(subject, predicate, object, graph) {
|
|
346
|
+
return [...this.readQuads(subject, predicate, object, graph)];
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// ### `readQuads` returns an generator of quads matching a pattern.
|
|
350
|
+
// Setting any field to `undefined` or `null` indicates a wildcard.
|
|
351
|
+
*readQuads(subject, predicate, object, graph) {
|
|
360
352
|
// Convert terms to internal string representation
|
|
361
353
|
subject = subject && termToId(subject);
|
|
362
354
|
predicate = predicate && termToId(predicate);
|
|
363
355
|
object = object && termToId(object);
|
|
364
356
|
graph = graph && termToId(graph);
|
|
365
357
|
|
|
366
|
-
const
|
|
358
|
+
const graphs = this._getGraphs(graph), ids = this._ids;
|
|
367
359
|
let content, subjectId, predicateId, objectId;
|
|
368
360
|
|
|
369
361
|
// Translate IRIs to internal index keys.
|
|
370
362
|
if (isString(subject) && !(subjectId = ids[subject]) ||
|
|
371
363
|
isString(predicate) && !(predicateId = ids[predicate]) ||
|
|
372
364
|
isString(object) && !(objectId = ids[object]))
|
|
373
|
-
return
|
|
365
|
+
return;
|
|
374
366
|
|
|
375
367
|
for (const graphId in graphs) {
|
|
376
368
|
// Only if the specified graph contains triples, there can be results
|
|
@@ -379,28 +371,27 @@ export default class N3Store {
|
|
|
379
371
|
if (subjectId) {
|
|
380
372
|
if (objectId)
|
|
381
373
|
// If subject and object are given, the object index will be the fastest
|
|
382
|
-
this._findInIndex(content.objects, objectId, subjectId, predicateId,
|
|
383
|
-
'object', 'subject', 'predicate', graphId
|
|
374
|
+
yield* this._findInIndex(content.objects, objectId, subjectId, predicateId,
|
|
375
|
+
'object', 'subject', 'predicate', graphId);
|
|
384
376
|
else
|
|
385
377
|
// If only subject and possibly predicate are given, the subject index will be the fastest
|
|
386
|
-
this._findInIndex(content.subjects, subjectId, predicateId, null,
|
|
387
|
-
'subject', 'predicate', 'object', graphId
|
|
378
|
+
yield* this._findInIndex(content.subjects, subjectId, predicateId, null,
|
|
379
|
+
'subject', 'predicate', 'object', graphId);
|
|
388
380
|
}
|
|
389
381
|
else if (predicateId)
|
|
390
382
|
// If only predicate and possibly object are given, the predicate index will be the fastest
|
|
391
|
-
this._findInIndex(content.predicates, predicateId, objectId, null,
|
|
392
|
-
'predicate', 'object', 'subject', graphId
|
|
383
|
+
yield* this._findInIndex(content.predicates, predicateId, objectId, null,
|
|
384
|
+
'predicate', 'object', 'subject', graphId);
|
|
393
385
|
else if (objectId)
|
|
394
386
|
// If only object is given, the object index will be the fastest
|
|
395
|
-
this._findInIndex(content.objects, objectId, null, null,
|
|
396
|
-
'object', 'subject', 'predicate', graphId
|
|
387
|
+
yield* this._findInIndex(content.objects, objectId, null, null,
|
|
388
|
+
'object', 'subject', 'predicate', graphId);
|
|
397
389
|
else
|
|
398
390
|
// If nothing is given, iterate subjects and predicates first
|
|
399
|
-
this._findInIndex(content.subjects, null, null, null,
|
|
400
|
-
'subject', 'predicate', 'object', graphId
|
|
391
|
+
yield* this._findInIndex(content.subjects, null, null, null,
|
|
392
|
+
'subject', 'predicate', 'object', graphId);
|
|
401
393
|
}
|
|
402
394
|
}
|
|
403
|
-
return quads;
|
|
404
395
|
}
|
|
405
396
|
|
|
406
397
|
// ### `match` returns a new dataset that is comprised of all quads in the current instance matching the given arguments.
|
|
@@ -481,60 +472,9 @@ export default class N3Store {
|
|
|
481
472
|
// and returns `true` if it returns truthy for any of them.
|
|
482
473
|
// Setting any field to `undefined` or `null` indicates a wildcard.
|
|
483
474
|
some(callback, subject, predicate, object, graph) {
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
object = object && termToId(object);
|
|
488
|
-
graph = graph && termToId(graph);
|
|
489
|
-
|
|
490
|
-
const graphs = this._getGraphs(graph), ids = this._ids;
|
|
491
|
-
let content, subjectId, predicateId, objectId;
|
|
492
|
-
|
|
493
|
-
// Translate IRIs to internal index keys.
|
|
494
|
-
if (isString(subject) && !(subjectId = ids[subject]) ||
|
|
495
|
-
isString(predicate) && !(predicateId = ids[predicate]) ||
|
|
496
|
-
isString(object) && !(objectId = ids[object]))
|
|
497
|
-
return false;
|
|
498
|
-
|
|
499
|
-
for (const graphId in graphs) {
|
|
500
|
-
// Only if the specified graph contains triples, there can be results
|
|
501
|
-
if (content = graphs[graphId]) {
|
|
502
|
-
// Choose the optimal index, based on what fields are present
|
|
503
|
-
if (subjectId) {
|
|
504
|
-
if (objectId) {
|
|
505
|
-
// If subject and object are given, the object index will be the fastest
|
|
506
|
-
if (this._findInIndex(content.objects, objectId, subjectId, predicateId,
|
|
507
|
-
'object', 'subject', 'predicate', graphId, callback, null))
|
|
508
|
-
return true;
|
|
509
|
-
}
|
|
510
|
-
else
|
|
511
|
-
// If only subject and possibly predicate are given, the subject index will be the fastest
|
|
512
|
-
if (this._findInIndex(content.subjects, subjectId, predicateId, null,
|
|
513
|
-
'subject', 'predicate', 'object', graphId, callback, null))
|
|
514
|
-
return true;
|
|
515
|
-
}
|
|
516
|
-
else if (predicateId) {
|
|
517
|
-
// If only predicate and possibly object are given, the predicate index will be the fastest
|
|
518
|
-
if (this._findInIndex(content.predicates, predicateId, objectId, null,
|
|
519
|
-
'predicate', 'object', 'subject', graphId, callback, null)) {
|
|
520
|
-
return true;
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
else if (objectId) {
|
|
524
|
-
// If only object is given, the object index will be the fastest
|
|
525
|
-
if (this._findInIndex(content.objects, objectId, null, null,
|
|
526
|
-
'object', 'subject', 'predicate', graphId, callback, null)) {
|
|
527
|
-
return true;
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
else
|
|
531
|
-
// If nothing is given, iterate subjects and predicates first
|
|
532
|
-
if (this._findInIndex(content.subjects, null, null, null,
|
|
533
|
-
'subject', 'predicate', 'object', graphId, callback, null)) {
|
|
534
|
-
return true;
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
}
|
|
475
|
+
for (const quad of this.readQuads(subject, predicate, object, graph))
|
|
476
|
+
if (callback(quad))
|
|
477
|
+
return true;
|
|
538
478
|
return false;
|
|
539
479
|
}
|
|
540
480
|
|
|
@@ -820,7 +760,7 @@ export default class N3Store {
|
|
|
820
760
|
// Can be used where iterables are expected: for...of loops, array spread operator,
|
|
821
761
|
// `yield*`, and destructuring assignment (order is not guaranteed).
|
|
822
762
|
*[Symbol.iterator]() {
|
|
823
|
-
yield* this.
|
|
763
|
+
yield* this.readQuads();
|
|
824
764
|
}
|
|
825
765
|
}
|
|
826
766
|
|
|
@@ -841,17 +781,19 @@ class DatasetCoreAndReadableStream extends Readable {
|
|
|
841
781
|
get filtered() {
|
|
842
782
|
if (!this._filtered) {
|
|
843
783
|
const { n3Store, graph, object, predicate, subject } = this;
|
|
844
|
-
const
|
|
845
|
-
|
|
784
|
+
const newStore = this._filtered = new N3Store({ factory: n3Store._factory });
|
|
785
|
+
for (const quad of n3Store.readQuads(subject, predicate, object, graph))
|
|
786
|
+
newStore.addQuad(quad);
|
|
846
787
|
}
|
|
847
788
|
return this._filtered;
|
|
848
789
|
}
|
|
790
|
+
|
|
849
791
|
get size() {
|
|
850
792
|
return this.filtered.size;
|
|
851
793
|
}
|
|
852
794
|
|
|
853
795
|
_read() {
|
|
854
|
-
for (const quad of this
|
|
796
|
+
for (const quad of this)
|
|
855
797
|
this.push(quad);
|
|
856
798
|
this.push(null);
|
|
857
799
|
}
|
|
@@ -873,6 +815,6 @@ class DatasetCoreAndReadableStream extends Readable {
|
|
|
873
815
|
}
|
|
874
816
|
|
|
875
817
|
*[Symbol.iterator]() {
|
|
876
|
-
yield* this.
|
|
818
|
+
yield* this._filtered || this.n3Store.readQuads(this.subject, this.predicate, this.object, this.graph);
|
|
877
819
|
}
|
|
878
820
|
}
|
package/src/N3Util.js
CHANGED
|
@@ -34,7 +34,7 @@ export function inDefaultGraph(quad) {
|
|
|
34
34
|
|
|
35
35
|
// Creates a function that prepends the given IRI to a local name
|
|
36
36
|
export function prefix(iri, factory) {
|
|
37
|
-
return prefixes({ '': iri }, factory)('');
|
|
37
|
+
return prefixes({ '': iri.value || iri }, factory)('');
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
// Creates a function that allows registering and expanding prefixes
|