From 4b67814d5e20736e09f25c0895450ce26fc17bdc Mon Sep 17 00:00:00 2001 From: Balduin Landolt <33053745+BalduinLandolt@users.noreply.github.com> Date: Sun, 22 Dec 2024 18:37:25 +0100 Subject: [PATCH] move more access into methods --- .../transformers/OntologyInferencer.scala | 2 +- .../responders/v2/OntologyResponderV2.scala | 27 ++++++++++--------- .../v2/ontology/CardinalityHandler.scala | 10 ++----- .../repo/model/OntologyCacheData.scala | 7 +++-- .../ontology/repo/service/OntologyCache.scala | 6 ++--- 5 files changed, 26 insertions(+), 26 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 196b37c295..4fd346737f 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 @@ -36,7 +36,7 @@ final case class OntologyInferencer( } } yield { // look up subclasses from ontology cache - val knownSubClasses = cache.classToSubclassLookup.get(baseClassIri.iri).getOrElse(Set(baseClassIri.iri)).toSeq + val knownSubClasses = cache.getSubClassesOf(baseClassIri.iri).getOrElse(Set(baseClassIri.iri)).toSeq // if provided, limit the child classes to those that belong to relevant ontologies val subClasses = 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 459a860b56..9df03890b5 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 @@ -262,7 +262,7 @@ final case class OntologyResponderV2( for { cacheData <- ontologyCache.getCacheData isSubClass <- ZIO - .fromOption(cacheData.classToSuperClassLookup.get(subClassIri)) + .fromOption(cacheData.getSuperClassesOf(subClassIri)) .map(_.contains(superClassIri)) .orElseFail(BadRequestException(s"Class $subClassIri not found")) } yield CheckSubClassResponseV2(isSubClass) @@ -277,7 +277,7 @@ final case class OntologyResponderV2( for { cacheData <- ontologyCache.getCacheData subClasses <- - ZIO.foreach(cacheData.classToSubclassLookup(classIri).toVector.sorted) { subClassIri => + ZIO.foreach(cacheData.getSubClassesOf(classIri).get.toVector.sorted) { subClassIri => val labelValueMaybe = cacheData .ontologies(subClassIri.getOntologyFromEntity) .classes(subClassIri) @@ -784,8 +784,9 @@ final case class OntologyResponderV2( // Check for rdfs:subClassOf cycles. allBaseClassIrisWithoutSelf: Set[SmartIri] = internalClassDef.subClassOf.flatMap { baseClassIri => - cacheData.classToSuperClassLookup - .getOrElse(baseClassIri, Set.empty[SmartIri]) + cacheData + .getSuperClassesOf(baseClassIri) + .getOrElse(Set.empty[SmartIri]) .toSet } @@ -1145,10 +1146,11 @@ final case class OntologyResponderV2( // Check that the new cardinalities are valid, and add any inherited cardinalities. allBaseClassIrisWithoutInternal = newInternalClassDef.subClassOf.toSeq.flatMap { baseClassIri => - cacheData.classToSuperClassLookup.getOrElse( - baseClassIri, - Seq.empty[SmartIri], - ) + cacheData + .getSuperClassesOf(baseClassIri) + .getOrElse( + Seq.empty[SmartIri], + ) } allBaseClassIris = internalClassIri +: allBaseClassIrisWithoutInternal @@ -1318,10 +1320,11 @@ final case class OntologyResponderV2( // Check that the new cardinalities are valid, and don't add any inherited cardinalities. newInternalClassDef = oldClassInfo.copy(directCardinalities = newClassInfo.directCardinalities) allBaseClassIrisWithoutInternal = newInternalClassDef.subClassOf.toSeq.flatMap { baseClassIri => - cacheData.classToSuperClassLookup.getOrElse( - baseClassIri, - Seq.empty[SmartIri], - ) + cacheData + .getSuperClassesOf(baseClassIri) + .getOrElse( + Seq.empty[SmartIri], + ) } allBaseClassIris = classIri +: allBaseClassIrisWithoutInternal diff --git a/webapi/src/main/scala/org/knora/webapi/responders/v2/ontology/CardinalityHandler.scala b/webapi/src/main/scala/org/knora/webapi/responders/v2/ontology/CardinalityHandler.scala index 335a99fdc6..6658980872 100644 --- a/webapi/src/main/scala/org/knora/webapi/responders/v2/ontology/CardinalityHandler.scala +++ b/webapi/src/main/scala/org/knora/webapi/responders/v2/ontology/CardinalityHandler.scala @@ -100,10 +100,7 @@ final case class CardinalityHandler( allBaseClassIrisWithoutInternal = newClassDefinitionWithRemovedCardinality.subClassOf.toSeq.flatMap { baseClassIri => - cacheData.classToSuperClassLookup.getOrElse( - baseClassIri, - Seq.empty[SmartIri], - ) + cacheData.getSuperClassesOf(baseClassIri).getOrElse(Seq.empty[SmartIri]) } allBaseClassIris = internalClassIri +: allBaseClassIrisWithoutInternal @@ -242,10 +239,7 @@ final case class CardinalityHandler( allBaseClassIrisWithoutInternal = newClassDefinitionWithRemovedCardinality.subClassOf.toSeq.flatMap { baseClassIri => - cacheData.classToSuperClassLookup.getOrElse( - baseClassIri, - Seq.empty[SmartIri], - ) + cacheData.getSuperClassesOf(baseClassIri).getOrElse(Seq.empty[SmartIri]) } allBaseClassIris = internalClassIri +: allBaseClassIrisWithoutInternal 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 94188e4b2e..09b9652e66 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 @@ -24,8 +24,8 @@ import org.knora.webapi.messages.v2.responder.ontologymessages.ReadPropertyInfoV */ case class OntologyCacheData( ontologies: Map[SmartIri, ReadOntologyV2], - classToSuperClassLookup: Map[SmartIri, Seq[SmartIri]], - classToSubclassLookup: Map[SmartIri, Set[SmartIri]], + private val classToSuperClassLookup: Map[SmartIri, Seq[SmartIri]], + private val classToSubclassLookup: Map[SmartIri, Set[SmartIri]], private val subPropertyOfRelations: Map[SmartIri, Set[SmartIri]], private val superPropertyOfRelations: Map[SmartIri, Set[SmartIri]], private val classDefinedInOntology: Map[SmartIri, SmartIri], @@ -50,6 +50,9 @@ case class OntologyCacheData( def getSubPropertiesOf(propertyIri: SmartIri): Option[Set[SmartIri]] = subPropertyOfRelations.get(propertyIri) def getSuperPropertiesOf(propertyIri: SmartIri): Option[Set[SmartIri]] = superPropertyOfRelations.get(propertyIri) + + def getSubClassesOf(classIri: SmartIri): Option[Set[SmartIri]] = classToSubclassLookup.get(classIri) + def getSuperClassesOf(classIri: SmartIri): Option[Seq[SmartIri]] = classToSuperClassLookup.get(classIri) } 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 25dcb63d2c..0344377792 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 @@ -352,9 +352,9 @@ object OntologyCache { if (OntologyConstants.Owl.ClassesThatCanBeKnoraClassConstraints.contains(constraintValueToBeChecked.toString)) { Set(constraintValueToBeChecked) } else { - cacheData.classToSuperClassLookup + cacheData + .getSuperClassesOf(constraintValueToBeChecked) .getOrElse( - constraintValueToBeChecked, errorFun( s"Property ${internalPropertyIri.toOntologySchema(errorSchema)} cannot have a ${constraintPredicateIri .toOntologySchema(errorSchema)} of " + @@ -881,7 +881,7 @@ final case class OntologyCacheLive(triplestore: TriplestoreService, cacheDataRef private def updateSubClasses(baseClassIri: SmartIri, cacheData: OntologyCacheData): OntologyCacheData = { // Get the class definitions of all the subclasses of the base class. - val allSubClassIris: Set[SmartIri] = cacheData.classToSubclassLookup(baseClassIri) + val allSubClassIris: Set[SmartIri] = cacheData.getSubClassesOf(baseClassIri).get val allSubClasses: Set[ReadClassInfoV2] = allSubClassIris.map { subClassIri => cacheData.ontologies(subClassIri.getOntologyFromEntity).classes(subClassIri)