diff --git a/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/dRootToSolid.kt b/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/dRootToSolid.kt index be9372b5..13b1cb55 100644 --- a/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/dRootToSolid.kt +++ b/cern-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/dRootToSolid.kt @@ -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 - } else { - "${name}_${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 { + 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) } } diff --git a/demo/playground/src/jvmMain/kotlin/rootParser.kt b/demo/playground/src/jvmMain/kotlin/rootParser.kt index d5fea32e..85a2f0d3 100644 --- a/demo/playground/src/jvmMain/kotlin/rootParser.kt +++ b/demo/playground/src/jvmMain/kotlin/rootParser.kt @@ -26,7 +26,7 @@ private fun Meta.countTypes(): Sequence = 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() } diff --git a/demo/playground/src/jvmMain/resources/root/geometry_run_7-2076.zip b/demo/playground/src/jvmMain/resources/root/geometry_run_7-2076.zip new file mode 100644 index 00000000..1680404c Binary files /dev/null and b/demo/playground/src/jvmMain/resources/root/geometry_run_7-2076.zip differ diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidGroup.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidGroup.kt index 8e04b638..d36057d3 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidGroup.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidGroup.kt @@ -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.() -> Unit) diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidReference.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidReference.kt index 80db0b3e..7203f06f 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidReference.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidReference.kt @@ -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 @@ -61,7 +63,7 @@ public class SolidReference( } override fun getValue(name: Name, inherit: Boolean?, includeStyles: Boolean?): Value? { - if(name == Vision.STYLE_KEY){ + if (name == Vision.STYLE_KEY) { return buildList { properties?.getValue(Vision.STYLE_KEY)?.list?.forEach { add(it) @@ -90,7 +92,7 @@ public class SolidReference( prototype.getStyleProperty(name)?.value?.let { return it } } - if(inheritFlag){ + if (inheritFlag) { //5. own inheritance parent?.properties?.getValue(name, inheritFlag, includeStyles)?.let { return it } //6. prototype inheritance @@ -226,9 +228,14 @@ internal class SolidReferenceChild( */ public fun MutableVisionContainer.ref( templateName: Name, - name: String? = null, + name: Name? = null, ): SolidReference = SolidReference(templateName).also { setChild(name, it) } +public fun MutableVisionContainer.ref( + templateName: Name, + name: String? = null, +): SolidReference = ref(templateName, name?.parseAsName()) + public fun MutableVisionContainer.ref( templateName: String, name: String? = null,