Last active
February 26, 2026 04:30
-
-
Save cristianvasquez/8187de9178c9e68858780b174956ba01 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Comunica test: Query default graph with selected named graphs (excluding inference) using a clean wrapper | |
| import { QueryEngine } from '@comunica/query-sparql'; | |
| import { DataFactory, Store } from 'n3'; | |
| const { namedNode, quad } = DataFactory; | |
| // --- Custom RDFJS source wrapper --- | |
| class FilteredDefaultGraphSource { | |
| constructor(store, defaultGraphIRIs) { | |
| this.store = store; | |
| this.defaultGraphIRIs = new Set(defaultGraphIRIs); | |
| } | |
| match(subject, predicate, object, graph) { | |
| // Merge only selected named graphs when querying the default graph | |
| if (!graph || graph.termType === 'DefaultGraph') { | |
| return Array.from(this.defaultGraphIRIs) | |
| .flatMap(g => this.store.getQuads(subject, predicate, object, g)) | |
| [Symbol.iterator](); | |
| } | |
| // Pass through named graph queries | |
| return this.store.getQuads(subject, predicate, object, graph)[Symbol.iterator](); | |
| } | |
| } | |
| // --- Initialize engine and store --- | |
| const engine = new QueryEngine(); | |
| const store = new Store(); | |
| // --- Add quads --- | |
| // Asserted graphs | |
| store.addQuad(quad(namedNode('http://example.org/a'), namedNode('http://example.org/p'), namedNode('http://example.org/asserted1'), namedNode('http://example.org/data1'))); | |
| store.addQuad(quad(namedNode('http://example.org/b'), namedNode('http://example.org/p'), namedNode('http://example.org/asserted2'), namedNode('http://example.org/data2'))); | |
| // Inference graphs | |
| store.addQuad(quad(namedNode('http://example.org/a'), namedNode('http://example.org/p'), namedNode('http://example.org/inferred1'), namedNode('http://example.org/data1?inference'))); | |
| store.addQuad(quad(namedNode('http://example.org/b'), namedNode('http://example.org/p'), namedNode('http://example.org/inferred2'), namedNode('http://example.org/data2?inference'))); | |
| // --- Wrap store for filtered default graph --- | |
| const defaultGraphIRIs = ['http://example.org/data1', 'http://example.org/data2']; | |
| const filteredSource = new FilteredDefaultGraphSource(store, defaultGraphIRIs); | |
| // --- SPARQL query --- | |
| const query = `PREFIX ex: <http://example.org/> | |
| SELECT ?s ?o WHERE { ?s ex:p ?o } ORDER BY ?s ?o`; | |
| // --- Helper to run query --- | |
| async function runQuery(description) { | |
| console.log(`\n=== ${description} ===`); | |
| const result = await engine.queryBindings(query, { sources: [filteredSource] }); | |
| const bindings = await result.toArray(); | |
| if (!bindings.length) console.log('(no results)'); | |
| bindings.forEach(b => console.log(`- ${b.get('s').value} ${b.get('o').value}`)); | |
| } | |
| // --- Execute --- | |
| (async () => { | |
| await runQuery('Query default graph includes only asserted graphs (excludes inference)'); | |
| })(); | |
| // Outputs: | |
| // | |
| // === Query default graph includes only asserted graphs (excludes inference) === | |
| // - http://example.org/a http://example.org/asserted1 | |
| // - http://example.org/b http://example.org/asserted2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment