-
Notifications
You must be signed in to change notification settings - Fork 132
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
feat/rdfstar support #311
feat/rdfstar support #311
Changes from 22 commits
2f7d6e8
37ab09c
f837b8d
da945e9
624e3e1
d27b920
2af5e05
0ac4c46
0337ed8
99d6b72
d258f22
56cc184
2f0f57d
0539be9
e7646d9
6140e86
bec8395
8ce8428
211bf07
4489772
c76f3fd
8a6dcbb
7df44bc
545e646
90e15b9
6907296
73791d8
8e3b997
2e655fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,6 @@ let DEFAULTGRAPH; | |
let _blankNodeCounter = 0; | ||
|
||
const escapedLiteral = /^"(.*".*)(?="[^"]*$)/; | ||
const quadId = /^<<("(?:""|[^"])*"[^ ]*|[^ ]+) ("(?:""|[^"])*"[^ ]*|[^ ]+) ("(?:""|[^"])*"[^ ]*|[^ ]+) ?("(?:""|[^"])*"[^ ]*|[^ ]+)?>>$/; | ||
|
||
// ## DataFactory singleton | ||
const DataFactory = { | ||
|
@@ -188,9 +187,12 @@ export class DefaultGraph extends Term { | |
// ## DefaultGraph singleton | ||
DEFAULTGRAPH = new DefaultGraph(); | ||
|
||
|
||
// ### Constructs a term from the given internal string ID | ||
export function termFromId(id, factory) { | ||
// The third 'nested' parameter of this function is to aid | ||
// with recursion over nested terms. It should not be used | ||
// by consumers of this library. | ||
// See https://github.com/rdfjs/N3.js/pull/311#discussion_r1061042725 | ||
export function termFromId(id, factory, nested) { | ||
factory = factory || DataFactory; | ||
|
||
// Falsy value or empty string indicate the default graph | ||
|
@@ -215,21 +217,28 @@ export function termFromId(id, factory) { | |
return factory.literal(id.substr(1, endPos - 1), | ||
id[endPos + 1] === '@' ? id.substr(endPos + 2) | ||
: factory.namedNode(id.substr(endPos + 3))); | ||
case '<': | ||
const components = quadId.exec(id); | ||
return factory.quad( | ||
termFromId(unescapeQuotes(components[1]), factory), | ||
termFromId(unescapeQuotes(components[2]), factory), | ||
termFromId(unescapeQuotes(components[3]), factory), | ||
components[4] && termFromId(unescapeQuotes(components[4]), factory) | ||
); | ||
case '[': | ||
id = JSON.parse(id); | ||
break; | ||
default: | ||
return factory.namedNode(id); | ||
if (!nested || !Array.isArray(id)) { | ||
return factory.namedNode(id); | ||
} | ||
} | ||
return factory.quad( | ||
termFromId(id[0], factory, true), | ||
termFromId(id[1], factory, true), | ||
termFromId(id[2], factory, true), | ||
id[3] && termFromId(id[3], factory, true) | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Questions for the future:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Any use of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh actually - I think I see more what you're saying is that quads could just use a set of ids/hashes in the internal representation that are then looked up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But that could be an internal function then indeed! |
||
|
||
// ### Constructs an internal string ID from the given term or ID string | ||
export function termToId(term) { | ||
// The third 'nested' parameter of this function is to aid | ||
// with recursion over nested terms. It should not be used | ||
// by consumers of this library. | ||
// See https://github.com/rdfjs/N3.js/pull/311#discussion_r1061042725 | ||
export function termToId(term, nested) { | ||
if (typeof term === 'string') | ||
return term; | ||
if (term instanceof Term && term.termType !== 'Quad') | ||
|
@@ -247,17 +256,15 @@ export function termToId(term) { | |
term.language ? `@${term.language}` : | ||
(term.datatype && term.datatype.value !== xsd.string ? `^^${term.datatype.value}` : '')}`; | ||
case 'Quad': | ||
// To identify RDF* quad components, we escape quotes by doubling them. | ||
// This avoids the overhead of backslash parsing of Turtle-like syntaxes. | ||
return `<<${ | ||
escapeQuotes(termToId(term.subject)) | ||
} ${ | ||
escapeQuotes(termToId(term.predicate)) | ||
} ${ | ||
escapeQuotes(termToId(term.object)) | ||
}${ | ||
(isDefaultGraph(term.graph)) ? '' : ` ${termToId(term.graph)}` | ||
}>>`; | ||
const res = [ | ||
termToId(term.subject, true), | ||
termToId(term.predicate, true), | ||
termToId(term.object, true), | ||
]; | ||
if (!isDefaultGraph(term.graph)) { | ||
res.push(termToId(term.graph, true)); | ||
} | ||
return nested ? res : JSON.stringify(res); | ||
default: throw new Error(`Unexpected termType: ${term.termType}`); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jeswr In what cases should
nested
be true? Is it the same asrdfStar
basically?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is really just an internal parameter that is used when recursively calling
termFromId
onquad
terms within other quads.The only place that parameter should ever be used is
N3.js/src/N3DataFactory.js
Lines 224 to 229 in 56cc184
So no this is not an
rdfStar
parameter because this should still be false/undefined on the asserted triple.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a comment clarifying this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So only by itself; gotcha.