Skip to content

Commit 023fdb9

Browse files
authored
feat: add log:isImpliedBy feature flag (#500)
* feat: add log:isImpliedBy feature flag * chore(docs): document isImpliedBy * chore: add comma * chore: rename parserIsImpledBy -> parserIsImpliedBy * chore: improve log:isImpliedBy docs
1 parent cbffbbf commit 023fdb9

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ prefix or, if set to an empty string, completely disable prefixing:
159159
const parser = new N3.Parser({ blankNodePrefix: '' });
160160
```
161161

162+
The parser can output a backwards chaining rule such as `_:q <= _:p.` in two ways:
163+
- as `_:p log:implies _:q.` (default)
164+
- as `_:q log:isImpliedBy _:p.` (when the `isImpliedBy` flag is set to `true`)
165+
```JavaScript
166+
const parser = new N3.Parser({ isImpliedBy: true });
167+
```
168+
162169
### From an RDF stream to quads
163170

164171
`N3.Parser` can parse [Node.js streams](http://nodejs.org/api/stream.html) as they grow,

src/IRIs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ export default {
2626
},
2727
log: {
2828
implies: `${SWAP}log#implies`,
29+
isImpliedBy: `${SWAP}log#isImpliedBy`,
2930
},
3031
};

src/N3Lexer.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ export default class N3Lexer {
5454
this._endOfFile = /^(?:#[^\n\r]*)?$/;
5555
options = options || {};
5656

57+
// Whether the log:isImpliedBy predicate is supported
58+
this._isImpliedBy = options.isImpliedBy;
59+
5760
// In line mode (N-Triples or N-Quads), only simple features may be parsed
5861
if (this._lineMode = !!options.lineMode) {
5962
this._n3Mode = false;
@@ -152,8 +155,11 @@ export default class N3Lexer {
152155
else if (input.length > 1 && input[1] === '<')
153156
type = '<<', matchLength = 2;
154157
// Try to find a backwards implication arrow
155-
else if (this._n3Mode && input.length > 1 && input[1] === '=')
156-
type = 'inverse', matchLength = 2, value = '>';
158+
else if (this._n3Mode && input.length > 1 && input[1] === '=') {
159+
matchLength = 2;
160+
if (this._isImpliedBy) type = 'abbreviation', value = '<';
161+
else type = 'inverse', value = '>';
162+
}
157163
break;
158164

159165
case '>':

src/N3Parser.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ export default class N3Parser {
2727
this._readPredicateOrNamedGraph = this._readPredicate;
2828
// Support triples in other graphs
2929
this._supportsQuads = !(isTurtle || isTriG || isNTriples || isN3);
30+
// Whether the log:isImpliedBy predicate is supported
31+
this._isImpliedBy = options.isImpliedBy;
3032
// Support nesting of triples
3133
this._supportsRDFStar = format === '' || /star|\*$/.test(format);
3234
// Disable relative IRIs in N-Triples or N-Quads mode
3335
if (isLineMode)
3436
this._resolveRelativeIRI = iri => { return null; };
3537
this._blankNodePrefix = typeof options.blankNodePrefix !== 'string' ? '' :
3638
options.blankNodePrefix.replace(/^(?!_:)/, '_:');
37-
this._lexer = options.lexer || new N3Lexer({ lineMode: isLineMode, n3: isN3 });
39+
this._lexer = options.lexer || new N3Lexer({ lineMode: isLineMode, n3: isN3, isImpliedBy: this._isImpliedBy });
3840
// Disable explicit quantifiers by default
3941
this._explicitQuantifiers = !!options.explicitQuantifiers;
4042
}
@@ -1096,6 +1098,7 @@ function initDataFactory(parser, factory) {
10961098
'a': factory.namedNode(namespaces.rdf.type),
10971099
'=': factory.namedNode(namespaces.owl.sameAs),
10981100
'>': factory.namedNode(namespaces.log.implies),
1101+
'<': factory.namedNode(namespaces.log.isImpliedBy),
10991102
};
11001103
parser.QUANTIFIERS_GRAPH = factory.namedNode('urn:n3:quantifiers');
11011104
}

test/N3Parser-test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,7 @@ describe('Parser', () => {
19831983

19841984
describe('A Parser instance for the N3 format', () => {
19851985
function parser() { return new Parser({ baseIRI: BASE_IRI, format: 'N3' }); }
1986+
function parserIsImpliedBy() { return new Parser({ baseIRI: BASE_IRI, format: 'N3', isImpliedBy: true }); }
19861987

19871988
it(
19881989
'should parse a single triple',
@@ -2046,6 +2047,12 @@ describe('Parser', () => {
20462047
['b', 'http://www.w3.org/2000/10/swap/log#implies', 'a']),
20472048
);
20482049

2050+
it(
2051+
'should parse a simple left implication',
2052+
shouldParse(parserIsImpliedBy, '<a> <= <b>.',
2053+
['a', 'http://www.w3.org/2000/10/swap/log#isImpliedBy', 'b']),
2054+
);
2055+
20492056
it(
20502057
'should parse a right implication between one-triple graphs',
20512058
shouldParse(parser, '{ ?a ?b <c>. } => { <d> <e> ?a }.',
@@ -2054,6 +2061,14 @@ describe('Parser', () => {
20542061
['d', 'e', '?a', '_:b1']),
20552062
);
20562063

2064+
it(
2065+
'should parse a right implication between one-triple graphs',
2066+
shouldParse(parserIsImpliedBy, '{ ?a ?b <c>. } => { <d> <e> ?a }.',
2067+
['_:b0', 'http://www.w3.org/2000/10/swap/log#implies', '_:b1'],
2068+
['?a', '?b', 'c', '_:b0'],
2069+
['d', 'e', '?a', '_:b1']),
2070+
);
2071+
20572072
it(
20582073
'should parse a right implication between two-triple graphs',
20592074
shouldParse(parser, '{ ?a ?b <c>. <d> <e> <f>. } => { <d> <e> ?a, <f> }.',
@@ -2072,6 +2087,14 @@ describe('Parser', () => {
20722087
['d', 'e', '?a', '_:b1']),
20732088
);
20742089

2090+
it(
2091+
'should parse a left implication between one-triple graphs',
2092+
shouldParse(parserIsImpliedBy, '{ ?a ?b <c>. } <= { <d> <e> ?a }.',
2093+
['_:b0', 'http://www.w3.org/2000/10/swap/log#isImpliedBy', '_:b1'],
2094+
['?a', '?b', 'c', '_:b0'],
2095+
['d', 'e', '?a', '_:b1']),
2096+
);
2097+
20752098
it(
20762099
'should parse a left implication between two-triple graphs',
20772100
shouldParse(parser, '{ ?a ?b <c>. <d> <e> <f>. } <= { <d> <e> ?a, <f> }.',

0 commit comments

Comments
 (0)