From 3e452b3a998e9f4d169c99bc69b479fc72135ab8 Mon Sep 17 00:00:00 2001 From: Balduin Landolt <33053745+BalduinLandolt@users.noreply.github.com> Date: Sun, 22 Dec 2024 18:29:10 +0100 Subject: [PATCH] move more access into functions --- .../transformers/OntologyInferencer.scala | 2 +- .../responders/v2/OntologyResponderV2.scala | 9 ++++---- .../v2/ontology/OntologyHelpers.scala | 22 +++++++++---------- .../repo/model/OntologyCacheData.scala | 7 ++++-- .../ontology/repo/service/OntologyCache.scala | 6 ++--- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/webapi/src/main/scala/org/knora/webapi/messages/util/search/gravsearch/transformers/OntologyInferencer.scala b/webapi/src/main/scala/org/knora/webapi/messages/util/search/gravsearch/transformers/OntologyInferencer.scala index 0b69c06acf..196b37c295 100644 --- a/webapi/src/main/scala/org/knora/webapi/messages/util/search/gravsearch/transformers/OntologyInferencer.scala +++ b/webapi/src/main/scala/org/knora/webapi/messages/util/search/gravsearch/transformers/OntologyInferencer.scala @@ -63,7 +63,7 @@ final case class OntologyInferencer( queryVariableSuffix: Option[String], ): Seq[QueryPattern] = { // look up subproperties from ontology cache - val knownSubProps = cache.superPropertyOfRelations.get(predIri).getOrElse(Set(predIri)).toSeq + val knownSubProps = cache.getSubPropertiesOf(predIri).getOrElse(Set(predIri)).toSeq // if provided, limit the child properties to those that belong to relevant ontologies val subProps = limitInferenceToOntologies match { diff --git a/webapi/src/main/scala/org/knora/webapi/responders/v2/OntologyResponderV2.scala b/webapi/src/main/scala/org/knora/webapi/responders/v2/OntologyResponderV2.scala index 650b949b5f..459a860b56 100644 --- a/webapi/src/main/scala/org/knora/webapi/responders/v2/OntologyResponderV2.scala +++ b/webapi/src/main/scala/org/knora/webapi/responders/v2/OntologyResponderV2.scala @@ -1974,10 +1974,11 @@ final case class OntologyResponderV2( // Check for rdfs:subPropertyOf cycles. allKnoraSuperPropertyIrisWithoutSelf: Set[SmartIri] = knoraSuperProperties.flatMap { superPropertyIri => - cacheData.subPropertyOfRelations.getOrElse( - superPropertyIri, - Set.empty[SmartIri], - ) + cacheData + .getSuperPropertiesOf(superPropertyIri) + .getOrElse( + Set.empty[SmartIri], + ) } _ <- ZIO.when(allKnoraSuperPropertyIrisWithoutSelf.contains(internalPropertyIri)) { diff --git a/webapi/src/main/scala/org/knora/webapi/responders/v2/ontology/OntologyHelpers.scala b/webapi/src/main/scala/org/knora/webapi/responders/v2/ontology/OntologyHelpers.scala index b113491121..20594995d5 100644 --- a/webapi/src/main/scala/org/knora/webapi/responders/v2/ontology/OntologyHelpers.scala +++ b/webapi/src/main/scala/org/knora/webapi/responders/v2/ontology/OntologyHelpers.scala @@ -176,7 +176,7 @@ object OntologyHelpers { directClassCardinalities: Map[SmartIri, Map[SmartIri, KnoraCardinalityInfo]], classCardinalitiesWithInheritance: Map[SmartIri, Map[SmartIri, KnoraCardinalityInfo]], allSubClassOfRelations: Map[SmartIri, Seq[SmartIri]], - allSubPropertyOfRelations: Map[SmartIri, Set[SmartIri]], + superPropertyLookup: SmartIri => Option[Set[SmartIri]], allPropertyDefs: Map[SmartIri, PropertyInfoContentV2], allKnoraResourceProps: Set[SmartIri], allLinkProps: Set[SmartIri], @@ -266,7 +266,7 @@ object OntologyHelpers { val maybePropertyAndSubproperty: Option[(SmartIri, SmartIri)] = OntologyHelpers.findPropertyAndSubproperty( propertyIris = allPropertyIrisForCardinalitiesInClass, - subPropertyOfRelations = allSubPropertyOfRelations, + superPropertyLookup = superPropertyLookup, ) maybePropertyAndSubproperty match { @@ -875,11 +875,11 @@ object OntologyHelpers { */ private def findPropertyAndSubproperty( propertyIris: Set[SmartIri], - subPropertyOfRelations: Map[SmartIri, Set[SmartIri]], + superPropertyLookup: SmartIri => Option[Set[SmartIri]], ): Option[(SmartIri, SmartIri)] = propertyIris.flatMap { propertyIri => val maybeBasePropertyIri: Option[SmartIri] = (propertyIris - propertyIri).find { otherPropertyIri => - subPropertyOfRelations.get(propertyIri).exists { (baseProperties: Set[SmartIri]) => + superPropertyLookup(propertyIri).exists { (baseProperties: Set[SmartIri]) => baseProperties.contains(otherPropertyIri) } } @@ -1038,7 +1038,7 @@ object OntologyHelpers { classIri = internalClassDef.classIri, thisClassCardinalities = classDefWithAddedLinkValueProps.directCardinalities, inheritableCardinalities = cardinalitiesAvailableToInherit, - allSubPropertyOfRelations = cacheData.subPropertyOfRelations, + superPropertyLookup = cacheData.getSuperPropertiesOf, errorSchema = ApiV2Complex, errorFun = { msg => throw BadRequestException(msg) }, ), @@ -1079,7 +1079,7 @@ object OntologyHelpers { _ <- findPropertyAndSubproperty( propertyIris = cardinalitiesForClassWithInheritance.keySet, - subPropertyOfRelations = cacheData.subPropertyOfRelations, + superPropertyLookup = cacheData.getSuperPropertiesOf, ) match { case Some((basePropertyIri, propertyIri)) => Validation.fail( @@ -1158,7 +1158,7 @@ object OntologyHelpers { classIri: SmartIri, thisClassCardinalities: Map[SmartIri, KnoraCardinalityInfo], inheritableCardinalities: Map[SmartIri, KnoraCardinalityInfo], - allSubPropertyOfRelations: Map[SmartIri, Set[SmartIri]], + superPropertyLookup: SmartIri => Option[Set[SmartIri]], errorSchema: OntologySchema, errorFun: String => Nothing, ): Map[SmartIri, KnoraCardinalityInfo] = { @@ -1169,7 +1169,7 @@ object OntologyHelpers { // If the class has a cardinality for a non-Knora property like rdfs:label (which can happen only // if it's a built-in class), we won't have any information about the base properties of that property. val basePropsOfThisClassProp: Set[SmartIri] = - allSubPropertyOfRelations.getOrElse(thisClassProp, Set.empty[SmartIri]) + superPropertyLookup(thisClassProp).getOrElse(Set.empty[SmartIri]) val overriddenBaseProps: Set[SmartIri] = inheritableCardinalities.foldLeft(Set.empty[SmartIri]) { case (acc, (baseClassProp, baseClassCardinality)) => @@ -1377,7 +1377,7 @@ object OntologyHelpers { def inheritCardinalitiesInLoadedClass( classIri: SmartIri, directSubClassOfRelations: Map[SmartIri, Set[SmartIri]], - allSubPropertyOfRelations: Map[SmartIri, Set[SmartIri]], + superPropertyLookup: SmartIri => Option[Set[SmartIri]], directClassCardinalities: Map[SmartIri, Map[SmartIri, KnoraCardinalityInfo]], ): Map[SmartIri, KnoraCardinalityInfo] = { // Recursively get properties that are available to inherit from base classes. If we have no information about @@ -1390,7 +1390,7 @@ object OntologyHelpers { acc ++ inheritCardinalitiesInLoadedClass( classIri = baseClass, directSubClassOfRelations = directSubClassOfRelations, - allSubPropertyOfRelations = allSubPropertyOfRelations, + superPropertyLookup = superPropertyLookup, directClassCardinalities = directClassCardinalities, ) } @@ -1405,7 +1405,7 @@ object OntologyHelpers { classIri = classIri, thisClassCardinalities = thisClassCardinalities, inheritableCardinalities = cardinalitiesAvailableToInherit, - allSubPropertyOfRelations = allSubPropertyOfRelations, + superPropertyLookup = superPropertyLookup, errorSchema = InternalSchema, (msg: String) => throw InconsistentRepositoryDataException(msg), ) diff --git a/webapi/src/main/scala/org/knora/webapi/slice/ontology/repo/model/OntologyCacheData.scala b/webapi/src/main/scala/org/knora/webapi/slice/ontology/repo/model/OntologyCacheData.scala index 5257e47ea4..94188e4b2e 100644 --- a/webapi/src/main/scala/org/knora/webapi/slice/ontology/repo/model/OntologyCacheData.scala +++ b/webapi/src/main/scala/org/knora/webapi/slice/ontology/repo/model/OntologyCacheData.scala @@ -26,8 +26,8 @@ case class OntologyCacheData( ontologies: Map[SmartIri, ReadOntologyV2], classToSuperClassLookup: Map[SmartIri, Seq[SmartIri]], classToSubclassLookup: Map[SmartIri, Set[SmartIri]], - subPropertyOfRelations: Map[SmartIri, Set[SmartIri]], - superPropertyOfRelations: Map[SmartIri, Set[SmartIri]], + private val subPropertyOfRelations: Map[SmartIri, Set[SmartIri]], + private val superPropertyOfRelations: Map[SmartIri, Set[SmartIri]], private val classDefinedInOntology: Map[SmartIri, SmartIri], private val propertyDefinedInOntology: Map[SmartIri, SmartIri], private val entityDefinedInOntology: Map[SmartIri, SmartIri], @@ -47,6 +47,9 @@ case class OntologyCacheData( def entityDefinedInOntology(propertyIri: SmartIri): Option[SmartIri] = entityDefinedInOntology.get(propertyIri) def classDefinedInOntology(classIri: SmartIri): Option[SmartIri] = classDefinedInOntology.get(classIri) def propertyDefinedInOntology(propertyIri: SmartIri): Option[SmartIri] = propertyDefinedInOntology.get(propertyIri) + + def getSubPropertiesOf(propertyIri: SmartIri): Option[Set[SmartIri]] = subPropertyOfRelations.get(propertyIri) + def getSuperPropertiesOf(propertyIri: SmartIri): Option[Set[SmartIri]] = superPropertyOfRelations.get(propertyIri) } object OntologyCacheData { val Empty = OntologyCacheData( diff --git a/webapi/src/main/scala/org/knora/webapi/slice/ontology/repo/service/OntologyCache.scala b/webapi/src/main/scala/org/knora/webapi/slice/ontology/repo/service/OntologyCache.scala index 68f1b7b2fd..25dcb63d2c 100644 --- a/webapi/src/main/scala/org/knora/webapi/slice/ontology/repo/service/OntologyCache.scala +++ b/webapi/src/main/scala/org/knora/webapi/slice/ontology/repo/service/OntologyCache.scala @@ -679,7 +679,7 @@ final case class OntologyCacheLive(triplestore: TriplestoreService, cacheDataRef OntologyHelpers.inheritCardinalitiesInLoadedClass( classIri = resourceClassIri, directSubClassOfRelations = directSubClassOfRelations, - allSubPropertyOfRelations = allSubPropertyOfRelations, + superPropertyLookup = allSubPropertyOfRelations.get, directClassCardinalities = directClassCardinalities, ) @@ -692,7 +692,7 @@ final case class OntologyCacheLive(triplestore: TriplestoreService, cacheDataRef directClassCardinalities = directClassCardinalities, classCardinalitiesWithInheritance = classCardinalitiesWithInheritance, allSubClassOfRelations = allSubClassOfRelations, - allSubPropertyOfRelations = allSubPropertyOfRelations, + superPropertyLookup = allSubPropertyOfRelations.get, allPropertyDefs = allPropertyDefs, allKnoraResourceProps = allKnoraResourceProps, allLinkProps = allLinkProps, @@ -914,7 +914,7 @@ final case class OntologyCacheLive(triplestore: TriplestoreService, cacheDataRef classIri = directSubClassIri, thisClassCardinalities = directSubClass.entityInfoContent.directCardinalities, inheritableCardinalities = inheritableCardinalities, - allSubPropertyOfRelations = cacheData.subPropertyOfRelations, + superPropertyLookup = cacheData.getSuperPropertiesOf, errorSchema = ApiV2Complex, errorFun = { (msg: String) => throw BadRequestException(msg)