Skip to content

Commit

Permalink
move more access into functions
Browse files Browse the repository at this point in the history
  • Loading branch information
BalduinLandolt committed Dec 22, 2024
1 parent 70e3ea9 commit 3e452b3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -266,7 +266,7 @@ object OntologyHelpers {

val maybePropertyAndSubproperty: Option[(SmartIri, SmartIri)] = OntologyHelpers.findPropertyAndSubproperty(
propertyIris = allPropertyIrisForCardinalitiesInClass,
subPropertyOfRelations = allSubPropertyOfRelations,
superPropertyLookup = superPropertyLookup,
)

maybePropertyAndSubproperty match {
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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) },
),
Expand Down Expand Up @@ -1079,7 +1079,7 @@ object OntologyHelpers {

_ <- findPropertyAndSubproperty(
propertyIris = cardinalitiesForClassWithInheritance.keySet,
subPropertyOfRelations = cacheData.subPropertyOfRelations,
superPropertyLookup = cacheData.getSuperPropertiesOf,
) match {
case Some((basePropertyIri, propertyIri)) =>
Validation.fail(
Expand Down Expand Up @@ -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] = {
Expand All @@ -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)) =>
Expand Down Expand Up @@ -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
Expand All @@ -1390,7 +1390,7 @@ object OntologyHelpers {
acc ++ inheritCardinalitiesInLoadedClass(
classIri = baseClass,
directSubClassOfRelations = directSubClassOfRelations,
allSubPropertyOfRelations = allSubPropertyOfRelations,
superPropertyLookup = superPropertyLookup,
directClassCardinalities = directClassCardinalities,
)
}
Expand All @@ -1405,7 +1405,7 @@ object OntologyHelpers {
classIri = classIri,
thisClassCardinalities = thisClassCardinalities,
inheritableCardinalities = cardinalitiesAvailableToInherit,
allSubPropertyOfRelations = allSubPropertyOfRelations,
superPropertyLookup = superPropertyLookup,
errorSchema = InternalSchema,
(msg: String) => throw InconsistentRepositoryDataException(msg),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ final case class OntologyCacheLive(triplestore: TriplestoreService, cacheDataRef
OntologyHelpers.inheritCardinalitiesInLoadedClass(
classIri = resourceClassIri,
directSubClassOfRelations = directSubClassOfRelations,
allSubPropertyOfRelations = allSubPropertyOfRelations,
superPropertyLookup = allSubPropertyOfRelations.get,
directClassCardinalities = directClassCardinalities,
)

Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3e452b3

Please sign in to comment.