UNPKG

@jahed/sparql-engine

Version:

SPARQL query engine for servers and web browsers.

64 lines 2.51 kB
import { Pipeline } from "./engine/pipeline/pipeline.js"; import { Bindings } from "./rdf/bindings.js"; import { isVariable, tripleToQuad } from "./utils/rdf.js"; /** * Bound a triple pattern using a set of bindings, i.e., substitute variables in the triple pattern * using the set of bindings provided * @param triple - Triple pattern * @param bindings - Set of bindings * @return An new, bounded triple pattern */ export function applyBindings(triple, bindings) { const newTriple = Object.assign({}, triple); if (isVariable(triple.subject) && bindings.has(triple.subject.value)) { newTriple.subject = bindings.get(triple.subject.value); } if (isVariable(triple.predicate) && bindings.has(triple.predicate.value)) { newTriple.predicate = bindings.get(triple.predicate.value); } if (isVariable(triple.object) && bindings.has(triple.object.value)) { newTriple.object = bindings.get(triple.object.value); } return newTriple; } /** * Recursively apply bindings to every triple in a SPARQL group pattern * @param group - SPARQL group pattern to process * @param bindings - Set of bindings to use * @return A new SPARQL group pattern with triples bounded */ export function deepApplyBindings(group, bindings) { switch (group.type) { case "bgp": // WARNING property paths are not supported here const bgp = { type: "bgp", triples: group.triples.map((t) => bindings.bound(tripleToQuad(t))), }; return bgp; case "group": case "optional": case "service": case "union": return { type: group.type, patterns: group.patterns.map((g) => deepApplyBindings(g, bindings)), }; case "query": let subQuery = group; subQuery.where = subQuery.where?.map((g) => deepApplyBindings(g, bindings)); return subQuery; default: return group; } } /** * Extends all set of bindings produced by an iterator with another set of bindings * @param source - Source {@link PipelineStage} * @param bindings - Bindings added to each set of bindings procuded by the iterator * @return A {@link PipelineStage} that extends bindins produced by the source iterator */ export function extendByBindings(source, bindings) { return Pipeline.getInstance().map(source, (b) => bindings.union(b)); } //# sourceMappingURL=utils.js.map