@jahed/sparql-engine
Version:
SPARQL query engine for servers and web browsers.
30 lines • 1.19 kB
JavaScript
// SPDX-License-Identifier: MIT
import { compact } from "lodash-es";
import { Pipeline } from "../../engine/pipeline/pipeline.js";
import { isVariable, tripleToQuad } from "../../utils/rdf.js";
/**
* A ConstructOperator transform solution mappings into RDF triples, according to a template
* @see {@link https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#construct}
* @param source - Source {@link PipelineStage}
* @param templates - Set of triples patterns in the CONSTRUCT clause
* @return A {@link PipelineStage} which evaluate the CONSTRUCT modifier
*/
export default function construct(source, query) {
const rawTriples = [];
const templates = (query.template || [])
.map((t) => tripleToQuad(t))
.filter((t) => {
if (isVariable(t.subject) ||
isVariable(t.predicate) ||
isVariable(t.object)) {
return true;
}
rawTriples.push(t);
return false;
});
const engine = Pipeline.getInstance();
return engine.endWith(engine.flatMap(source, (bindings) => {
return compact(templates.map((t) => bindings.bound(t)));
}), rawTriples);
}
//# sourceMappingURL=construct.js.map