All files / languages/sparql/services sparql-inference-filter.ts

100% Statements 3/3
100% Branches 0/0
100% Functions 3/3
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36                                          8x               5x 4x          
import * as rdfjs from '@rdfjs/types';
import { InferenceUri } from '@src/providers/inference-uri';
 
/**
 * Creates a filtered source that wraps an RDF/JS store and filters out quads
 * from inference graphs. The filter checks if the graph IRI ends with the
 * inference URI appendix and excludes those quads from the match results.
 * 
 * @param store The RDF/JS source to wrap.
 * @returns A filtered source that excludes inference graph quads.
 * 
 * @example
 * ```typescript
 * const filteredSource = createFilteredSource(store);
 * const source = { type: 'rdfjs', value: filteredSource };
 * // Queries using this source will not include inferred triples
 * ```
 */
export function createFilteredSource(store: rdfjs.Source<rdfjs.Quad>): {
	match(subject: rdfjs.Quad_Subject | null, predicate: rdfjs.Quad_Predicate | null, object: rdfjs.Quad_Object | null, graph: rdfjs.Quad_Graph | null): rdfjs.Stream<rdfjs.Quad>;
} {
	return {
		match(
			subject: rdfjs.Quad_Subject | null,
			predicate: rdfjs.Quad_Predicate | null,
			object: rdfjs.Quad_Object | null,
			graph: rdfjs.Quad_Graph | null
		): rdfjs.Stream<rdfjs.Quad> {
			// Call the original match and filter out inference graph quads
			return (store.match(subject, predicate, object, graph) as any).filter(
				(quad: rdfjs.Quad) => !InferenceUri.isInferenceUri(quad.graph.value)
			);
		}
	};
}