N3.js
N3.js copied to clipboard
Writer does not support Notation3
If we parse the statement {?s a ?o} => {?s a ?o}.
into RDF/JS quads and then write those quads back to text/n3
using the Stream Writer we get
_:n3-0 {
?s a ?o
}
_:n3-1 {
?s a ?o
}
_:n3-0 <http://www.w3.org/2000/10/swap/log#implies> _:n3-1.
When trying to parse this result again we get the error Error: Expected entity but got { on line 1.
Can be reproduced by running this script
function write(quads: Quad[]): Promise<string> {
return new Promise<string>((res) => {
const writer = new StreamWriter({ format: 'text/n3' });
let s: string = '';
Readable.from(quads)
.pipe(writer)
.on('data', d => {
s += d
}).on('end', () => { res(s) })
});
}
function parse(str: string): Quad[] {
const parser = new Parser({ format: 'text/n3' });
return parser.parse(str)
}
async function main() {
const parsed = parse(`{?s a ?o} => {?s a ?o}.`);
const backToString = await write(parsed);
console.log(backToString)
// Errors on this line
parse(backToString);
}
main();
I'm struggling to find a grammar that I can get my head around to work out how this needs to be written. I.e. do we need re-write it as {?s a ?o} => {?s a ?o}
; or can we make the above form valid n3 with a couple of minor tweaks.
I've also not been able to find any other JavaScript TypeScript N3 writers for this purpose.
@josd Would you be able to point me to the package / code responsible for serializing N3 in Eye as a reference point?
The code doing the reasoning output in eye starts at https://github.com/eyereasoner/eye/blob/6405dcb4b1dacb806af9e18f909cc06db31b3059/eye.pl#L3431 and ends at https://github.com/eyereasoner/eye/blob/6405dcb4b1dacb806af9e18f909cc06db31b3059/eye.pl#L4649 This also includes the writing of proofs and that's why it is so longwinded :-)
write those quads back to
text/n3
using the Stream Writer
Just want to note that N3.js at the moment does not support Notation3 writing (because it cannot be done in a streaming way).
Just as a note - the code I am currently using to do N3 writing is https://github.com/eyereasoner/eye-js/blob/main/lib/n3Writer.temp.ts