Skip to content

Commit ddf5b34

Browse files
jeswrtpluscode
andauthored
fix: use BlankNode according to RDF/JS spec
* fix: use BlankNode according to RDF/JS spec * chore: add turtle tests for external data factory --------- Co-authored-by: Tomasz Pluskiewicz <tpluscode@users.noreply.github.com>
1 parent 520054a commit ddf5b34

File tree

4 files changed

+112
-8
lines changed

4 files changed

+112
-8
lines changed

package-lock.json

Lines changed: 29 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"@babel/core": "^7.16.0",
3333
"@babel/preset-env": "^7.16.0",
3434
"@babel/register": "^7.16.0",
35+
"@rdfjs/data-model": "^1",
3536
"arrayify-stream": "^1.0.0",
3637
"browserify": "^17.0.0",
3738
"chai": "^4.0.2",
@@ -42,6 +43,7 @@
4243
"mocha": "^8.0.0",
4344
"nyc": "^14.1.1",
4445
"pre-commit": "^1.2.2",
46+
"rdf-isomorphic": "^1.3.1",
4547
"rdf-test-suite": "^1.19.2",
4648
"streamify-string": "^1.0.1",
4749
"uglify-js": "^3.14.3"

src/N3Parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export default class N3Parser {
8686
this._inversePredicate = false;
8787
// In N3, blank nodes are scoped to a formula
8888
// (using a dot as separator, as a blank node label cannot start with it)
89-
this._prefixes._ = (this._graph ? `${this._graph.id.substr(2)}.` : '.');
89+
this._prefixes._ = (this._graph ? `${this._graph.value}.` : '.');
9090
// Quantifiers are scoped to a formula
9191
this._quantified = Object.create(this._quantified);
9292
}

test/N3Parser-test.js

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Parser, NamedNode, BlankNode, Quad, termFromId } from '../src/';
1+
import rdfDataModel from '@rdfjs/data-model';
2+
import { isomorphic } from 'rdf-isomorphic';
3+
import { Parser, NamedNode, BlankNode, Quad, termFromId, DataFactory as DF } from '../src/';
24

35
const BASE_IRI = 'http://example.org/';
46

@@ -1872,6 +1874,83 @@ describe('Parser', () => {
18721874
});
18731875
});
18741876

1877+
describe('A parser instance with external data factory', () => {
1878+
it('should parse', () => {
1879+
const parser = new Parser({
1880+
baseIRI: BASE_IRI,
1881+
format: 'n3',
1882+
factory: rdfDataModel,
1883+
});
1884+
const quads = parser.parse(`
1885+
@prefix : <http://example.com/> .
1886+
{ :weather a :Raining } => { :weather a :Cloudy } .
1887+
`);
1888+
1889+
quads.length.should.be.gt(0);
1890+
1891+
const g1 = DF.blankNode();
1892+
const g2 = DF.blankNode();
1893+
1894+
isomorphic(quads, [
1895+
DF.quad(
1896+
DF.namedNode('http://example.com/weather'),
1897+
DF.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
1898+
DF.namedNode('http://example.com/Raining'),
1899+
g1
1900+
),
1901+
DF.quad(
1902+
DF.namedNode('http://example.com/weather'),
1903+
DF.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
1904+
DF.namedNode('http://example.com/Cloudy'),
1905+
g2
1906+
),
1907+
DF.quad(
1908+
g1,
1909+
DF.namedNode('http://www.w3.org/2000/10/swap/log#implies'),
1910+
g2
1911+
),
1912+
]).should.be.true;
1913+
});
1914+
});
1915+
1916+
describe('A turtle parser instance with external data factory', () => {
1917+
it('should parse', () => {
1918+
const parser = new Parser({
1919+
baseIRI: BASE_IRI,
1920+
format: 'turtle',
1921+
factory: rdfDataModel,
1922+
});
1923+
const quads = parser.parse(`
1924+
@prefix : <http://example.com/> .
1925+
:weather a :Raining .
1926+
1927+
:jeswr :knows [
1928+
:name "Thomas" ;
1929+
] .
1930+
`);
1931+
1932+
const bnode = DF.blankNode();
1933+
1934+
isomorphic(quads, [
1935+
DF.quad(
1936+
DF.namedNode('http://example.com/weather'),
1937+
DF.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
1938+
DF.namedNode('http://example.com/Raining')
1939+
),
1940+
DF.quad(
1941+
DF.namedNode('http://example.com/jeswr'),
1942+
DF.namedNode('http://example.com/knows'),
1943+
bnode
1944+
),
1945+
DF.quad(
1946+
bnode,
1947+
DF.namedNode('http://example.com/name'),
1948+
DF.literal('Thomas')
1949+
),
1950+
]).should.be.true;
1951+
});
1952+
});
1953+
18751954
describe('IRI resolution', () => {
18761955
describe('RFC3986 normal examples', () => {
18771956
itShouldResolve('http://a/bb/ccc/d;p?q', 'g:h', 'g:h');

0 commit comments

Comments
 (0)