Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for empty graphs in the subject and object positions #374

Draft
wants to merge 2 commits into
base: versions/2.0.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/N3Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -662,13 +662,35 @@ export default class N3Parser {

const graph = this._graph;

let closingEmptyGraph = false;

// Store the last quad of the formula
if (this._subject !== null)
this._emit(this._subject, this._predicate, this._object, graph);

if (this._subject !== null) {
// Catch the empty graph being closed when parsing N3.
// In this case, we emit the empty graph as the value "true"^^xsd:boolean
// The predicate value is set to 'null' in the _readSubject function,
// after which the graph is closed directly when processing an empty graph.
if (!this._predicate) {
closingEmptyGraph = true;
}
else {
this._emit(this._subject, this._predicate, this._object, graph);
}
}

// Restore the parent context containing this formula
this._restoreContext('formula', token);

if (closingEmptyGraph) {
const xsdBoolean = 'http://www.w3.org/2001/XMLSchema#boolean';
if (this._subject && this._subject.equals(graph))
this._subject = this._literal('true', this._namedNode(xsdBoolean));

if (this._object && this._object.equals(graph))
this._object = this._literal('true', this._namedNode(xsdBoolean));
}

// If the formula was in a list context, continue reading the list
if (this._contextStack.length > 0 && this._contextStack[this._contextStack.length - 1].type === 'list') {
return this._readListItem(token, graph);
Expand Down
24 changes: 23 additions & 1 deletion test/N3Parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1763,7 +1763,7 @@ describe('Parser', () => {
return isImpliedBy ? [to, 'http://www.w3.org/2000/10/swap/log#isImpliedBy', from] : [from, 'http://www.w3.org/2000/10/swap/log#implies', to];
}

describe(`A Parser instance for the N3 format with rdfStar support disabled and with ${isImpliedBy ? 'enabled' : 'disabled'}`, () => {
describe(`A Parser instance for the N3 format with rdfStar support disabled and with isImpliedBy ${isImpliedBy ? 'enabled' : 'disabled'}`, () => {
function parser() { return new Parser({ baseIRI: BASE_IRI, format: 'N3', rdfStar: false, isImpliedBy }); }

describe('should parse a single triple',
Expand Down Expand Up @@ -2337,6 +2337,28 @@ describe('Parser', () => {
it('should not parse RDF-star in the object position',
shouldNotParse(parser, '<a> <b> <<<a> <b> <c>>>.',
'Unexpected RDF-star syntax on line 1.'));

describe('should parse the empty graph as an rdf:value of "true"^^xsd:boolean in the object position',
shouldParse(parser, '<a> <b> {}.',
['a', 'b', '"true"^^http://www.w3.org/2001/XMLSchema#boolean']
));

describe('should parse the empty graph as an rdf:value of "true"^^xsd:boolean in the subject position',
shouldParse(parser, '{} <b> <c>.',
['"true"^^http://www.w3.org/2001/XMLSchema#boolean', 'b', 'c']
));

describe('should parse the empty graph in the object position as an rdf:value of "true"^^xsd:boolean while retaining the encompassing graph',
shouldParse(parser, '<a> <b> { <x> <y> {} }.',
['a', 'b', '_:b0'],
['x', 'y', '"true"^^http://www.w3.org/2001/XMLSchema#boolean', '_:b0']
));

describe('should parse the empty graph in the subject position as an rdf:value of "true"^^xsd:boolean while retaining the encompassing graph',
shouldParse(parser, '<a> <b> { {} <y> <z> }.',
['a', 'b', '_:b0'],
['"true"^^http://www.w3.org/2001/XMLSchema#boolean', 'y', 'z', '_:b0']
));
});
}

Expand Down
Loading