Skip to content

Commit

Permalink
move more access into methods
Browse files Browse the repository at this point in the history
  • Loading branch information
BalduinLandolt committed Dec 22, 2024
1 parent 3e452b3 commit 4b67814
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 " +
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4b67814

Please sign in to comment.