Skip to content

Instantly share code, notes, and snippets.

@dmrub
Created May 16, 2019 10:52
Show Gist options
  • Save dmrub/8daf70ad29708a49744d528abdd0175e to your computer and use it in GitHub Desktop.
Save dmrub/8daf70ad29708a49744d528abdd0175e to your computer and use it in GitHub Desktop.
N3.js example for parsing RDF string, storing tuples to store and retrieving them from store
const N3 = require('n3');
const { DataFactory } = N3;
const { namedNode, literal, defaultGraph, quad } = DataFactory;
const store = new N3.Store();
const parser = new N3.Parser();
parser.parse(
`PREFIX c: <http://example.org/cartoons#>
c:Tom a c:Cat.
c:Jerry a c:Mouse;
c:smarterThan c:Tom.
c:Tom c:builds c:House.`,
(error, quad, prefixes) => {
if (quad) {
console.log(quad);
store.addQuad(quad);
} else {
console.log("# Parsing done!", prefixes);
afterParsing();
}
});
function afterParsing() {
console.log("# Output all tuples from store of Form (subject, predicate, object) == (c:Tom, <any>, <any>):");
const tom = store.getQuads(namedNode('http://example.org/cartoons#Tom'), null, null);
console.log(tom);
console.log("Done");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment