Skip to content

Commit

Permalink
Stablize location and connection consistency handling
Browse files Browse the repository at this point in the history
  • Loading branch information
DivineThreepwood committed Jan 8, 2024
1 parent 5f0a5d9 commit a868456
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 199 deletions.
2 changes: 1 addition & 1 deletion lib/jul

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.openbase.bco.registry.unit.core.consistency.connectionconfig

import org.openbase.jul.exception.CouldNotPerformException
import org.openbase.jul.exception.printer.ExceptionPrinter
import org.openbase.jul.extension.protobuf.IdentifiableMessage
import org.openbase.jul.extension.protobuf.container.ProtoBufMessageMap
import org.openbase.jul.storage.registry.AbstractProtoBufRegistryConsistencyHandler
import org.openbase.jul.storage.registry.EntryModification
import org.openbase.jul.storage.registry.ProtoBufFileSynchronizedRegistry
import org.openbase.jul.storage.registry.ProtoBufRegistry
import org.openbase.type.domotic.registry.UnitRegistryDataType.UnitRegistryData
import org.openbase.type.domotic.unit.UnitConfigType.UnitConfig

/**
*
* @author [Tamino Huxohl](mailto:[email protected])
*/
class ConnectionLocationConsistencyHandler(
private val locationRegistry: ProtoBufFileSynchronizedRegistry<String, UnitConfig, UnitConfig.Builder, UnitRegistryData.Builder>,

Check warning

Code scanning / detekt

Line detected that is longer than the defined maximum line length in the code style. Warning

Line detected that is longer than the defined maximum line length in the code style.
) :
AbstractProtoBufRegistryConsistencyHandler<String, UnitConfig, UnitConfig.Builder>() {
@Throws(CouldNotPerformException::class, EntryModification::class)
override fun processData(
id: String,
entry: IdentifiableMessage<String, UnitConfig, UnitConfig.Builder>,
entryMap: ProtoBufMessageMap<String, UnitConfig, UnitConfig.Builder>,
registry: ProtoBufRegistry<String, UnitConfig, UnitConfig.Builder>,
) {
val connectionUnitConfig = entry.message.toBuilder()

val locationId: String? = try {
getLowestCommonParentLocation(connectionUnitConfig.connectionConfig.tileIdList, locationRegistry)?.id
} catch (ex: CouldNotPerformException) {
ExceptionPrinter.printHistory(
"Could not find parent location for connection [$connectionUnitConfig]",
ex,
logger
)
null
} ?: locationRegistry.messages.firstOrNull { it.locationConfig.root }?.id

locationId?.let {
if (locationId != connectionUnitConfig.placementConfig.locationId) {
val placement = connectionUnitConfig.placementConfig.toBuilder().setLocationId(locationId)
throw EntryModification(
entry.setMessage(connectionUnitConfig.setPlacementConfig(placement), this),
this
)
}
}
}

companion object {
fun getLowestCommonParentLocation(

Check warning

Code scanning / detekt

Restrict the number of return statements in methods. Warning

Function getLowestCommonParentLocation has 3 return statements which exceeds the limit of 2.
locationIds: List<String>,
locationUnitConfigRegistry: ProtoBufFileSynchronizedRegistry<String, UnitConfig, UnitConfig.Builder, UnitRegistryData.Builder>,

Check warning

Code scanning / detekt

Line detected that is longer than the defined maximum line length in the code style. Warning

Line detected that is longer than the defined maximum line length in the code style.
): UnitConfig? {
// list containing the paths from root to each location given by locationIds sorted by the lenght of the path, e.g.:

Check warning

Code scanning / detekt

Line detected that is longer than the defined maximum line length in the code style. Warning

Line detected that is longer than the defined maximum line length in the code style.
// home, apartment, hallway, entrance
// home, apartment, outdoor
val pathsFromRootMap: MutableList<List<UnitConfig>> = ArrayList()

// fill the list according to the description above
locationIds.forEach { id ->
var locationUnitConfig = locationUnitConfigRegistry.getMessage(id)
val pathFromRootList: MutableList<UnitConfig> = ArrayList()
pathFromRootList.add(locationUnitConfig)
while (!locationUnitConfig.locationConfig.root) {
locationUnitConfig =
locationUnitConfigRegistry.getMessage(locationUnitConfig.placementConfig.locationId)
// when adding a location at the front of the list, every entry is moved an index further
pathFromRootList.add(0, locationUnitConfig)
}
pathsFromRootMap.add(pathFromRootList)
}

// sort the list after their sizes:
// home, apartment, outdoor
// home, apartment, hallway, entrance
pathsFromRootMap.sortWith { o1: List<UnitConfig>, o2: List<UnitConfig> -> o2.size - o1.size }

// find the lowest common parent, e.g. for the example above apartment
// by returning the index before the first elements where the paths differ

// return null in case connection is not linked to any locations
if (pathsFromRootMap.isEmpty()) {
return null;
}

val shortestPath = pathsFromRootMap[0].size
(0 until shortestPath).forEach { i ->
val currentId = pathsFromRootMap[0][i].id
(1 until pathsFromRootMap.size).forEach { j ->
if (pathsFromRootMap[j][i].id != currentId) {
return pathsFromRootMap[0][i - 1]
}
}
}

// checking if a lowest common parent exists should not be necessary since a tile cannot be root
return pathsFromRootMap[0][0]
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.openbase.bco.registry.unit.core.consistency.connectionconfig

import org.openbase.jul.exception.CouldNotPerformException
import org.openbase.jul.extension.protobuf.IdentifiableMessage
import org.openbase.jul.extension.protobuf.container.ProtoBufMessageMap
import org.openbase.jul.storage.registry.AbstractProtoBufRegistryConsistencyHandler
import org.openbase.jul.storage.registry.EntryModification
import org.openbase.jul.storage.registry.ProtoBufFileSynchronizedRegistry
import org.openbase.jul.storage.registry.ProtoBufRegistry
import org.openbase.type.domotic.registry.UnitRegistryDataType
import org.openbase.type.domotic.unit.UnitConfigType
import org.openbase.type.domotic.unit.location.LocationConfigType.LocationConfig.LocationType

/**
*
* @author [Tamino Huxohl](mailto:[email protected])
*/
class ConnectionTilesConsistencyHandler(private val locationRegistry: ProtoBufFileSynchronizedRegistry<String, UnitConfigType.UnitConfig, UnitConfigType.UnitConfig.Builder, UnitRegistryDataType.UnitRegistryData.Builder>) :

Check warning

Code scanning / detekt

Line detected that is longer than the defined maximum line length in the code style. Warning

Line detected that is longer than the defined maximum line length in the code style.
AbstractProtoBufRegistryConsistencyHandler<String, UnitConfigType.UnitConfig, UnitConfigType.UnitConfig.Builder>() {
@Throws(CouldNotPerformException::class, EntryModification::class)
override fun processData(
id: String,
entry: IdentifiableMessage<String, UnitConfigType.UnitConfig, UnitConfigType.UnitConfig.Builder>,
entryMap: ProtoBufMessageMap<String, UnitConfigType.UnitConfig, UnitConfigType.UnitConfig.Builder>,
registry: ProtoBufRegistry<String, UnitConfigType.UnitConfig, UnitConfigType.UnitConfig.Builder>,
) {
val connectionUnitConfig = entry.message.toBuilder()
val connectionConfig = connectionUnitConfig.connectionConfigBuilder

// remove duplicated entries and location ids that are not tiles
entry.message.connectionConfig.tileIdList
.distinct()
.filter { tileId -> locationRegistry[tileId].message?.locationConfig?.locationType == LocationType.TILE }
.let { tileIds ->
if (connectionConfig.tileIdList.toList().sorted() != tileIds.sorted()) {
connectionConfig.clearTileId()
connectionConfig.addAllTileId(tileIds)
throw EntryModification(entry.setMessage(connectionUnitConfig, this), this)
}
}
}
}

0 comments on commit a868456

Please sign in to comment.