UNPKG

@knora/api

Version:

JavaScript library that handles API requests to Knora

187 lines 10.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Constants_1 = require("../Constants"); var read_ontology_1 = require("./read-ontology"); var resource_class_definition_1 = require("./resource-class-definition"); var resource_property_definition_1 = require("./resource-property-definition"); var standoff_class_definition_1 = require("./standoff-class-definition"); var system_property_definition_1 = require("./system-property-definition"); var OntologyConversionUtil; (function (OntologyConversionUtil) { /** * Given a Knora entity IRI, gets the ontology Iri. * External entity Iris are ignored. * * @param entityIri an entity Iri. * @param knoraApiConfig the Knora api configuration. * @return the ontology IRI as the only entry in an array, otherwise an empty array. */ OntologyConversionUtil.getOntologyIriFromEntityIri = function (entityIri, knoraApiConfig) { var ontologyIri = []; var projectEntityBase = "http://" + knoraApiConfig.apiHost; if (knoraApiConfig.apiPort !== null) { projectEntityBase = projectEntityBase + ":" + knoraApiConfig.apiPort; } projectEntityBase = projectEntityBase + "/ontology/"; // Check if the given entity Iri belongs to knora-api or a project ontology. // Ignore external entity Iris. if (entityIri.indexOf(Constants_1.Constants.KnoraApiV2) === 0) { ontologyIri.push(Constants_1.Constants.KnoraApiV2); } else if (entityIri.indexOf(projectEntityBase) === 0) { // split entity Iri on "#" var segments = entityIri.split(Constants_1.Constants.Delimiter); if (segments.length === 2) { // First segment identifies the project ontology the entity belongs to. ontologyIri.push(segments[0]); } else { console.error("Error: " + entityIri + " is not a valid Knora entity IRI."); } } return ontologyIri; }; /** * Determines resource class definitions when passed to filter() applied to an array of entity definitions. * * @param entity the entity definition to be analyzed. */ OntologyConversionUtil.filterResourceClassDefinitions = function (entity) { return entity.hasOwnProperty(Constants_1.Constants.IsResourceClass) && entity[Constants_1.Constants.IsResourceClass] === true; }; /** * Determines standoff class definitions when passed to filter() applied to an array of entity definitions. * * @param entity the entity definition to be analyzed. */ OntologyConversionUtil.filterStandoffClassDefinitions = function (entity) { return entity.hasOwnProperty(Constants_1.Constants.IsStandoffClass) && entity[Constants_1.Constants.IsStandoffClass] === true; }; /** * Determines resource property definitions when passed to filter() applied to an array of entity definitions. * * @param entity the entity definition to be analyzed. */ OntologyConversionUtil.filterResourcePropertyDefinitions = function (entity) { return entity.hasOwnProperty(Constants_1.Constants.IsResourceProperty) && entity[Constants_1.Constants.IsResourceProperty] === true; }; /** * Determines system property definitions when passed to filter() applied to an array of entity definitions. * * @param entity the entity definition to be analyzed. */ OntologyConversionUtil.filterSystemPropertyDefintions = function (entity) { return (entity["@type"] === Constants_1.Constants.DataTypeProperty || entity["@type"] === Constants_1.Constants.ObjectProperty) && !entity.hasOwnProperty(Constants_1.Constants.IsResourceProperty); }; /** * Converts an entity definition to the specified type. * * @param entity the entity definition to be converted. * @param dataType the target type of the conversion. * @param jsonConvert the converter to be used. */ OntologyConversionUtil.convertEntity = function (entity, dataType, jsonConvert) { return jsonConvert.deserializeObject(entity, dataType); }; /** * Given an array of entities, converts and adds them to the given ontology. * * @param ontology the ontology to which the definitions should be added. * @param entities the entities to be converted and added. * @param jsonConvert instance of JsonConvert to be used. */ var convertAndAddEntityDefinitions = function (ontology, entities, jsonConvert) { // Convert resource classes entities.filter(OntologyConversionUtil.filterResourceClassDefinitions).map(function (resclassJsonld) { return OntologyConversionUtil.convertEntity(resclassJsonld, resource_class_definition_1.ResourceClassDefinition, jsonConvert); }).forEach(function (resClass) { ontology.classes[resClass.id] = resClass; }); // Convert standoff classes entities.filter(OntologyConversionUtil.filterStandoffClassDefinitions).map(function (standoffclassJsonld) { return OntologyConversionUtil.convertEntity(standoffclassJsonld, standoff_class_definition_1.StandoffClassDefinition, jsonConvert); }).forEach(function (standoffClass) { ontology.classes[standoffClass.id] = standoffClass; }); // Convert resource properties (properties pointing to Knora values) entities.filter(OntologyConversionUtil.filterResourcePropertyDefinitions).map(function (propertyJsonld) { return OntologyConversionUtil.convertEntity(propertyJsonld, resource_property_definition_1.ResourcePropertyDefinition, jsonConvert); }).forEach(function (prop) { ontology.properties[prop.id] = prop; }); // Convert system properties (properties not pointing to Knora values) entities.filter(OntologyConversionUtil.filterSystemPropertyDefintions).map(function (propertyJsonld) { return OntologyConversionUtil.convertEntity(propertyJsonld, system_property_definition_1.SystemPropertyDefinition, jsonConvert); }).forEach(function (prop) { ontology.properties[prop.id] = prop; }); }; /** * Given an ontology, analyzes its direct dependencies and adds them to the given ontology. * * @param ontology the ontology whose direct dependencies should be analyzed. * @param knoraApiConfig the Knora API config to be used. */ var analyzeDirectDependencies = function (ontology, knoraApiConfig) { // Ontologies referenced by this ontology var referencedOntologies = new Set([]); // Collect ontologies referenced by this ontology in resource classes: // references to properties (cardinalities) and resource classes (super classes) for (var index in ontology.classes) { if (ontology.classes.hasOwnProperty(index)) { ontology.classes[index].propertiesList.forEach(function (prop) { OntologyConversionUtil.getOntologyIriFromEntityIri(prop.propertyIndex, knoraApiConfig) .forEach(function (ontoIri) { return referencedOntologies.add(ontoIri); }); }); ontology.classes[index].subClassOf.forEach(function (superClass) { OntologyConversionUtil.getOntologyIriFromEntityIri(superClass, knoraApiConfig) .forEach(function (ontoIri) { return referencedOntologies.add(ontoIri); }); }); } } // Collect ontologies referenced by this ontology in properties: // references to other properties (super properties) and resource classes (subject and object types) for (var index in ontology.properties) { if (ontology.properties.hasOwnProperty(index)) { if (ontology.properties[index].objectType !== undefined) { OntologyConversionUtil.getOntologyIriFromEntityIri(ontology.properties[index].objectType, knoraApiConfig) .forEach(function (ontoIri) { return referencedOntologies.add(ontoIri); }); } if (ontology.properties[index].subjectType !== undefined) { OntologyConversionUtil.getOntologyIriFromEntityIri(ontology.properties[index].subjectType, knoraApiConfig) .forEach(function (ontoIri) { return referencedOntologies.add(ontoIri); }); } ontology.properties[index].subPropertyOf.forEach(function (superProperty) { OntologyConversionUtil.getOntologyIriFromEntityIri(superProperty, knoraApiConfig) .forEach(function (ontoIri) { return referencedOntologies.add(ontoIri); }); }); } } // Remove this ontology from the collection referencedOntologies.delete(ontology.id); ontology.dependsOnOntologies = referencedOntologies; }; /** * Converts an ontology serialized as JSON-LD to an instance of `ReadOntology`. * * @param ontologyJsonld ontology as JSON-LD already processed by the jsonld-processor. * @param jsonConvert * @param knoraApiConfig * @return the ontology as a `ReadOntology`. */ OntologyConversionUtil.convertOntology = function (ontologyJsonld, jsonConvert, knoraApiConfig) { var ontology = jsonConvert.deserializeObject(ontologyJsonld, read_ontology_1.ReadOntology); // Access the collection of entities var entities = ontologyJsonld["@graph"]; if (!Array.isArray(entities)) throw new Error("An ontology is expected to have a member '@graph' containing an array of entities"); convertAndAddEntityDefinitions(ontology, entities, jsonConvert); analyzeDirectDependencies(ontology, knoraApiConfig); return ontology; }; })(OntologyConversionUtil = exports.OntologyConversionUtil || (exports.OntologyConversionUtil = {})); //# sourceMappingURL=OntologyConversionUtil.js.map