@faubulous/mentor-rdf 0.2.48 → 0.2.49

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/out/index.js CHANGED
@@ -665,7 +665,7 @@ eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexpo
665
665
  /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
666
666
 
667
667
  "use strict";
668
- eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.SparqlSyntaxParser = void 0;\nconst millan_1 = __webpack_require__(/*! millan */ \"./node_modules/millan/dist/millan.js\");\n/**\n * An extended version of the Millan SPARQL parser that also returns semantic errors such as undefined prefixes.\n */\nclass SparqlSyntaxParser {\n constructor() {\n this.parser = new millan_1.W3SpecSparqlParser();\n this.parse = (document) => {\n const { cst, errors, comments } = this.parser.parse(document);\n const tokens = this.parser.input;\n const semanticErrors = [];\n const declaredPrefixes = new Set();\n for (let i = 0; i < tokens.length; i++) {\n const token = tokens[i];\n const tokenName = token.tokenType?.name;\n switch (tokenName) {\n case \"PNAME_NS\": {\n const prefix = token.image.split(\":\")[0];\n declaredPrefixes.add(prefix);\n break;\n }\n case \"PNAME_LN\": {\n const prefix = token.image.split(\":\")[0];\n if (!declaredPrefixes.has(prefix)) {\n semanticErrors.push({\n message: `Undefined prefix: ${prefix}`,\n name: \"NoNamespacePrefixError\",\n token: token,\n resyncedTokens: [],\n context: {\n ruleStack: [],\n ruleOccurrenceStack: []\n }\n });\n }\n break;\n }\n }\n }\n return {\n comments: comments ? comments : [],\n cst,\n errors,\n semanticErrors,\n tokens,\n };\n };\n }\n get input() {\n return this.parser.input;\n }\n tokenize(document) {\n return this.parser.tokenize(document);\n }\n}\nexports.SparqlSyntaxParser = SparqlSyntaxParser;\n\n\n//# sourceURL=webpack://@faubulous/mentor-rdf/./src/rdf/parsers/sparql-syntax-parser.ts?");
668
+ eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.SparqlSyntaxParser = void 0;\nconst millan_1 = __webpack_require__(/*! millan */ \"./node_modules/millan/dist/millan.js\");\n/**\n * An extended version of the Millan SPARQL parser that also returns semantic errors such as undefined prefixes.\n */\nclass SparqlSyntaxParser {\n constructor() {\n this.parser = new millan_1.W3SpecSparqlParser();\n this.parse = (document) => {\n const { cst, errors, comments } = this.parser.parse(document);\n const tokens = this.parser.input;\n const semanticErrors = [];\n const declaredPrefixes = new Set();\n for (let i = 0; i < tokens.length; i++) {\n const token = tokens[i];\n const tokenName = token.tokenType?.name;\n switch (tokenName) {\n case \"PNAME_NS\": {\n const prefix = token.image.split(\":\")[0];\n declaredPrefixes.add(prefix);\n break;\n }\n case \"PNAME_LN\": {\n const prefix = token.image.split(\":\")[0];\n if (!declaredPrefixes.has(prefix)) {\n semanticErrors.push({\n message: `Undefined prefix: ${prefix}`,\n name: \"NoNamespacePrefixError\",\n token: token,\n resyncedTokens: [],\n context: {\n ruleStack: [],\n ruleOccurrenceStack: []\n }\n });\n }\n break;\n }\n }\n }\n return {\n comments: comments ? comments : [],\n cst,\n errors,\n semanticErrors,\n tokens,\n };\n };\n }\n get input() {\n return this.parser ? this.parser.input : [];\n }\n tokenize(document) {\n // Note: The Millan SPARQL parser is crashing when parsing large files to build the CST tree for error detection.\n // Therefore, we access the lexer directly to tokenize the document including comments.\n const lexer = this.parser.lexer;\n const result = lexer.tokenize(document);\n const tokens = result.tokens;\n const comments = result.groups.comments;\n return comments ? [...tokens, ...comments] : tokens;\n }\n}\nexports.SparqlSyntaxParser = SparqlSyntaxParser;\n\n\n//# sourceURL=webpack://@faubulous/mentor-rdf/./src/rdf/parsers/sparql-syntax-parser.ts?");
669
669
 
670
670
  /***/ }),
671
671
 
@@ -775,7 +775,7 @@ eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexpo
775
775
  /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
776
776
 
777
777
  "use strict";
778
- eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.ResourceRepository = void 0;\nconst tslib_1 = __webpack_require__(/*! tslib */ \"./node_modules/tslib/tslib.es6.mjs\");\nconst n3 = tslib_1.__importStar(__webpack_require__(/*! n3 */ \"./node_modules/n3/src/index.js\"));\nconst ontologies_1 = __webpack_require__(/*! ../ontologies */ \"./src/ontologies/index.ts\");\nconst uri_1 = __webpack_require__(/*! ./uri */ \"./src/rdf/uri.ts\");\n/**\n * A repository for retrieving resources from graphs.\n */\nclass ResourceRepository {\n constructor(store) {\n this.store = store;\n }\n /**\n * Inidiates if a node should not be included in the result of a definition query.\n * @param graphUris URIs of the graphs to search.\n * @param subject The focus node.\n * @param options Definition query options.\n * @param optionDefaults Custom default values for the query options.\n * @returns\n */\n skip(graphUris, subject, options, optionDefaults = { includeBlankNodes: false, includeReferenced: false }) {\n const opts = { ...optionDefaults, ...options };\n if (!opts.includeBlankNodes && subject.termType != \"NamedNode\") {\n // We are only interested in named nodes.\n return true;\n }\n if (!opts.includeReferenced && !this.hasSubject(graphUris, subject)) {\n // Skip the node if it is not explicitly defined in the graph as a subject.\n return true;\n }\n if (opts?.definedBy !== undefined) {\n const isDefined = this.isDefinedBy(graphUris, subject, opts.definedBy);\n // Do not skip the node if it has a different namespace but is *explicitly* defined by the given URI.\n if (opts.definedBy === null && isDefined || opts.definedBy !== null && !isDefined) {\n return true;\n }\n }\n else if (opts?.notDefinedBy !== undefined) {\n for (const source of opts.notDefinedBy) {\n if (this.isDefinedBy(graphUris, subject, source)) {\n return true;\n }\n }\n }\n return false;\n }\n /**\n * Indicate if a given URI exists as the subject of a triple in the graph.\n * @param graphUris URIs of the graphs to search.\n * @param subjectUri URI of the subject to search for.\n * @returns true if the URI is a subject, false otherwise.\n */\n hasSubject(graphUris, subjectUri) {\n let s;\n if (typeof subjectUri === \"string\") {\n s = n3.DataFactory.namedNode(subjectUri);\n }\n else if (subjectUri.termType !== \"Literal\") {\n s = subjectUri;\n }\n if (s) {\n for (let _ of this.store.match(graphUris, s, null, null, false)) {\n return true;\n }\n }\n return false;\n }\n /**\n * Indicate if a node is explicitly defined by the given URI using the `rdfs:isDefinedBy` property, or implicitly by sharing the same namespace.\n * @param graphUris URIs of the graphs to search.\n * @param node A named node to check if it is defined by the given URI.\n * @param definedBy URI of the vocabulary that defines the resource (rdfs:isDefinedBy). Provide `null` to only return resources that have no `rdfs:isDefinedBy` property.\n * @returns `true` if the node is defined by the given URI, `false` otherwise.\n */\n isDefinedBy(graphUris, node, definedBy) {\n const s = node;\n if (definedBy === null) {\n // If there is no definedBy URI, we assume that the resource is not\n // explicitly defined by any ontology.\n for (let _ of this.store.match(graphUris, s, ontologies_1.rdfs.isDefinedBy, null)) {\n return true;\n }\n }\n else {\n // We assume that a resource is defined by an ontology if it starts with the namespace of the ontology.\n if (node.value.startsWith(definedBy)) {\n return true;\n }\n // If the ontology is something like <http://purl.obolibrary.org/obo/bfo.owl> or <http://www.w3.org/ns/prov#>\n // and the subject is <http://purl.obolibrary.org/obo/BFO_0000001> or <http://www.w3.org/ns/prov#Entity>\n // then we assume that it is defined by the ontology. The OBO case shows that we need to remove the file\n // name in oder to be able to compare the namespace URIs.\n const ontologyNamespace = uri_1.Uri.getNormalizedUri(uri_1.Uri.getNamespaceUri(definedBy));\n const subjectNamespace = uri_1.Uri.getNormalizedUri(uri_1.Uri.getNamespaceUri(node.value));\n if (subjectNamespace === ontologyNamespace) {\n return true;\n }\n const o = n3.DataFactory.namedNode(uri_1.Uri.getNormalizedUri(definedBy));\n // The explicit annotation of the definition source has precedence over heuristic checks.\n for (let q of this.store.match(graphUris, s, ontologies_1.rdfs.isDefinedBy, null)) {\n if (uri_1.Uri.getNormalizedUri(q.object.value) === o.value) {\n return true;\n }\n }\n // A blank node is defined in the namespace if it is the object of a triple that is defined by the URI.\n if (s.termType === \"BlankNode\") {\n for (let q of this.store.match(graphUris, null, null, s, false)) {\n return this.isDefinedBy(graphUris, q.subject, definedBy);\n }\n }\n }\n return false;\n }\n /**\n * Indicate if a resource has a given type in the graph.\n * @param graphUris URIs of the graphs to search.\n * @param subjectUri URI or blank node id of the subject to match.\n * @param typeUri URI of the type to match.\n * @param options Optional query parameters.\n * @returns `true` if the resource has the type, `false` otherwise.\n */\n hasType(graphUris, subjectUri, typeUri, options) {\n const o = n3.DataFactory.namedNode(typeUri);\n for (let _ of this.store.match(graphUris, n3.DataFactory.namedNode(subjectUri), ontologies_1.rdf.type, o, options?.includeInferred)) {\n return true;\n }\n for (let _ of this.store.match(graphUris, n3.DataFactory.blankNode(subjectUri), ontologies_1.rdf.type, o, options?.includeInferred)) {\n return true;\n }\n return false;\n }\n}\nexports.ResourceRepository = ResourceRepository;\n\n\n//# sourceURL=webpack://@faubulous/mentor-rdf/./src/rdf/resource-repository.ts?");
778
+ eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.ResourceRepository = void 0;\nconst tslib_1 = __webpack_require__(/*! tslib */ \"./node_modules/tslib/tslib.es6.mjs\");\nconst n3 = tslib_1.__importStar(__webpack_require__(/*! n3 */ \"./node_modules/n3/src/index.js\"));\nconst ontologies_1 = __webpack_require__(/*! ../ontologies */ \"./src/ontologies/index.ts\");\nconst uri_1 = __webpack_require__(/*! ./uri */ \"./src/rdf/uri.ts\");\n/**\n * A repository for retrieving resources from graphs.\n */\nclass ResourceRepository {\n constructor(store) {\n this.store = store;\n }\n /**\n * Inidiates if a node should not be included in the result of a definition query.\n * @param graphUris URIs of the graphs to search.\n * @param subject The focus node.\n * @param options Definition query options.\n * @param optionDefaults Custom default values for the query options.\n * @returns\n */\n skip(graphUris, subject, options, optionDefaults = { includeBlankNodes: false, includeReferenced: false }) {\n const opts = { ...optionDefaults, ...options };\n if (!opts.includeBlankNodes && subject.termType != \"NamedNode\") {\n // We are only interested in named nodes.\n return true;\n }\n if (!opts.includeReferenced && !this.hasSubject(graphUris, subject)) {\n // Skip the node if it is not explicitly defined in the graph as a subject.\n return true;\n }\n if (opts?.definedBy !== undefined) {\n const isDefined = this.isDefinedBy(graphUris, subject, opts.definedBy);\n // Do not skip the node if it has a different namespace but is *explicitly* defined by the given URI.\n if (opts.definedBy === null && isDefined || opts.definedBy !== null && !isDefined) {\n return true;\n }\n }\n else if (opts?.notDefinedBy !== undefined) {\n for (const source of opts.notDefinedBy) {\n if (this.isDefinedBy(graphUris, subject, source)) {\n return true;\n }\n }\n }\n return false;\n }\n /**\n * Indicate if a given URI exists as the subject of a triple in the graph.\n * @param graphUris URIs of the graphs to search.\n * @param subjectUri URI of the subject to search for.\n * @returns true if the URI is a subject, false otherwise.\n */\n hasSubject(graphUris, subjectUri) {\n let s;\n if (typeof subjectUri === \"string\") {\n s = n3.DataFactory.namedNode(subjectUri);\n }\n else if (subjectUri.termType !== \"Literal\") {\n s = subjectUri;\n }\n if (s) {\n for (let _ of this.store.match(graphUris, s, null, null, false)) {\n return true;\n }\n }\n return false;\n }\n /**\n * Indicate if a node is explicitly defined by the given URI using the `rdfs:isDefinedBy` property, or implicitly by sharing the same namespace.\n * @param graphUris URIs of the graphs to search.\n * @param node A named node to check if it is defined by the given URI.\n * @param definedBy URI of the vocabulary that defines the resource (rdfs:isDefinedBy). Provide `null` to only return resources that have no `rdfs:isDefinedBy` property.\n * @returns `true` if the node is defined by the given URI, `false` otherwise.\n */\n isDefinedBy(graphUris, node, definedBy) {\n const s = node;\n if (definedBy === null) {\n // If there is no definedBy URI, we assume that the resource is not\n // explicitly defined by any ontology.\n for (let _ of this.store.match(graphUris, s, ontologies_1.rdfs.isDefinedBy, null)) {\n return true;\n }\n }\n else {\n // We assume that a resource is defined by an ontology if it starts with the namespace of the ontology.\n if (node.value.startsWith(definedBy)) {\n return true;\n }\n // If the ontology is something like <http://purl.obolibrary.org/obo/bfo.owl> or <http://www.w3.org/ns/prov#>\n // and the subject is <http://purl.obolibrary.org/obo/BFO_0000001> or <http://www.w3.org/ns/prov#Entity>\n // then we assume that it is defined by the ontology. The OBO case shows that we need to remove the file\n // name in oder to be able to compare the namespace URIs.\n const ontologyNamespace = uri_1.Uri.getNormalizedUri(uri_1.Uri.getNamespaceUri(definedBy));\n const subjectNamespace = uri_1.Uri.getNormalizedUri(uri_1.Uri.getNamespaceUri(node.value));\n if (subjectNamespace === ontologyNamespace) {\n return true;\n }\n const o = n3.DataFactory.namedNode(uri_1.Uri.getNormalizedUri(definedBy));\n // The explicit annotation of the definition source has precedence over heuristic checks.\n for (let q of this.store.match(graphUris, s, ontologies_1.rdfs.isDefinedBy, null)) {\n if (uri_1.Uri.getNormalizedUri(q.object.value) === o.value) {\n return true;\n }\n }\n // A blank node is defined in the namespace if it is the object of a triple that is defined by the URI.\n if (s.termType === \"BlankNode\") {\n for (let q of this.store.match(graphUris, null, null, s, false)) {\n return this.isDefinedBy(graphUris, q.subject, definedBy);\n }\n }\n }\n return false;\n }\n /**\n * Indicate if a resource has a given type in the graph.\n * @param graphUris URIs of the graphs to search.\n * @param subjectUri URI or blank node id of the subject to match.\n * @param typeUri URI of the type to match.\n * @param options Optional query parameters.\n * @returns `true` if the resource has the type, `false` otherwise.\n */\n hasType(graphUris, subjectUri, typeUri, options) {\n const o = n3.DataFactory.namedNode(typeUri);\n for (let _ of this.store.match(graphUris, n3.DataFactory.namedNode(subjectUri), ontologies_1.rdf.type, o, options?.includeInferred)) {\n return true;\n }\n for (let _ of this.store.match(graphUris, n3.DataFactory.blankNode(subjectUri), ontologies_1.rdf.type, o, options?.includeInferred)) {\n return true;\n }\n return false;\n }\n /**\n * Get the language tags used in the object of triples with a given set of predicates.\n * @param graphUris URIs of the graphs to search.\n * @param predicateUris URIs of the predicates to match.\n * @returns An array of language tag statistics, sorted by the number of occurrences in descending order.\n */\n getLanguageStats(graphUris, predicateUris) {\n const stats = {};\n const predicates = predicateUris ? new Set(predicateUris) : undefined;\n for (let q of this.store.match(graphUris, null, null, null, false)) {\n if (q.object.termType === \"Literal\" && q.object.language && (!predicates || predicates.has(q.predicate.value))) {\n if (!stats[q.object.language]) {\n stats[q.object.language] = { language: q.object.language, totalCount: 1, predicates: new Set([q.predicate.value]) };\n }\n else {\n stats[q.object.language].totalCount++;\n stats[q.object.language].predicates.add(q.predicate.value);\n }\n }\n }\n return Object.values(stats).sort((a, b) => b.totalCount - a.totalCount);\n }\n /**\n * Get the most frequently used language tag in the object of triples with a given set of predicates.\n * @param graphUris URIs of the graphs to search.\n * @param predicateUris URIs of the predicates to match.\n */\n getPrimaryLanguageTag(graphUris, predicateUris) {\n const stats = this.getLanguageStats(graphUris, predicateUris);\n return stats[0] ? stats[0].language : undefined;\n }\n}\nexports.ResourceRepository = ResourceRepository;\n\n\n//# sourceURL=webpack://@faubulous/mentor-rdf/./src/rdf/resource-repository.ts?");
779
779
 
780
780
  /***/ }),
781
781
 
@@ -40,6 +40,30 @@ export interface TypedInstanceQueryOptions extends QueryOptions {
40
40
  */
41
41
  includeSubTypes?: boolean;
42
42
  }
43
+ /**
44
+ * A literal value with an optional language tag.
45
+ */
46
+ export interface LanguageTaggedLiteral {
47
+ value: string;
48
+ language: string | undefined;
49
+ }
50
+ /**
51
+ * Provides information about the usage of language tags in a document.
52
+ */
53
+ export interface LanguageTagInfo {
54
+ /**
55
+ * The language tag.
56
+ */
57
+ language: string;
58
+ /**
59
+ * The number of occurrences of the language tag in the document.
60
+ */
61
+ totalCount: number;
62
+ /**
63
+ * The predicates that have tagged values.
64
+ */
65
+ predicates: Set<string>;
66
+ }
43
67
  /**
44
68
  * A repository for retrieving resources from graphs.
45
69
  */
@@ -82,4 +106,17 @@ export declare class ResourceRepository {
82
106
  * @returns `true` if the resource has the type, `false` otherwise.
83
107
  */
84
108
  hasType(graphUris: string | string[] | undefined, subjectUri: string, typeUri: string, options?: QueryOptions): boolean;
109
+ /**
110
+ * Get the language tags used in the object of triples with a given set of predicates.
111
+ * @param graphUris URIs of the graphs to search.
112
+ * @param predicateUris URIs of the predicates to match.
113
+ * @returns An array of language tag statistics, sorted by the number of occurrences in descending order.
114
+ */
115
+ getLanguageStats(graphUris: string | string[] | undefined, predicateUris: string[] | undefined): LanguageTagInfo[];
116
+ /**
117
+ * Get the most frequently used language tag in the object of triples with a given set of predicates.
118
+ * @param graphUris URIs of the graphs to search.
119
+ * @param predicateUris URIs of the predicates to match.
120
+ */
121
+ getPrimaryLanguageTag(graphUris: string | string[] | undefined, predicateUris: string[] | undefined): string | undefined;
85
122
  }
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "ontology",
11
11
  "vocabulary"
12
12
  ],
13
- "version": "0.2.48",
13
+ "version": "0.2.49",
14
14
  "private": false,
15
15
  "author": {
16
16
  "name": "Sebastian Faubel"