UNPKG

@knora/api

Version:

JavaScript library that handles API requests to Knora

117 lines 5.29 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var OntologyConversionUtil_1 = require("../models/v2/ontologies/OntologyConversionUtil"); var GenericCache_1 = require("./GenericCache"); /** * Caches ontologies obtained from Knora and handles direct dependencies between ontologies. */ var OntologyCache = /** @class */ (function (_super) { __extends(OntologyCache, _super); function OntologyCache(knoraApiConfig, v2Endpoint) { var _this = _super.call(this) || this; _this.knoraApiConfig = knoraApiConfig; _this.v2Endpoint = v2Endpoint; return _this; } /** * Gets an ontology from the cache including its direct dependencies. * * The ontology Iris are the keys of the resulting Map. * * @param ontologyIri the Iri of the ontology. * @returns the requested ontology and its direct dependencies. */ OntologyCache.prototype.getOntology = function (ontologyIri) { var _this = this; return this.getItem(ontologyIri).pipe(operators_1.mergeMap(function (ontology) { if (ontology.dependsOnOntologies.size > 0) { // get dependencies var deps_1 = []; ontology.dependsOnOntologies.forEach(function (depKey) { deps_1.push(_this.getItem(depKey)); }); // return when all dependencies have been resolved return rxjs_1.forkJoin(deps_1).pipe(operators_1.map(function (ontos) { var ontoMap = new Map(); // combine ontology and dependencies [ontology].concat(ontos).forEach(function (onto) { ontoMap.set(onto.id, onto); }); return ontoMap; })); } else { // no dependencies var ontoMap = new Map(); ontoMap.set(ontology.id, ontology); return rxjs_1.of(ontoMap); } })); }; /** * Gets a resource class definition including the property definitions * the resource class has cardinalities for. * * This method does not return third party ontology entities, e.g., rdfs. * * @param resourceClassIri */ OntologyCache.prototype.getResourceClassDefinition = function (resourceClassIri) { var _this = this; var ontoIri = OntologyConversionUtil_1.OntologyConversionUtil.getOntologyIriFromEntityIri(resourceClassIri, this.knoraApiConfig); if (ontoIri.length !== 1) throw Error("Invalid resource class Iri " + resourceClassIri); var ontology = this.getOntology(ontoIri[0]); return ontology.pipe(operators_1.map(function (ontosMap) { var requestedEntityDefs = { classes: {}, properties: {} }; var mainOnto = ontosMap.get(ontoIri[0]); if (mainOnto === undefined) throw new Error("Expected ontology not found"); if (mainOnto.classes.hasOwnProperty(resourceClassIri)) { requestedEntityDefs.classes[resourceClassIri] = mainOnto.classes[resourceClassIri]; mainOnto.classes[resourceClassIri].propertiesList.forEach(function (prop) { var fromOntoIri = OntologyConversionUtil_1.OntologyConversionUtil.getOntologyIriFromEntityIri(prop.propertyIndex, _this.knoraApiConfig); // only handle Knora property definitions if (fromOntoIri.length === 1) { var fromOnto = ontosMap.get(fromOntoIri[0]); if (fromOnto === undefined) throw new Error("Expected ontology not found"); requestedEntityDefs.properties[prop.propertyIndex] = fromOnto.properties[prop.propertyIndex]; } }); } return requestedEntityDefs; })); }; OntologyCache.prototype.requestItemFromKnora = function (key, isDependency) { return this.v2Endpoint.onto.getOntology(key).pipe(operators_1.map(function (onto) { return [onto]; })); }; OntologyCache.prototype.getKeyOfItem = function (item) { return item.id; }; OntologyCache.prototype.getDependenciesOfItem = function (item) { return Array.from(item.dependsOnOntologies); }; return OntologyCache; }(GenericCache_1.GenericCache)); exports.OntologyCache = OntologyCache; //# sourceMappingURL=OntologyCache.js.map