@faubulous/mentor-rdf 2.6.0 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -111,194 +111,12 @@ var Uri = class _Uri {
111
111
  }
112
112
  };
113
113
 
114
- // src/rdf/data-factory.ts
115
- var NamedNode = class {
116
- constructor(value) {
117
- this.termType = "NamedNode";
118
- this.value = value;
119
- }
120
- equals(other) {
121
- return other?.termType === this.termType && other.value === this.value;
122
- }
123
- };
124
- var BlankNode = class {
125
- constructor(value) {
126
- this.termType = "BlankNode";
127
- this.value = value;
128
- }
129
- equals(other) {
130
- return other?.termType === this.termType && other.value === this.value;
131
- }
132
- };
133
- var Literal = class {
134
- constructor(value, languageOrDatatype) {
135
- this.termType = "Literal";
136
- this.value = value;
137
- if (typeof languageOrDatatype === "string") {
138
- this.language = languageOrDatatype.toLowerCase();
139
- this.datatype = new NamedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString");
140
- } else if (languageOrDatatype) {
141
- this.language = "";
142
- this.datatype = languageOrDatatype;
143
- } else {
144
- this.language = "";
145
- this.datatype = new NamedNode("http://www.w3.org/2001/XMLSchema#string");
146
- }
147
- }
148
- equals(other) {
149
- if (other?.termType !== this.termType) {
150
- return false;
151
- }
152
- const otherLiteral = other;
153
- return this.value === otherLiteral.value && this.language === otherLiteral.language && this.datatype.equals(otherLiteral.datatype);
154
- }
155
- };
156
- var Variable = class {
157
- constructor(value) {
158
- this.termType = "Variable";
159
- this.value = value;
160
- }
161
- equals(other) {
162
- return other?.termType === this.termType && other.value === this.value;
163
- }
164
- };
165
- var DefaultGraph = class {
166
- constructor() {
167
- this.termType = "DefaultGraph";
168
- this.value = "";
169
- }
170
- equals(other) {
171
- return other?.termType === this.termType;
172
- }
173
- };
174
- var Quad = class {
175
- constructor(subject, predicate, object, graph) {
176
- this.termType = "Quad";
177
- this.value = "";
178
- this.subject = subject;
179
- this.predicate = predicate;
180
- this.object = object;
181
- this.graph = graph ?? new DefaultGraph();
182
- }
183
- equals(other) {
184
- if (other?.termType !== this.termType) {
185
- return false;
186
- }
187
- const otherQuad = other;
188
- return this.subject.equals(otherQuad.subject) && this.predicate.equals(otherQuad.predicate) && this.object.equals(otherQuad.object) && this.graph.equals(otherQuad.graph);
189
- }
190
- };
191
- var defaultGraphInstance = new DefaultGraph();
192
- var DataFactory = class {
193
- constructor() {
194
- this.blankNodeCounter = 0;
195
- }
196
- /**
197
- * Creates a new named node with the given IRI.
198
- * @param value The IRI of the named node.
199
- * @returns A new NamedNode instance.
200
- */
201
- namedNode(value) {
202
- return new NamedNode(value);
203
- }
204
- /**
205
- * Creates a new blank node with the given identifier.
206
- * If no identifier is provided, a unique one will be generated.
207
- * @param value Optional blank node identifier.
208
- * @returns A new BlankNode instance.
209
- */
210
- blankNode(value) {
211
- return new BlankNode(value ?? `b${++this.blankNodeCounter}`);
212
- }
213
- /**
214
- * Creates a new literal with the given value and optional language tag or datatype.
215
- * @param value The lexical value of the literal.
216
- * @param languageOrDatatype Either a language tag (string) or a datatype (NamedNode).
217
- * @returns A new Literal instance.
218
- */
219
- literal(value, languageOrDatatype) {
220
- return new Literal(value, languageOrDatatype);
221
- }
222
- /**
223
- * Creates a new variable with the given name.
224
- * @param value The name of the variable (without the leading '?').
225
- * @returns A new Variable instance.
226
- */
227
- variable(value) {
228
- return new Variable(value);
229
- }
230
- /**
231
- * Returns the singleton default graph instance.
232
- * @returns The default graph instance.
233
- */
234
- defaultGraph() {
235
- return defaultGraphInstance;
236
- }
237
- /**
238
- * Creates a new quad with the given subject, predicate, object, and optional graph.
239
- * @param subject The subject of the quad.
240
- * @param predicate The predicate of the quad.
241
- * @param object The object of the quad.
242
- * @param graph The graph of the quad (defaults to the default graph).
243
- * @returns A new Quad instance.
244
- */
245
- quad(subject, predicate, object, graph) {
246
- return new Quad(subject, predicate, object, graph);
247
- }
248
- /**
249
- * Creates a copy of an existing term.
250
- * @param original The term to copy.
251
- * @returns A new term instance with the same value.
252
- */
253
- fromTerm(original) {
254
- switch (original.termType) {
255
- case "NamedNode":
256
- return this.namedNode(original.value);
257
- case "BlankNode":
258
- return this.blankNode(original.value);
259
- case "Literal": {
260
- const literal = original;
261
- if (literal.language) {
262
- return this.literal(literal.value, literal.language);
263
- }
264
- return this.literal(literal.value, this.fromTerm(literal.datatype));
265
- }
266
- case "Variable":
267
- return this.variable(original.value);
268
- case "DefaultGraph":
269
- return this.defaultGraph();
270
- case "Quad": {
271
- const quad5 = original;
272
- return this.fromQuad(quad5);
273
- }
274
- default:
275
- throw new Error(`Unknown term type: ${original.termType}`);
276
- }
277
- }
278
- /**
279
- * Creates a copy of an existing quad.
280
- * @param original The quad to copy.
281
- * @returns A new quad instance with the same values.
282
- */
283
- fromQuad(original) {
284
- return this.quad(
285
- this.fromTerm(original.subject),
286
- this.fromTerm(original.predicate),
287
- this.fromTerm(original.object),
288
- this.fromTerm(original.graph)
289
- );
290
- }
291
- /**
292
- * Resets the blank node counter. Useful for testing.
293
- */
294
- resetBlankNodeCounter() {
295
- this.blankNodeCounter = 0;
296
- }
297
- };
298
- var dataFactory = new DataFactory();
114
+ // src/rdf/rdf-data-factory.ts
115
+ import { DataFactory } from "rdf-data-factory";
116
+ var RdfDataFactory = new DataFactory();
299
117
 
300
118
  // src/rdf/resource-repository.ts
301
- var { namedNode, blankNode } = dataFactory;
119
+ var { namedNode, blankNode } = RdfDataFactory;
302
120
  var ResourceRepository = class {
303
121
  constructor(store) {
304
122
  this.store = store;
@@ -480,6 +298,163 @@ var ResourceRepository = class {
480
298
  }
481
299
  return primary;
482
300
  }
301
+ /**
302
+ * Get the hierarchy relations of a predicate such as `rdfs:subClassOf`, read in a single pass.
303
+ * @param graphUris URIs of the graphs to search, `undefined` for the default graph.
304
+ * @param predicate The hierarchy predicate, e.g. `rdfs:subClassOf` or `rdfs:subPropertyOf`.
305
+ * @param options Optional query parameters.
306
+ * @returns A map from a node, named or blank, to its named parents, excluding the node itself.
307
+ */
308
+ getParentMap(graphUris, predicate, options) {
309
+ const result = /* @__PURE__ */ new Map();
310
+ for (let q of this.store.matchAll(graphUris, null, predicate, null, options?.includeInferred)) {
311
+ if (q.subject.termType != "NamedNode" && q.subject.termType != "BlankNode" || q.object.termType != "NamedNode" || q.subject.value == q.object.value) {
312
+ continue;
313
+ }
314
+ const parents = result.get(q.subject.value);
315
+ if (!parents) {
316
+ result.set(q.subject.value, [q.object.value]);
317
+ } else if (!parents.includes(q.object.value)) {
318
+ parents.push(q.object.value);
319
+ }
320
+ }
321
+ return result;
322
+ }
323
+ /**
324
+ * Get the root nodes of a hierarchy such as the class or the property hierarchy. A member with no
325
+ * visible parent is a root. A parent that is not one of the document's own nodes is referenced:
326
+ * the roots are then the top nodes of its chains of parents, as far as the graphs state them.
327
+ * @param members The nodes whose roots are wanted, e.g. the classes of the document.
328
+ * @param parents The parents of every node, see {@link getParentMap}.
329
+ * @param isOwn Whether a node belongs to the document; a chain that reaches one has no top of its own.
330
+ * @param isVisible Whether a parent is shown in the hierarchy at all.
331
+ * @param canBeRoot Whether a node is listed as a root, be it a member without visible parents or the top of a referenced chain.
332
+ * @returns An iterator of the root nodes.
333
+ */
334
+ *getHierarchyRoots(members, parents, isOwn, isVisible, canBeRoot = () => true) {
335
+ const yielded = /* @__PURE__ */ new Set();
336
+ const getVisibleParents = (iri) => (parents.get(iri) ?? []).filter(isVisible);
337
+ const finished = /* @__PURE__ */ new Set();
338
+ for (let m of members) {
339
+ let hasParent = false;
340
+ for (let p of getVisibleParents(m)) {
341
+ hasParent = true;
342
+ if (!isOwn(p)) {
343
+ for (let r of this._getTopNodes(p, getVisibleParents, isOwn, finished, /* @__PURE__ */ new Set())) {
344
+ if (!yielded.has(r) && canBeRoot(r)) {
345
+ yielded.add(r);
346
+ yield r;
347
+ }
348
+ }
349
+ }
350
+ }
351
+ if (!hasParent && !yielded.has(m) && canBeRoot(m)) {
352
+ yielded.add(m);
353
+ yield m;
354
+ }
355
+ }
356
+ }
357
+ /**
358
+ * Get the top nodes of the chains of parents of a referenced node.
359
+ * @param iri URI of a referenced node.
360
+ * @param getParents A function returning the visible parents of a node.
361
+ * @param isOwn Whether a node belongs to the document; a chain that reaches one has no top of its own.
362
+ * @param finished The referenced nodes whose top nodes were already yielded.
363
+ * @param path The nodes on the current chain, to stop at cycles.
364
+ * @returns An iterator of the top nodes not yielded for another chain before.
365
+ */
366
+ *_getTopNodes(iri, getParents, isOwn, finished, path) {
367
+ if (finished.has(iri)) {
368
+ return;
369
+ }
370
+ path.add(iri);
371
+ let isTop = true;
372
+ for (let p of getParents(iri)) {
373
+ if (isOwn(p) || finished.has(p)) {
374
+ isTop = false;
375
+ } else if (!path.has(p)) {
376
+ isTop = false;
377
+ yield* this._getTopNodes(p, getParents, isOwn, finished, path);
378
+ }
379
+ }
380
+ path.delete(iri);
381
+ finished.add(iri);
382
+ if (isTop) {
383
+ yield iri;
384
+ }
385
+ }
386
+ /**
387
+ * Indicate if a node is only referenced by the document: it is included if referenced terms
388
+ * are included, and it is not defined by another ontology of the document. Annotations or a
389
+ * declaration of a term from another vocabulary do not define it.
390
+ * @param graphUris URIs of the graphs to search, `undefined` for the default graph.
391
+ * @param node A node.
392
+ * @param options Optional query parameters.
393
+ * @param ontologies The other ontologies in the graphs, see {@link getOtherOntologiesOnce}.
394
+ * @returns `true` if the node is a referenced named node, `false` otherwise.
395
+ */
396
+ isReferenced(graphUris, node, options, ontologies) {
397
+ if (!(options?.includeReferenced ?? false) || node.termType != "NamedNode") {
398
+ return false;
399
+ }
400
+ return !ontologies().some((o) => this.isDefinedBy(graphUris, node, o));
401
+ }
402
+ /**
403
+ * Indicate if a node is a referenced term with one of the document's own terms below it in a
404
+ * hierarchy, so that the hierarchy from a referenced root down to the document is not broken.
405
+ * @param graphUris URIs of the graphs to search, `undefined` for the default graph.
406
+ * @param node A node.
407
+ * @param predicate The hierarchy predicate, e.g. `rdfs:subClassOf` or `rdfs:subPropertyOf`.
408
+ * @param options Optional query parameters.
409
+ * @param ontologies The other ontologies in the graphs, see {@link getOtherOntologiesOnce}.
410
+ * @param visited The nodes already visited, to stop at cycles.
411
+ * @returns `true` if a term of the document is a direct or indirect child of the node.
412
+ */
413
+ isReferencedAbove(graphUris, node, predicate, options, ontologies, visited = /* @__PURE__ */ new Set()) {
414
+ if (!this.isReferenced(graphUris, node, options, ontologies)) {
415
+ return false;
416
+ }
417
+ visited.add(node.value);
418
+ for (let q of this.store.matchAll(graphUris, null, predicate, node, options?.includeInferred)) {
419
+ if (visited.has(q.subject.value)) {
420
+ continue;
421
+ }
422
+ if (!this.skip(graphUris, q.subject, options) || this.isReferencedAbove(graphUris, q.subject, predicate, options, ontologies, visited)) {
423
+ return true;
424
+ }
425
+ }
426
+ return false;
427
+ }
428
+ /**
429
+ * Get an accessor for the other ontologies in the graphs that queries them on its first call
430
+ * only, as most queries never need them.
431
+ * @param graphUris URIs of the graphs to search, `undefined` for the default graph.
432
+ * @param options Optional query parameters.
433
+ * @returns A function returning the URIs of the other ontologies, see {@link _getOtherOntologies}.
434
+ */
435
+ getOtherOntologiesOnce(graphUris, options) {
436
+ let ontologies;
437
+ return () => ontologies ?? (ontologies = this._getOtherOntologies(graphUris, options));
438
+ }
439
+ /**
440
+ * Get the ontologies declared in the graphs other than the one the options select with `definedBy`.
441
+ * @param graphUris URIs of the graphs to search, `undefined` for the default graph.
442
+ * @param options Optional query parameters.
443
+ * @returns The URIs of the other ontologies, or none if the options select no ontology.
444
+ */
445
+ _getOtherOntologies(graphUris, options) {
446
+ if (!options?.definedBy) {
447
+ return [];
448
+ }
449
+ const definedBy = Uri.getNormalizedUri(options.definedBy);
450
+ const result = /* @__PURE__ */ new Set();
451
+ for (let q of this.store.matchAll(graphUris, null, rdf.type, owl.Ontology, false)) {
452
+ if (q.subject.termType == "NamedNode" && Uri.getNormalizedUri(q.subject.value) != definedBy) {
453
+ result.add(q.subject.value);
454
+ }
455
+ }
456
+ return [...result];
457
+ }
483
458
  };
484
459
 
485
460
  // src/rdf/concept-repository.ts
@@ -566,7 +541,7 @@ var ConceptRepository = class extends ResourceRepository {
566
541
  * @returns `true` if the resource is in a concept scheme, `false` otherwise.
567
542
  */
568
543
  hasConceptScheme(graphUris, subjectUri) {
569
- const s = dataFactory.namedNode(subjectUri);
544
+ const s = RdfDataFactory.namedNode(subjectUri);
570
545
  for (let _ of this.store.matchAll(graphUris, s, skos.inScheme, null)) {
571
546
  return true;
572
547
  }
@@ -580,8 +555,8 @@ var ConceptRepository = class extends ResourceRepository {
580
555
  * @returns `true` if the resource is in the given concept scheme, `false` otherwise.
581
556
  */
582
557
  isInScheme(graphUris, subjectUri, schemeUri) {
583
- const s = dataFactory.namedNode(subjectUri);
584
- const o = dataFactory.namedNode(schemeUri);
558
+ const s = RdfDataFactory.namedNode(subjectUri);
559
+ const o = RdfDataFactory.namedNode(schemeUri);
585
560
  for (let _ of this.store.matchAll(graphUris, s, skos.inScheme, o)) {
586
561
  return true;
587
562
  }
@@ -603,8 +578,8 @@ var ConceptRepository = class extends ResourceRepository {
603
578
  if (this.isInScheme(graphUris, subjectUri, schemeUri)) {
604
579
  return true;
605
580
  }
606
- const s = dataFactory.namedNode(subjectUri);
607
- const o = dataFactory.namedNode(schemeUri);
581
+ const s = RdfDataFactory.namedNode(subjectUri);
582
+ const o = RdfDataFactory.namedNode(schemeUri);
608
583
  for (let _ of this.store.matchAll(graphUris, s, skos.topConceptOf, o)) {
609
584
  return true;
610
585
  }
@@ -639,7 +614,7 @@ var ConceptRepository = class extends ResourceRepository {
639
614
  */
640
615
  *getConceptsOfScheme(graphUris, schemeUri) {
641
616
  const yielded = /* @__PURE__ */ new Set();
642
- const o = dataFactory.namedNode(schemeUri);
617
+ const o = RdfDataFactory.namedNode(schemeUri);
643
618
  for (let predicate of [skos.inScheme, skos.topConceptOf]) {
644
619
  for (let q of this.store.matchAll(graphUris, null, predicate, o)) {
645
620
  const s = q.subject.value;
@@ -667,7 +642,7 @@ var ConceptRepository = class extends ResourceRepository {
667
642
  */
668
643
  *getTopConcepts(graphUris, schemeUri) {
669
644
  const yielded = /* @__PURE__ */ new Set();
670
- const s = dataFactory.namedNode(schemeUri);
645
+ const s = RdfDataFactory.namedNode(schemeUri);
671
646
  for (let q of this.store.matchAll(graphUris, s, skos.hasTopConcept, null)) {
672
647
  const o = q.object;
673
648
  if (!yielded.has(o.value)) {
@@ -901,7 +876,7 @@ var ConceptRepository = class extends ResourceRepository {
901
876
  * @returns An array of URIs of the members of the collection.
902
877
  */
903
878
  getCollectionMembers(graphUris, collectionUri) {
904
- const s = dataFactory.namedNode(collectionUri);
879
+ const s = RdfDataFactory.namedNode(collectionUri);
905
880
  let result = [];
906
881
  for (let q of this.store.matchAll(graphUris, s, skos.member, null)) {
907
882
  result.push(q.object.value);
@@ -918,7 +893,7 @@ var ConceptRepository = class extends ResourceRepository {
918
893
  * @returns An array of URIs of the members of the collection.
919
894
  */
920
895
  hasCollectionMembers(graphUris, collectionUri) {
921
- const s = dataFactory.namedNode(collectionUri);
896
+ const s = RdfDataFactory.namedNode(collectionUri);
922
897
  for (let _ of this.store.matchAll(graphUris, s, skos.member, null)) {
923
898
  return true;
924
899
  }
@@ -934,7 +909,7 @@ var ConceptRepository = class extends ResourceRepository {
934
909
  * @returns `true` if the collection is an ordered collection, `false` otherwise.
935
910
  */
936
911
  isOrderedCollection(graphUris, collectionUri) {
937
- const s = dataFactory.namedNode(collectionUri);
912
+ const s = RdfDataFactory.namedNode(collectionUri);
938
913
  for (let _ of this.store.matchAll(graphUris, s, rdf.type, skos.OrderedCollection)) {
939
914
  return true;
940
915
  }
@@ -948,7 +923,7 @@ var ConceptRepository = class extends ResourceRepository {
948
923
  */
949
924
  isCollection(graphUris, subjectUri) {
950
925
  if (subjectUri) {
951
- const s = dataFactory.namedNode(subjectUri);
926
+ const s = RdfDataFactory.namedNode(subjectUri);
952
927
  for (let _ of this.store.matchAll(graphUris, s, rdf.type, skos.Collection)) {
953
928
  return true;
954
929
  }
@@ -971,7 +946,7 @@ var ConceptRepository = class extends ResourceRepository {
971
946
  */
972
947
  *getSuperCollections(graphUris, subjectUri) {
973
948
  const yielded = /* @__PURE__ */ new Set();
974
- const o = dataFactory.namedNode(subjectUri);
949
+ const o = RdfDataFactory.namedNode(subjectUri);
975
950
  for (let q of this.store.matchAll(graphUris, null, skos.member, o)) {
976
951
  const s = q.subject;
977
952
  if (!yielded.has(s.value) && this.isCollection(graphUris, s.value)) {
@@ -998,7 +973,7 @@ var ConceptRepository = class extends ResourceRepository {
998
973
  * @returns An iterator of URIs of the collections that reference the subject in a member list.
999
974
  */
1000
975
  *_getMemberListContainers(graphUris, subjectUri) {
1001
- const o = dataFactory.namedNode(subjectUri);
976
+ const o = RdfDataFactory.namedNode(subjectUri);
1002
977
  const visited = /* @__PURE__ */ new Set();
1003
978
  const listNodes = [];
1004
979
  for (let q of this.store.matchAll(graphUris, null, rdf.first, o)) {
@@ -1053,7 +1028,7 @@ var ConceptRepository = class extends ResourceRepository {
1053
1028
  if (yielded.has(m) || !this.isCollection(graphUris, m)) {
1054
1029
  continue;
1055
1030
  }
1056
- if (this._skipCollection(graphUris, dataFactory.namedNode(m), options)) {
1031
+ if (this._skipCollection(graphUris, RdfDataFactory.namedNode(m), options)) {
1057
1032
  continue;
1058
1033
  }
1059
1034
  yielded.add(m);
@@ -1175,7 +1150,7 @@ var ConceptRepository = class extends ResourceRepository {
1175
1150
  */
1176
1151
  *getBroaderConcepts(graphUris, subjectUri) {
1177
1152
  const yielded = /* @__PURE__ */ new Set();
1178
- const s = dataFactory.namedNode(subjectUri);
1153
+ const s = RdfDataFactory.namedNode(subjectUri);
1179
1154
  for (let q of this.store.matchAll(graphUris, null, skos.hasTopConcept, s)) {
1180
1155
  const s2 = q.subject;
1181
1156
  if (!yielded.has(s2.value)) {
@@ -1236,7 +1211,7 @@ var ConceptRepository = class extends ResourceRepository {
1236
1211
  return;
1237
1212
  }
1238
1213
  const yielded = /* @__PURE__ */ new Set();
1239
- const s = dataFactory.namedNode(subjectUri);
1214
+ const s = RdfDataFactory.namedNode(subjectUri);
1240
1215
  const scheme = options?.inScheme;
1241
1216
  for (let q of this.store.matchAll(graphUris, s, skos.hasTopConcept, null)) {
1242
1217
  const o = q.object;
@@ -1300,7 +1275,7 @@ var ConceptRepository = class extends ResourceRepository {
1300
1275
  */
1301
1276
  isConceptScheme(graphUris, subjectUri) {
1302
1277
  if (subjectUri) {
1303
- const s = dataFactory.namedNode(subjectUri);
1278
+ const s = RdfDataFactory.namedNode(subjectUri);
1304
1279
  for (let _ of this.store.matchAll(graphUris, s, rdf.type, skos.ConceptScheme)) {
1305
1280
  return true;
1306
1281
  }
@@ -1334,7 +1309,7 @@ var ConceptRepository = class extends ResourceRepository {
1334
1309
  };
1335
1310
 
1336
1311
  // src/rdf/class-repository.ts
1337
- var { namedNode: namedNode2 } = dataFactory;
1312
+ var { namedNode: namedNode2 } = RdfDataFactory;
1338
1313
  var ClassRepository = class extends ConceptRepository {
1339
1314
  constructor(store) {
1340
1315
  super(store);
@@ -1403,8 +1378,9 @@ var ClassRepository = class extends ConceptRepository {
1403
1378
  */
1404
1379
  hasSubClasses(graphUris, subjectUri, options) {
1405
1380
  const o = namedNode2(subjectUri);
1381
+ const ontologies = this.getOtherOntologiesOnce(graphUris, options);
1406
1382
  for (let q of this.store.matchAll(graphUris, null, rdfs.subClassOf, o)) {
1407
- if (!this.skip(graphUris, q.subject, options)) {
1383
+ if (!this.skip(graphUris, q.subject, options) || this.isReferencedAbove(graphUris, q.subject, rdfs.subClassOf, options, ontologies)) {
1408
1384
  return true;
1409
1385
  }
1410
1386
  }
@@ -1419,8 +1395,12 @@ var ClassRepository = class extends ConceptRepository {
1419
1395
  if (subjectUri) {
1420
1396
  const yielded = /* @__PURE__ */ new Set();
1421
1397
  const o = namedNode2(subjectUri);
1398
+ const ontologies = this.getOtherOntologiesOnce(graphUris, options);
1422
1399
  for (let q of this.store.matchAll(graphUris, null, rdfs.subClassOf, o, options?.includeInferred)) {
1423
- if (!this.skip(graphUris, q.subject, options) && !yielded.has(q.subject.value)) {
1400
+ if (yielded.has(q.subject.value)) {
1401
+ continue;
1402
+ }
1403
+ if (!this.skip(graphUris, q.subject, options) || this.isReferencedAbove(graphUris, q.subject, rdfs.subClassOf, options, ontologies)) {
1424
1404
  yielded.add(q.subject.value);
1425
1405
  yield q.subject.value;
1426
1406
  }
@@ -1601,28 +1581,23 @@ var ClassRepository = class extends ConceptRepository {
1601
1581
  * @returns An iterator of root classes in the repository.
1602
1582
  */
1603
1583
  *getRootClasses(graphUris, options) {
1604
- const yielded = /* @__PURE__ */ new Set();
1605
1584
  const classes = new Set(this.getClasses(graphUris, options));
1606
- for (let c of classes) {
1607
- let hasSuperClass = false;
1608
- for (let q of this.store.matchAll(graphUris, namedNode2(c), rdfs.subClassOf, null, options?.includeInferred)) {
1609
- const includeReferenced = options?.includeReferenced ?? false;
1610
- const skip = this.skip(graphUris, q.object, options);
1611
- if (q.object.termType != "NamedNode" || skip && (!includeReferenced || includeReferenced && this.hasSubject(graphUris, q.object.value))) {
1612
- continue;
1613
- }
1614
- hasSuperClass = q.object.value != q.subject.value;
1615
- if (!classes.has(q.object.value) && !yielded.has(q.object.value)) {
1616
- yielded.add(q.object.value);
1617
- yield q.object.value;
1618
- }
1619
- break;
1585
+ const ontologies = this.getOtherOntologiesOnce(graphUris, options);
1586
+ const superClasses = this.getParentMap(graphUris, rdfs.subClassOf, options);
1587
+ const visible = /* @__PURE__ */ new Map();
1588
+ const isVisible = (iri) => {
1589
+ if (classes.has(iri)) {
1590
+ return true;
1620
1591
  }
1621
- if (!hasSuperClass && !yielded.has(c)) {
1622
- yielded.add(c);
1623
- yield c;
1592
+ let result = visible.get(iri);
1593
+ if (result === void 0) {
1594
+ const node = namedNode2(iri);
1595
+ result = !this.skip(graphUris, node, options) || this.isReferenced(graphUris, node, options, ontologies);
1596
+ visible.set(iri, result);
1624
1597
  }
1625
- }
1598
+ return result;
1599
+ };
1600
+ yield* this.getHierarchyRoots(classes, superClasses, (iri) => classes.has(iri), isVisible);
1626
1601
  }
1627
1602
  /**
1628
1603
  * Indicate if there is an equivalent class of a given class.
@@ -1676,6 +1651,192 @@ var ClassRepository = class extends ConceptRepository {
1676
1651
  }
1677
1652
  };
1678
1653
 
1654
+ // src/rdf/data-factory.ts
1655
+ var NamedNode = class {
1656
+ constructor(value) {
1657
+ this.termType = "NamedNode";
1658
+ this.value = value;
1659
+ }
1660
+ equals(other) {
1661
+ return other?.termType === this.termType && other.value === this.value;
1662
+ }
1663
+ };
1664
+ var BlankNode = class {
1665
+ constructor(value) {
1666
+ this.termType = "BlankNode";
1667
+ this.value = value;
1668
+ }
1669
+ equals(other) {
1670
+ return other?.termType === this.termType && other.value === this.value;
1671
+ }
1672
+ };
1673
+ var Literal = class {
1674
+ constructor(value, languageOrDatatype) {
1675
+ this.termType = "Literal";
1676
+ this.value = value;
1677
+ if (typeof languageOrDatatype === "string") {
1678
+ this.language = languageOrDatatype.toLowerCase();
1679
+ this.datatype = new NamedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString");
1680
+ } else if (languageOrDatatype) {
1681
+ this.language = "";
1682
+ this.datatype = languageOrDatatype;
1683
+ } else {
1684
+ this.language = "";
1685
+ this.datatype = new NamedNode("http://www.w3.org/2001/XMLSchema#string");
1686
+ }
1687
+ }
1688
+ equals(other) {
1689
+ if (other?.termType !== this.termType) {
1690
+ return false;
1691
+ }
1692
+ const otherLiteral = other;
1693
+ return this.value === otherLiteral.value && this.language === otherLiteral.language && this.datatype.equals(otherLiteral.datatype);
1694
+ }
1695
+ };
1696
+ var Variable = class {
1697
+ constructor(value) {
1698
+ this.termType = "Variable";
1699
+ this.value = value;
1700
+ }
1701
+ equals(other) {
1702
+ return other?.termType === this.termType && other.value === this.value;
1703
+ }
1704
+ };
1705
+ var DefaultGraph = class {
1706
+ constructor() {
1707
+ this.termType = "DefaultGraph";
1708
+ this.value = "";
1709
+ }
1710
+ equals(other) {
1711
+ return other?.termType === this.termType;
1712
+ }
1713
+ };
1714
+ var Quad = class {
1715
+ constructor(subject, predicate, object, graph) {
1716
+ this.termType = "Quad";
1717
+ this.value = "";
1718
+ this.subject = subject;
1719
+ this.predicate = predicate;
1720
+ this.object = object;
1721
+ this.graph = graph ?? new DefaultGraph();
1722
+ }
1723
+ equals(other) {
1724
+ if (other?.termType !== this.termType) {
1725
+ return false;
1726
+ }
1727
+ const otherQuad = other;
1728
+ return this.subject.equals(otherQuad.subject) && this.predicate.equals(otherQuad.predicate) && this.object.equals(otherQuad.object) && this.graph.equals(otherQuad.graph);
1729
+ }
1730
+ };
1731
+ var defaultGraphInstance = new DefaultGraph();
1732
+ var DataFactory2 = class {
1733
+ constructor() {
1734
+ this.blankNodeCounter = 0;
1735
+ }
1736
+ /**
1737
+ * Creates a new named node with the given IRI.
1738
+ * @param value The IRI of the named node.
1739
+ * @returns A new NamedNode instance.
1740
+ */
1741
+ namedNode(value) {
1742
+ return new NamedNode(value);
1743
+ }
1744
+ /**
1745
+ * Creates a new blank node with the given identifier.
1746
+ * If no identifier is provided, a unique one will be generated.
1747
+ * @param value Optional blank node identifier.
1748
+ * @returns A new BlankNode instance.
1749
+ */
1750
+ blankNode(value) {
1751
+ return new BlankNode(value ?? `b${++this.blankNodeCounter}`);
1752
+ }
1753
+ /**
1754
+ * Creates a new literal with the given value and optional language tag or datatype.
1755
+ * @param value The lexical value of the literal.
1756
+ * @param languageOrDatatype Either a language tag (string) or a datatype (NamedNode).
1757
+ * @returns A new Literal instance.
1758
+ */
1759
+ literal(value, languageOrDatatype) {
1760
+ return new Literal(value, languageOrDatatype);
1761
+ }
1762
+ /**
1763
+ * Creates a new variable with the given name.
1764
+ * @param value The name of the variable (without the leading '?').
1765
+ * @returns A new Variable instance.
1766
+ */
1767
+ variable(value) {
1768
+ return new Variable(value);
1769
+ }
1770
+ /**
1771
+ * Returns the singleton default graph instance.
1772
+ * @returns The default graph instance.
1773
+ */
1774
+ defaultGraph() {
1775
+ return defaultGraphInstance;
1776
+ }
1777
+ /**
1778
+ * Creates a new quad with the given subject, predicate, object, and optional graph.
1779
+ * @param subject The subject of the quad.
1780
+ * @param predicate The predicate of the quad.
1781
+ * @param object The object of the quad.
1782
+ * @param graph The graph of the quad (defaults to the default graph).
1783
+ * @returns A new Quad instance.
1784
+ */
1785
+ quad(subject, predicate, object, graph) {
1786
+ return new Quad(subject, predicate, object, graph);
1787
+ }
1788
+ /**
1789
+ * Creates a copy of an existing term.
1790
+ * @param original The term to copy.
1791
+ * @returns A new term instance with the same value.
1792
+ */
1793
+ fromTerm(original) {
1794
+ switch (original.termType) {
1795
+ case "NamedNode":
1796
+ return this.namedNode(original.value);
1797
+ case "BlankNode":
1798
+ return this.blankNode(original.value);
1799
+ case "Literal": {
1800
+ const literal = original;
1801
+ if (literal.language) {
1802
+ return this.literal(literal.value, literal.language);
1803
+ }
1804
+ return this.literal(literal.value, this.fromTerm(literal.datatype));
1805
+ }
1806
+ case "Variable":
1807
+ return this.variable(original.value);
1808
+ case "DefaultGraph":
1809
+ return this.defaultGraph();
1810
+ case "Quad": {
1811
+ const quad5 = original;
1812
+ return this.fromQuad(quad5);
1813
+ }
1814
+ default:
1815
+ throw new Error(`Unknown term type: ${original.termType}`);
1816
+ }
1817
+ }
1818
+ /**
1819
+ * Creates a copy of an existing quad.
1820
+ * @param original The quad to copy.
1821
+ * @returns A new quad instance with the same values.
1822
+ */
1823
+ fromQuad(original) {
1824
+ return this.quad(
1825
+ this.fromTerm(original.subject),
1826
+ this.fromTerm(original.predicate),
1827
+ this.fromTerm(original.object),
1828
+ this.fromTerm(original.graph)
1829
+ );
1830
+ }
1831
+ /**
1832
+ * Resets the blank node counter. Useful for testing.
1833
+ */
1834
+ resetBlankNodeCounter() {
1835
+ this.blankNodeCounter = 0;
1836
+ }
1837
+ };
1838
+ var dataFactory = new DataFactory2();
1839
+
1679
1840
  // src/rdf/dataset-view.ts
1680
1841
  var DatasetView = class _DatasetView {
1681
1842
  constructor(store, options = {}) {
@@ -1851,7 +2012,7 @@ var DatasetView = class _DatasetView {
1851
2012
  };
1852
2013
 
1853
2014
  // src/rdf/property-repository.ts
1854
- var { namedNode: namedNode3 } = dataFactory;
2015
+ var { namedNode: namedNode3 } = RdfDataFactory;
1855
2016
  var PropertyRepository = class extends ClassRepository {
1856
2017
  constructor(store) {
1857
2018
  super(store);
@@ -1888,38 +2049,41 @@ var PropertyRepository = class extends ClassRepository {
1888
2049
  * @returns An iterator of properties of the given type.
1889
2050
  */
1890
2051
  *getRootPropertiesOfType(graphUris, typeUri, options) {
1891
- const yielded = /* @__PURE__ */ new Set();
1892
- const type = namedNode3(typeUri);
1893
- for (let q of this.store.matchAll(graphUris, null, rdf.type, type, options?.includeInferred)) {
1894
- if (this.skip(graphUris, q.subject, options)) {
1895
- continue;
1896
- }
1897
- let hasSuperProperty = false;
1898
- let referencedSuperProperty = null;
1899
- for (let q2 of this.store.matchAll(graphUris, q.subject, rdfs.subPropertyOf, null, options?.includeInferred)) {
1900
- if (options?.includeReferenced && !this.hasSubject(graphUris, q2.object.value)) {
1901
- referencedSuperProperty = q2.object.value;
1902
- } else if (this.skip(graphUris, q2.object, options)) {
1903
- continue;
2052
+ const members = /* @__PURE__ */ new Map();
2053
+ for (let q of this.store.matchAll(graphUris, null, rdf.type, namedNode3(typeUri), options?.includeInferred)) {
2054
+ if (!this.skip(graphUris, q.subject, options)) {
2055
+ members.set(q.subject.value, q.subject);
2056
+ }
2057
+ }
2058
+ const ontologies = this.getOtherOntologiesOnce(graphUris, options);
2059
+ const superProperties = this.getParentMap(graphUris, rdfs.subPropertyOf, options);
2060
+ const referenced = /* @__PURE__ */ new Map();
2061
+ const own = /* @__PURE__ */ new Map();
2062
+ const isReferenced = (iri) => {
2063
+ let result = referenced.get(iri);
2064
+ if (result === void 0) {
2065
+ const node = namedNode3(iri);
2066
+ if (!(options?.includeReferenced ?? false)) {
2067
+ result = false;
2068
+ } else if (options?.definedBy) {
2069
+ result = this.skip(graphUris, node, options) && this.isReferenced(graphUris, node, options, ontologies);
2070
+ } else {
2071
+ result = !this.hasSubject(graphUris, iri);
1904
2072
  }
1905
- hasSuperProperty = q2.object.value != q2.subject.value;
1906
- break;
1907
- }
1908
- if (referencedSuperProperty && !yielded.has(referencedSuperProperty)) {
1909
- yielded.add(referencedSuperProperty);
1910
- yield referencedSuperProperty;
1911
- }
1912
- if (hasSuperProperty) {
1913
- continue;
2073
+ referenced.set(iri, result);
1914
2074
  }
1915
- if (typeUri === RDF.Property && this._hasSpecificType(graphUris, q.subject)) {
1916
- continue;
1917
- }
1918
- if (!yielded.has(q.subject.value)) {
1919
- yielded.add(q.subject.value);
1920
- yield q.subject.value;
2075
+ return result;
2076
+ };
2077
+ const isOwn = (iri) => {
2078
+ let result = own.get(iri);
2079
+ if (result === void 0) {
2080
+ result = !isReferenced(iri) && !this.skip(graphUris, namedNode3(iri), options);
2081
+ own.set(iri, result);
1921
2082
  }
1922
- }
2083
+ return result;
2084
+ };
2085
+ const canBeRoot = (iri) => typeUri !== RDF.Property || !this._hasSpecificType(graphUris, members.get(iri) ?? namedNode3(iri));
2086
+ yield* this.getHierarchyRoots(members.keys(), superProperties, isOwn, (iri) => isOwn(iri) || isReferenced(iri), canBeRoot);
1923
2087
  }
1924
2088
  /**
1925
2089
  * Indicate whether a property is stated to be of a type that is more specific than rdf:Property.
@@ -1999,8 +2163,9 @@ var PropertyRepository = class extends ClassRepository {
1999
2163
  */
2000
2164
  hasSubProperties(graphUris, subjectUri, options) {
2001
2165
  const o = namedNode3(subjectUri);
2166
+ const ontologies = this.getOtherOntologiesOnce(graphUris, options);
2002
2167
  for (let q of this.store.matchAll(graphUris, null, rdfs.subPropertyOf, o, options?.includeInferred)) {
2003
- if (!this.skip(graphUris, q.subject, options)) {
2168
+ if (!this.skip(graphUris, q.subject, options) || this.isReferencedAbove(graphUris, q.subject, rdfs.subPropertyOf, options, ontologies)) {
2004
2169
  return true;
2005
2170
  }
2006
2171
  }
@@ -2016,8 +2181,9 @@ var PropertyRepository = class extends ClassRepository {
2016
2181
  if (subjectUri) {
2017
2182
  const yielded = /* @__PURE__ */ new Set();
2018
2183
  const o = namedNode3(subjectUri);
2184
+ const ontologies = this.getOtherOntologiesOnce(graphUris, options);
2019
2185
  for (let q of this.store.matchAll(graphUris, null, rdfs.subPropertyOf, o, options?.includeInferred)) {
2020
- if (this.skip(graphUris, q.subject, options)) {
2186
+ if (this.skip(graphUris, q.subject, options) && !this.isReferencedAbove(graphUris, q.subject, rdfs.subPropertyOf, options, ontologies)) {
2021
2187
  continue;
2022
2188
  }
2023
2189
  if (!yielded.has(q.subject.value)) {
@@ -2035,28 +2201,23 @@ var PropertyRepository = class extends ClassRepository {
2035
2201
  * @returns An iterator of root properties in the repository.
2036
2202
  */
2037
2203
  *getRootProperties(graphUris, options) {
2038
- const yielded = /* @__PURE__ */ new Set();
2039
2204
  const properties = new Set(this.getProperties(graphUris, options));
2040
- for (let p of properties) {
2041
- let hasSuperProperty = false;
2042
- for (let q of this.store.matchAll(graphUris, namedNode3(p), rdfs.subPropertyOf, null, options?.includeInferred)) {
2043
- const includeReferenced = options?.includeReferenced ?? false;
2044
- const skip = this.skip(graphUris, q.object, options);
2045
- if (q.object.termType != "NamedNode" || skip && (!includeReferenced || includeReferenced && this.hasSubject(graphUris, q.object.value))) {
2046
- continue;
2047
- }
2048
- hasSuperProperty = q.object.value != q.subject.value;
2049
- if (!properties.has(q.object.value) && !yielded.has(q.object.value)) {
2050
- yielded.add(q.object.value);
2051
- yield q.object.value;
2052
- }
2053
- break;
2205
+ const ontologies = this.getOtherOntologiesOnce(graphUris, options);
2206
+ const superProperties = this.getParentMap(graphUris, rdfs.subPropertyOf, options);
2207
+ const visible = /* @__PURE__ */ new Map();
2208
+ const isVisible = (iri) => {
2209
+ if (properties.has(iri)) {
2210
+ return true;
2054
2211
  }
2055
- if (!hasSuperProperty && !yielded.has(p)) {
2056
- yielded.add(p);
2057
- yield p;
2212
+ let result = visible.get(iri);
2213
+ if (result === void 0) {
2214
+ const node = namedNode3(iri);
2215
+ result = !this.skip(graphUris, node, options) || this.isReferenced(graphUris, node, options, ontologies);
2216
+ visible.set(iri, result);
2058
2217
  }
2059
- }
2218
+ return result;
2219
+ };
2220
+ yield* this.getHierarchyRoots(properties, superProperties, (iri) => properties.has(iri), isVisible);
2060
2221
  }
2061
2222
  /**
2062
2223
  * Indicate if there is an equivalent property of a given property.
@@ -2123,7 +2284,7 @@ var PropertyRepository = class extends ClassRepository {
2123
2284
  };
2124
2285
 
2125
2286
  // src/rdf/individual-repository.ts
2126
- var { namedNode: namedNode4 } = dataFactory;
2287
+ var { namedNode: namedNode4 } = RdfDataFactory;
2127
2288
  var IndividualRepository = class extends PropertyRepository {
2128
2289
  constructor(store) {
2129
2290
  super(store);
@@ -2200,7 +2361,7 @@ var IndividualRepository = class extends PropertyRepository {
2200
2361
 
2201
2362
  // src/rdf/reasoners/reasoner.ts
2202
2363
  import { RdfStore } from "rdf-stores";
2203
- var { namedNode: namedNode5, blankNode: blankNode2 } = dataFactory;
2364
+ var { namedNode: namedNode5, blankNode: blankNode2 } = RdfDataFactory;
2204
2365
  var DefaultInferenceGraphHandler = class {
2205
2366
  getGraphUri(uri) {
2206
2367
  const u = typeof uri === "string" ? uri : uri.value;
@@ -2306,7 +2467,7 @@ var ReasonerBase = class {
2306
2467
  };
2307
2468
 
2308
2469
  // src/rdf/reasoners/skos-reasoner.ts
2309
- var { quad } = dataFactory;
2470
+ var { quad } = RdfDataFactory;
2310
2471
  var SkosReasoner = class extends ReasonerBase {
2311
2472
  constructor(targetUriGenerator = new DefaultInferenceGraphHandler()) {
2312
2473
  super(targetUriGenerator);
@@ -2428,7 +2589,7 @@ var SkosReasoner = class extends ReasonerBase {
2428
2589
  };
2429
2590
 
2430
2591
  // src/rdf/reasoners/rdfs-reasoner.ts
2431
- var { quad: quad2 } = dataFactory;
2592
+ var { quad: quad2 } = RdfDataFactory;
2432
2593
  var RdfsReasoner = class extends SkosReasoner {
2433
2594
  constructor(targetUriGenerator = new DefaultInferenceGraphHandler()) {
2434
2595
  super(targetUriGenerator);
@@ -2662,7 +2823,7 @@ var RdfsReasoner = class extends SkosReasoner {
2662
2823
  };
2663
2824
 
2664
2825
  // src/rdf/reasoners/shacl-reasoner.ts
2665
- var { quad: quad3 } = dataFactory;
2826
+ var { quad: quad3 } = RdfDataFactory;
2666
2827
  var ShaclReasoner = class extends RdfsReasoner {
2667
2828
  constructor(targetUriGenerator = new DefaultInferenceGraphHandler()) {
2668
2829
  super(targetUriGenerator);
@@ -2824,7 +2985,7 @@ var ShaclReasoner = class extends RdfsReasoner {
2824
2985
  };
2825
2986
 
2826
2987
  // src/rdf/reasoners/owl-reasoner.ts
2827
- var { quad: quad4 } = dataFactory;
2988
+ var { quad: quad4 } = RdfDataFactory;
2828
2989
  var OwlReasoner = class extends ShaclReasoner {
2829
2990
  constructor(targetUriGenerator = new DefaultInferenceGraphHandler()) {
2830
2991
  super(targetUriGenerator);
@@ -3008,7 +3169,7 @@ var OwlReasoner = class extends ShaclReasoner {
3008
3169
  };
3009
3170
 
3010
3171
  // src/rdf/shape-repository.ts
3011
- var { namedNode: namedNode6 } = dataFactory;
3172
+ var { namedNode: namedNode6 } = RdfDataFactory;
3012
3173
  var ShapeRepository = class extends IndividualRepository {
3013
3174
  constructor(store) {
3014
3175
  super(store);
@@ -7797,7 +7958,7 @@ var Store = class {
7797
7958
  };
7798
7959
 
7799
7960
  // src/rdf/vocabulary-repository.ts
7800
- var { namedNode: namedNode7 } = dataFactory;
7961
+ var { namedNode: namedNode7 } = RdfDataFactory;
7801
7962
  var VocabularyRepository = class extends ShapeRepository {
7802
7963
  /**
7803
7964
  * Get all concept schemes in the repository.
@@ -7916,7 +8077,7 @@ export {
7916
8077
  BlankNode,
7917
8078
  ClassRepository,
7918
8079
  DCAT,
7919
- DataFactory,
8080
+ DataFactory2 as DataFactory,
7920
8081
  DatasetView,
7921
8082
  DefaultGraph,
7922
8083
  DefaultInferenceGraphHandler,