@jahed/sparql-engine
Version:
SPARQL query engine for servers and web browsers.
33 lines • 1.51 kB
JavaScript
// SPDX-License-Identifier: MIT
import { concat, intersection } from "lodash-es";
import { Pipeline } from "../engine/pipeline/pipeline.js";
import { Bindings } from "../rdf/bindings.js";
/**
* Evaluates a SPARQL MINUS clause
* @see {@link https://www.w3.org/TR/sparql11-query/#neg-minus}
* @param leftSource - Left input {@link PipelineStage}
* @param rightSource - Right input {@link PipelineStage}
* @return A {@link PipelineStage} which evaluate the MINUS operation
*/
export default function minus(leftSource, rightSource) {
// first materialize the right source in a buffer, then apply difference on the left source
const engine = Pipeline.getInstance();
let op = engine.reduce(rightSource, (acc, b) => concat(acc, b), []);
return engine.mergeMap(op, (buffer) => {
return engine.filter(leftSource, (bindings) => {
const leftKeys = Array.from(bindings.variables());
// mu_a is compatible with mu_b if,
// for all v in intersection(dom(mu_a), dom(mu_b)), mu_a[v] = mu_b[v]
const isCompatible = buffer.some((b) => {
const rightKeys = Array.from(b.variables());
const commonKeys = intersection(leftKeys, rightKeys);
return commonKeys.every((k) => {
return b.get(k)?.equals(bindings.get(k));
});
});
// only output non-compatible bindings
return !isCompatible;
});
});
}
//# sourceMappingURL=minus.js.map