Skip to content

Instantly share code, notes, and snippets.

@aljoshakoecher
Last active July 15, 2024 10:50
Show Gist options
  • Save aljoshakoecher/82495f07edf26003db5225c51fa3331c to your computer and use it in GitHub Desktop.
Save aljoshakoecher/82495f07edf26003db5225c51fa3331c to your computer and use it in GitHub Desktop.
This is a little script to test query behavior of Comunica with queries that contain an OPTIONAL part. In this current state (i.e. one line in rdfData commented out and OPTIONAL part last), it works. But if you swap line 40 with the OPTIONAL part in lines 41-43, no bindings are returned.
const { QueryEngine } = require("@comunica/query-sparql");
const { Store, Parser } = require("n3");
const rdfData = `
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix ex: <http://example.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
ex:personA a ex:Person.
#ex:personA ex:hasName "Homer".
`;
function storeTriples(rdfData) {
const store = new Store();
const parser = new Parser();
return new Promise(function(resolve, reject) {
// parse and resolve / reject promise on completion / error
parser.parse(rdfData, (error, quad) => {
if (error) {
console.log(`Error while parsing`);
reject(error);
}
if (quad) store.add(quad);
else {
console.log("Done parsing!");
resolve(store);
}
});
});
}
const queryString = `
PREFIX ex: <http://example.org/>
SELECT ?person ?personName WHERE {
?person a ex:Person
OPTIONAL {
?person ex:hasName ?personName
}
}
`;
/**
* Load RDF data and print query results
*/
async function runTest() {
const queryEngine = new QueryEngine();
const store = await storeTriples(rdfData);
const bindingsStream = await queryEngine.queryBindings(queryString, {sources: [store]});
bindingsStream.on('data', (binding) => {
console.log(binding.toString());
});
}
runTest();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment