Skip to content

Instantly share code, notes, and snippets.

@aljoshakoecher
Created July 5, 2024 20:04
Show Gist options
  • Save aljoshakoecher/5ff47c37ebffb963cb1f2f529cd8af16 to your computer and use it in GitHub Desktop.
Save aljoshakoecher/5ff47c37ebffb963cb1f2f529cd8af16 to your computer and use it in GitHub Desktop.
Small script that shows a bug (?) in Comunica. An RDF dataset is parsed and stored in an N3Store and also imported into a local GraphDB (needs to be done manually). A query is executed against both the N3Store and the GraphDB instance. It returns valid results for the N3Store but causes an error when querying GraphDB
const { QueryEngine } = require("@comunica/query-sparql");
const { Store, Parser } = require("n3");
const rdfData = `
@prefix : </#>.
@prefix ont: <http://example.org/ontology#>.
@prefix m: <http://openmath.org/vocab/math#>.
ont:myApplication_equals
a m:Application;
m:arguments
( [ a m:Variable; m:name "x" ]
[ a m:Variable; m:name "y" ] );
m:operator "http://www.openmath.org/cd/relation1#eq".`;
const queryString = `
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX OM: <http://openmath.org/vocab/math#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?application (count(?argumentList)-1 as ?position) ?operator (COALESCE(?argNam, ?arg) AS ?argName) ?argValue ?argType WHERE {
?application OM:arguments/rdf:rest* ?argumentList;
OM:operator ?operator.
?argumentList rdf:rest*/rdf:first ?arg.
?arg a ?argType.
# ?argType rdfs:subClassOf OM:Object.
OPTIONAL {
?arg OM:name ?argNam.
}
OPTIONAL {
?arg OM:value ?argValue.
}
}
GROUP BY ?application ?argNam ?argValue ?operator ?argType ?arg`;
const n3Store = new Store();
const n3Parser = new Parser();
async function printBindingStream(bindingsStream) {
// Iterate over the results and print them
bindingsStream.on('data', (binding) => {
console.log('----- Binding -----');
console.log(binding.toString());
});
bindingsStream.on('end', () => {
console.log('Query execution completed');
});
bindingsStream.on('error', (error) => {
console.error('Error executing query:', error);
});
}
async function runQueryAndPrintBindings(source) {
try {
const queryEngine = new QueryEngine();
const bindingsStream = await queryEngine.queryBindings(queryString, {
sources: [source],
});
printBindingStream(bindingsStream);
} catch (error) {
throw new Error(`SPARQL query failed: ${error}`);
}
}
/**
* Parses data and stores and stores it in n3Store, then runs query against it
*/
function testQueryingStore() {
n3Parser.parse(rdfData, (error, quad, prefixes) => {
if (error) {
console.error(error);
return;
}
if (quad) {
n3Store.addQuad(quad);
} else {
console.log('Parsing completed');
runQueryAndPrintBindings(n3Store);
}
});
}
/**
* Runs the query against a local graphDB that contains only the rdfData given above
*/
function testQueryingGraphDB() {
runQueryAndPrintBindings({type: 'sparql', value: 'http://localhost:7200/repositories/comunica-debug'});
}
testQueryingStore();
testQueryingGraphDB();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment