Skip to content

Commit 78f5acf

Browse files
committed
feat: allow nested graph terms
1 parent 4ef4fc0 commit 78f5acf

2 files changed

Lines changed: 61 additions & 28 deletions

File tree

src/N3Store.js

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { default as N3DataFactory, termToId, termFromId } from './N3DataFactory';
33
import { Readable } from 'readable-stream';
44
import namespaces from './IRIs';
5+
import { isDefaultGraph } from './N3Util';
56

67
// ## Constructor
78
export default class N3Store {
@@ -36,45 +37,33 @@ export default class N3Store {
3637
const q = this._factory.quad(
3738
this._termFromId(entities[terms[1]]),
3839
this._termFromId(entities[terms[2]]),
39-
this._termFromId(entities[terms[3]])
40-
// terms[4] && this._termFromId(entities[terms[4]])
40+
this._termFromId(entities[terms[3]]),
41+
terms[4] && this._termFromId(entities[terms[4]])
4142
);
4243
return q;
4344
}
4445
return termFromId(id, factory);
4546
}
4647

47-
_termToId(term) {
48-
if (term && term.termType === 'Quad') {
49-
// const g = this._termToNewNumericId(term.graph)
50-
const res = `.${
51-
this._termToNewNumericId(term.subject)
52-
}.${
53-
this._termToNewNumericId(term.predicate)
54-
}.${
55-
this._termToNewNumericId(term.object)
56-
}`;
57-
return res;
58-
}
59-
return termToId(term);
60-
}
61-
6248
_termToNumericId(term) {
6349
if (term.termType === 'Quad') {
6450
const s = this._termToNumericId(term.subject),
6551
p = this._termToNumericId(term.predicate),
6652
o = this._termToNumericId(term.object);
53+
let g;
6754

68-
return s && p && o && this._ids[`.${s}.${p}.${o}`];
55+
return s && p && o && (isDefaultGraph(term.graph) || (g = this._termToNumericId(term.graph))) &&
56+
this._ids[g ? `.${s}.${p}.${o}.${g}` : `.${s}.${p}.${o}`];
6957
}
70-
71-
return this._ids[this._termToId(term)];
58+
return this._ids[termToId(term)];
7259
}
7360

7461
_termToNewNumericId(term) {
7562
// This assumes that no graph term is present - we may wish to error if there is one
7663
const str = term && term.termType === 'Quad' ?
77-
`.${this._termToNewNumericId(term.subject)}.${this._termToNewNumericId(term.predicate)}.${this._termToNewNumericId(term.object)}`
64+
`.${this._termToNewNumericId(term.subject)}.${this._termToNewNumericId(term.predicate)}.${this._termToNewNumericId(term.object)}${
65+
isDefaultGraph(term.graph) ? '' : `.${this._termToNewNumericId(term.graph)}`
66+
}`
7867
: termToId(term);
7968

8069
return this._ids[str] || (this._ids[this._entities[++this._id] = str] = this._id);
@@ -264,7 +253,7 @@ export default class N3Store {
264253
predicate = subject.predicate, subject = subject.subject;
265254

266255
// Convert terms to internal string representation
267-
graph = this._termToId(graph);
256+
graph = termToId(graph);
268257

269258
// Find the graph that will contain the triple
270259
let graphItem = this._graphs[graph];
@@ -326,7 +315,7 @@ export default class N3Store {
326315
predicate = subject.predicate, subject = subject.subject;
327316

328317
// Convert terms to internal string representation
329-
graph = this._termToId(graph);
318+
graph = termToId(graph);
330319

331320
// Find internal identifiers for all components
332321
// and verify the quad exists.
@@ -392,7 +381,7 @@ export default class N3Store {
392381
// Setting any field to `undefined` or `null` indicates a wildcard.
393382
*readQuads(subject, predicate, object, graph) {
394383
// Convert terms to internal string representation
395-
graph = graph && this._termToId(graph);
384+
graph = graph && termToId(graph);
396385

397386
const graphs = this._getGraphs(graph);
398387
let content, subjectId, predicateId, objectId;
@@ -447,7 +436,7 @@ export default class N3Store {
447436
// Setting any field to `undefined` or `null` indicates a wildcard.
448437
countQuads(subject, predicate, object, graph) {
449438
// Convert terms to internal string representation
450-
graph = graph && this._termToId(graph);
439+
graph = graph && termToId(graph);
451440

452441
const graphs = this._getGraphs(graph);
453442
let count = 0, content, subjectId, predicateId, objectId;
@@ -526,7 +515,7 @@ export default class N3Store {
526515
// Setting any field to `undefined` or `null` indicates a wildcard.
527516
forSubjects(callback, predicate, object, graph) {
528517
// Convert terms to internal string representation
529-
graph = graph && this._termToId(graph);
518+
graph = graph && termToId(graph);
530519

531520
const graphs = this._getGraphs(graph);
532521
let content, predicateId, objectId;
@@ -571,7 +560,7 @@ export default class N3Store {
571560
// Setting any field to `undefined` or `null` indicates a wildcard.
572561
forPredicates(callback, subject, object, graph) {
573562
// Convert terms to internal string representation
574-
graph = graph && this._termToId(graph);
563+
graph = graph && termToId(graph);
575564

576565
const graphs = this._getGraphs(graph);
577566
let content, subjectId, objectId;
@@ -616,7 +605,7 @@ export default class N3Store {
616605
// Setting any field to `undefined` or `null` indicates a wildcard.
617606
forObjects(callback, subject, predicate, graph) {
618607
// Convert terms to internal string representation
619-
graph = graph && this._termToId(graph);
608+
graph = graph && termToId(graph);
620609

621610
const graphs = this._getGraphs(graph);
622611
let content, subjectId, predicateId;

test/N3Store-test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,50 @@ describe('Store', () => {
340340
});
341341
});
342342

343+
// These tests should probably be broken in the future; they are here to serve to use that we should to a mver bump
344+
// at such a time
345+
describe('A store with quoted quads', () => {
346+
let store;
347+
beforeEach(() => {
348+
store = new Store([
349+
new Quad(
350+
new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o2')),
351+
new NamedNode('p1'),
352+
new NamedNode('o2')),
353+
new Quad(
354+
new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o2'), new NamedNode('g')),
355+
new NamedNode('p1'),
356+
new NamedNode('o2')),
357+
]);
358+
});
359+
360+
it('should have the correct size', () => {
361+
store.size.should.equal(2);
362+
});
363+
364+
it('should get all quads with shared predicate', () => {
365+
store.getQuads(null, new NamedNode('p1'), null).length.should.equal(2);
366+
});
367+
368+
it('should get all quads with shared predicate 2', () => {
369+
store.getQuads(
370+
new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o2')),
371+
new NamedNode('p1'),
372+
null
373+
).length.should.equal(1);
374+
store.getQuads(
375+
new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o2'), new NamedNode('g')),
376+
new NamedNode('p1'),
377+
null
378+
).length.should.equal(1);
379+
store.getQuads(
380+
new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o2'), new NamedNode('g2')),
381+
new NamedNode('p1'),
382+
null
383+
).length.should.equal(0);
384+
});
385+
});
386+
343387
describe('A Store with 7 elements', () => {
344388
const store = new Store();
345389
store.addQuad('s1', 'p1', 'o1').should.be.true;

0 commit comments

Comments
 (0)