UNPKG

@dasch-swiss/dsp-js

Version:

JavaScript library that handles API requests to Knora

138 lines 6.73 kB
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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { forkJoin, of } from "rxjs"; import { map, mergeMap } from "rxjs"; import { OntologyConversionUtil } from "../../models/v2/ontologies/OntologyConversionUtil"; import { ResourceClassDefinition } from "../../models/v2/ontologies/resource-class-definition"; import { GenericCache } from "../GenericCache"; import { ResourceClassAndPropertyDefinitions } from "./resource-class-and-property-definitions"; import { ResourceClassDefinitionWithPropertyDefinition } from "./resource-class-definition-with-property-definition"; /** * 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(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 forkJoin(deps_1).pipe(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 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.getOntologyIriFromEntityIri(resourceClassIri, this.knoraApiConfig); if (ontoIri.length !== 1) throw Error("Invalid resource class Iri " + resourceClassIri); var ontology = this.getOntology(ontoIri[0]); return ontology.pipe(map(function (ontosMap) { var _a; var mainOnto = ontosMap.get(ontoIri[0]); if (mainOnto === undefined) throw new Error("Expected ontology not found"); if (mainOnto.classes.hasOwnProperty(resourceClassIri) && mainOnto.classes[resourceClassIri] instanceof ResourceClassDefinition) { var tmpClasses = {}; var tmpProps_1 = {}; tmpClasses[resourceClassIri] = mainOnto.classes[resourceClassIri]; // filter out non Knora properties tmpClasses[resourceClassIri].propertiesList = tmpClasses[resourceClassIri].propertiesList.filter(function (hasProp) { return OntologyConversionUtil.getOntologyIriFromEntityIri(hasProp.propertyIndex, _this.knoraApiConfig).length === 1; }); tmpClasses[resourceClassIri].propertiesList.forEach(function (prop) { // prop could refer to entities in the ontology the requested resource class belongs to // or to other ontologies the resource class has prop cardinalities for, e.g. knora api or another project ontology. var fromOntoIri = OntologyConversionUtil.getOntologyIriFromEntityIri(prop.propertyIndex, _this.knoraApiConfig); if (fromOntoIri.length === 1) { var fromOnto = ontosMap.get(fromOntoIri[0]); if (fromOnto === undefined) throw new Error("Expected ontology not found"); tmpProps_1[prop.propertyIndex] = fromOnto.properties[prop.propertyIndex]; } }); return new ResourceClassAndPropertyDefinitions((_a = {}, _a[resourceClassIri] = new ResourceClassDefinitionWithPropertyDefinition(tmpClasses[resourceClassIri], tmpProps_1), _a), tmpProps_1); } else { // resource class not found // TODO: Should an error be thrown? return new ResourceClassAndPropertyDefinitions({}, {}); } })); }; /** * Public method to access the reloadItem method * * @param key the id of the information to be returned. * @return the item */ OntologyCache.prototype.reloadCachedItem = function (key) { return this.reloadItem(key); }; OntologyCache.prototype.requestItemFromKnora = function (key, isDependency) { return this.v2Endpoint.onto.getOntology(key).pipe(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)); export { OntologyCache }; //# sourceMappingURL=OntologyCache.js.map