From c6435ff9831eaf1db129064798371d8aba3774da Mon Sep 17 00:00:00 2001 From: Ruben Verborgh Date: Thu, 12 Apr 2018 00:30:06 +0200 Subject: [PATCH] Make everything quads. --- LICENSE.md | 2 +- README.md | 93 ++++++++++++----------- lib/N3Parser.js | 52 ++++++------- lib/N3Store.js | 104 ++++++++++++------------- lib/N3StreamParser.js | 10 +-- lib/N3StreamWriter.js | 2 +- lib/N3Writer.js | 52 ++++++------- perf/N3Parser-perf.js | 6 +- perf/N3Store-perf.js | 26 +++---- spec/SpecTester.js | 28 +++---- test/N3Store-test.js | 171 +++++++++++++++++++++--------------------- test/N3Writer-test.js | 107 +++++++++++++------------- 12 files changed, 327 insertions(+), 326 deletions(-) diff --git a/LICENSE.md b/LICENSE.md index 94fe52f4..8f1c08b4 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ # License The MIT License (MIT) -Copyright ©2012–2016 Ruben Verborgh +Copyright ©2012–2018 Ruben Verborgh Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/README.md b/README.md index 975b3197..ad22881b 100644 --- a/README.md +++ b/README.md @@ -58,11 +58,14 @@ console.log(myQuad.object.datatype.value); // http://www.w3.org/1999/02/22-rdf-s console.log(myQuad.object.language); // en ``` +In the rest of this document, we will treat “triples” and “quads” equally: +we assume that a quad is simply a triple in a named or default graph. + ## Parsing -### From an RDF document to triples +### From an RDF document to quads -`N3.Parser` transforms Turtle, TriG, N-Triples, or N-Quads document into triples through a callback: +`N3.Parser` transforms Turtle, TriG, N-Triples, or N-Quads document into quads through a callback: ```JavaScript const parser = new N3.Parser(); parser.parse( @@ -77,9 +80,9 @@ parser.parse( console.log("# That's all, folks!", prefixes); }); ``` -The callback's first argument is an error value, the second is a triple. -If there are no more triples, -the callback is invoked one last time with `null` for `triple` +The callback's first argument is an optional error value, the second is a quad. +If there are no more quads, +the callback is invoked one last time with `null` for `quad` and a hash of prefixes as third argument.
Pass a second callback to `parse` to retrieve prefixes as they are read. @@ -103,10 +106,10 @@ const parser4 = N3.Parser({ format: 'Notation3' }); const parser5 = N3.Parser({ format: 'text/n3' }); ``` -### From an RDF stream to triples +### From an RDF stream to quads `N3.Parser` can parse [Node.js streams](http://nodejs.org/api/stream.html) as they grow, -returning triples as soon as they're ready. +returning quads as soon as they're ready. ```JavaScript const parser = N3.Parser(), @@ -126,8 +129,8 @@ streamParser.pipe(new SlowConsumer()); function SlowConsumer() { const writer = new require('stream').Writable({ objectMode: true }); - writer._write = (triple, encoding, done) => { - console.log(triple); + writer._write = (quad, encoding, done) => { + console.log(quad); setTimeout(done, 1000); }; return writer; @@ -138,19 +141,19 @@ A dedicated `prefix` event signals every prefix with `prefix` and `term` argumen ## Writing -### From triples to a string +### From quads to a string -`N3.Writer` serializes triples as an RDF document. -Write triples through `addTriple`. +`N3.Writer` serializes quads as an RDF document. +Write quads through `addQuad`. ```JavaScript const writer = N3.Writer({ prefixes: { c: 'http://example.org/cartoons#' } }); -writer.addTriple( +writer.addQuad( namedNode('http://example.org/cartoons#Tom'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), namedNode('http://example.org/cartoons#Cat') ); -writer.addTriple(quad( +writer.addQuad(quad( namedNode('http://example.org/cartoons#Tom'), namedNode('http://example.org/cartoons#name'), literal('Tom') @@ -158,7 +161,7 @@ writer.addTriple(quad( writer.end((error, result) => console.log(result)); ``` -By default, `N3.Writer` writes Turtle (or TriG for triples with a `graph` property). +By default, `N3.Writer` writes Turtle (or TriG if some quads are in a named graph).
To write N-Triples (or N-Quads) instead, pass a `format` argument upon creation: @@ -167,18 +170,18 @@ const writer1 = N3.Writer({ format: 'N-Triples' }); const writer2 = N3.Writer({ format: 'application/trig' }); ``` -### From triples to an RDF stream +### From quads to an RDF stream -`N3.Writer` can also write triples to a Node.js stream. +`N3.Writer` can also write quads to a Node.js stream. ```JavaScript const writer = N3.Writer(process.stdout, { end: false, prefixes: { c: 'http://example.org/cartoons#' } }); -writer.addTriple( +writer.addQuad( namedNode('http://example.org/cartoons#Tom'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), namedNode('http://example.org/cartoons#Cat') ); -writer.addTriple(quad( +writer.addQuad(quad( namedNode('http://example.org/cartoons#Tom'), namedNode('http://example.org/cartoons#name'), literal('Tom') @@ -186,7 +189,7 @@ writer.addTriple(quad( writer.end(); ``` -### From a triple stream to an RDF stream +### From a quad stream to an RDF stream `N3.StreamWriter` is a [Node.js stream](http://nodejs.org/api/stream.html) and [RDF.js Sink](http://rdf.js.org/#sink-interface) implementation. @@ -209,14 +212,14 @@ The `blank` and `list` functions allow you to create them manually instead: ```JavaScript const writer = N3.Writer({ prefixes: { c: 'http://example.org/cartoons#', foaf: 'http://xmlns.com/foaf/0.1/' } }); -writer.addTriple( +writer.addQuad( writer.blank( namedNode('http://xmlns.com/foaf/0.1/givenName'), literal('Tom', 'en')), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), namedNode('http://example.org/cartoons#Cat') ); -writer.addTriple(quad( +writer.addQuad(quad( namedNode('http://example.org/cartoons#Jerry'), namedNode('http://xmlns.com/foaf/0.1/knows'), writer.blank([{ @@ -227,7 +230,7 @@ writer.addTriple(quad( object: literal('Tom', 'en'), }]) )); -writer.addTriple( +writer.addQuad( namedNode('http://example.org/cartoons#Mammy'), namedNode('http://example.org/cartoons#hasPets'), writer.list([ @@ -248,46 +251,46 @@ Then, we find triples with `:Mickey` as subject. ```JavaScript const store = N3.Store(); -store.addTriple( +store.addQuad( namedNode('http://ex.org/Pluto'), namedNode('http://ex.org/type'), namedNode('http://ex.org/Dog') ); -store.addTriple( +store.addQuad( namedNode('http://ex.org/Mickey'), namedNode('http://ex.org/type'), namedNode('http://ex.org/Mouse') ); -const mickey = store.getTriples(namedNode('http://ex.org/Mickey'), null, null)[0]; +const mickey = store.getQuads(namedNode('http://ex.org/Mickey'), null, null)[0]; console.log(mickey); ``` -### Addition and deletion of triples/quads +### Addition and deletion of quads The store provides the following manipulation methods ([documentation](http://rubenverborgh.github.io/N3.js/docs/N3Store.html)): -- `addTriple` to insert one triple/quad -- `addTriples` to insert an array of triples/quads -- `removeTriple` to remove one triple/quad -- `removeTriples` to remove an array of triples/quads +- `addQuad` to insert one quad +- `addQuads` to insert an array of quads +- `removeQuad` to remove one quad +- `removeQuads` to remove an array of quads - `createBlankNode` returns an unused blank node identifier -### Searching triples/quads or entities +### Searching quads or entities The store provides the following search methods ([documentation](http://rubenverborgh.github.io/N3.js/docs/N3Store.html)): -- `getTriples` returns an array of triples/quads matching the given pattern -- `countTriples` counts the number of triples/quads matching the given pattern -- `forEach` executes a callback on all matching triples/quads -- `every` returns whether a callback on matching triples/quads always returns true -- `some` returns whether a callback on matching triples/quads returns true at least once -- `getSubjects` returns an array of unique subjects occurring in matching triples -- `forSubjects` executes a callback on unique subjects occurring in matching triples -- `getPredicates` returns an array of unique predicates occurring in matching triple -- `forPredicates` executes a callback on unique predicates occurring in matching triples -- `getObjects` returns an array of unique objects occurring in matching triple -- `forObjects` executes a callback on unique objects occurring in matching triples -- `getGraphs` returns an array of unique graphs occurring in matching triple -- `forGraphs` executes a callback on unique graphs occurring in matching triples +- `getQuads` returns an array of quads matching the given pattern +- `countQuads` counts the number of quads matching the given pattern +- `forEach` executes a callback on all matching quads +- `every` returns whether a callback on matching quads always returns true +- `some` returns whether a callback on matching quads returns true at least once +- `getSubjects` returns an array of unique subjects occurring in matching quads +- `forSubjects` executes a callback on unique subjects occurring in matching quads +- `getPredicates` returns an array of unique predicates occurring in matching quad +- `forPredicates` executes a callback on unique predicates occurring in matching quads +- `getObjects` returns an array of unique objects occurring in matching quad +- `forObjects` executes a callback on unique objects occurring in matching quads +- `getGraphs` returns an array of unique graphs occurring in matching quad +- `forGraphs` executes a callback on unique graphs occurring in matching quads ## Compatibility ### Format specifications diff --git a/lib/N3Parser.js b/lib/N3Parser.js index 25b31179..a3c78f4a 100644 --- a/lib/N3Parser.js +++ b/lib/N3Parser.js @@ -210,12 +210,12 @@ N3Parser.prototype = { return value; }, - // ### `_readSubject` reads a triple's subject + // ### `_readSubject` reads a quad's subject _readSubject: function (token) { this._predicate = null; switch (token.type) { case '[': - // Start a new triple with a new blank node as subject + // Start a new quad with a new blank node as subject this._saveContext('blank', this._graph, this._subject = this._blankNode(), null, null); return this._readBlankNodeHead; @@ -262,7 +262,7 @@ N3Parser.prototype = { return this._readPredicateOrNamedGraph; }, - // ### `_readPredicate` reads a triple's predicate + // ### `_readPredicate` reads a quad's predicate _readPredicate: function (token) { var type = token.type; switch (type) { @@ -294,14 +294,14 @@ N3Parser.prototype = { return this._readObject; }, - // ### `_readObject` reads a triple's object + // ### `_readObject` reads a quad's object _readObject: function (token) { switch (token.type) { case 'literal': this._object = new Literal(token.value); return this._readDataTypeOrLang; case '[': - // Start a new triple with a new blank node as subject + // Start a new quad with a new blank node as subject this._saveContext('blank', this._graph, this._subject, this._predicate, this._subject = this._blankNode()); return this._readBlankNodeHead; @@ -328,7 +328,7 @@ N3Parser.prototype = { return this._getContextEndReader(); }, - // ### `_readPredicateOrNamedGraph` reads a triple's predicate, or a named graph + // ### `_readPredicateOrNamedGraph` reads a quad's predicate, or a named graph _readPredicateOrNamedGraph: function (token) { return token.type === '{' ? this._readGraph(token) : this._readPredicate(token); }, @@ -359,7 +359,7 @@ N3Parser.prototype = { if (token.type !== ']') return this._readBlankNodePunctuation(token); - // Store blank node triple + // Store blank node quad if (this._subject !== null) this._quad(this._subject, this._predicate, this._object, this._graph); @@ -379,7 +379,7 @@ N3Parser.prototype = { _readPredicateAfterBlank: function (token) { // If a dot follows a blank node in top context, there is no predicate if (token.type === '.' && !this._contextStack.length) { - this._subject = null; // cancel the current triple + this._subject = null; // cancel the current quad return this._readPunctuation(token); } return this._readPredicate(token); @@ -397,14 +397,14 @@ N3Parser.prototype = { switch (token.type) { case '[': - // Stack the current list triple and start a new triple with a blank node as subject + // Stack the current list quad and start a new quad with a blank node as subject this._saveContext('blank', this._graph, list = this._blankNode(), RDF_FIRST, this._subject = item = this._blankNode()); next = this._readBlankNodeHead; break; case '(': - // Stack the current list triple and start a new list + // Stack the current list quad and start a new list this._saveContext('list', this._graph, list = this._blankNode(), RDF_FIRST, RDF_NIL); this._subject = null; @@ -412,7 +412,7 @@ N3Parser.prototype = { case ')': // Closing the list; restore the parent context this._restoreContext(); - // If this list is contained within a parent list, return the membership triple here. + // If this list is contained within a parent list, return the membership quad here. // This will be ` rdf:first .`. if (stack.length !== 0 && stack[stack.length - 1].type === 'list') this._quad(this._subject, this._predicate, this._object, this._graph); @@ -527,7 +527,7 @@ N3Parser.prototype = { if (token.type !== '}') return this._readPunctuation(token); - // Store the last triple of the formula + // Store the last quad of the formula if (this._subject !== null) this._quad(this._subject, this._predicate, this._object, this._graph); @@ -538,7 +538,7 @@ N3Parser.prototype = { return this._object === null ? this._readPredicate : this._getContextEndReader(); }, - // ### `_readPunctuation` reads punctuation between triples or triple parts + // ### `_readPunctuation` reads punctuation between quads or quad parts _readPunctuation: function (token) { var next, subject = this._subject, graph = this._graph, inversePredicate = this._inversePredicate; @@ -572,7 +572,7 @@ N3Parser.prototype = { } return this._error('Expected punctuation to follow "' + this._object.id + '"', token); } - // A triple has been completed now, so return it + // A quad has been completed now, so return it if (subject !== null) { var predicate = this._predicate, object = this._object; if (!inversePredicate) @@ -598,7 +598,7 @@ N3Parser.prototype = { default: return this._error('Expected punctuation to follow "' + this._object.id + '"', token); } - // A triple has been completed now, so return it + // A quad has been completed now, so return it this._quad(this._subject, this._predicate, this._object, this._graph); return next; }, @@ -761,7 +761,7 @@ N3Parser.prototype = { // If we were reading an object, replace the subject by the path's object else subject = this._object, this._object = object; - // Emit the path's current triple and read its next section + // Emit the path's current quad and read its next section this._quad(subject, predicate, object, this._graph); return this._readPath; }, @@ -778,7 +778,7 @@ N3Parser.prototype = { // If we were reading an object, replace the subject by the path's subject else object = this._object, this._object = subject; - // Emit the path's current triple and read its next section + // Emit the path's current quad and read its next section this._quad(subject, predicate, object, this._graph); return this._readPath; }, @@ -799,7 +799,7 @@ N3Parser.prototype = { } }, - // ### `_quad` emits a triple through the callback + // ### `_quad` emits a quad through the callback _quad: function (subject, predicate, object, graph) { this._callback(null, new Quad(subject, predicate, object, graph || DEFAULTGRAPH)); @@ -902,8 +902,8 @@ N3Parser.prototype = { // ## Public methods - // ### `parse` parses the N3 input and emits each parsed triple through the callback - parse: function (input, tripleCallback, prefixCallback) { + // ### `parse` parses the N3 input and emits each parsed quad through the callback + parse: function (input, quadCallback, prefixCallback) { var self = this; // The read callback is the next function to be executed when a token arrives. // We start reading in the top context. @@ -916,19 +916,19 @@ N3Parser.prototype = { this._inversePredicate = false; this._quantified = Object.create(null); - // Parse synchronously if no triple callback is given - if (!tripleCallback) { - var triples = [], error; - this._callback = function (e, t) { e ? (error = e) : t && triples.push(t); }; + // Parse synchronously if no quad callback is given + if (!quadCallback) { + var quads = [], error; + this._callback = function (e, t) { e ? (error = e) : t && quads.push(t); }; this._lexer.tokenize(input).every(function (token) { return self._readCallback = self._readCallback(token); }); if (error) throw error; - return triples; + return quads; } // Parse asynchronously otherwise, executing the read callback when a token arrives - this._callback = tripleCallback; + this._callback = quadCallback; this._lexer.tokenize(input, function (error, token) { if (error !== null) self._callback(error), self._callback = noop; diff --git a/lib/N3Store.js b/lib/N3Store.js index e5e2a258..787bf3b0 100644 --- a/lib/N3Store.js +++ b/lib/N3Store.js @@ -1,4 +1,4 @@ -// **N3Store** objects store N3 triples by graph in memory. +// **N3Store** objects store N3 quads by graph in memory. var DataFactory = require('./N3DataFactory'); var Term = DataFactory.Term, @@ -6,11 +6,11 @@ var Term = DataFactory.Term, Quad = DataFactory.Quad; // ## Constructor -function N3Store(triples, options) { +function N3Store(quads, options) { if (!(this instanceof N3Store)) - return new N3Store(triples, options); + return new N3Store(quads, options); - // The number of triples is initially zero + // The number of quads is initially zero this._size = 0; // `_graphs` contains subject, predicate, and object indexes per graph this._graphs = Object.create(null); @@ -23,27 +23,27 @@ function N3Store(triples, options) { // `_blankNodeIndex` is the index of the last automatically named blank node this._blankNodeIndex = 0; - // Shift parameters if `triples` is not given - if (!options && triples && !triples[0]) - options = triples, triples = null; + // Shift parameters if `quads` is not given + if (!options && quads && !quads[0]) + options = quads, quads = null; options = options || {}; - // Add triples if passed - if (triples) - this.addTriples(triples); + // Add quads if passed + if (quads) + this.addQuads(quads); } N3Store.prototype = { // ## Public properties - // ### `size` returns the number of triples in the store + // ### `size` returns the number of quads in the store get size() { - // Return the triple count if if was cached + // Return the quad count if if was cached var size = this._size; if (size !== null) return size; - // Calculate the number of triples by counting to the deepest level + // Calculate the number of quads by counting to the deepest level size = 0; var graphs = this._graphs, subjects, subject; for (var graphKey in graphs) @@ -55,22 +55,22 @@ N3Store.prototype = { // ## Private methods - // ### `_addToIndex` adds a triple to a three-layered index. + // ### `_addToIndex` adds a quad to a three-layered index. // Returns if the index has changed, if the entry did not already exist. _addToIndex: function (index0, key0, key1, key2) { // Create layers as necessary var index1 = index0[key0] || (index0[key0] = {}); var index2 = index1[key1] || (index1[key1] = {}); - // Setting the key to _any_ value signals the presence of the triple + // Setting the key to _any_ value signals the presence of the quad var existed = key2 in index2; if (!existed) index2[key2] = null; return !existed; }, - // ### `_removeFromIndex` removes a triple from a three-layered index + // ### `_removeFromIndex` removes a quad from a three-layered index _removeFromIndex: function (index0, key0, key1, key2) { - // Remove the triple from the index + // Remove the quad from the index var index1 = index0[key0], index2 = index1[key1], key; delete index2[key2]; @@ -81,15 +81,15 @@ N3Store.prototype = { delete index0[key0]; }, - // ### `_findInIndex` finds a set of triples in a three-layered index. + // ### `_findInIndex` finds a set of quads in a three-layered index. // The index base is `index0` and the keys at each level are `key0`, `key1`, and `key2`. // Any of these keys can be undefined, which is interpreted as a wildcard. // `name0`, `name1`, and `name2` are the names of the keys at each level, - // used when reconstructing the resulting triple + // used when reconstructing the resulting quad // (for instance: _subject_, _predicate_, and _object_). - // Finally, `graph` will be the graph of the created triples. + // Finally, `graph` will be the graph of the created quads. // If `callback` is given, each result is passed through it - // and iteration halts when it returns truthy for any triple. + // and iteration halts when it returns truthy for any quad. // If instead `array` is given, each result is added to the array. _findInIndex: function (index0, key0, key1, key2, name0, name1, name2, graph, callback, array) { var tmp, index1, index2, varCount = !key0 + !key1 + !key2, @@ -110,7 +110,7 @@ N3Store.prototype = { if (index2 = index1[value1]) { // If a key is specified, use only that part of index 2, if it exists. var values = key2 ? (key2 in index2 ? [key2] : []) : Object.keys(index2); - // Create triples for all items found in index 2. + // Create quads for all items found in index 2. for (var l = values.length - 1; l >= 0; l--) { var result = new Quad(null, null, null, Term.fromId(graph)); result[name0] = Term.fromId(entity0); @@ -162,7 +162,7 @@ N3Store.prototype = { } }, - // ### `_countInIndex` counts matching triples in a three-layered index. + // ### `_countInIndex` counts matching quads in a three-layered index. // The index base is `index0` and the keys at each level are `key0`, `key1`, and `key2`. // Any of these keys can be undefined, which is interpreted as a wildcard. _countInIndex: function (index0, key0, key1, key2) { @@ -176,9 +176,9 @@ N3Store.prototype = { if (key1) (tmp = index1, index1 = {})[key1] = tmp[key1]; for (var value1 in index1) { if (index2 = index1[value1]) { - // If a key is specified, count the triple if it exists + // If a key is specified, count the quad if it exists if (key2) (key2 in index2) && count++; - // Otherwise, count all triples + // Otherwise, count all quads else count += Object.keys(index2).length; } } @@ -211,10 +211,10 @@ N3Store.prototype = { // ## Public methods - // ### `addTriple` adds a new triple to the store. - // Returns if the triple index has changed, if the triple did not already exist. - addTriple: function (subject, predicate, object, graph) { - // Shift arguments if a triple object is given instead of components + // ### `addQuad` adds a new quad to the store. + // Returns if the quad index has changed, if the quad did not already exist. + addQuad: function (subject, predicate, object, graph) { + // Shift arguments if a quad object is given instead of components if (!predicate) graph = subject.graph, object = subject.object, predicate = subject.predicate, subject = subject.subject; @@ -248,27 +248,27 @@ N3Store.prototype = { this._addToIndex(graphItem.predicates, predicate, object, subject); this._addToIndex(graphItem.objects, object, subject, predicate); - // The cached triple count is now invalid + // The cached quad count is now invalid this._size = null; return changed; }, - // ### `addTriples` adds multiple triples to the store - addTriples: function (triples) { - for (var i = triples.length - 1; i >= 0; i--) - this.addTriple(triples[i]); + // ### `addQuads` adds multiple quads to the store + addQuads: function (quads) { + for (var i = quads.length - 1; i >= 0; i--) + this.addQuad(quads[i]); }, // ### `import` adds a stream of quads to the store import: function (stream) { var self = this; - stream.on('data', function (quad) { self.addTriple(quad); }); + stream.on('data', function (quad) { self.addQuad(quad); }); return stream; }, - // ### `removeTriple` removes a triple from the store if it exists - removeTriple: function (subject, predicate, object, graph) { - // Shift arguments if a triple object is given instead of components + // ### `removeQuad` removes a quad from the store if it exists + removeQuad: function (subject, predicate, object, graph) { + // Shift arguments if a quad object is given instead of components if (!predicate) graph = subject.graph, object = subject.object, predicate = subject.predicate, subject = subject.subject; @@ -280,7 +280,7 @@ N3Store.prototype = { graph = Term.toId(graph); // Find internal identifiers for all components - // and verify the triple exists. + // and verify the quad exists. var graphItem, ids = this._ids, graphs = this._graphs, subjects, predicates; if (!(subject = ids[subject]) || !(predicate = ids[predicate]) || !(object = ids[object]) || !(graphItem = graphs[graph]) || @@ -301,22 +301,22 @@ N3Store.prototype = { return true; }, - // ### `removeTriples` removes multiple triples from the store - removeTriples: function (triples) { - for (var i = triples.length - 1; i >= 0; i--) - this.removeTriple(triples[i]); + // ### `removeQuads` removes multiple quads from the store + removeQuads: function (quads) { + for (var i = quads.length - 1; i >= 0; i--) + this.removeQuad(quads[i]); }, // ### `remove` removes a stream of quads from the store remove: function (stream) { var self = this; - stream.on('data', function (quad) { self.removeTriple(quad); }); + stream.on('data', function (quad) { self.removeQuad(quad); }); return stream; }, - // ### `getTriples` returns an array of triples matching a pattern. + // ### `getQuads` returns an array of quads matching a pattern. // Setting any field to `undefined` or `null` indicates a wildcard. - getTriples: function (subject, predicate, object, graph) { + getQuads: function (subject, predicate, object, graph) { // Convert terms to internal string representation subject = subject && Term.toId(subject); predicate = predicate && Term.toId(predicate); @@ -363,9 +363,9 @@ N3Store.prototype = { return quads; }, - // ### `countTriples` returns the number of triples matching a pattern. + // ### `countQuads` returns the number of quads matching a pattern. // Setting any field to `undefined` or `null` indicates a wildcard. - countTriples: function (subject, predicate, object, graph) { + countQuads: function (subject, predicate, object, graph) { // Convert terms to internal string representation subject = subject && Term.toId(subject); predicate = predicate && Term.toId(predicate); @@ -406,7 +406,7 @@ N3Store.prototype = { return count; }, - // ### `forEach` executes the callback on all triples. + // ### `forEach` executes the callback on all quads. // Setting any field to `undefined` or `null` indicates a wildcard. forEach: function (callback, subject, predicate, object, graph) { this.some(function (quad) { @@ -415,7 +415,7 @@ N3Store.prototype = { }, subject, predicate, object, graph); }, - // ### `every` executes the callback on all triples, + // ### `every` executes the callback on all quads, // and returns `true` if it returns truthy for all them. // Setting any field to `undefined` or `null` indicates a wildcard. every: function (callback, subject, predicate, object, graph) { @@ -427,7 +427,7 @@ N3Store.prototype = { return some && every; }, - // ### `some` executes the callback on all triples, + // ### `some` executes the callback on all quads, // and returns `true` if it returns truthy for any of them. // Setting any field to `undefined` or `null` indicates a wildcard. some: function (callback, subject, predicate, object, graph) { @@ -447,7 +447,7 @@ N3Store.prototype = { return false; for (var graphId in graphs) { - // Only if the specified graph contains triples, there can be result + // Only if the specified graph contains triples, there can be results if (content = graphs[graphId]) { // Choose the optimal index, based on what fields are present if (subjectId) { diff --git a/lib/N3StreamParser.js b/lib/N3StreamParser.js index ea4abdd0..9efe78d7 100644 --- a/lib/N3StreamParser.js +++ b/lib/N3StreamParser.js @@ -16,15 +16,15 @@ function N3StreamParser(options) { var self = this, parser = new N3Parser(options), onData, onEnd; // Pass dummy stream to obtain `data` and `end` callbacks parser.parse({ - on: function (event, cb) { + on: function (event, callback) { switch (event) { - case 'data': onData = cb; break; - case 'end': onEnd = cb; break; + case 'data': onData = callback; break; + case 'end': onEnd = callback; break; } }, }, - // Handle triples by pushing them down the pipeline - function (error, t) { error && self.emit('error', error) || t && self.push(t); }, + // Handle quads by pushing them down the pipeline + function (error, quad) { error && self.emit('error', error) || quad && self.push(quad); }, // Emit prefixes through the `prefix` event function (prefix, uri) { self.emit('prefix', prefix, uri); }); diff --git a/lib/N3StreamWriter.js b/lib/N3StreamWriter.js index 1c11720d..372cd6f0 100644 --- a/lib/N3StreamWriter.js +++ b/lib/N3StreamWriter.js @@ -20,7 +20,7 @@ function N3StreamWriter(options) { }, options); // Implement Transform methods on top of writer - this._transform = function (triple, encoding, done) { writer.addTriple(triple, done); }; + this._transform = function (quad, encoding, done) { writer.addQuad(quad, done); }; this._flush = function (done) { writer.end(done); }; } util.inherits(N3StreamWriter, Transform); diff --git a/lib/N3Writer.js b/lib/N3Writer.js index cc51e6be..95340719 100644 --- a/lib/N3Writer.js +++ b/lib/N3Writer.js @@ -49,7 +49,7 @@ function N3Writer(outputStream, options) { options.prefixes && this.addPrefixes(options.prefixes); } else { - this._writeTriple = this._writeTripleLine; + this._writeQuad = this._writeQuadLine; } } @@ -66,8 +66,8 @@ N3Writer.prototype = { this._outputStream.write(string, 'utf8', callback); }, - // ### `_writeTriple` writes the triple to the output stream - _writeTriple: function (subject, predicate, object, graph, done) { + // ### `_writeQuad` writes the quad to the output stream + _writeQuad: function (subject, predicate, object, graph, done) { try { // Write the graph's label if it has changed if (!graph.equals(this._graph)) { @@ -88,7 +88,7 @@ N3Writer.prototype = { this._encodePredicate(this._predicate = predicate) + ' ' + this._encodeObject(object), done); } - // Different subject; write the whole triple + // Different subject; write the whole quad else this._write((this._subject === null ? '' : '.\n') + this._encodeIriOrBlank(this._subject = subject) + ' ' + @@ -98,25 +98,25 @@ N3Writer.prototype = { catch (error) { done && done(error); } }, - // ### `_writeTripleLine` writes the triple or quad to the output stream as a single line - _writeTripleLine: function (subject, predicate, object, graph, done) { - // Write the triple without prefixes + // ### `_writeQuadLine` writes the quad to the output stream as a single line + _writeQuadLine: function (subject, predicate, object, graph, done) { + // Write the quad without prefixes delete this._prefixMatch; - this._write(this.tripleToString(subject, predicate, object, graph), done); + this._write(this.quadToString(subject, predicate, object, graph), done); }, - // ### `tripleToString` serializes a triple or quad as a string - tripleToString: function (subject, predicate, object, graph) { + // ### `quadToString` serializes a quad as a string + quadToString: function (subject, predicate, object, graph) { return this._encodeIriOrBlank(subject) + ' ' + this._encodeIriOrBlank(predicate) + ' ' + this._encodeObject(object) + (graph && graph.value ? ' ' + this._encodeIriOrBlank(graph) + '.\n' : '.\n'); }, - // ### `triplesToString` serializes an array of triples or quads as a string - triplesToString: function (triples) { - return triples.map(function (t) { - return this.tripleToString(t.subject, t.predicate, t.object, t.graph); + // ### `quadsToString` serializes an array of quads as a string + quadsToString: function (quads) { + return quads.map(function (t) { + return this.quadToString(t.subject, t.predicate, t.object, t.graph); }, this).join(''); }, @@ -165,23 +165,23 @@ N3Writer.prototype = { throw new Error('Cannot write because the writer has been closed.'); }, - // ### `addTriple` adds the triple to the output stream - addTriple: function (subject, predicate, object, graph, done) { - // The triple was given as a triple object, so shift parameters + // ### `addQuad` adds the quad to the output stream + addQuad: function (subject, predicate, object, graph, done) { + // The quad was given as an object, so shift parameters if (object === undefined) - this._writeTriple(subject.subject, subject.predicate, subject.object, subject.graph, predicate); + this._writeQuad(subject.subject, subject.predicate, subject.object, subject.graph, predicate); // The optional `graph` parameter was not provided else if (typeof graph === 'function') - this._writeTriple(subject, predicate, object, DEFAULTGRAPH, graph); + this._writeQuad(subject, predicate, object, DEFAULTGRAPH, graph); // The `graph` parameter was provided else - this._writeTriple(subject, predicate, object, graph || DEFAULTGRAPH, done); + this._writeQuad(subject, predicate, object, graph || DEFAULTGRAPH, done); }, - // ### `addTriples` adds the triples to the output stream - addTriples: function (triples) { - for (var i = 0; i < triples.length; i++) - this.addTriple(triples[i]); + // ### `addQuads` adds the quads to the output stream + addQuads: function (quads) { + for (var i = 0; i < quads.length; i++) + this.addQuad(quads[i]); }, // ### `addPrefix` adds the prefix to the output stream @@ -203,7 +203,7 @@ N3Writer.prototype = { if (/[#\/]$/.test(iri) && prefixIRIs[iri] !== (prefix += ':')) { hasPrefixes = true; prefixIRIs[iri] = prefix; - // Finish a possible pending triple + // Finish a possible pending quad if (this._subject !== null) { this._write(this._inDefaultGraph ? '.\n' : '\n}\n'); this._subject = null, this._graph = ''; @@ -284,7 +284,7 @@ N3Writer.prototype = { // ### `end` signals the end of the output stream end: function (done) { - // Finish a possible pending triple + // Finish a possible pending quad if (this._subject !== null) { this._write(this._inDefaultGraph ? '.\n' : '\n}\n'); this._subject = null; diff --git a/perf/N3Parser-perf.js b/perf/N3Parser-perf.js index 938592ad..29f7245c 100755 --- a/perf/N3Parser-perf.js +++ b/perf/N3Parser-perf.js @@ -14,13 +14,13 @@ var TEST = '- Parsing file ' + filename; console.time(TEST); var count = 0; -new N3.Parser({ documentIRI: base }).parse(fs.createReadStream(filename), function (error, triple) { +new N3.Parser({ documentIRI: base }).parse(fs.createReadStream(filename), function (error, quad) { assert(!error, error); - if (triple) + if (quad) count++; else { console.timeEnd(TEST); - console.log('* Triples parsed: ' + count); + console.log('* Quads parsed: ' + count); console.log('* Memory usage: ' + Math.round(process.memoryUsage().rss / 1024 / 1024) + 'MB'); } }); diff --git a/perf/N3Store-perf.js b/perf/N3Store-perf.js index 2856fc5d..e2fd527b 100755 --- a/perf/N3Store-perf.js +++ b/perf/N3Store-perf.js @@ -19,7 +19,7 @@ var i, j, k, l; for (i = 0; i < dim; i++) for (j = 0; j < dim; j++) for (k = 0; k < dim; k++) - store.addTriple(prefix + i, prefix + j, prefix + k); + store.addQuad(prefix + i, prefix + j, prefix + k); console.timeEnd(TEST); console.log('* Memory usage for triples: ' + Math.round(process.memoryUsage().rss / 1024 / 1024) + 'MB'); @@ -29,30 +29,30 @@ console.time(TEST); for (i = 0; i < dim; i++) for (j = 0; j < dim; j++) for (k = 0; k < dim; k++) - assert.equal(store.getTriples(prefix + i, prefix + j, prefix + k, '').length, 1); + assert.equal(store.getQuads(prefix + i, prefix + j, prefix + k, '').length, 1); console.timeEnd(TEST); TEST = '- Finding all ' + dimCubed + ' triples in the default graph ' + dimSquared * 2 + ' times (1 variable)'; console.time(TEST); for (i = 0; i < dim; i++) for (j = 0; j < dim; j++) - assert.equal(store.getTriples(prefix + i, prefix + j, null, '').length, dim); + assert.equal(store.getQuads(prefix + i, prefix + j, null, '').length, dim); for (i = 0; i < dim; i++) for (j = 0; j < dim; j++) - assert.equal(store.getTriples(prefix + i, null, prefix + j, '').length, dim); + assert.equal(store.getQuads(prefix + i, null, prefix + j, '').length, dim); for (i = 0; i < dim; i++) for (j = 0; j < dim; j++) - assert.equal(store.getTriples(null, prefix + i, prefix + j, '').length, dim); + assert.equal(store.getQuads(null, prefix + i, prefix + j, '').length, dim); console.timeEnd(TEST); TEST = '- Finding all ' + dimCubed + ' triples in the default graph ' + dimSquared * 3 + ' times (2 variables)'; console.time(TEST); for (i = 0; i < dim; i++) - assert.equal(store.getTriples(prefix + i, null, null, '').length, dimSquared); + assert.equal(store.getQuads(prefix + i, null, null, '').length, dimSquared); for (j = 0; j < dim; j++) - assert.equal(store.getTriples(null, prefix + j, null, '').length, dimSquared); + assert.equal(store.getQuads(null, prefix + j, null, '').length, dimSquared); for (k = 0; k < dim; k++) - assert.equal(store.getTriples(null, null, prefix + k, '').length, dimSquared); + assert.equal(store.getQuads(null, null, prefix + k, '').length, dimSquared); console.timeEnd(TEST); console.log(); @@ -70,7 +70,7 @@ for (i = 0; i < dim; i++) for (j = 0; j < dim; j++) for (k = 0; k < dim; k++) for (l = 0; l < dim; l++) - store.addTriple(prefix + i, prefix + j, prefix + k, prefix + l); + store.addQuad(prefix + i, prefix + j, prefix + k, prefix + l); console.timeEnd(TEST); console.log('* Memory usage for quads: ' + Math.round(process.memoryUsage().rss / 1024 / 1024) + 'MB'); @@ -78,11 +78,11 @@ console.log('* Memory usage for quads: ' + Math.round(process.memoryUsage().rss TEST = '- Finding all ' + dimQuads + ' quads ' + dimCubed * 4 + ' times'; console.time(TEST); for (i = 0; i < dim; i++) - assert.equal(store.getTriples(prefix + i, null, null, null).length, dimCubed); + assert.equal(store.getQuads(prefix + i, null, null, null).length, dimCubed); for (j = 0; j < dim; j++) - assert.equal(store.getTriples(null, prefix + j, null, null).length, dimCubed); + assert.equal(store.getQuads(null, prefix + j, null, null).length, dimCubed); for (k = 0; k < dim; k++) - assert.equal(store.getTriples(null, null, prefix + k, null).length, dimCubed); + assert.equal(store.getQuads(null, null, prefix + k, null).length, dimCubed); for (l = 0; l < dim; l++) - assert.equal(store.getTriples(null, null, null, prefix + l).length, dimCubed); + assert.equal(store.getQuads(null, null, null, prefix + l).length, dimCubed); console.timeEnd(TEST); diff --git a/spec/SpecTester.js b/spec/SpecTester.js index 41422023..be492e75 100755 --- a/spec/SpecTester.js +++ b/spec/SpecTester.js @@ -120,14 +120,14 @@ SpecTester.prototype._fetch = function (filename, callback) { // Parses the tests manifest into tests SpecTester.prototype._parseManifest = function (manifestContents, callback) { - // Parse the manifest into triples + // Parse the manifest into quads var manifest = {}, testStore = new N3.Store(), self = this; - new N3.Parser({ format: 'text/turtle' }).parse(manifestContents, function (error, triple) { - // Store triples until there are no more - if (error) return callback(error); - if (triple) return testStore.addTriple(triple.subject, triple.predicate, triple.object); + new N3.Parser({ format: 'text/turtle' }).parse(manifestContents, function (error, quad) { + // Store quads until there are no more + if (error) return callback(error); + if (quad) return testStore.addQuad(quad.subject, quad.predicate, quad.object); - // Once all triples are there, get the first item of the test list + // Once all quads are there, get the first item of the test list var tests = manifest.tests = [], skipped = manifest.skipped = [], itemHead = testStore.getObjects('', prefixes.mf + 'entries')[0]; @@ -135,19 +135,19 @@ SpecTester.prototype._parseManifest = function (manifestContents, callback) { while (itemHead && itemHead.value !== nil) { // Find and store the item's properties var itemValue = testStore.getObjects(itemHead, first)[0], - itemTriples = testStore.getTriples(itemValue, null, null), + itemQuads = testStore.getQuads(itemValue, null, null), test = { id: itemValue.value.replace(/^#/, '') }; - itemTriples.forEach(function (triple) { - var propertyMatch = triple.predicate.value.match(/#(.+)/); + itemQuads.forEach(function (quad) { + var propertyMatch = quad.predicate.value.match(/#(.+)/); if (propertyMatch) - test[propertyMatch[1]] = triple.object.value; + test[propertyMatch[1]] = quad.object.value; }); test.negative = /Negative/.test(test.type); test.skipped = self._skipNegative && test.negative; (!test.skipped ? tests : skipped).push(test); // Find the next test item - itemHead = testStore.getTriples(itemHead, rest, null)[0].object; + itemHead = testStore.getQuads(itemHead, rest, null)[0].object; } return callback(null, manifest); }); @@ -163,9 +163,9 @@ SpecTester.prototype._performTest = function (test, actionStream, callback) { resultWriter = new N3.Writer(fs.createWriteStream(resultFile), { format: 'N-Quads' }), config = { format: this._name, documentIRI: url.resolve(this._manifest, test.action) }, parser = new N3.Parser(config), self = this; - parser.parse(actionStream, function (error, triple) { + parser.parse(actionStream, function (error, quad) { if (error) test.error = error; - if (triple) resultWriter.addTriple(triple); + if (quad) resultWriter.addQuad(quad); // Verify the result after it has been written else resultWriter.end(function () { @@ -250,7 +250,7 @@ SpecTester.prototype._generateEarlReport = function (tests, callback) { report.addPrefix('manifest', manifest); function addTriple(s, p, o) { - report.addTriple(Term.fromId(s), Term.fromId(p), Term.fromId(o)); + report.addQuad(Term.fromId(s), Term.fromId(p), Term.fromId(o)); } addTriple(reportFile, prefixes.foaf + 'primaryTopic', app); diff --git a/test/N3Store-test.js b/test/N3Store-test.js index 8a84535a..bacf17e0 100644 --- a/test/N3Store-test.js +++ b/test/N3Store-test.js @@ -5,8 +5,7 @@ var Readable = require('stream').Readable, var Term = DataFactory.Term, NamedNode = DataFactory.NamedNode, DefaultGraph = DataFactory.DefaultGraph, - Quad = DataFactory.Quad, - Triple = DataFactory.Triple; + Quad = DataFactory.Quad; describe('N3Store', function () { describe('The N3Store module', function () { @@ -31,14 +30,14 @@ describe('N3Store', function () { }); it('should be empty', function () { - store.getTriples().should.be.empty; + store.getQuads().should.be.empty; }); describe('when importing a stream of 2 quads', function () { before(function (done) { var stream = new ArrayReader([ - new Triple(new NamedNode('s1'), new NamedNode('p2'), new NamedNode('o2')), - new Triple(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o1')), + new Quad(new NamedNode('s1'), new NamedNode('p2'), new NamedNode('o2')), + new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o1')), ]); var events = store.import(stream); events.on('end', done); @@ -50,8 +49,8 @@ describe('N3Store', function () { describe('when removing a stream of 2 quads', function () { before(function (done) { var stream = new ArrayReader([ - new Triple(new NamedNode('s1'), new NamedNode('p2'), new NamedNode('o2')), - new Triple(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o1')), + new Quad(new NamedNode('s1'), new NamedNode('p2'), new NamedNode('o2')), + new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o1')), ]); var events = store.remove(stream); events.on('end', done); @@ -88,8 +87,8 @@ describe('N3Store', function () { it('should still have size 0 (instead of null) after adding and removing a triple', function () { expect(store.size).to.eql(0); - store.addTriple(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')).should.be.true; - store.removeTriple(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')).should.be.true; + store.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')).should.be.true; + store.removeQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')).should.be.true; expect(store.size).to.eql(0); }); @@ -97,9 +96,9 @@ describe('N3Store', function () { store.createBlankNode().value.should.eql('b0'); store.createBlankNode().value.should.eql('b1'); - store.addTriple('_:b0', '_:b1', '_:b2').should.be.true; + store.addQuad('_:b0', '_:b1', '_:b2').should.be.true; store.createBlankNode().value.should.eql('b3'); - store.removeTriples(store.getTriples()); + store.removeQuads(store.getQuads()); }); it('should be able to generate named blank nodes', function () { @@ -109,9 +108,9 @@ describe('N3Store', function () { }); it('should be able to store triples with generated blank nodes', function () { - store.addTriple(store.createBlankNode('x'), new NamedNode('b'), new NamedNode('c')).should.be.true; - shouldIncludeAll(store.getTriples(null, new NamedNode('b')), ['_:x', 'b', 'c'])(); - store.removeTriples(store.getTriples()); + store.addQuad(store.createBlankNode('x'), new NamedNode('b'), new NamedNode('c')).should.be.true; + shouldIncludeAll(store.getQuads(null, new NamedNode('b')), ['_:x', 'b', 'c'])(); + store.removeQuads(store.getQuads()); }); }); @@ -128,7 +127,7 @@ describe('N3Store', function () { describe('adding a triple that already exists', function () { it('should return false', function () { - store.addTriple('s1', 'p1', 'o1').should.be.false; + store.addQuad('s1', 'p1', 'o1').should.be.false; }); it('should not increase the size', function () { @@ -138,7 +137,7 @@ describe('N3Store', function () { describe('adding a triple that did not exist yet', function () { it('should return true', function () { - store.addTriple('s1', 'p1', 'o4').should.be.true; + store.addQuad('s1', 'p1', 'o4').should.be.true; }); it('should increase the size', function () { @@ -148,7 +147,7 @@ describe('N3Store', function () { describe('removing an existing triple', function () { it('should return true', function () { - store.removeTriple('s1', 'p1', 'o4').should.be.true; + store.removeQuad('s1', 'p1', 'o4').should.be.true; }); it('should decrease the size', function () { @@ -158,7 +157,7 @@ describe('N3Store', function () { describe('removing a non-existing triple', function () { it('should return false', function () { - store.removeTriple('s1', 'p1', 'o5').should.be.false; + store.removeQuad('s1', 'p1', 'o5').should.be.false; }); it('should not decrease the size', function () { @@ -169,13 +168,13 @@ describe('N3Store', function () { describe('An N3Store with 5 elements', function () { var store = new N3Store(); - store.addTriple('s1', 'p1', 'o1').should.be.true; - store.addTriple({ subject: 's1', predicate: 'p1', object: 'o2' }).should.be.true; - store.addTriples([ + store.addQuad('s1', 'p1', 'o1').should.be.true; + store.addQuad({ subject: 's1', predicate: 'p1', object: 'o2' }).should.be.true; + store.addQuads([ { subject: 's1', predicate: 'p2', object: 'o2' }, { subject: 's2', predicate: 'p1', object: 'o1' }, ]); - store.addTriple('s1', 'p1', 'o1', 'c4').should.be.true; + store.addQuad('s1', 'p1', 'o1', 'c4').should.be.true; it('should have size 5', function () { store.size.should.eql(5); @@ -183,7 +182,7 @@ describe('N3Store', function () { describe('when searched without parameters', function () { it('should return all items', - shouldIncludeAll(store.getTriples(), + shouldIncludeAll(store.getQuads(), ['s1', 'p1', 'o1'], ['s1', 'p1', 'o2'], ['s1', 'p2', 'o2'], @@ -193,7 +192,7 @@ describe('N3Store', function () { describe('when searched with an existing subject parameter', function () { it('should return all items with this subject in all graphs', - shouldIncludeAll(store.getTriples(new NamedNode('s1'), null, null), + shouldIncludeAll(store.getQuads(new NamedNode('s1'), null, null), ['s1', 'p1', 'o1'], ['s1', 'p1', 'o2'], ['s1', 'p2', 'o2'], @@ -201,16 +200,16 @@ describe('N3Store', function () { }); describe('when searched with a non-existing subject parameter', function () { - itShouldBeEmpty(store.getTriples(new NamedNode('s3'), null, null)); + itShouldBeEmpty(store.getQuads(new NamedNode('s3'), null, null)); }); describe('when searched with a non-existing subject parameter that exists elsewhere', function () { - itShouldBeEmpty(store.getTriples(new NamedNode('p1'), null, null)); + itShouldBeEmpty(store.getQuads(new NamedNode('p1'), null, null)); }); describe('when searched with an existing predicate parameter', function () { it('should return all items with this predicate in all graphs', - shouldIncludeAll(store.getTriples(null, new NamedNode('p1'), null), + shouldIncludeAll(store.getQuads(null, new NamedNode('p1'), null), ['s1', 'p1', 'o1'], ['s1', 'p1', 'o2'], ['s2', 'p1', 'o1'], @@ -218,70 +217,70 @@ describe('N3Store', function () { }); describe('when searched with a non-existing predicate parameter', function () { - itShouldBeEmpty(store.getTriples(null, new NamedNode('p3'), null)); + itShouldBeEmpty(store.getQuads(null, new NamedNode('p3'), null)); }); describe('when searched with an existing object parameter', function () { it('should return all items with this object in all graphs', - shouldIncludeAll(store.getTriples(null, null, new NamedNode('o1')), + shouldIncludeAll(store.getQuads(null, null, new NamedNode('o1')), ['s1', 'p1', 'o1'], ['s2', 'p1', 'o1'], ['s1', 'p1', 'o1', 'c4'])); }); describe('when searched with a non-existing object parameter', function () { - itShouldBeEmpty(store.getTriples(null, null, new NamedNode('o4'))); + itShouldBeEmpty(store.getQuads(null, null, new NamedNode('o4'))); }); describe('when searched with existing subject and predicate parameters', function () { it('should return all items with this subject and predicate in all graphs', - shouldIncludeAll(store.getTriples(new NamedNode('s1'), new NamedNode('p1'), null), + shouldIncludeAll(store.getQuads(new NamedNode('s1'), new NamedNode('p1'), null), ['s1', 'p1', 'o1'], ['s1', 'p1', 'o2'], ['s1', 'p1', 'o1', 'c4'])); }); describe('when searched with non-existing subject and predicate parameters', function () { - itShouldBeEmpty(store.getTriples(new NamedNode('s2'), new NamedNode('p2'), null)); + itShouldBeEmpty(store.getQuads(new NamedNode('s2'), new NamedNode('p2'), null)); }); describe('when searched with existing subject and object parameters', function () { it('should return all items with this subject and object in all graphs', - shouldIncludeAll(store.getTriples(new NamedNode('s1'), null, new NamedNode('o1')), + shouldIncludeAll(store.getQuads(new NamedNode('s1'), null, new NamedNode('o1')), ['s1', 'p1', 'o1'], ['s1', 'p1', 'o1', 'c4'])); }); describe('when searched with non-existing subject and object parameters', function () { - itShouldBeEmpty(store.getTriples(new NamedNode('s2'), new NamedNode('p2'), null)); + itShouldBeEmpty(store.getQuads(new NamedNode('s2'), new NamedNode('p2'), null)); }); describe('when searched with existing predicate and object parameters', function () { it('should return all items with this predicate and object in all graphs', - shouldIncludeAll(store.getTriples(null, new NamedNode('p1'), new NamedNode('o1')), + shouldIncludeAll(store.getQuads(null, new NamedNode('p1'), new NamedNode('o1')), ['s1', 'p1', 'o1'], ['s2', 'p1', 'o1'], ['s1', 'p1', 'o1', 'c4'])); }); describe('when searched with non-existing predicate and object parameters in the default graph', function () { - itShouldBeEmpty(store.getTriples(null, new NamedNode('p2'), new NamedNode('o3'), new DefaultGraph())); + itShouldBeEmpty(store.getQuads(null, new NamedNode('p2'), new NamedNode('o3'), new DefaultGraph())); }); describe('when searched with existing subject, predicate, and object parameters', function () { it('should return all items with this subject, predicate, and object in all graphs', - shouldIncludeAll(store.getTriples(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o1')), + shouldIncludeAll(store.getQuads(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o1')), ['s1', 'p1', 'o1'], ['s1', 'p1', 'o1', 'c4'])); }); describe('when searched with a non-existing triple', function () { - itShouldBeEmpty(store.getTriples(new NamedNode('s2'), new NamedNode('p2'), new NamedNode('o1'))); + itShouldBeEmpty(store.getQuads(new NamedNode('s2'), new NamedNode('p2'), new NamedNode('o1'))); }); describe('when searched with the default graph parameter', function () { it('should return all items in the default graph', - shouldIncludeAll(store.getTriples(null, null, null, new DefaultGraph()), + shouldIncludeAll(store.getQuads(null, null, null, new DefaultGraph()), ['s1', 'p1', 'o1'], ['s1', 'p1', 'o2'], ['s1', 'p2', 'o2'], @@ -290,12 +289,12 @@ describe('N3Store', function () { describe('when searched with an existing named graph parameter', function () { it('should return all items in that graph', - shouldIncludeAll(store.getTriples(null, null, null, new NamedNode('c4')), + shouldIncludeAll(store.getQuads(null, null, null, new NamedNode('c4')), ['s1', 'p1', 'o1', 'c4'])); }); describe('when searched with a non-existing named graph parameter', function () { - itShouldBeEmpty(store.getTriples(null, null, null, new NamedNode('c5'))); + itShouldBeEmpty(store.getQuads(null, null, null, new NamedNode('c5'))); }); describe('getSubjects', function () { @@ -775,170 +774,170 @@ describe('N3Store', function () { describe('when counted without parameters', function () { it('should count all items in all graphs', function () { - store.countTriples().should.equal(5); + store.countQuads().should.equal(5); }); }); describe('when counted with an existing subject parameter', function () { it('should count all items with this subject in all graphs', function () { - store.countTriples(new NamedNode('s1'), null, null).should.equal(4); + store.countQuads(new NamedNode('s1'), null, null).should.equal(4); }); }); describe('when counted with a non-existing subject parameter', function () { it('should be empty', function () { - store.countTriples(new NamedNode('s3'), null, null).should.equal(0); + store.countQuads(new NamedNode('s3'), null, null).should.equal(0); }); }); describe('when counted with a non-existing subject parameter that exists elsewhere', function () { it('should be empty', function () { - store.countTriples(new NamedNode('p1'), null, null).should.equal(0); + store.countQuads(new NamedNode('p1'), null, null).should.equal(0); }); }); describe('when counted with an existing predicate parameter', function () { it('should count all items with this predicate in all graphs', function () { - store.countTriples(null, new NamedNode('p1'), null).should.equal(4); + store.countQuads(null, new NamedNode('p1'), null).should.equal(4); }); }); describe('when counted with a non-existing predicate parameter', function () { it('should be empty', function () { - store.countTriples(null, new NamedNode('p3'), null).should.equal(0); + store.countQuads(null, new NamedNode('p3'), null).should.equal(0); }); }); describe('when counted with an existing object parameter', function () { it('should count all items with this object in all graphs', function () { - store.countTriples(null, null, 'o1').should.equal(3); + store.countQuads(null, null, 'o1').should.equal(3); }); }); describe('when counted with a non-existing object parameter', function () { it('should be empty', function () { - store.countTriples(null, null, 'o4').should.equal(0); + store.countQuads(null, null, 'o4').should.equal(0); }); }); describe('when counted with existing subject and predicate parameters', function () { it('should count all items with this subject and predicate in all graphs', function () { - store.countTriples('s1', 'p1', null).should.equal(3); + store.countQuads('s1', 'p1', null).should.equal(3); }); }); describe('when counted with non-existing subject and predicate parameters', function () { it('should be empty', function () { - store.countTriples('s2', 'p2', null).should.equal(0); + store.countQuads('s2', 'p2', null).should.equal(0); }); }); describe('when counted with existing subject and object parameters', function () { it('should count all items with this subject and object in all graphs', function () { - store.countTriples('s1', null, 'o1').should.equal(2); + store.countQuads('s1', null, 'o1').should.equal(2); }); }); describe('when counted with non-existing subject and object parameters', function () { it('should be empty', function () { - store.countTriples('s2', 'p2', null).should.equal(0); + store.countQuads('s2', 'p2', null).should.equal(0); }); }); describe('when counted with existing predicate and object parameters', function () { it('should count all items with this predicate and object in all graphs', function () { - store.countTriples(null, 'p1', 'o1').should.equal(3); + store.countQuads(null, 'p1', 'o1').should.equal(3); }); }); describe('when counted with non-existing predicate and object parameters', function () { it('should be empty', function () { - store.countTriples(null, 'p2', 'o3').should.equal(0); + store.countQuads(null, 'p2', 'o3').should.equal(0); }); }); describe('when counted with existing subject, predicate, and object parameters', function () { it('should count all items with this subject, predicate, and object in all graphs', function () { - store.countTriples('s1', 'p1', 'o1').should.equal(2); + store.countQuads('s1', 'p1', 'o1').should.equal(2); }); }); describe('when counted with a non-existing triple', function () { it('should be empty', function () { - store.countTriples('s2', 'p2', 'o1').should.equal(0); + store.countQuads('s2', 'p2', 'o1').should.equal(0); }); }); describe('when counted with the default graph parameter', function () { it('should count all items in the default graph', function () { - store.countTriples(null, null, null, new DefaultGraph()).should.equal(4); + store.countQuads(null, null, null, new DefaultGraph()).should.equal(4); }); }); describe('when counted with an existing named graph parameter', function () { it('should count all items in that graph', function () { - store.countTriples(null, null, null, 'c4').should.equal(1); + store.countQuads(null, null, null, 'c4').should.equal(1); }); }); describe('when counted with a non-existing named graph parameter', function () { it('should be empty', function () { - store.countTriples(null, null, null, 'c5').should.equal(0); + store.countQuads(null, null, null, 'c5').should.equal(0); }); }); describe('when trying to remove a triple with a non-existing subject', function () { - before(function () { store.removeTriple(new NamedNode('s0'), new NamedNode('p1'), new NamedNode('o1')).should.be.false; }); + before(function () { store.removeQuad(new NamedNode('s0'), new NamedNode('p1'), new NamedNode('o1')).should.be.false; }); it('should still have size 5', function () { store.size.should.eql(5); }); }); describe('when trying to remove a triple with a non-existing predicate', function () { - before(function () { store.removeTriple(new NamedNode('s1'), new NamedNode('p0'), new NamedNode('o1')).should.be.false; }); + before(function () { store.removeQuad(new NamedNode('s1'), new NamedNode('p0'), new NamedNode('o1')).should.be.false; }); it('should still have size 5', function () { store.size.should.eql(5); }); }); describe('when trying to remove a triple with a non-existing object', function () { - before(function () { store.removeTriple(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o0')).should.be.false; }); + before(function () { store.removeQuad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o0')).should.be.false; }); it('should still have size 5', function () { store.size.should.eql(5); }); }); describe('when trying to remove a triple for which no subjects exist', function () { - before(function () { store.removeTriple(new NamedNode('o1'), new NamedNode('p1'), new NamedNode('o1')).should.be.false; }); + before(function () { store.removeQuad(new NamedNode('o1'), new NamedNode('p1'), new NamedNode('o1')).should.be.false; }); it('should still have size 5', function () { store.size.should.eql(5); }); }); describe('when trying to remove a triple for which no predicates exist', function () { - before(function () { store.removeTriple(new NamedNode('s1'), new NamedNode('s1'), new NamedNode('o1')).should.be.false; }); + before(function () { store.removeQuad(new NamedNode('s1'), new NamedNode('s1'), new NamedNode('o1')).should.be.false; }); it('should still have size 5', function () { store.size.should.eql(5); }); }); describe('when trying to remove a triple for which no objects exist', function () { - before(function () { store.removeTriple(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('s1')).should.be.false; }); + before(function () { store.removeQuad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('s1')).should.be.false; }); it('should still have size 5', function () { store.size.should.eql(5); }); }); describe('when trying to remove a triple that does not exist', function () { - before(function () { store.removeTriple(new NamedNode('s1'), new NamedNode('p2'), new NamedNode('o1')).should.be.false; }); + before(function () { store.removeQuad(new NamedNode('s1'), new NamedNode('p2'), new NamedNode('o1')).should.be.false; }); it('should still have size 5', function () { store.size.should.eql(5); }); }); describe('when trying to remove an incomplete triple', function () { - before(function () { store.removeTriple(new NamedNode('s1'), null, null).should.be.false; }); + before(function () { store.removeQuad(new NamedNode('s1'), null, null).should.be.false; }); it('should still have size 5', function () { store.size.should.eql(5); }); }); describe('when trying to remove a triple with a non-existing graph', function () { - before(function () { store.removeTriple(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o1'), new NamedNode('c0')).should.be.false; }); + before(function () { store.removeQuad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o1'), new NamedNode('c0')).should.be.false; }); it('should still have size 5', function () { store.size.should.eql(5); }); }); describe('when removing an existing triple', function () { - before(function () { store.removeTriple(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o1')).should.be.true; }); + before(function () { store.removeQuad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o1')).should.be.true; }); it('should have size 4', function () { store.size.should.eql(4); }); it('should not contain that triple anymore', - shouldIncludeAll(function () { return store.getTriples(); }, + shouldIncludeAll(function () { return store.getQuads(); }, ['s1', 'p1', 'o2'], ['s1', 'p2', 'o2'], ['s2', 'p1', 'o1'], @@ -946,32 +945,32 @@ describe('N3Store', function () { }); describe('when removing an existing triple from a named graph', function () { - before(function () { store.removeTriple(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o1'), new NamedNode('c4')).should.be.true; }); + before(function () { store.removeQuad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o1'), new NamedNode('c4')).should.be.true; }); it('should have size 3', function () { store.size.should.eql(3); }); - itShouldBeEmpty(function () { return store.getTriples(null, null, null, 'c4'); }); + itShouldBeEmpty(function () { return store.getQuads(null, null, null, 'c4'); }); }); describe('when removing multiple triples', function () { before(function () { - store.removeTriples([ - new Triple(new NamedNode('s1'), new NamedNode('p2'), new NamedNode('o2')), - new Triple(new NamedNode('s2'), new NamedNode('p1'), new NamedNode('o1')), + store.removeQuads([ + new Quad(new NamedNode('s1'), new NamedNode('p2'), new NamedNode('o2')), + new Quad(new NamedNode('s2'), new NamedNode('p1'), new NamedNode('o1')), ]); }); it('should have size 1', function () { store.size.should.eql(1); }); it('should not contain those triples anymore', - shouldIncludeAll(function () { return store.getTriples(); }, + shouldIncludeAll(function () { return store.getQuads(); }, ['s1', 'p1', 'o2'])); }); describe('when adding and removing a triple', function () { before(function () { - store.addTriple(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')).should.be.true; - store.removeTriple(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')).should.be.true; + store.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')).should.be.true; + store.removeQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')).should.be.true; }); it('should have an unchanged size', function () { store.size.should.eql(1); }); @@ -981,17 +980,17 @@ describe('N3Store', function () { describe('An N3Store containing a blank node', function () { var store = new N3Store(); var b1 = store.createBlankNode(); - store.addTriple(new NamedNode('s1'), new NamedNode('p1'), b1).should.be.true; + store.addQuad(new NamedNode('s1'), new NamedNode('p1'), b1).should.be.true; describe('when searched with more than one variable', function () { it('should return a triple with the blank node as an object', - shouldIncludeAll(store.getTriples(), + shouldIncludeAll(store.getQuads(), ['s1', 'p1', '_:' + b1.value])); }); describe('when searched with one variable', function () { it('should return a triple with the blank node as an object', - shouldIncludeAll(store.getTriples('s1', 'p1'), + shouldIncludeAll(store.getQuads('s1', 'p1'), ['s1', 'p1', '_:' + b1.value])); }); }); @@ -1002,14 +1001,14 @@ describe('N3Store', function () { // Test inspired by http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/. // The value `__proto__` is not supported however – fixing it introduces too much overhead. it('should be able to contain entities with JavaScript object property names', function () { - store.addTriple('toString', 'valueOf', 'toLocaleString', 'hasOwnProperty').should.be.true; - shouldIncludeAll(store.getTriples(null, null, null, 'hasOwnProperty'), + store.addQuad('toString', 'valueOf', 'toLocaleString', 'hasOwnProperty').should.be.true; + shouldIncludeAll(store.getQuads(null, null, null, 'hasOwnProperty'), ['toString', 'valueOf', 'toLocaleString', 'hasOwnProperty'])(); }); it('should be able to contain entities named "null"', function () { - store.addTriple('null', 'null', 'null', 'null').should.be.true; - shouldIncludeAll(store.getTriples(null, null, null, 'null'), ['null', 'null', 'null', 'null'])(); + store.addQuad('null', 'null', 'null', 'null').should.be.true; + shouldIncludeAll(store.getQuads(null, null, null, 'null'), ['null', 'null', 'null', 'null'])(); }); }); }); diff --git a/test/N3Writer-test.js b/test/N3Writer-test.js index d3fee928..dbdaa50c 100644 --- a/test/N3Writer-test.js +++ b/test/N3Writer-test.js @@ -4,8 +4,7 @@ var DataFactory = require('../N3').DataFactory; var Term = DataFactory.Term, NamedNode = DataFactory.NamedNode, Literal = DataFactory.Literal, - Quad = DataFactory.Quad, - Triple = DataFactory.Triple; + Quad = DataFactory.Quad; describe('N3Writer', function () { describe('The N3Writer module', function () { @@ -25,19 +24,19 @@ describe('N3Writer', function () { describe('An N3Writer instance', function () { it('should serialize a single triple', function () { var writer = N3Writer(); - writer.tripleToString(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')).should.equal(' .\n'); + writer.quadToString(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')).should.equal(' .\n'); }); it('should serialize a single quad', function () { var writer = N3Writer(); - writer.tripleToString(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), new NamedNode('g')).should.equal(' .\n'); + writer.quadToString(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), new NamedNode('g')).should.equal(' .\n'); }); it('should serialize an array of triples', function () { var writer = N3Writer(); var triples = [new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')), new Quad(new NamedNode('d'), new NamedNode('e'), new NamedNode('f'))]; - writer.triplesToString(triples).should.equal(' .\n .\n'); + writer.quadsToString(triples).should.equal(' .\n .\n'); }); @@ -219,7 +218,7 @@ describe('N3Writer', function () { it('sends output through end when no stream argument is given', function (done) { var writer = new N3Writer(), notCalled = true; - writer.addTriple(new Triple(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')), function () { notCalled = false; }); + writer.addQuad(new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')), function () { notCalled = false; }); writer.end(function (error, output) { output.should.equal(' .\n'); done(notCalled || error); @@ -228,7 +227,7 @@ describe('N3Writer', function () { it('respects the prefixes argument when no stream argument is given', function (done) { var writer = new N3Writer({ prefixes: { a: 'b#' } }); - writer.addTriple(new Triple(new NamedNode('b#a'), new NamedNode('b#b'), new NamedNode('b#c'))); + writer.addQuad(new Quad(new NamedNode('b#a'), new NamedNode('b#b'), new NamedNode('b#c'))); writer.end(function (error, output) { output.should.equal('@prefix a: .\n\na:a a:b a:c.\n'); done(error); @@ -239,7 +238,7 @@ describe('N3Writer', function () { var writer = new N3Writer(); writer.addPrefix('a', 'b#'); writer.addPrefix('a', 'b#'); - writer.addTriple(new Triple(new NamedNode('b#a'), new NamedNode('b#b'), new NamedNode('b#c'))); + writer.addQuad(new Quad(new NamedNode('b#a'), new NamedNode('b#b'), new NamedNode('b#c'))); writer.addPrefix('a', 'b#'); writer.addPrefix('a', 'b#'); writer.addPrefix('b', 'b#'); @@ -254,9 +253,9 @@ describe('N3Writer', function () { it('serializes triples of a graph with a prefix declaration in between', function (done) { var writer = new N3Writer(); writer.addPrefix('a', 'b#'); - writer.addTriple(new Quad(new NamedNode('b#a'), new NamedNode('b#b'), new NamedNode('b#c'), new NamedNode('b#g'))); + writer.addQuad(new Quad(new NamedNode('b#a'), new NamedNode('b#b'), new NamedNode('b#c'), new NamedNode('b#g'))); writer.addPrefix('d', 'e#'); - writer.addTriple(new Quad(new NamedNode('b#a'), new NamedNode('b#b'), new NamedNode('b#d'), new NamedNode('b#g'))); + writer.addQuad(new Quad(new NamedNode('b#a'), new NamedNode('b#b'), new NamedNode('b#d'), new NamedNode('b#g'))); writer.end(function (error, output) { output.should.equal('@prefix a: .\n\na:g {\na:a a:b a:c\n}\n' + '@prefix d: .\n\na:g {\na:a a:b a:d\n}\n'); @@ -266,8 +265,8 @@ describe('N3Writer', function () { it('should accept triples with separated components', function (done) { var writer = N3Writer(); - writer.addTriple(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')); - writer.addTriple(new NamedNode('a'), new NamedNode('b'), new NamedNode('d')); + writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')); + writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('d')); writer.end(function (error, output) { output.should.equal(' , .\n'); done(error); @@ -276,8 +275,8 @@ describe('N3Writer', function () { it('should accept quads with separated components', function (done) { var writer = N3Writer(); - writer.addTriple(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), new NamedNode('g')); - writer.addTriple(new NamedNode('a'), new NamedNode('b'), new NamedNode('d'), new NamedNode('g')); + writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), new NamedNode('g')); + writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('d'), new NamedNode('g')); writer.end(function (error, output) { output.should.equal(' {\n , \n}\n'); done(error); @@ -286,8 +285,8 @@ describe('N3Writer', function () { it('should serialize triples with an empty blank node as object', function (done) { var writer = N3Writer(); - writer.addTriple(new NamedNode('a1'), new NamedNode('b'), writer.blank()); - writer.addTriple(new NamedNode('a2'), new NamedNode('b'), writer.blank([])); + writer.addQuad(new NamedNode('a1'), new NamedNode('b'), writer.blank()); + writer.addQuad(new NamedNode('a2'), new NamedNode('b'), writer.blank([])); writer.end(function (error, output) { output.should.equal(' [].\n' + ' [].\n'); @@ -297,9 +296,9 @@ describe('N3Writer', function () { it('should serialize triples with a one-triple blank node as object', function (done) { var writer = N3Writer(); - writer.addTriple(new NamedNode('a1'), new NamedNode('b'), writer.blank(new NamedNode('d'), new NamedNode('e'))); - writer.addTriple(new NamedNode('a2'), new NamedNode('b'), writer.blank({ predicate: new NamedNode('d'), object: new NamedNode('e') })); - writer.addTriple(new NamedNode('a3'), new NamedNode('b'), writer.blank([{ predicate: new NamedNode('d'), object: new NamedNode('e') }])); + writer.addQuad(new NamedNode('a1'), new NamedNode('b'), writer.blank(new NamedNode('d'), new NamedNode('e'))); + writer.addQuad(new NamedNode('a2'), new NamedNode('b'), writer.blank({ predicate: new NamedNode('d'), object: new NamedNode('e') })); + writer.addQuad(new NamedNode('a3'), new NamedNode('b'), writer.blank([{ predicate: new NamedNode('d'), object: new NamedNode('e') }])); writer.end(function (error, output) { output.should.equal(' [ ].\n' + ' [ ].\n' + @@ -310,7 +309,7 @@ describe('N3Writer', function () { it('should serialize triples with a two-triple blank node as object', function (done) { var writer = N3Writer(); - writer.addTriple(new NamedNode('a'), new NamedNode('b'), writer.blank([ + writer.addQuad(new NamedNode('a'), new NamedNode('b'), writer.blank([ { predicate: new NamedNode('d'), object: new NamedNode('e') }, { predicate: new NamedNode('f'), object: new Literal('"g"') }, ])); @@ -325,7 +324,7 @@ describe('N3Writer', function () { it('should serialize triples with a three-triple blank node as object', function (done) { var writer = N3Writer(); - writer.addTriple(new NamedNode('a'), new NamedNode('b'), writer.blank([ + writer.addQuad(new NamedNode('a'), new NamedNode('b'), writer.blank([ { predicate: new NamedNode('d'), object: new NamedNode('e') }, { predicate: new NamedNode('f'), object: new Literal('"g"') }, { predicate: new NamedNode('h'), object: new NamedNode('i') }, @@ -342,7 +341,7 @@ describe('N3Writer', function () { it('should serialize triples with predicate-sharing blank node triples as object', function (done) { var writer = N3Writer(); - writer.addTriple(new NamedNode('a'), new NamedNode('b'), writer.blank([ + writer.addQuad(new NamedNode('a'), new NamedNode('b'), writer.blank([ { predicate: new NamedNode('d'), object: new NamedNode('e') }, { predicate: new NamedNode('d'), object: new NamedNode('f') }, { predicate: new NamedNode('g'), object: new NamedNode('h') }, @@ -359,14 +358,14 @@ describe('N3Writer', function () { it('should serialize triples with nested blank nodes as object', function (done) { var writer = N3Writer(); - writer.addTriple(new NamedNode('a1'), new NamedNode('b'), writer.blank([ + writer.addQuad(new NamedNode('a1'), new NamedNode('b'), writer.blank([ { predicate: new NamedNode('d'), object: writer.blank() }, ])); - writer.addTriple(new NamedNode('a2'), new NamedNode('b'), writer.blank([ + writer.addQuad(new NamedNode('a2'), new NamedNode('b'), writer.blank([ { predicate: new NamedNode('d'), object: writer.blank(new NamedNode('e'), new NamedNode('f')) }, { predicate: new NamedNode('g'), object: writer.blank(new NamedNode('h'), new Literal('"i"')) }, ])); - writer.addTriple(new NamedNode('a3'), new NamedNode('b'), writer.blank([ + writer.addQuad(new NamedNode('a3'), new NamedNode('b'), writer.blank([ { predicate: new NamedNode('d'), object: writer.blank([ { predicate: new NamedNode('g'), object: writer.blank(new NamedNode('h'), new NamedNode('i')) }, { predicate: new NamedNode('j'), object: writer.blank(new NamedNode('k'), new Literal('"l"')) }, @@ -392,8 +391,8 @@ describe('N3Writer', function () { it('should serialize triples with an empty blank node as subject', function (done) { var writer = N3Writer(); - writer.addTriple(writer.blank(), new NamedNode('b'), new NamedNode('c')); - writer.addTriple(writer.blank([]), new NamedNode('b'), new NamedNode('c')); + writer.addQuad(writer.blank(), new NamedNode('b'), new NamedNode('c')); + writer.addQuad(writer.blank([]), new NamedNode('b'), new NamedNode('c')); writer.end(function (error, output) { output.should.equal('[] .\n' + '[] .\n'); @@ -403,9 +402,9 @@ describe('N3Writer', function () { it('should serialize triples with a one-triple blank node as subject', function (done) { var writer = N3Writer(); - writer.addTriple(writer.blank(new NamedNode('a'), new NamedNode('b')), new NamedNode('c'), new NamedNode('d')); - writer.addTriple(writer.blank({ predicate: new NamedNode('a'), object: new NamedNode('b') }), new NamedNode('c'), new NamedNode('d')); - writer.addTriple(writer.blank([{ predicate: new NamedNode('a'), object: new NamedNode('b') }]), new NamedNode('c'), new NamedNode('d')); + writer.addQuad(writer.blank(new NamedNode('a'), new NamedNode('b')), new NamedNode('c'), new NamedNode('d')); + writer.addQuad(writer.blank({ predicate: new NamedNode('a'), object: new NamedNode('b') }), new NamedNode('c'), new NamedNode('d')); + writer.addQuad(writer.blank([{ predicate: new NamedNode('a'), object: new NamedNode('b') }]), new NamedNode('c'), new NamedNode('d')); writer.end(function (error, output) { output.should.equal('[ ] .\n' + '[ ] .\n' + @@ -416,8 +415,8 @@ describe('N3Writer', function () { it('should serialize triples with an empty blank node as graph', function (done) { var writer = N3Writer(); - writer.addTriple(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), writer.blank()); - writer.addTriple(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), writer.blank([])); + writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), writer.blank()); + writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), writer.blank([])); writer.end(function (error, output) { output.should.equal('[] {\n \n}\n' + '[] {\n \n}\n'); @@ -427,8 +426,8 @@ describe('N3Writer', function () { it('should serialize triples with an empty list as object', function (done) { var writer = N3Writer(); - writer.addTriple(new NamedNode('a1'), new NamedNode('b'), writer.list()); - writer.addTriple(new NamedNode('a2'), new NamedNode('b'), writer.list([])); + writer.addQuad(new NamedNode('a1'), new NamedNode('b'), writer.list()); + writer.addQuad(new NamedNode('a2'), new NamedNode('b'), writer.list([])); writer.end(function (error, output) { output.should.equal(' ().\n' + ' ().\n'); @@ -438,8 +437,8 @@ describe('N3Writer', function () { it('should serialize triples with a one-element list as object', function (done) { var writer = N3Writer(); - writer.addTriple(new NamedNode('a1'), new NamedNode('b'), writer.list([new NamedNode('c')])); - writer.addTriple(new NamedNode('a2'), new NamedNode('b'), writer.list([new Literal('"c"')])); + writer.addQuad(new NamedNode('a1'), new NamedNode('b'), writer.list([new NamedNode('c')])); + writer.addQuad(new NamedNode('a2'), new NamedNode('b'), writer.list([new Literal('"c"')])); writer.end(function (error, output) { output.should.equal(' ().\n' + ' ("c").\n'); @@ -449,8 +448,8 @@ describe('N3Writer', function () { it('should serialize triples with a three-element list as object', function (done) { var writer = N3Writer(); - writer.addTriple(new NamedNode('a1'), new NamedNode('b'), writer.list([new NamedNode('c'), new NamedNode('d'), new NamedNode('e')])); - writer.addTriple(new NamedNode('a2'), new NamedNode('b'), writer.list([new Literal('"c"'), new Literal('"d"'), new Literal('"e"')])); + writer.addQuad(new NamedNode('a1'), new NamedNode('b'), writer.list([new NamedNode('c'), new NamedNode('d'), new NamedNode('e')])); + writer.addQuad(new NamedNode('a2'), new NamedNode('b'), writer.list([new Literal('"c"'), new Literal('"d"'), new Literal('"e"')])); writer.end(function (error, output) { output.should.equal(' ( ).\n' + ' ("c" "d" "e").\n'); @@ -460,8 +459,8 @@ describe('N3Writer', function () { it('should serialize triples with an empty list as subject', function (done) { var writer = N3Writer(); - writer.addTriple(writer.list(), new NamedNode('b1'), new NamedNode('c')); - writer.addTriple(writer.list([]), new NamedNode('b2'), new NamedNode('c')); + writer.addQuad(writer.list(), new NamedNode('b1'), new NamedNode('c')); + writer.addQuad(writer.list([]), new NamedNode('b2'), new NamedNode('c')); writer.end(function (error, output) { output.should.equal('() .\n' + '() .\n'); @@ -471,8 +470,8 @@ describe('N3Writer', function () { it('should serialize triples with a one-element list as subject', function (done) { var writer = N3Writer(); - writer.addTriple(writer.list([new NamedNode('a')]), new NamedNode('b1'), new NamedNode('c')); - writer.addTriple(writer.list([new NamedNode('a')]), new NamedNode('b2'), new NamedNode('c')); + writer.addQuad(writer.list([new NamedNode('a')]), new NamedNode('b1'), new NamedNode('c')); + writer.addQuad(writer.list([new NamedNode('a')]), new NamedNode('b2'), new NamedNode('c')); writer.end(function (error, output) { output.should.equal('() .\n' + '() .\n'); @@ -482,7 +481,7 @@ describe('N3Writer', function () { it('should serialize triples with a three-element list as subject', function (done) { var writer = N3Writer(); - writer.addTriple(writer.list([new NamedNode('a1'), new Literal('"b"'), new Literal('"c"')]), new NamedNode('d'), new NamedNode('e')); + writer.addQuad(writer.list([new NamedNode('a1'), new Literal('"b"'), new Literal('"c"')]), new NamedNode('d'), new NamedNode('e')); writer.end(function (error, output) { output.should.equal('( "b" "c") .\n'); done(error); @@ -491,8 +490,8 @@ describe('N3Writer', function () { it('should accept triples in bulk', function (done) { var writer = N3Writer(); - writer.addTriples([new Triple(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')), - new Triple(new NamedNode('a'), new NamedNode('b'), new NamedNode('d'))]); + writer.addQuads([new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')), + new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('d'))]); writer.end(function (error, output) { output.should.equal(' , .\n'); done(error); @@ -501,19 +500,19 @@ describe('N3Writer', function () { it('should not allow writing after end', function (done) { var writer = N3Writer(); - writer.addTriple(new Triple(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'))); + writer.addQuad(new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'))); writer.end(); - writer.addTriple(new Triple(new NamedNode('d'), new NamedNode('e'), new NamedNode('f')), function (error) { + writer.addQuad(new Quad(new NamedNode('d'), new NamedNode('e'), new NamedNode('f')), function (error) { error.should.be.an.instanceof(Error); error.should.have.property('message', 'Cannot write because the writer has been closed.'); done(); }); }); - it('should write simple triples in N-Triples mode', function (done) { - var writer = N3Writer({ format: 'N-Triples' }); - writer.addTriple(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')); - writer.addTriple(new NamedNode('a'), new NamedNode('b'), new NamedNode('d')); + it('should write simple triples in N-Quads mode', function (done) { + var writer = N3Writer({ format: 'N-Quads' }); + writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')); + writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('d')); writer.end(function (error, output) { output.should.equal(' .\n .\n'); done(error); @@ -524,8 +523,8 @@ describe('N3Writer', function () { var writer = N3Writer({ format: 'N-Quads' }); var called = false; function callback() { called = true; } - writer.addTriple(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), callback); - writer.addTriple(new NamedNode('a'), new NamedNode('b'), new NamedNode('d'), new NamedNode('g')); + writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), callback); + writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('d'), new NamedNode('g')); writer.end(function (error, output) { called.should.be.true; output.should.equal(' .\n .\n'); @@ -573,7 +572,7 @@ function shouldSerialize(/* prefixes?, tripleArrays..., expectedResult */) { (function next() { var item = tripleArrays.shift(); if (item) - writer.addTriple(new Quad(Term.fromId(item[0]), Term.fromId(item[1]), Term.fromId(item[2]), Term.fromId(item[3])), next); + writer.addQuad(new Quad(Term.fromId(item[0]), Term.fromId(item[1]), Term.fromId(item[2]), Term.fromId(item[3])), next); else writer.end(function (error) { try {