@jahed/sparql-engine
Version:
SPARQL query engine for servers and web browsers.
35 lines • 1.42 kB
JavaScript
import { Pipeline } from "../../engine/pipeline/pipeline.js";
import { isWildcard, UNBOUND } from "../../utils/rdf.js";
/**
* Evaluates a SPARQL SELECT operation, i.e., perform a selection over sets of solutions bindings
* @see {@link https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#select}
* @param source - Input {@link PipelineStage}
* @param query - SELECT query
* @return A {@link PipelineStage} which evaluate the SELECT modifier
*/
export default function select(source, query) {
if (!("variables" in query)) {
throw new Error("Not a select query.");
}
if (isWildcard(query.variables[0])) {
return Pipeline.getInstance().map(source, (bindings) => {
// return bindings.mapValues((k, v) => (rdf.isVariable(k) ? v : null));
return bindings;
});
}
const variables = query.variables.map((v) => "variable" in v ? v.variable : v);
return Pipeline.getInstance().map(source, (bindings) => {
bindings = variables.reduce((obj, v) => {
if (bindings.has(v.value)) {
obj.set(v.value, bindings.get(v.value));
}
else {
obj.set(v.value, UNBOUND);
}
return obj;
}, bindings.empty());
// return bindings.mapValues((k, v) => (rdf.isVariable(k) ? v : null));
return bindings;
});
}
//# sourceMappingURL=select.js.map