All files / rdf property-repository.ts

100% Statements 117/117
91.52% Branches 54/59
100% Functions 23/23
100% Lines 112/112

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              8x                 9x         9x   9x               40x   40x 7417x 1522x     5895x 4912x   4912x                         18x   18x 419x 187x       18x 18x 18x 18x             18x 81x   81x 50x   50x 5x 45x 12x   33x     50x     81x       18x 138x   138x 50x   50x     138x           137x   72x                     102x 76x 73x       29x                   11x 11x   11x 10x 1x     9x 9x     9x                         8x   8x 5x   5x     3x                   3x                       1x                   3x 3x   3x 2x 2x       1x                   12x 8x 8x 8x   8x     13x 1x     12x 12x     12x     4x                   10x 10x     10x     10x   10x 126x 93x     33x   33x 26x   26x   26x     33x     108x                 3x     3x 2x     1x                 3x   3x 2x     1x                 5x   5x 4x     1x                   6x   6x 77x 79x   77x 75x   79x 12x   12x         2x 2x   2x          
import { Quad_Subject } from "@rdfjs/types";
import { RDF, rdf, rdfs, owl } from "../ontologies";
import { ClassRepository } from "./class-repository";
import { Store } from "./store";
import { DefinitionQueryOptions } from "./resource-repository";
import { RdfDataFactory } from "./rdf-data-factory";
 
const { namedNode } = RdfDataFactory;
 
/**
 * A repository for retrieving properties from graphs.
 */
export class PropertyRepository extends ClassRepository {
    /**
     * The predicate used to indicate the domain of a property.
     */
    public domainPredicate = rdfs.domain;
 
    /**
     * The predicate used to indicate the range of a property.
     */
    public rangePredicate = rdfs.range;
 
    constructor(store: Store) { super(store); }
 
    /**
     * Get all properties in the repository.
     * @param options Optional options for retrieving properties.
     * @returns A list of all properties in the repository.
     */
    *getProperties(graphUris: string | string[] | undefined, options?: DefinitionQueryOptions): IterableIterator<string> {
        const yielded = new Set<string>();
 
        for (let q of this.store.matchAll(graphUris, null, rdf.type, rdf.Property, options?.includeInferred)) {
            if (this.skip(graphUris, q.subject, options)) {
                continue;
            }
 
            if (!yielded.has(q.subject.value)) {
                yielded.add(q.subject.value);
 
                yield q.subject.value;
            }
        }
    }
 
    /**
     * Get properties of a given type.
     * @param typeUri URI of a property type.
     * @param includeInferred Indicate if properties of a more specific type should be included in the result.
     * @returns An iterator of properties of the given type.
     */
    *getRootPropertiesOfType(graphUris: string | string[] | undefined, typeUri: string, options?: DefinitionQueryOptions): IterableIterator<string> {
        // The properties of the given type, by their URI or blank node identifier.
        const members = new Map<string, Quad_Subject>();
 
        for (let q of this.store.matchAll(graphUris, null, rdf.type, namedNode(typeUri), options?.includeInferred)) {
            if (!this.skip(graphUris, q.subject, options)) {
                members.set(q.subject.value, q.subject);
            }
        }
 
        const ontologies = this.getOtherOntologiesOnce(graphUris, options);
        const superProperties = this.getParentMap(graphUris, rdfs.subPropertyOf, options);
        const referenced = new Map<string, boolean>();
        const own = new Map<string, boolean>();
 
        // A referenced super property is listed with the properties of the given type: we must assume that
        // it is of the same type or a super type of it. With an ontology selected, a super property is
        // referenced unless another ontology of the document defines it; annotations or a declaration of
        // a property of another vocabulary do not define it. Without one, it is referenced if the document
        // states nothing about it.
        const isReferenced = (iri: string): boolean => {
            let result = referenced.get(iri);
 
            if (result === undefined) {
                const node = namedNode(iri);
 
                if (!(options?.includeReferenced ?? false)) {
                    result = false;
                } else if (options?.definedBy) {
                    result = this.skip(graphUris, node, options) && this.isReferenced(graphUris, node, options, ontologies);
                } else {
                    result = !this.hasSubject(graphUris, iri);
                }
 
                referenced.set(iri, result);
            }
 
            return result;
        };
 
        // A super property of the document that is not skipped; its sub properties are listed below it.
        const isOwn = (iri: string): boolean => {
            let result = own.get(iri);
 
            if (result === undefined) {
                result = !isReferenced(iri) && !this.skip(graphUris, namedNode(iri), options);
 
                own.set(iri, result);
            }
 
            return result;
        };
 
        // In the case of rdf:Property, we do not want to include properties that have a more specific type.
        // Every property is an instance of rdf:Property, so they would be listed twice otherwise. This holds
        // for a referenced root as well, such as an external property the document declares with its type.
        const canBeRoot = (iri: string): boolean => typeUri !== RDF.Property || !this._hasSpecificType(graphUris, members.get(iri) ?? namedNode(iri));
 
        yield* this.getHierarchyRoots(members.keys(), superProperties, isOwn, iri => isOwn(iri) || isReferenced(iri), canBeRoot);
    }
 
    /**
     * Indicate whether a property is stated to be of a type that is more specific than rdf:Property.
     * Only asserted types are taken into account, because every property is an inferred rdf:Property.
     * @param graphUris URI of the graph or an array of graphs to search.
     * @param property The node of a property.
     * @returns `true` if the property has a more specific asserted type, `false` otherwise.
     */
    private _hasSpecificType(graphUris: string | string[] | undefined, property: Quad_Subject): boolean {
        for (let q of this.store.matchAll(graphUris, property, rdf.type, null, false)) {
            if (q.object.value !== RDF.Property) {
                return true;
            }
        }
 
        return false;
    }
 
    /**
     * Get the super properties of a given property.
     * @param subjectUri URI of a property.
     * @param options Optional options for retrieving properties.
     * @returns An array of super properties of the given property, an empty array if the property has no super properties.
     */
    *getSuperProperties(graphUris: string | string[] | undefined, subjectUri: string, options?: DefinitionQueryOptions): IterableIterator<string> {
        const yielded = new Set<string>();
        const s = namedNode(subjectUri);
 
        for (let q of this.store.matchAll(graphUris, s, rdfs.subPropertyOf, null, options?.includeInferred)) {
            if (this.skip(graphUris, q.object, options)) {
                continue;
            }
 
            Eif (!yielded.has(q.object.value)) {
                yield q.object.value;
            }
 
            yielded.add(q.object.value);
        }
    }
 
    /**
     * Recursively find the first path from a given property to a root property.
     * @param subjectUri URI of a property.
     * @param path The current property path.
     * @param backtrack Set of URIs that have already been visited.
     * @param options Optional options for retrieving properties.
     * @returns The first path that is found from the given property to a root class.
     */
    private _getRootPropertyPath(graphUris: string | string[] | undefined, subjectUri: string, path: string[], backtrack: Set<string>, options?: DefinitionQueryOptions): string[] {
        const superClasses = [...this.getSuperProperties(graphUris, subjectUri, options)];
 
        for (let o of superClasses.filter(o => !backtrack.has(o))) {
            backtrack.add(o);
 
            return this._getRootPropertyPath(graphUris, o, [...path, o], backtrack, options);
        }
 
        return path;
    }
 
    /**
     * Get the first discovered path from a given property to a root property.
     * @param subjectUri URI of a property.
     * @param options Optional options for retrieving properties.
     * @returns A string array containing the first path that is found from the given property to a root property.
     */
    getRootPropertiesPath(graphUris: string | string[] | undefined, subjectUri: string, options?: DefinitionQueryOptions): string[] {
        return this._getRootPropertyPath(graphUris, subjectUri, [], new Set<string>(), options);
    }
 
    /**
     * Indicate if a given property is direct or indirect (inferred) sub property of another property.
     * @param graphUris URIs of the graphs to search, `undefined` for the default graph.
     * @param subjectUri URI of the sub property.
     * @param classUri URI of the super property.
     * @param options Optional query parameters.
     * @returns `true` if the property is a sub property of the other property, false otherwise.
     */
    isSubPropertyOf(graphUris: string | string[] | undefined, subjectUri: string, classUri: string, options?: DefinitionQueryOptions): boolean {
        return this.getRootPropertiesPath(graphUris, subjectUri, options).includes(classUri);
    }
 
    /**
     * Indicate if there are sub properties of a given property.
     * @param subjectUri URI of a property.
     * @param options Optional options for retrieving properties.
     * @returns true if the property has sub properties, false otherwise.
     */
    hasSubProperties(graphUris: string | string[] | undefined, subjectUri: string, options?: DefinitionQueryOptions): boolean {
        const o = namedNode(subjectUri);
        const ontologies = this.getOtherOntologiesOnce(graphUris, options);
 
        for (let q of this.store.matchAll(graphUris, null, rdfs.subPropertyOf, o, options?.includeInferred)) {
            Eif (!this.skip(graphUris, q.subject, options) || this.isReferencedAbove(graphUris, q.subject, rdfs.subPropertyOf, options, ontologies)) {
                return true;
            }
        }
 
        return false;
    }
 
    /**
     * Get the sub properties of a given property or all root properties.
     * @param subjectUri URI of a property or undefined to get all root properties.
     * @param options Optional options for retrieving properties.
     * @returns An array of sub properties of the given property, an empty array if the property has no sub properties.
     */
    *getSubProperties(graphUris: string | string[] | undefined, subjectUri?: string, options?: DefinitionQueryOptions): IterableIterator<string> {
        if (subjectUri) {
            const yielded = new Set<string>();
            const o = namedNode(subjectUri);
            const ontologies = this.getOtherOntologiesOnce(graphUris, options);
 
            for (let q of this.store.matchAll(graphUris, null, rdfs.subPropertyOf, o, options?.includeInferred)) {
                // A referenced property is listed if it leads to a property of the document, so that
                // the hierarchy from a referenced root property down to the document is not broken.
                if (this.skip(graphUris, q.subject, options) && !this.isReferencedAbove(graphUris, q.subject, rdfs.subPropertyOf, options, ontologies)) {
                    continue;
                }
 
                Eif (!yielded.has(q.subject.value)) {
                    yield q.subject.value;
                }
 
                yielded.add(q.subject.value);
            }
        } else {
            yield* this.getRootProperties(graphUris, options);
        }
    }
 
    /**
     * Get all properties from the repository that have no super properties.
     * @param options Optional options for retrieving properties.
     * @returns An iterator of root properties in the repository.
     */
    *getRootProperties(graphUris: string | string[] | undefined, options?: DefinitionQueryOptions): IterableIterator<string> {
        const properties = new Set<string>(this.getProperties(graphUris, options));
        const ontologies = this.getOtherOntologiesOnce(graphUris, options);
 
        // All super property relations are read in one pass rather than per property.
        const superProperties = this.getParentMap(graphUris, rdfs.subPropertyOf, options);
 
        // Whether a super property is shown depends on the node alone, so it is decided once.
        const visible = new Map<string, boolean>();
 
        const isVisible = (iri: string): boolean => {
            if (properties.has(iri)) {
                return true;
            }
 
            let result = visible.get(iri);
 
            if (result === undefined) {
                const node = namedNode(iri);
 
                result = !this.skip(graphUris, node, options) || this.isReferenced(graphUris, node, options, ontologies);
 
                visible.set(iri, result);
            }
 
            return result;
        };
 
        yield* this.getHierarchyRoots(properties, superProperties, iri => properties.has(iri), isVisible);
    }
 
    /**
     * Indicate if there is an equivalent property of a given property.
     * @param propertyUri URI of a property.
     * @returns true if the property has an equivalent property, false otherwise.
     */
    public hasEquivalentProperty(graphUris: string | string[] | undefined, propertyUri: string): boolean {
        const s = namedNode(propertyUri);
 
        // The OWL resoner will assert the equivalent class relationship in both directions.
        for (let _ of this.store.matchAll(graphUris, s, owl.equivalentProperty, null)) {
            return true;
        }
 
        return false;
    }
 
    /**
     * Get the domain of a given property.
     * @param propertyUri URI of a property.
     * @returns The URI of the domain of the given property. If no domain is specified, rdfs:Resource is returned.
     */
    public getDomain(graphUris: string | string[] | undefined, propertyUri: string): string {
        const s = namedNode(propertyUri);
 
        for (let q of this.store.matchAll(graphUris, s, this.domainPredicate, null)) {
            return q.object.value;
        }
 
        return rdfs.Resource.value;
    }
 
    /**
     * Get the range of a given property.
     * @param propertyUri URI of a property.
     * @returns The URI of the range of the given property. If no range is specified, `undefined` is returned.
     */
    public getRange(graphUris: string | string[] | undefined, propertyUri: string): string | undefined {
        const s = namedNode(propertyUri);
 
        for (let q of this.store.matchAll(graphUris, s, this.rangePredicate, null)) {
            return q.object.value;
        }
 
        return undefined;
    }
 
    /**
     * Get all asserted and inferred property types.
     * @param graphUris The URI of the graph or an array of graphs to search for property types.
     * @param options Optional options for retrieving properties.
     * @returns An iterator of all property types in the repository.
     */
    *getPropertyTypes(graphUris: string | string[] | undefined, options?: DefinitionQueryOptions): IterableIterator<string> {
        const yielded = new Set<string>();
 
        for (let property of this.getProperties(graphUris, options)) {
            const p = namedNode(property);
            const types = new Set<string>(Array.from(this.store.matchAll(graphUris, p, rdf.type, null, false)).map(t => t.object.value));
 
            if (types.size > 0) {
                for (let t of types) {
                    // Do not assert rdf:Property for properties that have multiple types.
                    if ((t !== RDF.Property || types.size == 1) && !yielded.has(t)) {
                        yielded.add(t);
 
                        yield t;
                    }
                }
            } else {
                // If there are no asserted types, we need to infer rdf:Property.
                Eif (!yielded.has(RDF.Property)) {
                    yielded.add(RDF.Property);
 
                    yield RDF.Property;
                }
            }
        }
    }
}