@faubulous/mentor-rdf 2.2.0 → 2.2.2

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
@@ -2603,13 +2603,13 @@ var DefaultGraph = class {
2603
2603
  }
2604
2604
  };
2605
2605
  var Quad = class {
2606
- constructor(subject, predicate, object, graph2) {
2606
+ constructor(subject, predicate, object, graph) {
2607
2607
  this.termType = "Quad";
2608
2608
  this.value = "";
2609
2609
  this.subject = subject;
2610
2610
  this.predicate = predicate;
2611
2611
  this.object = object;
2612
- this.graph = graph2 ?? new DefaultGraph();
2612
+ this.graph = graph ?? new DefaultGraph();
2613
2613
  }
2614
2614
  equals(other) {
2615
2615
  if (other?.termType !== this.termType) {
@@ -2673,8 +2673,8 @@ var DataFactory = class {
2673
2673
  * @param graph The graph of the quad (defaults to the default graph).
2674
2674
  * @returns A new Quad instance.
2675
2675
  */
2676
- quad(subject, predicate, object, graph2) {
2677
- return new Quad(subject, predicate, object, graph2);
2676
+ quad(subject, predicate, object, graph) {
2677
+ return new Quad(subject, predicate, object, graph);
2678
2678
  }
2679
2679
  /**
2680
2680
  * Creates a copy of an existing term.
@@ -4127,17 +4127,17 @@ var DatasetView = class _DatasetView {
4127
4127
  }
4128
4128
  const scope = [];
4129
4129
  const seen = /* @__PURE__ */ new Set();
4130
- const push = (graph2) => {
4131
- const key = graph2.termType + ":" + graph2.value;
4130
+ const push = (graph) => {
4131
+ const key = graph.termType + ":" + graph.value;
4132
4132
  if (!seen.has(key)) {
4133
4133
  seen.add(key);
4134
- scope.push(graph2);
4134
+ scope.push(graph);
4135
4135
  }
4136
4136
  };
4137
- for (const graph2 of graphs) {
4138
- push(graph2);
4139
- if (options.includeInferred !== false && store.reasoner && graph2.termType === "NamedNode") {
4140
- push(store.dataFactory.namedNode(store.reasoner.targetUriGenerator.getGraphUri(graph2.value)));
4137
+ for (const graph of graphs) {
4138
+ push(graph);
4139
+ if (options.includeInferred !== false && store.reasoner && graph.termType === "NamedNode") {
4140
+ push(store.dataFactory.namedNode(store.reasoner.targetUriGenerator.getGraphUri(graph.value)));
4141
4141
  }
4142
4142
  }
4143
4143
  return scope;
@@ -4154,8 +4154,8 @@ var DatasetView = class _DatasetView {
4154
4154
  if (this._scope === void 0) {
4155
4155
  n = this._store.countQuads(this._s, this._p, this._o, null);
4156
4156
  } else {
4157
- for (const graph2 of this._scope) {
4158
- n += this._store.countQuads(this._s, this._p, this._o, graph2);
4157
+ for (const graph of this._scope) {
4158
+ n += this._store.countQuads(this._s, this._p, this._o, graph);
4159
4159
  }
4160
4160
  }
4161
4161
  this._cachedSize = n;
@@ -4191,7 +4191,7 @@ var DatasetView = class _DatasetView {
4191
4191
  if (this._o && !this._o.equals(quad5.object)) {
4192
4192
  return false;
4193
4193
  }
4194
- if (this._scope !== void 0 && !this._scope.some((graph2) => graph2.equals(quad5.graph))) {
4194
+ if (this._scope !== void 0 && !this._scope.some((graph) => graph.equals(quad5.graph))) {
4195
4195
  return false;
4196
4196
  }
4197
4197
  return this._store.has(quad5);
@@ -4206,14 +4206,14 @@ var DatasetView = class _DatasetView {
4206
4206
  * @param graph The optional graph.
4207
4207
  * @returns A new read-only dataset view of the matching quads.
4208
4208
  */
4209
- match(subject, predicate, object, graph2) {
4209
+ match(subject, predicate, object, graph) {
4210
4210
  const s = _DatasetView._intersectTerm(this._s, subject);
4211
4211
  const p = _DatasetView._intersectTerm(this._p, predicate);
4212
4212
  const o = _DatasetView._intersectTerm(this._o, object);
4213
4213
  let scope = this._scope;
4214
- if (graph2 != null) {
4215
- if (scope === void 0 || scope.some((g) => g.equals(graph2))) {
4216
- scope = [graph2];
4214
+ if (graph != null) {
4215
+ if (scope === void 0 || scope.some((g) => g.equals(graph))) {
4216
+ scope = [graph];
4217
4217
  } else {
4218
4218
  scope = [];
4219
4219
  }
@@ -4249,8 +4249,8 @@ var DatasetView = class _DatasetView {
4249
4249
  if (this._scope === void 0) {
4250
4250
  yield* this._store.matchQuads(this._s, this._p, this._o, null);
4251
4251
  } else {
4252
- for (const graph2 of this._scope) {
4253
- yield* this._store.matchQuads(this._s, this._p, this._o, graph2);
4252
+ for (const graph of this._scope) {
4253
+ yield* this._store.matchQuads(this._s, this._p, this._o, graph);
4254
4254
  }
4255
4255
  }
4256
4256
  }
@@ -4632,11 +4632,11 @@ var ReasonerBase = class {
4632
4632
  throw new Error(`Invalid value for targetUriGenerator: ${targetUriGenerator}`);
4633
4633
  }
4634
4634
  }
4635
- getGraphNode(graph2) {
4636
- if (typeof graph2 === "string") {
4637
- return namedNode5(graph2);
4635
+ getGraphNode(graph) {
4636
+ if (typeof graph === "string") {
4637
+ return namedNode5(graph);
4638
4638
  } else {
4639
- return graph2;
4639
+ return graph;
4640
4640
  }
4641
4641
  }
4642
4642
  isW3CNode(term) {
@@ -4678,9 +4678,9 @@ var ReasonerBase = class {
4678
4678
  * @param object An object URI or null to match any object.
4679
4679
  * @todo Refactor and merge with the same method in the Mentor RDF Store class.
4680
4680
  */
4681
- *match(graph2, subject, predicate, object) {
4682
- if (graph2 !== void 0) {
4683
- yield* this.store.match(subject, predicate, object, graph2);
4681
+ *match(graph, subject, predicate, object) {
4682
+ if (graph !== void 0) {
4683
+ yield* this.store.match(subject, predicate, object, graph);
4684
4684
  } else {
4685
4685
  yield* this.store.match(subject, predicate, object);
4686
4686
  }
@@ -4842,6 +4842,18 @@ var RdfsReasoner = class extends SkosReasoner {
4842
4842
  this.classes = /* @__PURE__ */ new Set();
4843
4843
  this.properties = /* @__PURE__ */ new Set();
4844
4844
  this.individuals = /* @__PURE__ */ new Set();
4845
+ /**
4846
+ * The super properties of each property, as asserted with `rdfs:subPropertyOf`.
4847
+ */
4848
+ this._superProperties = /* @__PURE__ */ new Map();
4849
+ /**
4850
+ * The ranges asserted for each property.
4851
+ */
4852
+ this._ranges = /* @__PURE__ */ new Map();
4853
+ /**
4854
+ * The domains asserted for each property.
4855
+ */
4856
+ this._domains = /* @__PURE__ */ new Map();
4845
4857
  }
4846
4858
  isIgnoredNode(term) {
4847
4859
  switch (term.value) {
@@ -4866,6 +4878,47 @@ var RdfsReasoner = class extends SkosReasoner {
4866
4878
  for (let individual of individuals) {
4867
4879
  this.store.add(quad2(individual, rdf.type, owl.NamedIndividual, this.targetGraph));
4868
4880
  }
4881
+ this._inferInheritedRangesAndDomains();
4882
+ }
4883
+ /**
4884
+ * Infers the ranges and domains that properties inherit from their super properties
4885
+ * (OWL 2 RL rules scm-rng2 and scm-dom2), following `rdfs:subPropertyOf` chains.
4886
+ */
4887
+ _inferInheritedRangesAndDomains() {
4888
+ for (const { property, superProperties } of this._superProperties.values()) {
4889
+ for (const superProperty of this._getAncestorProperties(property.value, superProperties)) {
4890
+ for (const range of this._ranges.get(superProperty) ?? []) {
4891
+ this.store.add(quad2(property, rdfs.range, range, this.targetGraph));
4892
+ }
4893
+ for (const domain of this._domains.get(superProperty) ?? []) {
4894
+ this.store.add(quad2(property, rdfs.domain, domain, this.targetGraph));
4895
+ }
4896
+ }
4897
+ }
4898
+ }
4899
+ /**
4900
+ * Gets the transitive super properties of a property, nearest first. Cycles in the
4901
+ * property hierarchy are followed only once.
4902
+ * @param property The IRI or blank node id of a property.
4903
+ * @param superProperties The direct super properties of the property.
4904
+ * @returns The IRIs of all super properties, excluding the property itself.
4905
+ */
4906
+ _getAncestorProperties(property, superProperties) {
4907
+ const result = [];
4908
+ const visited = /* @__PURE__ */ new Set([property]);
4909
+ const queue = [...superProperties];
4910
+ while (queue.length > 0) {
4911
+ const current = queue.shift();
4912
+ if (visited.has(current)) {
4913
+ continue;
4914
+ }
4915
+ visited.add(current);
4916
+ result.push(current);
4917
+ for (const next of this._superProperties.get(current)?.superProperties ?? []) {
4918
+ queue.push(next);
4919
+ }
4920
+ }
4921
+ return result;
4869
4922
  }
4870
4923
  resetState() {
4871
4924
  super.resetState();
@@ -4873,6 +4926,9 @@ var RdfsReasoner = class extends SkosReasoner {
4873
4926
  this.classes.clear();
4874
4927
  this.properties.clear();
4875
4928
  this.individuals.clear();
4929
+ this._superProperties.clear();
4930
+ this._ranges.clear();
4931
+ this._domains.clear();
4876
4932
  }
4877
4933
  applyInference(quad5) {
4878
4934
  super.applyInference(quad5);
@@ -4966,13 +5022,49 @@ var RdfsReasoner = class extends SkosReasoner {
4966
5022
  }
4967
5023
  return;
4968
5024
  }
4969
- case rdfs.range.value:
5025
+ case rdfs.subPropertyOf.value: {
5026
+ this._addSuperProperty(s, o);
5027
+ return;
5028
+ }
5029
+ case rdfs.range.value: {
5030
+ this.assertProperty(s);
5031
+ this._addToList(this._ranges, s.value, o);
5032
+ return;
5033
+ }
4970
5034
  case rdfs.domain.value: {
4971
5035
  this.assertProperty(s);
5036
+ this._addToList(this._domains, s.value, o);
4972
5037
  return;
4973
5038
  }
4974
5039
  }
4975
5040
  }
5041
+ /**
5042
+ * Records that a property is a sub property of another property.
5043
+ * @param property The sub property.
5044
+ * @param superProperty The super property.
5045
+ */
5046
+ _addSuperProperty(property, superProperty) {
5047
+ let entry = this._superProperties.get(property.value);
5048
+ if (!entry) {
5049
+ entry = { property, superProperties: /* @__PURE__ */ new Set() };
5050
+ this._superProperties.set(property.value, entry);
5051
+ }
5052
+ entry.superProperties.add(superProperty.value);
5053
+ }
5054
+ /**
5055
+ * Adds a value to the list stored under a key.
5056
+ * @param map The map of lists.
5057
+ * @param key The key of the list.
5058
+ * @param value The value to add.
5059
+ */
5060
+ _addToList(map, key, value) {
5061
+ const list = map.get(key);
5062
+ if (list) {
5063
+ list.push(value);
5064
+ } else {
5065
+ map.set(key, [value]);
5066
+ }
5067
+ }
4976
5068
  };
4977
5069
 
4978
5070
  // src/rdf/reasoners/shacl-reasoner.ts
@@ -9292,11 +9384,9 @@ xsd:usignedByte a rdfs:Datatype ;
9292
9384
  // src/rdf/store.ts
9293
9385
  import { RdfStore as RdfStore2 } from "rdf-stores";
9294
9386
  import { RdfXmlParser } from "rdfxml-streaming-parser";
9295
- import { graph, serialize } from "rdflib";
9296
9387
 
9297
9388
  // src/rdf/utils.ts
9298
- import * as rdflib from "rdflib";
9299
- var toRdflibTerm = (term) => {
9389
+ var toRdflibTerm = (term, rdflib) => {
9300
9390
  switch (term.termType) {
9301
9391
  case "NamedNode":
9302
9392
  return rdflib.namedNode(term.value);
@@ -9451,18 +9541,18 @@ var Store = class {
9451
9541
  * Indicates if a graph holds at least one triple, without reading more than one.
9452
9542
  * @param graph The graph term, or its URI with an empty string for the default graph.
9453
9543
  */
9454
- _hasTriples(graph2) {
9455
- return !this._store.readQuads(null, null, null, this._getGraphTerm(graph2)).next().done;
9544
+ _hasTriples(graph) {
9545
+ return !this._store.readQuads(null, null, null, this._getGraphTerm(graph)).next().done;
9456
9546
  }
9457
9547
  /**
9458
9548
  * Resolves a graph URI to its term; an empty string denotes the default graph.
9459
9549
  * @param graph The graph term or URI.
9460
9550
  */
9461
- _getGraphTerm(graph2) {
9462
- if (typeof graph2 !== "string") {
9463
- return graph2;
9551
+ _getGraphTerm(graph) {
9552
+ if (typeof graph !== "string") {
9553
+ return graph;
9464
9554
  }
9465
- return graph2 === "" ? this.dataFactory.defaultGraph() : this.dataFactory.namedNode(graph2);
9555
+ return graph === "" ? this.dataFactory.defaultGraph() : this.dataFactory.namedNode(graph);
9466
9556
  }
9467
9557
  /**
9468
9558
  * Load a set of triples into the store.
@@ -9495,17 +9585,17 @@ var Store = class {
9495
9585
  ));
9496
9586
  }
9497
9587
  _loadQuads(quads, graphUri, executeInference = true, clearGraph = true, onQuad) {
9498
- const graph2 = this.dataFactory.namedNode(graphUri.replace("\\", "/"));
9588
+ const graph = this.dataFactory.namedNode(graphUri.replace("\\", "/"));
9499
9589
  if (clearGraph) {
9500
9590
  this.deleteGraphs([graphUri], true);
9501
9591
  }
9502
9592
  for (let q of quads) {
9503
- this._ds.add(this.dataFactory.quad(q.subject, q.predicate, q.object, graph2));
9593
+ this._ds.add(this.dataFactory.quad(q.subject, q.predicate, q.object, graph));
9504
9594
  if (onQuad) {
9505
9595
  onQuad(q);
9506
9596
  }
9507
9597
  }
9508
- this._touchGraph(graph2.value);
9598
+ this._touchGraph(graph.value);
9509
9599
  if (this.reasoner && executeInference) {
9510
9600
  this._expand(graphUri);
9511
9601
  }
@@ -9611,15 +9701,16 @@ var Store = class {
9611
9701
  * @returns A string serialization of the triples in the graph in the specified format.
9612
9702
  */
9613
9703
  async serializeGraph(sourceGraphUri, targetFormat = "text/turtle", targetGraphUri, prefixes) {
9704
+ const rdflib = await import("rdflib");
9614
9705
  return new Promise((resolve, reject) => {
9615
9706
  const sourceGraph = this.dataFactory.namedNode(sourceGraphUri);
9616
9707
  const targetGraph = targetGraphUri ? this.dataFactory.namedNode(targetGraphUri) : void 0;
9617
- const store = graph();
9618
- const g = targetGraph ? toRdflibTerm(targetGraph) : void 0;
9708
+ const store = rdflib.graph();
9709
+ const g = targetGraph ? toRdflibTerm(targetGraph, rdflib) : void 0;
9619
9710
  for (const q of this._store.readQuads(null, null, null, sourceGraph)) {
9620
- const s = toRdflibTerm(q.subject);
9621
- const p = toRdflibTerm(q.predicate);
9622
- const o = toRdflibTerm(q.object);
9711
+ const s = toRdflibTerm(q.subject, rdflib);
9712
+ const p = toRdflibTerm(q.predicate, rdflib);
9713
+ const o = toRdflibTerm(q.object, rdflib);
9623
9714
  store.add(s, p, o, g);
9624
9715
  }
9625
9716
  const callback = (error, result) => {
@@ -9629,7 +9720,7 @@ var Store = class {
9629
9720
  resolve(result ?? "");
9630
9721
  }
9631
9722
  };
9632
- serialize(null, store, null, targetFormat, callback, prefixes);
9723
+ rdflib.serialize(null, store, null, targetFormat, callback, prefixes);
9633
9724
  });
9634
9725
  }
9635
9726
  /**
@@ -9721,8 +9812,8 @@ var Store = class {
9721
9812
  * @param object The optional object.
9722
9813
  * @param graph The optional graph.
9723
9814
  */
9724
- countQuads(subject, predicate, object, graph2) {
9725
- return this._store.countQuads(subject, predicate, object, graph2);
9815
+ countQuads(subject, predicate, object, graph) {
9816
+ return this._store.countQuads(subject, predicate, object, graph);
9726
9817
  }
9727
9818
  /**
9728
9819
  * Returns a lazy, read-only dataset view of the quads matching the given pattern.
@@ -9736,12 +9827,12 @@ var Store = class {
9736
9827
  * @param graph The optional graph.
9737
9828
  * @returns A read-only dataset view of the matching quads.
9738
9829
  */
9739
- match(subject, predicate, object, graph2) {
9830
+ match(subject, predicate, object, graph) {
9740
9831
  return new DatasetView(this, {
9741
9832
  subject: subject ?? null,
9742
9833
  predicate: predicate ?? null,
9743
9834
  object: object ?? null,
9744
- graphs: graph2 != null ? [graph2] : void 0,
9835
+ graphs: graph != null ? [graph] : void 0,
9745
9836
  includeInferred: false
9746
9837
  });
9747
9838
  }
@@ -9753,8 +9844,8 @@ var Store = class {
9753
9844
  * @param object The optional object.
9754
9845
  * @param graph The optional graph.
9755
9846
  */
9756
- *matchQuads(subject, predicate, object, graph2) {
9757
- yield* this._store.readQuads(subject, predicate, object, graph2);
9847
+ *matchQuads(subject, predicate, object, graph) {
9848
+ yield* this._store.readQuads(subject, predicate, object, graph);
9758
9849
  }
9759
9850
  /**
9760
9851
  * Get an RDF/JS `Source` interface for the store that streams matching quads.
@@ -9763,7 +9854,7 @@ var Store = class {
9763
9854
  */
9764
9855
  asSource() {
9765
9856
  return {
9766
- match: (subject, predicate, object, graph2) => this._store.match(subject, predicate, object, graph2)
9857
+ match: (subject, predicate, object, graph) => this._store.match(subject, predicate, object, graph)
9767
9858
  };
9768
9859
  }
9769
9860
  /**
@@ -9784,10 +9875,10 @@ var Store = class {
9784
9875
  const o = object;
9785
9876
  if (graphUris !== void 0) {
9786
9877
  const graphs = Array.isArray(graphUris) ? graphUris : [graphUris];
9787
- for (const graph2 of graphs.map((g) => this.dataFactory.namedNode(g))) {
9788
- yield* this._store.readQuads(s, p, o, graph2);
9878
+ for (const graph of graphs.map((g) => this.dataFactory.namedNode(g))) {
9879
+ yield* this._store.readQuads(s, p, o, graph);
9789
9880
  if (includeInferred !== false && this.reasoner) {
9790
- const inferenceGraphUri = this.reasoner.targetUriGenerator.getGraphUri(graph2.value);
9881
+ const inferenceGraphUri = this.reasoner.targetUriGenerator.getGraphUri(graph.value);
9791
9882
  const inferenceGraph = this.dataFactory.namedNode(inferenceGraphUri);
9792
9883
  yield* this._store.readQuads(s, p, o, inferenceGraph);
9793
9884
  }
@@ -9842,7 +9933,7 @@ var Store = class {
9842
9933
  };
9843
9934
 
9844
9935
  // src/rdf/vocabulary-repository.ts
9845
- var { namedNode: namedNode8 } = dataFactory;
9936
+ var { namedNode: namedNode7 } = dataFactory;
9846
9937
  var VocabularyRepository = class extends ShapeRepository {
9847
9938
  /**
9848
9939
  * Get all concept schemes in the repository.
@@ -9909,7 +10000,7 @@ var VocabularyRepository = class extends ShapeRepository {
9909
10000
  * @returns The version of the ontology, or undefined if it is not found.
9910
10001
  */
9911
10002
  getOntologyVersionInfo(graphUris, ontologyUri) {
9912
- const s = namedNode8(ontologyUri);
10003
+ const s = namedNode7(ontologyUri);
9913
10004
  for (const q of this.store.matchAll(graphUris, s, owl.versionInfo, null)) {
9914
10005
  const match = q.object.value.match(/(\d{4}[\/-]\d{2}[\/-]\d{2})/);
9915
10006
  if (match && match.length > 1) {