All files / rdf individual-repository.ts

96.66% Statements 29/30
90.9% Branches 20/22
100% Functions 4/4
96.66% Lines 29/30

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            6x           6x                   9x 9x   9x 460x 1x       459x 471x       471x 79x   79x                         27x   27x 2342x 3x     2339x 1900x 1900x   1900x                         476x 476x   476x 939x   939x 1x     938x 35x       441x    
import { rdf, owl } from "../ontologies";
import { Store } from "./store";
import { PropertyRepository } from "./property-repository";
import { DefinitionQueryOptions } from "./resource-repository";
import { dataFactory } from "./data-factory";
 
const { namedNode } = dataFactory;
 
/**
 * A repository for retrieving classes from graphs.
 */
export class IndividualRepository extends PropertyRepository {
    constructor(store: Store) { super(store); }
 
    /**
     * Get all classes that have instances or all types of a specific individual.
     * @param graphUris The graph URIs to search.
     * @param subjectUri The URI of a subject for which to get the types (optional).
     * @param options Options for retrieving the individuals.
     * @returns An iterator of all individual types, or all types of a specific individual.
     */
    *getIndividualTypes(graphUris: string | string[] | undefined, subjectUri?: string, options?: DefinitionQueryOptions): IterableIterator<string> {
        const yielded = new Set<string>();
        const subject = subjectUri ? namedNode(subjectUri) : null;
 
        for (const q of this.store.matchAll(graphUris, subject, rdf.type, owl.NamedIndividual)) {
            if (this.skip(graphUris, q.subject, options)) {
                continue;
            }
 
            // Note: We do not include inferred types here.
            for (const p of this.store.matchAll(graphUris, q.subject, rdf.type, null, false)) {
                Iif (p.object.equals(owl.NamedIndividual)) {
                    continue;
                }
 
                if (!yielded.has(p.object.value)) {
                    yielded.add(p.object.value);
 
                    yield p.object.value;
                }
            }
        }
    }
 
    /**
     * Get all individuals in the repository.
     * @param typeUri The type of the individuals to get.
     * @param options Options for retrieving the individuals.
     * @returns An iterator of all individuals in the repository.
     */
    *getIndividuals(graphUris: string | string[] | undefined, typeUri?: string, options?: DefinitionQueryOptions): IterableIterator<string> {
        const yielded = new Set<string>();
 
        for (const q of this.store.matchAll(graphUris, null, rdf.type, owl.NamedIndividual)) {
            if (this.skip(graphUris, q.subject, options)) {
                continue;
            }
 
            if (!typeUri || this.isInstanceOfType(graphUris, q.subject.value, typeUri)) {
                Eif (!yielded.has(q.subject.value)) {
                    yielded.add(q.subject.value);
                    
                    yield q.subject.value;
                }
            }
        }
    }
 
    /**
     * Indicate if the subject is an instance of the type, or of one of its super types.
     * @param subjectUri URI of the subject.
     * @param typeUri URI of the type to check.
     * @returns true if the subject is an instance of the type, or of one of its super types.
     */
    isInstanceOfType(graphUris: string | string[] | undefined, subjectUri: string, typeUri: string): boolean {
        const subject = namedNode(subjectUri);
        const type = namedNode(typeUri);
 
        for (const q of this.store.matchAll(graphUris, subject, rdf.type, null)) {
            const o = q.object;
 
            if (o.termType != "NamedNode") {
                continue;
            }
 
            if (o.equals(type) || this.isSubClassOf(graphUris, o.value, type.value)) {
                return true;
            }
        }
 
        return false;
    }
}