All files / rdf concept-repository.ts

89.42% Statements 296/331
81.2% Branches 121/149
94.87% Functions 37/39
89.42% Lines 296/331

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 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056                        6x   6x 5752x   5752x 2879x   2879x                                                             22x   22x 881x   881x 431x   431x       22x 29x   29x 13x   13x                               587x       587x 544x     43x 26x     17x                   47x   47x 33x     14x                     26x 26x   26x 16x     10x                               9x 3x     6x 6x   6x       6x                             3x       3x                           58x 58x   58x 116x 7341x   7341x 7290x   7290x                                           32x 32x   32x 54x   54x 54x   54x       31x 43x   43x 2x   2x           31x                   31x                   31x 31x   31x 4362x 17x   17x                               31x   31x   4627x 4457x       31x 4453x 4439x       31x                                   22x 22x   22x 22x   22x 48x     22x 2915x   2915x   2915x 5946x 3070x         2876x 9x     2867x   2867x                           24x   24x 6217x   6217x 5421x   796x       24x 2973x     24x 3105x     24x 74x     24x 65x     24x                       8x   8x 11x 1452x       8x   8x 2939x   2939x 2897x     42x   42x 34x                                 2x 2x 2x 2x   2x 13x 7x   7x         2x 13x 2x   2x                       2x   2x 12x 4x     8x   10x 8x         2x                     9x   9x   9x 13x   13x 10x 4x   4x                         348x   348x   348x 13952x     348x 10x     348x                   3x   3x 1x     2x 1x     1x                                                   12494x 12494x   12494x 534x     11960x 31x       11929x                             266x 266x   266x 212x   212x 212x   212x       67x 15x 15x   15x                                 67x 67x 67x   67x 15x 15x   15x         67x 32x   32x 15x     20x 17x 17x   17x                               239x 208x 205x       34x                         122x 121x   121x 4754x 4635x     119x 2x     117x   117x     1x                       6x 3x     3x                                   12x 12x     12x 230x 30x   30x       12x   3x       9x   9x 25x       9x 225x 2x   2x   2x                                   27x   27x 225x   225x   9206x 198x   198x                                 9x                       15x 9x 6x   6x       9x                 15x 15x   15x 3x   3x 3x   3x       12x 8x   8x 8x   8x       7x                   7x 3x   3x                           2x 1x     1x                           19x 1x   1x     18x 4x   3x     14x 14x 14x   19x                   14x 10x   10x 10x   10x 10x         13x                   13x 17x   17x 8x   8x 6x                       3x 2x     1x                     3x                   20x 20x   20x 5x       15x                 3x                     10x 7x 7x   7x       3x    
import * as rdfjs from "@rdfjs/types";
import { DefinitionQueryOptions, ResourceRepository } from "./resource-repository";
import { rdf, skos } from "../ontologies";
import { RdfDataFactory } from "./rdf-data-factory";
 
export class ConceptRepository extends ResourceRepository {
    /**
     * Get all concepts.
     * @param graphUris URIs of the graphs to search for concepts.
     * @returns An iterator of URIs of all concepts.
     */
    *getConcepts(graphUris: string | string[] | undefined): IterableIterator<string> {
        const yielded = new Set<string>();
 
        for (let q of this.store.matchAll(graphUris, null, rdf.type, skos.Concept)) {
            const s = q.subject;
 
            if (!yielded.has(s.value)) {
                yielded.add(s.value);
 
                yield s.value;
            }
        }
    }
 
    /**
     * Get all concept schemes.
     * @param graphUris URIs of the graphs to search for concepts.
     * @returns An iterator of URIs of all concept schemes.
     */
    *getConceptSchemes(graphUris: string | string[] | undefined): IterableIterator<string> {
        const yielded = new Set<string>();
 
        for (let q of this.store.matchAll(graphUris, null, rdf.type, skos.ConceptScheme)) {
            const s = q.subject;
 
            if (!yielded.has(s.value)) {
                yielded.add(s.value);
 
                yield s.value;
            }
        }
    }
 
    /**
     * Get all collections.
     * @param graphUris URI of the graphs to search for collections.
     * @param options Optional query parameters.
     * @returns An iterator of URIs of all collections.
     */
    *getCollections(graphUris: string | string[] | undefined, options?: DefinitionQueryOptions): IterableIterator<string> {
        const yielded = new Set<string>();
 
        for (let q of this.store.matchAll(graphUris, null, rdf.type, skos.Collection)) {
            const s = q.subject;
 
            if (!yielded.has(s.value) && !this._skipCollection(graphUris, s, options)) {
                yielded.add(s.value);
 
                yield s.value;
            }
        }
 
        for (let q of this.store.matchAll(graphUris, null, rdf.type, skos.OrderedCollection)) {
            const s = q.subject;
 
            if (!yielded.has(s.value) && !this._skipCollection(graphUris, s, options)) {
                yielded.add(s.value);
 
                yield s.value;
            }
        }
    }
 
    /**
     * Indicate if a collection should be excluded from a query result.
     *
     * Note: In contrast to other resource types, blank nodes and referenced collections are
     * included by default so that the result is unchanged when no options are provided.
     * @param graphUris URI of the graphs to search for collections.
     * @param subject The collection to test.
     * @param options Optional query parameters.
     * @returns `true` if the collection should be excluded, `false` otherwise.
     */
    private _skipCollection(graphUris: string | string[] | undefined, subject: rdfjs.Quad_Subject, options?: DefinitionQueryOptions): boolean {
        Iif (this.skip(graphUris, subject, options, { includeBlankNodes: true, includeReferenced: true })) {
            return true;
        }
 
        if (options?.inScheme === undefined) {
            return false;
        }
 
        if (options.inScheme === null) {
            return this.hasConceptScheme(graphUris, subject.value);
        }
 
        return !this.isInScheme(graphUris, subject.value, options.inScheme);
    }
 
    /**
     * Indicate if a resource is associated with any concept scheme via `skos:inScheme`.
     * @param graphUris URIs of the graphs to search for concepts.
     * @param subjectUri URI of a resource.
     * @returns `true` if the resource is in a concept scheme, `false` otherwise.
     */
    hasConceptScheme(graphUris: string | string[] | undefined, subjectUri: string): boolean {
        const s = RdfDataFactory.namedNode(subjectUri);
 
        for (let _ of this.store.matchAll(graphUris, s, skos.inScheme, null)) {
            return true;
        }
 
        return false;
    }
 
    /**
     * Indicate if a resource is associated with a given concept scheme via `skos:inScheme`.
     * @param graphUris URIs of the graphs to search for concepts.
     * @param subjectUri URI of a resource.
     * @param schemeUri URI of a concept scheme.
     * @returns `true` if the resource is in the given concept scheme, `false` otherwise.
     */
    isInScheme(graphUris: string | string[] | undefined, subjectUri: string, schemeUri: string): boolean {
        const s = RdfDataFactory.namedNode(subjectUri);
        const o = RdfDataFactory.namedNode(schemeUri);
 
        for (let _ of this.store.matchAll(graphUris, s, skos.inScheme, o)) {
            return true;
        }
 
        return false;
    }
 
    /**
     * Indicate if a concept is explicitly associated with a given concept scheme, either via
     * `skos:inScheme` or via `skos:topConceptOf`.
     *
     * Note: The `skos:broader` and `skos:narrower` relations are *not* taken into account, because
     * they may cross the boundaries of a concept scheme. Use {@link getAllConceptsInScheme} to get
     * the concepts that are displayed below a concept scheme.
     * @param graphUris URIs of the graphs to search for concepts.
     * @param subjectUri URI of a concept.
     * @param schemeUri URI of a concept scheme.
     * @returns `true` if the concept is associated with the concept scheme, `false` otherwise.
     */
    isConceptOfScheme(graphUris: string | string[] | undefined, subjectUri: string, schemeUri: string): boolean {
        if (this.isInScheme(graphUris, subjectUri, schemeUri)) {
            return true;
        }
 
        const s = RdfDataFactory.namedNode(subjectUri);
        const o = RdfDataFactory.namedNode(schemeUri);
 
        for (let _ of this.store.matchAll(graphUris, s, skos.topConceptOf, o)) {
            return true;
        }
 
        return false;
    }
 
    /**
     * Indicate if a concept can be displayed below a given concept scheme.
     *
     * Concepts that are not associated with any concept scheme belong to every concept scheme they
     * can be reached from, so that they are not hidden in vocabularies that only annotate their top
     * concepts with `skos:inScheme`.
     * @param graphUris URIs of the graphs to search for concepts.
     * @param subjectUri URI of a concept.
     * @param schemeUri URI of a concept scheme.
     * @returns `true` if the concept can be displayed below the concept scheme, `false` otherwise.
     */
    private _isVisibleInScheme(graphUris: string | string[] | undefined, subjectUri: string, schemeUri: string): boolean {
        Iif (this.isConceptOfScheme(graphUris, subjectUri, schemeUri)) {
            return true;
        }
 
        return !this.hasConceptScheme(graphUris, subjectUri);
    }
 
    /**
     * Get the concepts that are explicitly associated with a concept scheme, either via
     * `skos:inScheme` or via `skos:topConceptOf`.
     *
     * Note: Collections can be associated with a concept scheme via `skos:inScheme` as well. They
     * are not returned, because `skos:Collection` is disjoint with `skos:Concept`.
     * @param graphUris URIs of the graphs to search for concepts.
     * @param schemeUri URI of a concept scheme.
     * @returns An iterator of URIs of the concepts of the concept scheme.
     */
    *getConceptsOfScheme(graphUris: string | string[] | undefined, schemeUri: string): IterableIterator<string> {
        const yielded = new Set<string>();
        const o = RdfDataFactory.namedNode(schemeUri);
 
        for (let predicate of [skos.inScheme, skos.topConceptOf]) {
            for (let q of this.store.matchAll(graphUris, null, predicate, o)) {
                const s = q.subject.value;
 
                if (!yielded.has(s) && !this.isCollection(graphUris, s)) {
                    yielded.add(s);
 
                    yield s;
                }
            }
        }
    }
 
    /**
     * Get the top concepts of a concept scheme.
     *
     * In addition to the concepts that are explicitly designated as top concepts via
     * `skos:hasTopConcept` or `skos:topConceptOf`, this returns the concepts of the scheme that have
     * no broader concept *within the same scheme*. Without the latter, concepts whose `skos:broader`
     * property refers to a concept of another scheme would not be displayed below the scheme that
     * they are associated with.
     *
     * Note: A concept that is its own broader concept is not excluded, so that recursive definitions
     * do not remove it from the list of top concepts.
     * @param graphUris URIs of the graphs to search for concepts.
     * @param schemeUri URI of a concept scheme.
     * @returns An iterator of URIs of the top concepts of the concept scheme.
     */
    *getTopConcepts(graphUris: string | string[] | undefined, schemeUri: string): IterableIterator<string> {
        const yielded = new Set<string>();
        const s = RdfDataFactory.namedNode(schemeUri);
 
        for (let q of this.store.matchAll(graphUris, s, skos.hasTopConcept, null)) {
            const o = q.object;
 
            Eif (!yielded.has(o.value)) {
                yielded.add(o.value);
 
                yield o.value;
            }
        }
 
        for (let q of this.store.matchAll(graphUris, null, skos.topConceptOf, s)) {
            const o = q.subject;
 
            if (!yielded.has(o.value)) {
                yielded.add(o.value);
 
                yield o.value;
            }
        }
 
        // Note: A concept scheme is not a concept, so these two patterns are not valid SKOS. They
        // are matched for backwards compatibility with documents that use them regardless.
        for (let q of this.store.matchAll(graphUris, s, skos.narrower, null)) {
            const o = q.object;
 
            if (!yielded.has(o.value)) {
                yielded.add(o.value);
 
                yield o.value;
            }
        }
 
        for (let q of this.store.matchAll(graphUris, null, skos.broader, s)) {
            const o = q.subject;
 
            if (!yielded.has(o.value)) {
                yielded.add(o.value);
 
                yield o.value;
            }
        }
 
        const concepts = new Set<string>(this.getConceptsOfScheme(graphUris, schemeUri));
        const nested = this._nestedConceptsOfScheme(graphUris, concepts);
 
        for (let c of concepts) {
            if (!yielded.has(c) && !nested.has(c)) {
                yielded.add(c);
 
                yield c;
            }
        }
    }
 
    /**
     * Get the concepts of a concept scheme that have a broader concept within the same scheme.
     *
     * The hierarchical relations are enumerated once and matched against the set of concepts of the
     * scheme, which is considerably cheaper than querying the broader concepts of every single
     * concept of the scheme.
     * @param graphUris URIs of the graphs to search for concepts.
     * @param concepts Set of the concepts that are associated with the concept scheme.
     * @returns A set of URIs of the concepts that are nested below another concept of the scheme.
     */
    private _nestedConceptsOfScheme(graphUris: string | string[] | undefined, concepts: Set<string>): Set<string> {
        const result = new Set<string>();
 
        for (let q of this.store.matchAll(graphUris, null, skos.broader, null)) {
            // Note: A concept that is its own broader concept remains a top concept.
            if (q.subject.value !== q.object.value && concepts.has(q.subject.value) && concepts.has(q.object.value)) {
                result.add(q.subject.value);
            }
        }
 
        for (let q of this.store.matchAll(graphUris, null, skos.narrower, null)) {
            if (q.subject.value !== q.object.value && concepts.has(q.subject.value) && concepts.has(q.object.value)) {
                result.add(q.object.value);
            }
        }
 
        return result;
    }
 
    /**
     * Get all concepts that are displayed below a concept scheme, including indirectly narrower ones.
     *
     * The traversal starts at the {@link getTopConcepts} of the scheme and follows the narrower
     * concepts that can be displayed below the scheme, so the result is exactly the set of concepts
     * that a hierarchical view of the scheme can reach.
     *
     * Note: The hierarchical relations of the graphs are indexed once per call instead of querying
     * the narrower concepts of every single concept, which is an order of magnitude faster on large
     * thesauri. Prefer {@link getConceptsOfScheme} when the *associated* concepts are sufficient.
     * @param graphUris URIs of the graphs to search for concepts.
     * @param schemeUri URI of a concept scheme.
     * @returns An iterator of URIs of all concepts below the concept scheme.
     */
    *getAllConceptsInScheme(graphUris: string | string[] | undefined, schemeUri: string): IterableIterator<string> {
        const concepts = new Set<string>(this.getConceptsOfScheme(graphUris, schemeUri));
        const narrowerConcepts = this._getNarrowerConceptIndex(graphUris);
 
        const visited = new Set<string>();
        const stack = [...this.getTopConcepts(graphUris, schemeUri)];
 
        for (let c of stack) {
            visited.add(c);
        }
 
        while (stack.length) {
            const current = stack.pop()!;
 
            yield current;
 
            for (let n of narrowerConcepts.get(current) ?? []) {
                if (visited.has(n)) {
                    continue;
                }
 
                // Concepts of another scheme are displayed below that scheme, not below this one.
                // Concepts without any scheme are displayed wherever they are reached from.
                if (!concepts.has(n) && this.hasConceptScheme(graphUris, n)) {
                    continue;
                }
 
                visited.add(n);
 
                stack.push(n);
            }
        }
    }
 
    /**
     * Index the narrower concept relations of the given graphs.
     *
     * All four hierarchical relations are enumerated once, which is considerably cheaper than
     * querying {@link getNarrowerConcepts} for every concept of a large thesaurus.
     * @param graphUris URIs of the graphs to search for concepts.
     * @returns A map of concept URIs to the URIs of their narrower concepts.
     */
    private _getNarrowerConceptIndex(graphUris: string | string[] | undefined): Map<string, string[]> {
        const result = new Map<string, string[]>();
 
        const add = (broaderUri: string, narrowerUri: string) => {
            const narrowerConcepts = result.get(broaderUri);
 
            if (narrowerConcepts) {
                narrowerConcepts.push(narrowerUri);
            } else {
                result.set(broaderUri, [narrowerUri]);
            }
        };
 
        for (let q of this.store.matchAll(graphUris, null, skos.narrower, null)) {
            add(q.subject.value, q.object.value);
        }
 
        for (let q of this.store.matchAll(graphUris, null, skos.broader, null)) {
            add(q.object.value, q.subject.value);
        }
 
        for (let q of this.store.matchAll(graphUris, null, skos.hasTopConcept, null)) {
            add(q.subject.value, q.object.value);
        }
 
        for (let q of this.store.matchAll(graphUris, null, skos.topConceptOf, null)) {
            add(q.object.value, q.subject.value);
        }
 
        return result;
    }
 
    /**
     * Get the concepts that are not displayed below any concept scheme, i.e. the concepts that are
     * neither associated with a concept scheme nor reachable from the top concepts of one.
     * @param graphUris URIs of the graphs to search for concepts.
     * @param options Optional query options. `definedBy` and `notDefinedBy` filter the concepts by
     * their definition source.
     * @returns An iterator of URIs of the concepts outside of all concept schemes.
     */
    *getUnscopedConcepts(graphUris: string | string[] | undefined, options?: DefinitionQueryOptions): IterableIterator<string> {
        const scopedConcepts = new Set<string>();
 
        for (let scheme of this.getConceptSchemes(graphUris)) {
            for (let c of this.getAllConceptsInScheme(graphUris, scheme)) {
                scopedConcepts.add(c);
            }
        }
 
        const yielded = new Set<string>();
 
        for (let q of this.store.matchAll(graphUris, null, rdf.type, skos.Concept)) {
            const s = q.subject;
 
            if (yielded.has(s.value) || scopedConcepts.has(s.value)) {
                continue;
            }
 
            yielded.add(s.value);
 
            if (!this.skip(graphUris, s, options, { includeBlankNodes: true, includeReferenced: true })) {
                yield s.value;
            }
        }
    }
 
    /**
     * Get the root concepts of the concepts that are not displayed below any concept scheme, see
     * {@link getUnscopedConcepts}. These are the concepts without a broader concept among them,
     * plus one concept of every group of concepts that can only be reached through a cycle of
     * broader concepts, so that every unscoped concept can be reached from one of the roots.
     *
     * Note: A concept that is its own broader concept is not excluded, as in {@link getTopConcepts}.
     * @param graphUris URIs of the graphs to search for concepts.
     * @param options Optional query options, see {@link getUnscopedConcepts}.
     * @returns An iterator of URIs of the root concepts outside of all concept schemes.
     */
    *getUnscopedRootConcepts(graphUris: string | string[] | undefined, options?: DefinitionQueryOptions): IterableIterator<string> {
        const concepts = new Set<string>(this.getUnscopedConcepts(graphUris, options));
        const narrowerConcepts = this._getNarrowerConceptIndex(graphUris);
        const nested = this._getNestedConcepts(concepts, narrowerConcepts);
        const reachable = new Set<string>();
 
        for (let c of concepts) {
            if (!nested.has(c)) {
                this._markReachableConcepts(c, concepts, narrowerConcepts, reachable);
 
                yield c;
            }
        }
 
        // Yield one concept per group that is only reachable through a cycle of broader concepts.
        for (let c of concepts) {
            if (!reachable.has(c)) {
                this._markReachableConcepts(c, concepts, narrowerConcepts, reachable);
 
                yield c;
            }
        }
    }
 
    /**
     * Get the concepts of a set that have a broader concept within the same set.
     * @param concepts The set of concepts.
     * @param narrowerConcepts The narrower concept index of the graphs, see {@link _getNarrowerConceptIndex}.
     * @returns The concepts that are nested below another concept of the set.
     */
    private _getNestedConcepts(concepts: Set<string>, narrowerConcepts: Map<string, string[]>): Set<string> {
        const result = new Set<string>();
 
        for (let [broader, narrower] of narrowerConcepts) {
            if (!concepts.has(broader)) {
                continue;
            }
 
            for (let n of narrower) {
                // Note: A concept that is its own broader concept remains a root concept.
                if (n !== broader && concepts.has(n)) {
                    result.add(n);
                }
            }
        }
 
        return result;
    }
 
    /**
     * Mark a concept and the concepts of a set that can be reached from it via narrower concepts.
     * @param conceptUri The concept to start from.
     * @param concepts The set of concepts that can be marked.
     * @param narrowerConcepts The narrower concept index of the graphs, see {@link _getNarrowerConceptIndex}.
     * @param reachable The set of reachable concepts, which is extended in place.
     */
    private _markReachableConcepts(conceptUri: string, concepts: Set<string>, narrowerConcepts: Map<string, string[]>, reachable: Set<string>): void {
        const stack: string[] = [conceptUri];
 
        reachable.add(conceptUri);
 
        while (stack.length) {
            const current = stack.pop()!;
 
            for (let n of narrowerConcepts.get(current) ?? []) {
                if (concepts.has(n) && !reachable.has(n)) {
                    reachable.add(n);
 
                    stack.push(n);
                }
            }
        }
    }
 
    /**
     * Get the members of a collection. This includes both `skos:member` and `skos:memberList` properties.
     * @param graphUris URI of the graphs to search for collections.
     * @param collectionUri URI of a collection.
     * @returns An array of URIs of the members of the collection.
     */
    getCollectionMembers(graphUris: string | string[] | undefined, collectionUri: string): string[] {
        const s = RdfDataFactory.namedNode(collectionUri);
 
        let result: string[] = [];
 
        for (let q of this.store.matchAll(graphUris, s, skos.member, null)) {
            result.push(q.object.value);
        }
 
        for (let q of this.store.matchAll(graphUris, s, skos.memberList, null)) {
            result = [...result, ...this.store.getListItems(graphUris, q.object.value)];
        }
 
        return result;
    }
 
    /**
     * Get the members of a collection. This includes both `skos:member` and `skos:memberList` properties.
     * @param graphUris URI of the graphs to search for collections.
     * @param collectionUri URI of a collection.
     * @returns An array of URIs of the members of the collection.
     */
    hasCollectionMembers(graphUris: string | string[] | undefined, collectionUri: string): boolean {
        const s = RdfDataFactory.namedNode(collectionUri);
 
        for (let _ of this.store.matchAll(graphUris, s, skos.member, null)) {
            return true;
        }
 
        for (let _ of this.store.matchAll(graphUris, s, skos.memberList, null)) {
            return true;
        }
 
        return false;
    }
 
    /**
     * Indicates whether a collection is an ordered collection.
     * @param graphUris URI of the graphs to search for collections.
     * @param collectionUri URI of a collection.
     * @returns `true` if the collection is an ordered collection, `false` otherwise.
     */
    isOrderedCollection(graphUris: string | string[] | undefined, collectionUri: string): boolean {
        const s = RdfDataFactory.namedNode(collectionUri);
 
        for (let _ of this.store.matchAll(graphUris, s, rdf.type, skos.OrderedCollection)) {
            return true;
        }
 
        return false;
    }
 
    /**
     * Indicates whether a subject is a collection.
     * @param graphUris URI of the graphs to search for collections.
     * @param subjectUri URI of a subject.
     * @returns `true` if the subject is a `skos:Collection` or a `skos:OrderedCollection`, `false` otherwise.
     */
    isCollection(graphUris: string | string[] | undefined, subjectUri: string): boolean {
        Eif (subjectUri) {
            const s = RdfDataFactory.namedNode(subjectUri);
 
            for (let _ of this.store.matchAll(graphUris, s, rdf.type, skos.Collection)) {
                return true;
            }
 
            for (let _ of this.store.matchAll(graphUris, s, rdf.type, skos.OrderedCollection)) {
                return true;
            }
        }
 
        return false;
    }
 
    /**
     * Get the collections that have a given subject as a member. This includes both `skos:member`
     * and `skos:memberList` properties.
     *
     * Note: A collection that is a member of itself is returned as its own super collection, which
     * is consistent with {@link getBroaderConcepts}. Use {@link hasSuperCollections} to test whether
     * a collection is a member of *another* collection.
     * @param graphUris URI of the graphs to search for collections.
     * @param subjectUri URI of a collection or concept.
     * @returns An iterator of URIs of the collections that contain the subject.
     */
    *getSuperCollections(graphUris: string | string[] | undefined, subjectUri: string): IterableIterator<string> {
        const yielded = new Set<string>();
        const o = RdfDataFactory.namedNode(subjectUri);
 
        for (let q of this.store.matchAll(graphUris, null, skos.member, o)) {
            const s = q.subject;
 
            Eif (!yielded.has(s.value) && this.isCollection(graphUris, s.value)) {
                yielded.add(s.value);
 
                yield s.value;
            }
        }
 
        for (let s of this._getMemberListContainers(graphUris, subjectUri)) {
            Eif (!yielded.has(s) && this.isCollection(graphUris, s)) {
                yielded.add(s);
 
                yield s;
            }
        }
    }
 
    /**
     * Get the collections that reference a given subject in an ordered member list.
     *
     * Starting from the list nodes that have the subject as their first item, the `rdf:rest` chain
     * is traversed *backwards* towards the head of the list, and every visited list node is tested
     * for being the object of a `skos:memberList` property. This is considerably cheaper than
     * expanding all member lists in the graph, and it also resolves collections that share a list.
     * @param graphUris URI of the graphs to search for collections.
     * @param subjectUri URI of a collection or concept.
     * @returns An iterator of URIs of the collections that reference the subject in a member list.
     */
    private *_getMemberListContainers(graphUris: string | string[] | undefined, subjectUri: string): IterableIterator<string> {
        const o = RdfDataFactory.namedNode(subjectUri);
        const visited = new Set<string>();
        const listNodes: rdfjs.Quad_Subject[] = [];
 
        for (let q of this.store.matchAll(graphUris, null, rdf.first, o)) {
            Eif (!visited.has(q.subject.value)) {
                visited.add(q.subject.value);
 
                listNodes.push(q.subject);
            }
        }
 
        // Note: The visited set guards against cyclic lists and against lists that share a tail.
        while (listNodes.length) {
            const listNode = listNodes.pop()!;
 
            for (let q of this.store.matchAll(graphUris, null, skos.memberList, listNode)) {
                yield q.subject.value;
            }
 
            for (let q of this.store.matchAll(graphUris, null, rdf.rest, listNode)) {
                Eif (!visited.has(q.subject.value)) {
                    visited.add(q.subject.value);
 
                    listNodes.push(q.subject);
                }
            }
        }
    }
 
    /**
     * Indicates whether a collection is a member of another collection.
     *
     * Note: A collection that is a member of itself is *not* considered to have a super collection,
     * so that recursive definitions do not remove it from the list of root collections.
     * @param graphUris URI of the graphs to search for collections.
     * @param subjectUri URI of a collection.
     * @returns `true` if the collection is a member of another collection, `false` otherwise.
     */
    hasSuperCollections(graphUris: string | string[] | undefined, subjectUri: string): boolean {
        for (const c of this.getSuperCollections(graphUris, subjectUri)) {
            if (c !== subjectUri) {
                return true;
            }
        }
 
        return false;
    }
 
    /**
     * Get the members of a collection that are collections themselves, or all root collections.
     *
     * The order of the members of a `skos:OrderedCollection` is preserved.
     * @param graphUris URI of the graphs to search for collections.
     * @param subjectUri URI of a collection or `undefined` to get all root collections.
     * @param options Optional query parameters.
     * @returns An iterator of URIs of the nested collections of the given collection, or of the root collections if no subject is provided.
     */
    *getSubCollections(graphUris: string | string[] | undefined, subjectUri?: string, options?: DefinitionQueryOptions): IterableIterator<string> {
        if (subjectUri) {
            const yielded = new Set<string>();
 
            for (let m of this.getCollectionMembers(graphUris, subjectUri)) {
                if (yielded.has(m) || !this.isCollection(graphUris, m)) {
                    continue;
                }
 
                if (this._skipCollection(graphUris, RdfDataFactory.namedNode(m), options)) {
                    continue;
                }
 
                yielded.add(m);
 
                yield m;
            }
        } else {
            yield* this.getRootCollections(graphUris, options);
        }
    }
 
    /**
     * Indicates whether a collection has members that are collections themselves.
     * @param graphUris URI of the graphs to search for collections.
     * @param subjectUri URI of a collection.
     * @param options Optional query parameters.
     * @returns `true` if the collection has nested collections, `false` otherwise.
     */
    hasSubCollections(graphUris: string | string[] | undefined, subjectUri: string, options?: DefinitionQueryOptions): boolean {
        for (const _ of this.getSubCollections(graphUris, subjectUri, options)) {
            return true;
        }
 
        return false;
    }
 
    /**
     * Get all collections that are not a member of another collection.
     *
     * Every collection returned by {@link getCollections} is either returned by this method, or is
     * reachable from one of the returned collections via {@link getSubCollections}. If a group of
     * collections is only reachable through a membership cycle, the first collection of that group
     * is returned as a root collection so that no collection is lost.
     *
     * Note: Collections that are identified by a blank node cannot be queried for their members and
     * are therefore always returned as root collections.
     * @param graphUris URI of the graphs to search for collections.
     * @param options Optional query parameters.
     * @returns An iterator of URIs of the root collections.
     */
    *getRootCollections(graphUris: string | string[] | undefined, options?: DefinitionQueryOptions): IterableIterator<string> {
        const roots = new Set<string>();
        const collections = new Set<string>(this.getCollections(graphUris, options));
 
        // Collect the collections that are not a member of another collection.
        for (let c of collections) {
            if (!this.hasSuperCollections(graphUris, c)) {
                roots.add(c);
 
                yield c;
            }
        }
 
        if (roots.size === collections.size) {
            // No collection is nested in another one, so none of them can be missing.
            return;
        }
 
        // Mark all collections that can be reached from the root collections.
        const reachable = new Set<string>(roots);
 
        for (let c of roots) {
            this._markReachableCollections(graphUris, c, collections, reachable);
        }
 
        // Yield one collection per group that is only reachable through a membership cycle.
        for (let c of collections) {
            if (!reachable.has(c)) {
                reachable.add(c);
 
                this._markReachableCollections(graphUris, c, collections, reachable);
 
                yield c;
            }
        }
    }
 
    /**
     * Mark all collections that can be reached from a given collection through nested collections.
     *
     * The traversal uses the same criteria as {@link getSubCollections} so that the marked
     * collections are exactly the collections that are visible below the given collection. Since
     * the set of reachable collections is shared between all calls, the members of a collection are
     * enumerated at most once per {@link getRootCollections} call.
     * @param graphUris URI of the graphs to search for collections.
     * @param subjectUri URI of the collection to start the traversal from.
     * @param collections Set of all collections that are in the scope of the query.
     * @param reachable Set of collections that have already been reached.
     */
    private _markReachableCollections(graphUris: string | string[] | undefined, subjectUri: string, collections: Set<string>, reachable: Set<string>): void {
        const stack: string[] = [subjectUri];
 
        while (stack.length) {
            const current = stack.pop()!;
 
            for (let m of this.getCollectionMembers(graphUris, current)) {
                // Note: The set lookups keep concept members from reaching the store queries.
                if (collections.has(m) && !reachable.has(m) && this.isCollection(graphUris, m)) {
                    reachable.add(m);
 
                    stack.push(m);
                }
            }
        }
    }
 
    /**
     * Get the first discovered path from a given collection to a root collection.
     *
     * The path does not contain the given collection and is ordered from the closest super
     * collection to the root collection. If the collection is part of a membership cycle, the path
     * ends at the last collection that has not been visited yet and does not reach a root collection.
     * @param graphUris URI of the graphs to search for collections.
     * @param subjectUri URI of a collection.
     * @returns The first path that is found from the given collection to a root collection.
     */
    getRootCollectionPath(graphUris: string | string[] | undefined, subjectUri: string): string[] {
        return this._getRootCollectionPath(graphUris, subjectUri, [], new Set<string>([subjectUri]));
    }
 
    /**
     * Recursively find the first path from a given collection to a root collection.
     * @param graphUris URI of the graphs to search for collections.
     * @param subjectUri URI of a collection.
     * @param path The current collection path.
     * @param backtrack Set of URIs that have already been visited.
     * @returns The first path that is found from the given collection to a root collection.
     */
    private _getRootCollectionPath(graphUris: string | string[] | undefined, subjectUri: string, path: string[], backtrack: Set<string>): string[] {
        for (let o of this.getSuperCollections(graphUris, subjectUri)) {
            if (!backtrack.has(o)) {
                backtrack.add(o);
 
                return this._getRootCollectionPath(graphUris, o, [...path, o], backtrack);
            }
        }
 
        return path;
    }
 
    /**
     * Get all broader concepts of a concept scheme.
     * @param graphUris URIs of the graphs to search for concepts.
     * @param subjectUri URI of a concept scheme.
     */
    *getBroaderConcepts(graphUris: string | string[] | undefined, subjectUri: string): IterableIterator<string> {
        const yielded = new Set<string>();
        const s = RdfDataFactory.namedNode(subjectUri);
 
        for (let q of this.store.matchAll(graphUris, null, skos.hasTopConcept, s)) {
            const s = q.subject;
 
            Eif (!yielded.has(s.value)) {
                yielded.add(s.value);
 
                yield s.value;
            }
        }
 
        for (let q of this.store.matchAll(graphUris, null, skos.narrower, s)) {
            const s = q.subject;
 
            Eif (!yielded.has(s.value)) {
                yielded.add(s.value);
 
                yield s.value;
            }
        }
 
        for (let q of this.store.matchAll(graphUris, s, skos.topConceptOf, null)) {
            const o = q.object;
 
            if (!yielded.has(o.value)) {
                yielded.add(o.value);
 
                yield o.value;
            }
        }
 
        for (let q of this.store.matchAll(graphUris, s, skos.broader, null)) {
            const o = q.object;
 
            Iif (!yielded.has(o.value)) {
                yielded.add(o.value);
 
                yield o.value;
            }
        }
    }
 
    /**
     * Indicates whether a concept has broader concepts.
     * @param graphUris URIs of the graphs to search for concepts.
     * @param subjectUri URI of a concept.
     */
    hasBroaderConcepts(graphUris: string | string[] | undefined, subjectUri: string): boolean {
        for(const _ of this.getBroaderConcepts(graphUris, subjectUri)) {
            return true;
        }
 
        return false;
    }
 
    /**
     * Get all narrower concepts of a concept or the top concepts of a concept scheme.
     *
     * If the subject is a concept scheme, the {@link getTopConcepts} of that scheme are returned. If
     * the `inScheme` option is set, only concepts that can be displayed below that concept scheme are
     * returned, which excludes concepts that are associated with a *different* scheme.
     * @param graphUris URIs of the graphs to search for concepts.
     * @param subjectUri URI of a concept or concept scheme, or `undefined` to get all concept schemes.
     * @param options Optional query parameters.
     */
    *getNarrowerConcepts(graphUris: string | string[] | undefined, subjectUri?: string, options?: DefinitionQueryOptions): IterableIterator<string> {
        if (!subjectUri) {
            yield* this.getConceptSchemes(graphUris);
 
            return;
        }
 
        if (this.isConceptScheme(graphUris, subjectUri)) {
            yield* this.getTopConcepts(graphUris, subjectUri);
 
            return;
        }
 
        const yielded = new Set<string>();
        const s = RdfDataFactory.namedNode(subjectUri);
        const scheme = options?.inScheme;
 
        for (let q of this.store.matchAll(graphUris, s, skos.hasTopConcept, null)) {
            const o = q.object;
 
            if (!yielded.has(o.value)) {
                yielded.add(o.value);
 
                yield o.value;
            }
        }
 
        for (let q of this.store.matchAll(graphUris, s, skos.narrower, null)) {
            const o = q.object;
 
            Eif (!yielded.has(o.value)) {
                yielded.add(o.value);
 
                Eif (!scheme || this._isVisibleInScheme(graphUris, o.value, scheme)) {
                    yield o.value;
                }
            }
        }
 
        for (let q of this.store.matchAll(graphUris, null, skos.topConceptOf, s)) {
            const o = q.subject;
 
            if (!yielded.has(o.value)) {
                yielded.add(o.value);
 
                yield o.value;
            }
        }
 
        for (let q of this.store.matchAll(graphUris, null, skos.broader, s)) {
            const o = q.subject;
 
            if (!yielded.has(o.value)) {
                yielded.add(o.value);
 
                if (!scheme || this._isVisibleInScheme(graphUris, o.value, scheme)) {
                    yield o.value;
                }
            }
        }
    }
 
    /**
     * Indicates whether a concept has narrower concepts.
     * @param graphUris URIs of the graphs to search for concepts.
     * @param subjectUri URI of a concept.
     */
    hasNarrowerConcepts(graphUris: string | string[] | undefined, subjectUri: string): boolean {
        for(const _ of this.getNarrowerConcepts(graphUris, subjectUri)) {
            return true;
        }
 
        return false;
    }
 
    /**
     * Indicates whether a concept is a narrower concept of another concept.
     * @param graphUris URIs of the graphs to search for concepts.
     * @param subjectUri URI of a concept.
     * @param broaderUri URI of a broader concept.
     * @returns `true` if the concept is a narrower concept of the broader concept, `false` otherwise.
     */
    isNarrowerConceptOf(graphUris: string | string[] | undefined, subjectUri: string, broaderUri: string): boolean {
        return this.getConceptSchemePath(graphUris, subjectUri).includes(broaderUri);
    }
 
    /**
     * Indicates whether a subject is a concept scheme.
     * @param graphUris URIs of the graphs to search for concepts.
     * @param subjectUri URI of a subject.
     * @returns `true` if the subject is a concept scheme, `false` otherwise.
     */
    isConceptScheme(graphUris: string | string[] | undefined, subjectUri: string): boolean {
        Eif (subjectUri) {
            const s = RdfDataFactory.namedNode(subjectUri);
 
            for (let _ of this.store.matchAll(graphUris, s, rdf.type, skos.ConceptScheme)) {
                return true;
            }
        }
 
        return false;
    }
 
    /**
     * Get all broader concepts up to and including a concept scheme.
     * @param graphUris URIs of the graphs to search for concepts.
     * @param subjectUri URI of a concept.
     */
    getConceptSchemePath(graphUris: string | string[] | undefined, subjectUri: string): string[] {
        return this._getConceptSchemePath(graphUris, subjectUri, [], new Set<string>([subjectUri]));
    }
 
    /**
     * Recursively find the first path from a given class to a root class.
     * @param subjectUri URI of a class.
     * @param path The current class path.
     * @param backtrack Set of URIs that have already been visited.
     * @returns The first path that is found from the given class to a root class.
     */
    private _getConceptSchemePath(graphUris: string | string[] | undefined, subjectUri: string, path: string[], backtrack: Set<string>): string[] {
        for (let o of this.getBroaderConcepts(graphUris, subjectUri)) {
            Eif (!backtrack.has(o)) {
                backtrack.add(o);
 
                return this._getConceptSchemePath(graphUris, o, [...path, o], backtrack);
            }
        }
 
        return path;
    }
}