n3 2.7.12 → 2.9.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 +46 -2
- package/browser/n3.esm.min.js +11 -11
- package/browser/n3.min.js +9 -9
- package/lib/N3Parser.js +42 -21
- package/lib/N3Reasoner.js +13 -4
- package/lib/N3Store.js +385 -27
- package/package.json +1 -1
- package/src/N3Parser.js +47 -26
- package/src/N3Reasoner.js +14 -4
- package/src/N3Store.js +418 -32
package/lib/N3Parser.js
CHANGED
|
@@ -1446,14 +1446,16 @@ class N3Parser {
|
|
|
1446
1446
|
|
|
1447
1447
|
// ### `parse` parses the N3 input and emits each parsed quad through the onQuad callback.
|
|
1448
1448
|
parse(input, quadCallback, prefixCallback, versionCallback) {
|
|
1449
|
-
// The second parameter accepts an object
|
|
1449
|
+
// The second parameter also accepts an object of named callbacks.
|
|
1450
1450
|
// As a second and third parameter it still accepts a separate quadCallback and prefixCallback for backward compatibility as well
|
|
1451
|
-
let onQuad, onPrefix, onComment, onVersion;
|
|
1452
|
-
if (quadCallback && (quadCallback.onQuad || quadCallback.onPrefix || quadCallback.onComment || quadCallback.onVersion)) {
|
|
1451
|
+
let onQuad, onPrefix, onComment, onVersion, onToken, onTokenEnd;
|
|
1452
|
+
if (quadCallback && (quadCallback.onQuad || quadCallback.onPrefix || quadCallback.onComment || quadCallback.onVersion || quadCallback.onToken || quadCallback.onTokenEnd)) {
|
|
1453
1453
|
onQuad = quadCallback.onQuad;
|
|
1454
1454
|
onPrefix = quadCallback.onPrefix;
|
|
1455
1455
|
onComment = quadCallback.onComment;
|
|
1456
1456
|
onVersion = quadCallback.onVersion;
|
|
1457
|
+
onToken = quadCallback.onToken;
|
|
1458
|
+
onTokenEnd = quadCallback.onTokenEnd;
|
|
1457
1459
|
} else {
|
|
1458
1460
|
onQuad = quadCallback;
|
|
1459
1461
|
onPrefix = prefixCallback;
|
|
@@ -1473,36 +1475,55 @@ class N3Parser {
|
|
|
1473
1475
|
this._expectOf = false;
|
|
1474
1476
|
this._quantified = Object.create(null);
|
|
1475
1477
|
this._emptyFormula = false;
|
|
1478
|
+
let readToken = token => {
|
|
1479
|
+
return this._readCallback = this._readCallback(token);
|
|
1480
|
+
};
|
|
1481
|
+
|
|
1482
|
+
// Comments bypass the grammar, but participate in the token lifecycle.
|
|
1483
|
+
if (onComment || this._lexer.comments) {
|
|
1484
|
+
this._lexer.comments = true;
|
|
1485
|
+
const readGrammarToken = readToken;
|
|
1486
|
+
readToken = token => {
|
|
1487
|
+
if (token.type !== 'comment') return readGrammarToken(token);
|
|
1488
|
+
if (onComment) onComment(token.value);
|
|
1489
|
+
return this._readCallback;
|
|
1490
|
+
};
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
// Install observers once per parse, leaving the ordinary token path intact.
|
|
1494
|
+
if (onToken || onTokenEnd) {
|
|
1495
|
+
const consumeToken = readToken;
|
|
1496
|
+
readToken = token => {
|
|
1497
|
+
try {
|
|
1498
|
+
try {
|
|
1499
|
+
if (onToken) onToken(token);
|
|
1500
|
+
return consumeToken(token);
|
|
1501
|
+
} finally {
|
|
1502
|
+
if (onTokenEnd) onTokenEnd(token);
|
|
1503
|
+
}
|
|
1504
|
+
} catch (error) {
|
|
1505
|
+
// A consumer exception aborts this parse, including later chunks.
|
|
1506
|
+
this._readCallback = null;
|
|
1507
|
+
throw error;
|
|
1508
|
+
}
|
|
1509
|
+
};
|
|
1510
|
+
}
|
|
1476
1511
|
|
|
1477
|
-
// Parse synchronously if no quad callback is given
|
|
1512
|
+
// Parse synchronously if no quad callback is given.
|
|
1478
1513
|
if (!onQuad) {
|
|
1479
1514
|
const quads = [];
|
|
1480
1515
|
let error;
|
|
1481
1516
|
this._callback = (e, t) => {
|
|
1482
1517
|
e ? error = e : t && quads.push(t);
|
|
1483
1518
|
};
|
|
1484
|
-
this._lexer.tokenize(input).every(
|
|
1485
|
-
return this._readCallback = this._readCallback(token);
|
|
1486
|
-
});
|
|
1519
|
+
this._lexer.tokenize(input).every(readToken);
|
|
1487
1520
|
if (error) throw error;
|
|
1488
1521
|
return quads;
|
|
1489
1522
|
}
|
|
1490
|
-
|
|
1491
|
-
if (error !== null) this._callback(error), this._callback = noop;else if (this._readCallback)
|
|
1523
|
+
const processNextToken = (error, token) => {
|
|
1524
|
+
if (error !== null) this._callback(error), this._callback = noop;else if (this._readCallback) readToken(token);
|
|
1492
1525
|
};
|
|
1493
1526
|
|
|
1494
|
-
// Enable checking for comments on every token when a commentCallback has been set
|
|
1495
|
-
if (onComment) {
|
|
1496
|
-
// Enable the lexer to return comments as tokens first (disabled by default)
|
|
1497
|
-
this._lexer.comments = true;
|
|
1498
|
-
// Patch the processNextToken function
|
|
1499
|
-
processNextToken = (error, token) => {
|
|
1500
|
-
if (error !== null) this._callback(error), this._callback = noop;else if (this._readCallback) {
|
|
1501
|
-
if (token.type === 'comment') onComment(token.value);else this._readCallback = this._readCallback(token);
|
|
1502
|
-
}
|
|
1503
|
-
};
|
|
1504
|
-
}
|
|
1505
|
-
|
|
1506
1527
|
// Parse asynchronously otherwise, executing the read callback when a token arrives
|
|
1507
1528
|
this._callback = onQuad;
|
|
1508
1529
|
this._lexer.tokenize(input, processNextToken);
|
package/lib/N3Reasoner.js
CHANGED
|
@@ -34,10 +34,17 @@ class N3Reasoner {
|
|
|
34
34
|
this._maxPremiseDepth = options.maxPremiseDepth === undefined ? Infinity : options.maxPremiseDepth;
|
|
35
35
|
}
|
|
36
36
|
_add(subject, predicate, object, graphItem, c, cb) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
const store = this._store;
|
|
38
|
+
// Observing views must see each derivation before indexing
|
|
39
|
+
// (stores from other N3 versions have no observers)
|
|
40
|
+
if (store._observers) {
|
|
41
|
+
if (!store._addQuad(subject, predicate, object, this._graphId)) return;
|
|
42
|
+
} else {
|
|
43
|
+
// Only add to the remaining indexes if there is not already a value in the index
|
|
44
|
+
if (!store._addToIndex(graphItem.subjects, subject, predicate, object)) return;
|
|
45
|
+
store._addToIndex(graphItem.predicates, predicate, object, subject);
|
|
46
|
+
store._addToIndex(graphItem.objects, object, subject, predicate);
|
|
47
|
+
}
|
|
41
48
|
// Count genuinely new derivations and fail past the budget. The check comes
|
|
42
49
|
// after all three indexes are updated, so a caught error leaves the store
|
|
43
50
|
// in a consistent state (the reasoning result is merely incomplete).
|
|
@@ -224,6 +231,8 @@ class N3Reasoner {
|
|
|
224
231
|
const graphs = this._store._getGraphs();
|
|
225
232
|
try {
|
|
226
233
|
for (const graphId in graphs) {
|
|
234
|
+
// Observers compare numeric graph identifiers
|
|
235
|
+
this._graphId = Number(graphId);
|
|
227
236
|
this._reasonGraphNaive(rules, graphs[graphId]);
|
|
228
237
|
}
|
|
229
238
|
} finally {
|