n3 2.1.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -78,6 +78,16 @@ console.log(myQuad.object.datatype.value); // http://www.w3.org/1999/02/22-rdf-s
78
78
  console.log(myQuad.object.language); // en
79
79
  ```
80
80
 
81
+ Always create terms through a data factory such as `N3.DataFactory`,
82
+ and not by instantiating the term classes directly:
83
+ direct construction is deprecated,
84
+ because the factory functions are where term validation can be applied.
85
+ In line with the [RDF/JS specification](http://rdf.js.org/data-model-spec/),
86
+ N3.js assumes that the value of any RDF/JS term it receives —
87
+ whether from its own factory or from another implementation —
88
+ was already validated when the term was created,
89
+ and does not re-validate terms.
90
+
81
91
  In the rest of this document, we will treat “triples” and “quads” equally:
82
92
  we assume that a quad is simply a triple in a named or default graph.
83
93
 
@@ -204,6 +214,41 @@ function SlowConsumer() {
204
214
  A dedicated `prefix` event signals every prefix with `prefix` and `term` arguments.
205
215
  A dedicated `comment` event can be enabled by setting `comments: true` in the N3.StreamParser constructor.
206
216
 
217
+ Note that `prefix` and `comment` events are emitted as soon as they are parsed,
218
+ whereas quads can remain buffered until the consumer is ready to read them.
219
+ The order of these events relative to `data` events is therefore
220
+ not guaranteed to match the position of prefixes and comments in the document.
221
+ If their position matters,
222
+ use `N3.Parser` with the `onQuad`, `onPrefix` and `onComment` callbacks instead,
223
+ which are invoked in document order.
224
+
225
+ ### From a Web Stream to quads
226
+
227
+ N3.js consumes [Node.js streams](http://nodejs.org/api/stream.html) natively,
228
+ but sources such as `fetch` produce [Web Streams](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API).
229
+ On Node.js 17 or higher, convert such a stream into a Node.js stream:
230
+
231
+ ```JavaScript
232
+ const streamParser = new N3.StreamParser(),
233
+ { Readable } = require('stream');
234
+ Readable.fromWeb(response.body).pipe(streamParser);
235
+ ```
236
+
237
+ In browsers (or anywhere without Node.js streams),
238
+ write the chunks to the parser directly,
239
+ since `N3.StreamParser` exposes a standard writable stream interface:
240
+
241
+ ```JavaScript
242
+ const streamParser = new N3.StreamParser(),
243
+ reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
244
+ (async () => {
245
+ for (let result; !(result = await reader.read()).done;)
246
+ if (!streamParser.write(result.value))
247
+ await new Promise(resolve => streamParser.once('drain', resolve));
248
+ streamParser.end();
249
+ })();
250
+ ```
251
+
207
252
  ## Writing
208
253
 
209
254
  ### From quads to a string
@@ -411,6 +456,23 @@ reasoner.reason(rulesDataset);
411
456
 
412
457
  **Note**: N3.js currently only supports rules with [Basic Graph Patterns](https://www.w3.org/TR/sparql11-query/#BasicGraphPattern) in the premise and conclusion. Built-ins and backward-chaining are *not* supported. For an RDF/JS reasoner that supports all Notation3 reasoning features, see [eye-js](https://github.com/eyereasoner/eye-js/).
413
458
 
459
+ ### Limiting reasoning cost
460
+
461
+ When reasoning over rules or data that are not fully trusted,
462
+ optional budgets bound the work `reason()` may perform:
463
+
464
+ ```JavaScript
465
+ const reasoner = new Reasoner(store, {
466
+ maxDerivations: 100000, // maximum number of quads reason() may derive
467
+ maxPremiseDepth: 10, // maximum number of premise triples per rule
468
+ });
469
+ reasoner.reason(rulesDataset);
470
+ ```
471
+
472
+ Both budgets are unbounded by default;
473
+ `reason()` throws when one is exceeded,
474
+ leaving any quads derived up to that point in the store.
475
+
414
476
  ## Compatibility
415
477
  ### Format specifications
416
478
  The N3.js parser and writer is fully compatible with the following W3C specifications: