All files / rdf property-repository.ts

100% Statements 113/113
92.42% Branches 61/66
100% Functions 17/17
100% Lines 110/110

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            7x                 7x         7x   7x               45x   45x 8112x 1489x     6623x 5660x   5660x                       13x 13x   13x 267x 215x     52x 52x   52x       24x   22x 2x   1x     23x 23x       52x 18x   18x     52x 22x     30x   30x   1x   1x 1x     29x     30x 26x   26x                       11x 11x   11x 10x 1x     9x 9x     9x                         8x   8x 5x   5x     3x                   3x                       1x                   2x   2x 1x 1x       1x                   8x 5x 5x   5x 10x 1x     9x 9x     9x     3x                   7x 7x   7x 253x   253x 112x 112x     112x 12x       100x     100x 3x   3x     100x     253x 155x   155x                     3x     3x 2x     1x                 2x   2x 1x     1x                 3x   3x 1x     2x                   6x   6x 77x 79x   77x 75x   79x 12x   12x         2x 2x   2x          
import { RDF, rdf, rdfs, owl } from "../ontologies";
import { ClassRepository } from "./class-repository";
import { Store } from "./store";
import { DefinitionQueryOptions } from "./resource-repository";
import { dataFactory } from "./data-factory";
 
const { namedNode } = dataFactory;
 
/**
 * 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> {
        const yielded = new Set<string>();
        const type = namedNode(typeUri);
 
        for (let q of this.store.matchAll(graphUris, null, rdf.type, type, options?.includeInferred)) {
            if (this.skip(graphUris, q.subject, options)) {
                continue;
            }
 
            let hasSuperProperty = false;
            let referencedSuperProperty: string | null = null;
 
            for (let q2 of this.store.matchAll(graphUris, q.subject, rdfs.subPropertyOf, null, options?.includeInferred)) {
                // Note: We have not skipped the property so it is relevant for our result. However, if we want 
                // to include referenced properties, then we include the referenced super property instead of the
                // current one.
                if (options?.includeReferenced && !this.hasSubject(graphUris, q2.object.value)) {
                    // We must assume that the referenced super property is of the same type as the given type or a super type of it.
                    referencedSuperProperty = q2.object.value;
                } else if (this.skip(graphUris, q2.object, options)) {
                    // If the super property is skipped, then we ignore it.
                    continue;
                }
 
                hasSuperProperty = q2.object.value != q2.subject.value;
                break;
            }
 
            // Yield referenced super property if applicable
            if (referencedSuperProperty && !yielded.has(referencedSuperProperty)) {
                yielded.add(referencedSuperProperty);
 
                yield referencedSuperProperty;
            }
 
            if (hasSuperProperty) {
                continue;
            }
 
            let shouldYield = false;
 
            if (typeUri === RDF.Property && options?.includeInferred === false) {
                // In the case of rdf:Property, we do not want to include properties that have a more specific type.
                const t = Array.from(this.store.matchAll(graphUris, q.subject, rdf.type, null, options?.includeInferred)).map(q => q.object.value);
 
                Eif (new Set(t).size == 1) {
                    shouldYield = true;
                }
            } else {
                shouldYield = true;
            }
 
            if (shouldYield && !yielded.has(q.subject.value)) {
                yielded.add(q.subject.value);
 
                yield q.subject.value;
            }
        }
    }
 
    /**
     * 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);
 
        for (let q of this.store.matchAll(graphUris, null, rdfs.subPropertyOf, o, options?.includeInferred)) {
            Eif (!this.skip(graphUris, q.subject, options)) {
                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);
 
            for (let q of this.store.matchAll(graphUris, null, rdfs.subPropertyOf, o, options?.includeInferred)) {
                if (this.skip(graphUris, q.subject, options)) {
                    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 yielded = new Set<string>();
        const properties = new Set<string>(this.getProperties(graphUris, options));
 
        for (let p of properties) {
            let hasSuperProperty = false;
 
            for (let q of this.store.matchAll(graphUris, namedNode(p), rdfs.subPropertyOf, null, options?.includeInferred)) {
                const includeReferenced = options?.includeReferenced ?? false;
                const skip = this.skip(graphUris, q.object, options);
 
                // Do not skip the super property if it is only referenced and the includeReferenced option is set.
                if (q.object.termType != "NamedNode" || skip && (!includeReferenced || includeReferenced && this.hasSubject(graphUris, q.object.value))) {
                    continue;
                }
 
                // We have at least one super property that is not the property itself.
                hasSuperProperty = q.object.value != q.subject.value;
 
                // If we have a super property that is not in the list of properties and not excluded by the options, we add it to the result.
                if (!properties.has(q.object.value) && !yielded.has(q.object.value)) {
                    yielded.add(q.object.value);
 
                    yield q.object.value;
                }
 
                break;
            }
 
            if (!hasSuperProperty && !yielded.has(p)) {
                yielded.add(p);
 
                yield p;
            }
        }
    }
 
    /**
     * 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;
                }
            }
        }
    }
}