All files / rdf/tests helpers.ts

96.66% Statements 29/30
87.5% Branches 7/8
100% Functions 10/10
96.42% Lines 27/28

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                            114x                 139x 139x 1x     139x 139x   139x   1x                         78x 78x   78x     78x     77x                     2x   630x                     2x 2x   2x                     39x 39x 38x   38x 1x     37x                     1x 1x   1x  
import * as rdfjs from "@rdfjs/types";
import * as fs from "fs";
import * as path from "path";
import * as url from "url";
import { BLANK_NODE_TOKEN_NAMES, createFileBlankNodeIdGenerator, getBlankNodeIdFromToken, TurtleLexer } from "@faubulous/mentor-rdf-parsers";
import { Reasoner } from "../reasoners/reasoner";
import { Store } from "../store";
 
/**
 * Get the URL of a file.
 * @param filePath A file system path.
 * @returns A URL to the file.
 */
export function pathToFileURL(filePath: string): string {
    return url.pathToFileURL(path.resolve(filePath)).href;
}
 
/**
 * Read the content of a file.
 * @param filePath A file path.
 * @returns The content of the file as a string.
 */
export async function readFile(filePath: string): Promise<string> {
    return new Promise((resolve, reject) => {
        if (!fs.existsSync(filePath)) {
            reject(`File not found: ${filePath}`);
        }
 
        try {
            const result = fs.readFileSync(filePath).toString();
 
            resolve(result);
        } catch (error) {
            reject(error);
        }
    });
}
 
/**
 * Load a Turtle or N3 file into a store.
 * @param store An RDF store.
 * @param filePath Path to a file containing RDF triples in Turtle or N3 format.
 * @param onQuad Callback function that will be called for each parsed triple.
 * @returns A promise that resolves to the URI of the graph that was loaded.
 */
export async function loadFile(store: Store, filePath: string, graphUri?: string, onQuad?: (quad: rdfjs.Quad) => void): Promise<string> {
    const graph = graphUri ?? pathToFileURL(filePath);
    const data = await readFile(filePath);
 
    Iif (filePath.endsWith('.nq')) {
        store.loadNQuads(data, graph, true, true, onQuad);
    } else {
        store.loadTurtle(data, graph, true, true, onQuad);
    }
 
    return graph;
}
 
/**
 * Get the ids the store gives the blank nodes of a Turtle file loaded into a graph, in the order
 * their tokens are written: the id of the first `[` or `(` comes first.
 * @param filePath Path to a Turtle file.
 * @param graphUri The URI of the graph the file is loaded into.
 * @returns The blank node ids.
 */
export async function getBlankNodeIds(filePath: string, graphUri: string): Promise<string[]> {
    const tokens = new TurtleLexer(createFileBlankNodeIdGenerator(graphUri)).tokenize(await readFile(filePath)).tokens;
 
    return tokens.filter(token => BLANK_NODE_TOKEN_NAMES.has(token.tokenType.name)).map(token => getBlankNodeIdFromToken(token)!);
}
 
/**
 * Create a RDF triple store from a string.
 * @param filePath Path to a file containing RDF triples in Turtle or N3 format.
 * @param reasoner A reasoner to be used with the store.
 * @param onQuad Callback function that will be called for each parsed triple.
 * @returns A promise that resolves to an RDF store.
 */
export async function createStoreFromString(filePath: string, reasoner?: Reasoner, onQuad?: (quad: rdfjs.Quad) => void): Promise<Store> {
    const graphUri = pathToFileURL(filePath);
    const data = await readFile(filePath);
 
    return new Store(reasoner).loadTurtle(data, graphUri, reasoner != null, true, onQuad);
}
 
/**
* Create a RDF triple store from a file.
* @param path Path to a file containing RDF triples in Turtle or N3 format.
* @param reasoner A reasoner to be used with the store.
* @param onQuad Callback function that will be called for each parsed triple.
* @returns A promise that resolves to an RDF store.
*/
export async function createStoreFromFile(filePath: string, reasoner?: Reasoner, onQuad?: (quad: rdfjs.Quad) => void): Promise<Store> {
    const graphUri = pathToFileURL(filePath);
    const data = await readFile(filePath);
    const store = new Store(reasoner);
 
    if (filePath.endsWith('.nq')) {
        return store.loadNQuads(data, graphUri, reasoner != null, true, onQuad);
    }
 
    return store.loadTurtle(data, graphUri, reasoner != null, true, onQuad);
}
 
/**
* Create a RDF triple store from a file.
* @param path Path to a file containing RDF triples in Turtle or N3 format.
* @param reasoner A reasoner to be used with the store.
* @param onQuad Callback function that will be called for each parsed triple.
* @returns A promise that resolves to an RDF store.
*/
export async function createStoreFromXmlFile(filePath: string, reasoner?: Reasoner, onQuad?: (quad: rdfjs.Quad) => void): Promise<Store> {
    const graphUri = pathToFileURL(filePath);
    const stream = fs.createReadStream(filePath);
 
    return new Store(reasoner).loadFromXmlStream(stream, graphUri, reasoner != null, true, onQuad);
}