add a workaround for root duplicating names

This commit is contained in:
Alexander Nozik 2023-08-22 21:47:07 +03:00
parent f5fba4747e
commit 1e29c5dbaa
5 changed files with 27 additions and 19 deletions

View File

@ -4,6 +4,7 @@ import space.kscience.dataforge.meta.*
import space.kscience.dataforge.names.Name
import space.kscience.dataforge.names.parseAsName
import space.kscience.dataforge.names.plus
import space.kscience.dataforge.names.withIndex
import space.kscience.visionforge.MutableVisionContainer
import space.kscience.visionforge.isEmpty
import space.kscience.visionforge.set
@ -223,7 +224,7 @@ private fun SolidGroup.addShape(
"TGeoShapeAssembly" -> {
val fVolume by shape.dObject(::DGeoVolume)
fVolume?.let { volume ->
addRootVolume(volume, context, block = block)
addRootVolume(volume, context, name = volume.fName.ifEmpty { null }, block = block)
}
}
@ -348,29 +349,29 @@ private fun SolidGroup.addRootVolume(
cache: Boolean = true,
block: Solid.() -> Unit = {},
) {
val combinedName = if (volume.fName.isEmpty()) {
name
} else if (name == null) {
volume.fName
val combinedName = name?.parseAsName()?.let {
// this fix is required to work around malformed root files with duplicated node names
if (get(it) != null) {
it.withIndex(volume.hashCode().toString(16))
} else {
"${name}_${volume.fName}"
it
}
}
if (!cache) {
val group = buildVolume(volume, context)?.apply(block)
setChild(combinedName?.let { Name.parse(it) }, group)
val group = buildVolume(volume, context)?.apply(block) ?: return
setChild(combinedName, group)
} else {
val templateName = volumesName + volume.name
val existing = getPrototype(templateName)
val existing = context.prototypeHolder.getPrototype(templateName)
if (existing == null) {
context.prototypeHolder.prototypes {
val group = buildVolume(volume, context)
val group = buildVolume(volume, context) ?: return@prototypes
setChild(templateName, group)
}
}
ref(templateName, name).apply(block)
ref(templateName, combinedName).apply(block)
}
}

View File

@ -26,7 +26,7 @@ private fun Meta.countTypes(): Sequence<String> = sequence {
}
fun main() {
val string = ZipInputStream(TGeoManager::class.java.getResourceAsStream("/root/BM@N_geometry.zip")!!).use {
val string = ZipInputStream(TGeoManager::class.java.getResourceAsStream("/root/geometry_run_7-2076.zip")!!).use {
it.nextEntry
it.readAllBytes().decodeToString()
}

View File

@ -17,7 +17,7 @@ import space.kscience.visionforge.VisionBuilder
*/
public interface PrototypeHolder {
/**
* Build or update prototype tree
* Build or update the prototype tree
*/
@VisionBuilder
public fun prototypes(builder: MutableVisionContainer<Solid>.() -> Unit)

View File

@ -2,7 +2,9 @@ package space.kscience.visionforge.solid
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@ -226,9 +228,14 @@ internal class SolidReferenceChild(
*/
public fun MutableVisionContainer<Solid>.ref(
templateName: Name,
name: String? = null,
name: Name? = null,
): SolidReference = SolidReference(templateName).also { setChild(name, it) }
public fun MutableVisionContainer<Solid>.ref(
templateName: Name,
name: String? = null,
): SolidReference = ref(templateName, name?.parseAsName())
public fun MutableVisionContainer<Solid>.ref(
templateName: String,
name: String? = null,