All files / rdf store.ts

83.33% Statements 105/126
81.03% Branches 47/58
72.97% Functions 27/37
84.42% Lines 103/122

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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441                                                                        35x           35x                     9x             572080x               35x                     3x 3x 3x 3x 3x 3x 3x                 1x   1x                                                       15x   15x 281611x     15x                       93x     93x   93x   93x 78x   78x       93x 537080x   537080x 6360x       93x 71x                           95x 95x   95x 533082x 3x 533079x 532987x 92x 92x   92x                               1x 1x   1x   1x                       1x   4095x   1x   1x                             3x 3x 3x 3x         3x   3x 10203x 10203x 10203x   10203x     3x 3x     3x       3x                   10x   10x 6x     4x               2x 2x                 172x 172x   172x 3790x                         2x       6x       6x   6x       6x   6x 6x   6x 2x   4x   4x                                                                     36490x 1x     36487x 36487x 36487x   36487x 29080x   29267x 29186x 67096x     6128x 5374x 5374x   5374x 21874x         7407x                         8x                         5x   5x       5x    
import * as n3 from "n3";
import * as rdfjs from "@rdfjs/types";
import * as src from '../ontologies/src';
import { RdfStore } from "rdf-stores";
import { rdf, RDF } from '../ontologies';
import { _RDF, _RDFA, _RDFS, _OWL, _SH, _SKOS, _XSD } from "../ontologies";
import { EventEmitter } from "stream";
import { Reasoner } from "./reasoners/reasoner";
import { RdfXmlParser } from "rdfxml-streaming-parser";
import { graph, serialize } from "rdflib";
import { toRdflibTerm } from "./utils";
 
/**
 * Indicates an error when a triple is not found in the store.
 */
export class TripleNotFoundError extends Error {
    /**
     * Create a new instance of the error.
     * @param message The error message.
     */
    constructor(subject: rdfjs.Quad_Subject | null, predicate: rdfjs.Quad_Predicate | null, object: rdfjs.Quad_Object | null) {
        super(`No triple was found matching the pattern: ${subject} ${predicate} ${object}`);
 
        this.name = "TripleNotFoundError";
    }
}
 
/*
 * A store for RDF triples with support for reasoning.
 */
export class Store implements rdfjs.Source<rdfjs.Quad> {
    /**
     * The adapted RDF.js triple store implementation.
     * 
     * Note: Please do not use this directly, but rather use the `_ds` property to access the dataset.
     */
    private readonly _store = RdfStore.createDefault();
 
    /**
     * The RDF dataset containing the triples in the store, which should primarily be used
     * for any operations so that the store can be swapped out with a different implementation.
     */
    private readonly _ds: rdfjs.DatasetCore = this._store.asDataset();
 
    /**
     * The reasoner to be used for inference.
     */
    readonly reasoner?: Reasoner;
 
    /**
     * Get the number of triples in all graphs of the store.
     */
    get size(): number {
        return this._ds.size;
    }
 
    /**
     * Get the RDF.js DataFactory used by the store.
     */
    get dataFactory(): rdfjs.DataFactory {
        return this._store.dataFactory;
    }
 
    /**
     * Create a new RDF triple store.
     * @param reasoner The reasoner to be used for inference.
     */
    constructor(reasoner?: Reasoner) {
        this.reasoner = reasoner;
    }
 
    [Symbol.iterator](): Iterator<rdfjs.Quad, any, any> {
        return this._ds[Symbol.iterator]();
    }
 
    /**
     * Loads a set of W3C Standard ontologies into the store (RDF, RDFA, RDFS, OWL, SKOS, SHACL, XSD).
     */
    async loadFrameworkOntologies(executeInference: boolean = true): Promise<void> {
        await this.loadFromTurtleStream(src.rdf, _RDF, executeInference);
        await this.loadFromTurtleStream(src.rdfa, _RDFA, executeInference);
        await this.loadFromTurtleStream(src.rdfs, _RDFS, executeInference);
        await this.loadFromTurtleStream(src.owl, _OWL, executeInference);
        await this.loadFromTurtleStream(src.sh, _SH, executeInference);
        await this.loadFromTurtleStream(src.skos, _SKOS, executeInference);
        await this.loadFromTurtleStream(src.xsd, _XSD, executeInference);
    }
 
    /**
     * Add a quad to the store. Existing quads with the same subject, predicate, object, and graph will be ignored.
     * @param quad The quad to be added.
     * @returns The store instance.
     */
    add(quad: rdfjs.Quad): this {
        this._ds.add(quad);
 
        return this;
    }
 
    /**
     * Delete a quad from the store.
     * @param quad The quad to be deleted.
     * @returns The store instance.
     */
    delete(quad: rdfjs.Quad): this {
        this._ds.delete(quad);
 
        return this;
    }
 
    /**
     * Indicates if the store contains a specific quad.
     * @param quad The quad to be checked.
     * @returns `true` if the quad is found in the store, `false` otherwise.
     */
    has(quad: rdfjs.Quad): boolean {
        return this._ds.has(quad);
    }
 
    /**
     * Get the URIs of the graphs in the triple store.
     * @returns An array of graph URIs in no particular order.
     */
    getGraphs(): string[] {
        const result = new Set<string>();
 
        for (const q of this._ds.match(null, null, null, null)) {
            result.add(q.graph.value);
        }
 
        return Array.from(result);
    }
 
    /**
     * Load a set of triples into the store.
     * @param quads An array of quads to be loaded into the store.
     * @param graphUri The target graph URI.
     * @param executeInference Indicates if inference should be executed after loading the triples.
     * @param clearGraph Indicates if the graph should be cleared before loading.
     * @param onQuad A callback function that will be called for each parsed triple.
     */
    private _loadQuads(quads: rdfjs.Quad[], graphUri: string, executeInference: boolean = true, clearGraph: boolean = true, onQuad?: (quad: rdfjs.Quad) => void) {
        const graph = this.dataFactory.namedNode(graphUri.replace('\\', '\/'));
 
        // Only clear the graph once if there had been no errors.
        Eif (clearGraph) {
            // Note: Using the match method to clear the graph results in an error because the graph is being modified.
            this.deleteGraphs([graphUri]);
 
            if (this.reasoner) {
                const inferenceGraph = this.dataFactory.namedNode(this.reasoner.targetUriGenerator.getGraphUri(graphUri));
 
                this.deleteGraphs([inferenceGraph.value]);
            }
        }
 
        for (let q of quads) {
            this._ds.add(this.dataFactory.quad(q.subject, q.predicate, q.object, graph));
 
            if (onQuad) {
                onQuad(q);
            }
        }
 
        if (this.reasoner && executeInference) {
            this.reasoner.expand(this._ds, graphUri);
        }
    }
 
    /**
     * Create an RDF store from N3, Turtle or N-Triples data.
     * @param input Input data or stream in to be parsed.
     * @param graphUri URI of the graph to in which the triples will be created.
     * @param executeInference Indicates if inference should be executed after loading the triples.
     * @param clearGraph Indicates if the graph should be cleared before loading.
     * @param onQuad Callback function that will be called for each parsed triple.
     * @returns A promise that resolves to an RDF store.
     */
    async loadFromTurtleStream(input: string | EventEmitter, graphUri: string, executeInference: boolean = true, clearGraph: boolean = true, onQuad?: (quad: rdfjs.Quad) => void): Promise<Store> {
        return new Promise((resolve, reject) => {
            let quads: rdfjs.Quad[] = [];
 
            new n3.Parser({}).parse(input, (error, quad, done) => {
                if (error) {
                    reject(error);
                } else if (quad) {
                    quads.push(quad);
                } else if (Edone) {
                    this._loadQuads(quads, graphUri, executeInference, clearGraph, onQuad);
 
                    resolve(this);
                }
            });
        });
    }
 
    /**
     * Create an RDF store from RDF/XML data.
     * @param input Input data or stream format to be parsed.
     * @param graphUri URI of the graph to in which the triples will be created.
     * @param executeInference Indicates if inference should be executed after loading the triples.
     * @param clearGraph Indicates if the graph should be cleared before loading.
     * @param onQuad Callback function that will be called for each parsed triple.
     * @returns A promise that resolves to an RDF store.
     */
    async loadFromXmlStream(input: string | EventEmitter, graphUri: string, executeInference: boolean = true, clearGraph: boolean = true, onQuad?: (quad: rdfjs.Quad) => void): Promise<Store> {
        return new Promise((resolve, reject) => {
            let quads: rdfjs.Quad[] = [];
 
            const parser = new RdfXmlParser();
 
            Iif (typeof input === 'string') {
                parser.on('error', (error) => reject(error));
                parser.on('data', (quad) => quads.push(quad));
                parser.on('end', () => {
                    this._loadQuads(quads, graphUri, executeInference, clearGraph, onQuad);
 
                    resolve(this);
                });
 
                parser.write(input);
                parser.end();
            } else {
                parser.import(input)
                    .on('error', (error) => reject(error))
                    .on('data', (quad) => quads.push(quad))
                    .on('end', () => {
                        this._loadQuads(quads, graphUri, executeInference, clearGraph, onQuad);
 
                        resolve(this);
                    });
            }
        });
    }
 
    /**
     * Write the triples in the store into a string in Turtle format.
     * @param sourceGraphUri A graph URI.
     * @param targetFormat Optional mime type of the serialization format (e.g., 'text/turtle').
     * @param targetGraphUri Optional target graph URI. If not provided, the source graph URI will be used for serialization formats that support quads.
     * @param prefixes Optional prefixes to be used in the serialization.
     * @returns A string serialization of the triples in the graph in the specified format.
     */
    async serializeGraph(sourceGraphUri: string, targetFormat: string = 'text/turtle', targetGraphUri?: string, prefixes?: Record<string, string>): Promise<string> {
        return new Promise((resolve, reject) => {
            const sourceGraph = this.dataFactory.namedNode(sourceGraphUri);
            const targetGraph = targetGraphUri ? this.dataFactory.namedNode(targetGraphUri) : undefined;
            const store = graph();
 
            // Unfortunately the termType values of RDFJS terms are not directly compatible with rdflib.
            // For serialization, rdflib expectes a compareTerm function to sort blank nodes which does not
            // exist in the RDFJS interface or n3.Terms.
            const g = targetGraph ? toRdflibTerm(targetGraph) as any : undefined;
 
            for (const q of this._ds.match(null, null, null, sourceGraph)) {
                const s = toRdflibTerm(q.subject) as any;
                const p = toRdflibTerm(q.predicate) as any;
                const o = toRdflibTerm(q.object) as any;
 
                store.add(s, p, o, g);
            }
 
            const callback = (error: any, result?: string) => {
                Iif (error) {
                    reject(error);
                } else {
                    resolve(result ?? '');
                }
            };
 
            serialize(null, store as any, null, targetFormat, callback, prefixes);
        });
    }
 
    /**
     * Indicates if the store contains triples in a given graph.
     * @param graphUri A graph URI.
     * @returns `true` if the store contains triples in the graph URI, `false` otherwise.
     */
    hasGraph(graphUri: rdfjs.Quad_Graph | string): boolean {
        const uri = typeof graphUri === 'string' ? this.dataFactory.namedNode(graphUri) : graphUri;
 
        for (const _ of this._ds.match(null, null, null, uri)) {
            return true;
        }
 
        return false;
    }
 
    /**
     * Apply inference to the given graph and store the triples in the associated inference graph.
     * @param graphUri A graph URI.
     */
    executeInference(graphUri: rdfjs.Quad_Graph | string) {
        Eif (this.reasoner) {
            this.reasoner.expand(this._ds, graphUri);
        }
    }
 
    /**
     * Delete named graphs from the store.
     * @param graphUris URIs of the graphs to be deleted.
     */
    deleteGraphs(graphUris: string[]) {
        for (const graphUri of graphUris) {
            const graph = this.dataFactory.namedNode(graphUri);
 
            for (const quad of this._ds.match(null, null, null, graph)) {
                this._ds.delete(quad);
            }
        }
    }
 
    /**
     * Get the URIs of ordered list members in the store.
     * @param graphUris Optional graph URI or array of graph URIs to query.
     * @param listUri URI of the list to get the items from.
     * @returns An array of URIs of the items in the list.
     */
    getListItems(graphUris: string | string[] | undefined, listUri: string): string[] {
        // To do: Fix #10
        const list = listUri.includes(':') ?
            this.dataFactory.namedNode(listUri) :
            this.dataFactory.blankNode(listUri);
 
        return this._getListItems(graphUris, list).map(t => t.value);
    }
 
    private _getListItems(graphUris: string | string[] | undefined, subject: rdfjs.Quad_Subject): rdfjs.Quad_Subject[] {
        const first = Array.from(this.matchAll(graphUris, subject, rdf.first, null));
 
        Iif (!first.length) {
            return [];
        }
 
        const rest = Array.from(this.matchAll(graphUris, subject, rdf.rest, null));
 
        const firstItem = first[0].object as rdfjs.Quad_Subject;
        const restList = rest[0]?.object as rdfjs.Quad_Subject;
 
        if (restList.value === RDF.nil) {
            return [firstItem];
        } else {
            const restItems = this._getListItems(graphUris, restList);
 
            return [firstItem, ...restItems];
        }
    }
 
    /**
     * Returns the exact cardinality of the quads matching the pattern.
     * @param subject The optional subject.
     * @param predicate The optional predicate.
     * @param object The optional object.
     * @param graph The optional graph.
     */
    countQuads(subject?: rdfjs.Term | null, predicate?: rdfjs.Term | null, object?: rdfjs.Term | null, graph?: rdfjs.Term | null): number {
        return this._store.countQuads(subject, predicate, object, graph);
    }
 
    /**
     * Returns a stream of quads matching the given pattern.
     * @param subject The optional subject.
     * @param predicate The optional predicate.
     * @param object The optional object.
     * @param graph The optional graph.
     * @returns A stream of matching quads.
     */
    match(subject?: rdfjs.Term | null, predicate?: rdfjs.Term | null, object?: rdfjs.Term | null, graph?: rdfjs.Term | null): rdfjs.Stream<rdfjs.Quad> {
        return this._store.match(subject, predicate, object, graph);
    }
 
    /**
     * Query the store for triples matching the given pattern supporting multiple graphs.
     * @param graphUris Optional graph URI or array of graph URIs to query.
     * @param subject A subject URI or null to match any subject.
     * @param predicate A predicate URI or null to match any predicate.
     * @param object An object URI or null to match any object.
     */
    *matchAll(graphUris: string | string[] | undefined, subject: rdfjs.Quad_Subject | null, predicate: rdfjs.Quad_Predicate | null, object: rdfjs.Quad_Object | null, includeInferred?: boolean) {
        if (includeInferred && !this.reasoner) {
            throw new Error('Reasoner is not available to include inferred triples.');
        }
 
        const s = subject as n3.Term;
        const p = predicate as n3.Term;
        const o = object as n3.Term;
 
        if (graphUris !== undefined) {
            const graphs = Array.isArray(graphUris) ? graphUris : [graphUris];
 
            for (const graph of graphs.map(g => this.dataFactory.namedNode(g))) {
                for (const q of this._ds.match(s, p, o, graph)) {
                    yield q;
                }
 
                if (includeInferred !== false && this.reasoner) {
                    const inferenceGraphUri = this.reasoner.targetUriGenerator.getGraphUri(graph.value);
                    const inferenceGraph = this.dataFactory.namedNode(inferenceGraphUri);
 
                    for (let q of this._ds.match(s, p, o, inferenceGraph)) {
                        yield q;
                    }
                }
            }
        } else {
            yield* this._ds.match(s, p, o);
        }
    }
 
    /**
     * Indicate if there are triples matching the given pattern in the store.
     * @param graphUris Optional graph URI or array of graph URIs to query.
     * @param subject A subject URI or null to match any subject.
     * @param predicate A predicate URI or null to match any predicate.
     * @param object An object URI or null to match any object.
     * @returns `true` if there are triples matching the pattern, `false` otherwise.
     */
    any(graphUris: string | string[] | undefined, subject: rdfjs.Quad_Subject | null, predicate: rdfjs.Quad_Predicate | null, object: rdfjs.Quad_Object | null, includeInferred?: boolean): boolean {
        return this.matchAll(graphUris, subject, predicate, object, includeInferred).next().done === false;
    }
 
    /**
     * Get the first triple matching the given pattern in the store.
     * @param graphUris Optional graph URI or array of graph URIs to query.
     * @param subject A subject URI or null to match any subject.
     * @param predicate A predicate URI or null to match any predicate.
     * @param object An object URI or null to match any object.
     * @returns The first triple that matches the pattern.
     * @throws {TripleNotFoundError} If no triple is found matching the pattern.
     */
    first(graphUris: string | string[] | undefined, subject: rdfjs.Quad_Subject | null, predicate: rdfjs.Quad_Predicate | null, object: rdfjs.Quad_Object | null, includeInferred?: boolean) {
        const result = this.matchAll(graphUris, subject, predicate, object, includeInferred).next().value;
 
        Iif (!result) {
            throw new TripleNotFoundError(subject, predicate, object);
        }
 
        return result;
    }
}