rdfjs / N3.js

Lightning fast, spec-compatible, streaming RDF for JavaScript
http://rdf.js.org/N3.js/
Other
676 stars 127 forks source link

Writer does not support Notation3 #316

Open jeswr opened 1 year ago

jeswr commented 1 year ago

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();
jeswr commented 1 year ago

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.

jeswr commented 1 year ago

@josd Would you be able to point me to the package / code responsible for serializing N3 in Eye as a reference point?

josd commented 1 year ago

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 :-)

RubenVerborgh commented 1 year ago

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).

jeswr commented 1 year ago

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