@jahed/sparql-engine
Version:
SPARQL query engine for servers and web browsers.
75 lines • 2.44 kB
JavaScript
import { termToString } from "rdf-string";
import Graph from "./graph.js";
import UnionGraph from "./union-graph.js";
/**
* An abstraction over an RDF datasets, i.e., a collection of RDF graphs.
* @abstract
*/
export default class Dataset {
_graphFactory;
/**
* Constructor
*/
constructor() {
this._graphFactory = () => {
throw new Error("Named graphs not supported.");
};
}
/**
* Get an UnionGraph, i.e., the dynamic union of several graphs,
* from the RDF Graphs in the Dataset.
* @param iris - Iris of the named graphs to include in the union
* @param includeDefault - True if the default graph should be included
* @return The dynamic union of several graphs in the Dataset
*/
async getUnionGraph(iris, includeDefault = false) {
let graphs = [];
if (includeDefault) {
graphs.push(this.getDefaultGraph());
}
for (const iri of iris) {
graphs.push(await this.getNamedGraph(iri));
}
return new UnionGraph(graphs);
}
/**
* Returns all Graphs in the Dataset, including the Default one
* @param includeDefault - True if the default graph should be included
* @return The list of all graphs in the Dataset
*/
async *getAllGraphs(includeDefault = true) {
if (includeDefault) {
yield this.getDefaultGraph();
}
for (const iri of this.iris) {
yield this.getNamedGraph(iri);
}
}
/**
* Set the Graph Factory used by te dataset to create new RDF graphs on-demand
* @param factory - Graph Factory
*/
setGraphFactory(factory) {
this._graphFactory = factory;
}
/**
* Create a new RDF Graph, using the current Graph Factory.
* This Graph factory can be set using the "setGraphFactory" method.
* @param iri - IRI of the graph to create
* @return A new RDF Graph
*/
async createGraph(iri) {
const graph = await this._graphFactory(iri);
if (!graph.iri.equals(iri)) {
const error = new Error("graph.iri must match given iri.");
console.log({
error,
iri: termToString(iri),
graphIri: termToString(graph.iri),
});
throw error;
}
return graph;
}
}
//# sourceMappingURL=dataset.js.map