From 7c9315b0297c2df46ffa990cba23728bdb06ff94 Mon Sep 17 00:00:00 2001 From: darksnake Date: Sat, 27 Feb 2021 21:40:58 +0300 Subject: [PATCH] Module loading refactor --- .../vision/gdml/demo/GDMLAppComponent.kt | 8 +-- .../dataforge/vision/gdml/demo/readFile.kt | 8 +-- .../dataforge/vision/gdml/demo/saveToJson.kt | 4 +- .../vision/solid/FileSerializationTest.kt | 2 +- .../VisionForgePlayGroundForJupyter.kt | 5 +- demo/muon-monitor/build.gradle.kts | 1 + .../mipt/npm/muon/monitor/server/MMServer.kt | 4 +- .../src/jvmMain/kotlin/gdmlCubes.kt | 5 +- .../src/jvmMain/kotlin/generateSchema.kt | 4 +- .../src/jvmMain/kotlin/plotlyVision.kt | 5 +- .../src/jvmMain/kotlin/randomSpheres.kt | 3 +- .../src/jvmMain/kotlin/simpleCube.kt | 5 +- .../main/kotlin/ru/mipt/npm/sat/satServer.kt | 2 +- .../vision/bootstrap/outputConfig.kt | 2 +- .../hep/dataforge/vision/VisionForge.kt | 5 ++ .../vision/html/VisionTagConsumer.kt | 2 + .../hep/dataforge/vision/visionForgeJs.kt | 4 ++ .../hep/dataforge/vision/visionForgeJvm.kt | 11 +++- .../dataforge/vision/gdml/TestConvertor.kt | 4 +- .../hep/dataforge/vision/gdml/TestCubes.kt | 6 +- .../hep/dataforge/vision/solid/SolidGroup.kt | 56 ++++++++----------- .../solid/{SolidManager.kt => Solids.kt} | 13 ++--- .../hep/dataforge/vision/solid/ConvexTest.kt | 2 +- .../vision/solid/SerializationTest.kt | 8 +-- .../dataforge/vision/solid/SolidPluginTest.kt | 2 +- .../vision/solid/SolidReferenceTest.kt | 5 +- .../vision/solid/VisionUpdateTest.kt | 2 +- .../vision/solid/three/ThreePlugin.kt | 3 +- 28 files changed, 90 insertions(+), 91 deletions(-) rename visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/{SolidManager.kt => Solids.kt} (88%) diff --git a/demo/gdml/src/jsMain/kotlin/hep/dataforge/vision/gdml/demo/GDMLAppComponent.kt b/demo/gdml/src/jsMain/kotlin/hep/dataforge/vision/gdml/demo/GDMLAppComponent.kt index b0caeeb8..13d68ab2 100644 --- a/demo/gdml/src/jsMain/kotlin/hep/dataforge/vision/gdml/demo/GDMLAppComponent.kt +++ b/demo/gdml/src/jsMain/kotlin/hep/dataforge/vision/gdml/demo/GDMLAppComponent.kt @@ -10,13 +10,11 @@ import hep.dataforge.vision.gdml.toVision import hep.dataforge.vision.react.ThreeCanvasComponent import hep.dataforge.vision.react.flexColumn import hep.dataforge.vision.solid.Solid -import hep.dataforge.vision.solid.SolidManager +import hep.dataforge.vision.solid.Solids import hep.dataforge.vision.solid.specifications.Canvas3DOptions import hep.dataforge.vision.solid.three.ThreeCanvas import kotlinx.browser.window import kotlinx.css.* -import space.kscience.gdml.Gdml -import space.kscience.gdml.decodeFromString import org.w3c.files.FileReader import org.w3c.files.get import react.RProps @@ -24,6 +22,8 @@ import react.child import react.dom.h1 import react.functionalComponent import react.useState +import space.kscience.gdml.Gdml +import space.kscience.gdml.decodeFromString import styled.css import styled.styledDiv @@ -52,7 +52,7 @@ val GDMLApp = functionalComponent("GDMLApp") { props -> } fun loadData(name: String, data: String) { - val visionManager = props.context.plugins.fetch(SolidManager).visionManager + val visionManager = props.context.plugins.fetch(Solids).visionManager val parsedVision = when { name.endsWith(".gdml") || name.endsWith(".xml") -> { val gdml = Gdml.decodeFromString(data) diff --git a/demo/gdml/src/jvmMain/kotlin/hep/dataforge/vision/gdml/demo/readFile.kt b/demo/gdml/src/jvmMain/kotlin/hep/dataforge/vision/gdml/demo/readFile.kt index e1756d51..754de41f 100644 --- a/demo/gdml/src/jvmMain/kotlin/hep/dataforge/vision/gdml/demo/readFile.kt +++ b/demo/gdml/src/jvmMain/kotlin/hep/dataforge/vision/gdml/demo/readFile.kt @@ -1,14 +1,10 @@ package hep.dataforge.vision.gdml.demo import hep.dataforge.misc.DFExperimental -import hep.dataforge.values.asValue -import hep.dataforge.vision.gdml.readFile -import hep.dataforge.vision.gdml.toVision -import hep.dataforge.vision.solid.SolidGroup -import hep.dataforge.vision.solid.SolidManager -import hep.dataforge.vision.solid.SolidMaterial import hep.dataforge.vision.Vision import hep.dataforge.vision.VisionManager +import hep.dataforge.vision.gdml.readFile +import hep.dataforge.vision.gdml.toVision import space.kscience.gdml.Gdml import java.io.File import java.util.zip.GZIPInputStream diff --git a/demo/gdml/src/jvmMain/kotlin/hep/dataforge/vision/gdml/demo/saveToJson.kt b/demo/gdml/src/jvmMain/kotlin/hep/dataforge/vision/gdml/demo/saveToJson.kt index 472d28cf..9f99aec3 100644 --- a/demo/gdml/src/jvmMain/kotlin/hep/dataforge/vision/gdml/demo/saveToJson.kt +++ b/demo/gdml/src/jvmMain/kotlin/hep/dataforge/vision/gdml/demo/saveToJson.kt @@ -2,7 +2,7 @@ package hep.dataforge.vision.gdml.demo import hep.dataforge.vision.gdml.readFile import hep.dataforge.vision.gdml.toVision -import hep.dataforge.vision.solid.SolidManager +import hep.dataforge.vision.solid.Solids import space.kscience.gdml.Gdml import space.kscience.gdml.LUnit import java.io.File @@ -21,7 +21,7 @@ fun main(args: Array) { lUnit = LUnit.CM } - val json = SolidManager.encodeToString(vision) + val json = Solids.encodeToString(vision) println(json) File(outputFileName).writeText(json) //File("D:\\Work\\Projects\\gdml.kt\\gdml-source\\cubes.json").writeText(json) diff --git a/demo/gdml/src/jvmTest/kotlin/hep/dataforge/vision/solid/FileSerializationTest.kt b/demo/gdml/src/jvmTest/kotlin/hep/dataforge/vision/solid/FileSerializationTest.kt index 4fe68578..1d3c686e 100644 --- a/demo/gdml/src/jvmTest/kotlin/hep/dataforge/vision/solid/FileSerializationTest.kt +++ b/demo/gdml/src/jvmTest/kotlin/hep/dataforge/vision/solid/FileSerializationTest.kt @@ -9,7 +9,7 @@ class FileSerializationTest { @Ignore fun testFileRead(){ val text = this::class.java.getResourceAsStream("/cubes.json").readBytes().decodeToString() - val visual = SolidManager.decodeFromString(text) as SolidGroup + val visual = Solids.decodeFromString(text) as SolidGroup visual["composite_001".asName()] } } \ No newline at end of file diff --git a/demo/jupyter-playground/src/main/kotlin/hep/dataforge/playground/VisionForgePlayGroundForJupyter.kt b/demo/jupyter-playground/src/main/kotlin/hep/dataforge/playground/VisionForgePlayGroundForJupyter.kt index 4ae7cf4b..d7764b81 100644 --- a/demo/jupyter-playground/src/main/kotlin/hep/dataforge/playground/VisionForgePlayGroundForJupyter.kt +++ b/demo/jupyter-playground/src/main/kotlin/hep/dataforge/playground/VisionForgePlayGroundForJupyter.kt @@ -10,7 +10,8 @@ import hep.dataforge.vision.html.embedVisionFragment import hep.dataforge.vision.html.fragment import hep.dataforge.vision.plotly.toVision import hep.dataforge.vision.plotly.usePlotly -import hep.dataforge.vision.solid.useSolids +import hep.dataforge.vision.plugins +import hep.dataforge.vision.solid.Solids import hep.dataforge.vision.visionManager import kotlinx.html.div import kotlinx.html.id @@ -51,7 +52,7 @@ internal class VisionForgePlayGroundForJupyter : JupyterIntegration() { resource(jsResource) onLoaded { - VisionForge.useSolids() + VisionForge.plugins.fetch(Solids) VisionForge.usePlotly() } diff --git a/demo/muon-monitor/build.gradle.kts b/demo/muon-monitor/build.gradle.kts index 8b51da72..ce4ea12a 100644 --- a/demo/muon-monitor/build.gradle.kts +++ b/demo/muon-monitor/build.gradle.kts @@ -51,6 +51,7 @@ kotlin { implementation("io.ktor:ktor-client-js:$ktorVersion") implementation("io.ktor:ktor-client-serialization:$ktorVersion") implementation(project(":visionforge-threejs")) + implementation(devNpm("webpack-bundle-analyzer", "4.4.0")) } } } diff --git a/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/server/MMServer.kt b/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/server/MMServer.kt index ada656cc..0b66e091 100644 --- a/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/server/MMServer.kt +++ b/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/server/MMServer.kt @@ -4,7 +4,7 @@ package ru.mipt.npm.muon.monitor.server import hep.dataforge.context.Context import hep.dataforge.context.Global import hep.dataforge.misc.DFExperimental -import hep.dataforge.vision.solid.SolidManager +import hep.dataforge.vision.solid.Solids import io.ktor.application.Application import io.ktor.application.call import io.ktor.application.install @@ -38,7 +38,7 @@ private val generator = Cos2TrackGenerator(JDKRandomGenerator(223)) fun Application.module(context: Context = Global) { val currentDir = File(".").absoluteFile environment.log.info("Current directory: $currentDir") - val solidManager = context.plugins.load(SolidManager) + val solidManager = context.plugins.load(Solids) install(DefaultHeaders) install(CallLogging) diff --git a/demo/playground/src/jvmMain/kotlin/gdmlCubes.kt b/demo/playground/src/jvmMain/kotlin/gdmlCubes.kt index 2caefaaa..b5a10dc5 100644 --- a/demo/playground/src/jvmMain/kotlin/gdmlCubes.kt +++ b/demo/playground/src/jvmMain/kotlin/gdmlCubes.kt @@ -6,7 +6,7 @@ import hep.dataforge.vision.gdml.toVision import hep.dataforge.vision.html.ResourceLocation import hep.dataforge.vision.html.fragment import hep.dataforge.vision.invoke -import hep.dataforge.vision.solid.useSolids +import hep.dataforge.vision.solid.Solids import space.kscience.gdml.* internal val cubes = Gdml { @@ -60,12 +60,11 @@ internal val cubes = Gdml { } @DFExperimental -fun main() = VisionForge { +fun main() = VisionForge(Solids) { val content = VisionForge.fragment { vision("canvas") { cubes.toVision() } } - useSolids() makeVisionFile(content, resourceLocation = ResourceLocation.SYSTEM) } \ No newline at end of file diff --git a/demo/playground/src/jvmMain/kotlin/generateSchema.kt b/demo/playground/src/jvmMain/kotlin/generateSchema.kt index 25f82ea1..1b48dbc8 100644 --- a/demo/playground/src/jvmMain/kotlin/generateSchema.kt +++ b/demo/playground/src/jvmMain/kotlin/generateSchema.kt @@ -2,14 +2,14 @@ package hep.dataforge.vision.examples import com.github.ricky12awesome.jss.encodeToSchema import hep.dataforge.vision.solid.SolidGroup -import hep.dataforge.vision.solid.SolidManager +import hep.dataforge.vision.solid.Solids import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.json.Json @ExperimentalSerializationApi fun main() { val schema = Json { - serializersModule = SolidManager.serializersModuleForSolids + serializersModule = Solids.serializersModuleForSolids prettyPrintIndent = " " prettyPrint = true ignoreUnknownKeys = true diff --git a/demo/playground/src/jvmMain/kotlin/plotlyVision.kt b/demo/playground/src/jvmMain/kotlin/plotlyVision.kt index 0058232d..0341e73f 100644 --- a/demo/playground/src/jvmMain/kotlin/plotlyVision.kt +++ b/demo/playground/src/jvmMain/kotlin/plotlyVision.kt @@ -5,12 +5,12 @@ import hep.dataforge.vision.VisionForge import hep.dataforge.vision.html.ResourceLocation import hep.dataforge.vision.html.fragment import hep.dataforge.vision.invoke +import hep.dataforge.vision.plotly.PlotlyPlugin import hep.dataforge.vision.plotly.plotly -import hep.dataforge.vision.plotly.usePlotly import kscience.plotly.scatter @DFExperimental -fun main() = VisionForge { +fun main() = VisionForge(PlotlyPlugin) { val fragment = fragment { vision { plotly { @@ -21,6 +21,5 @@ fun main() = VisionForge { } } } - usePlotly() makeVisionFile(fragment, resourceLocation = ResourceLocation.SYSTEM) } \ No newline at end of file diff --git a/demo/playground/src/jvmMain/kotlin/randomSpheres.kt b/demo/playground/src/jvmMain/kotlin/randomSpheres.kt index bed0ebbf..9526d318 100644 --- a/demo/playground/src/jvmMain/kotlin/randomSpheres.kt +++ b/demo/playground/src/jvmMain/kotlin/randomSpheres.kt @@ -11,7 +11,7 @@ import java.nio.file.Paths import kotlin.random.Random @OptIn(DFExperimental::class) -fun main() = VisionForge.invoke { +fun main() = VisionForge(Solids) { val random = Random(112233) val fragment = fragment { @@ -33,7 +33,6 @@ fun main() = VisionForge.invoke { } } } - useSolids() makeVisionFile( fragment, Paths.get("randomSpheres.html"), diff --git a/demo/playground/src/jvmMain/kotlin/simpleCube.kt b/demo/playground/src/jvmMain/kotlin/simpleCube.kt index e8dc6cf5..519738d7 100644 --- a/demo/playground/src/jvmMain/kotlin/simpleCube.kt +++ b/demo/playground/src/jvmMain/kotlin/simpleCube.kt @@ -5,12 +5,12 @@ import hep.dataforge.vision.VisionForge import hep.dataforge.vision.html.ResourceLocation import hep.dataforge.vision.html.fragment import hep.dataforge.vision.invoke +import hep.dataforge.vision.solid.Solids import hep.dataforge.vision.solid.box import hep.dataforge.vision.solid.solid -import hep.dataforge.vision.solid.useSolids @DFExperimental -fun main() = VisionForge.invoke { +fun main() = VisionForge(Solids) { val content = fragment { vision("canvas") { solid { @@ -18,6 +18,5 @@ fun main() = VisionForge.invoke { } } } - useSolids() makeVisionFile(content, resourceLocation = ResourceLocation.SYSTEM) } \ No newline at end of file diff --git a/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/satServer.kt b/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/satServer.kt index 92b1b7d0..242d29d5 100644 --- a/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/satServer.kt +++ b/demo/sat-demo/src/main/kotlin/ru/mipt/npm/sat/satServer.kt @@ -16,7 +16,7 @@ import kotlin.random.Random fun main() { val satContext = Global.context("sat") { - plugin(SolidManager) + plugin(Solids) } //Create a geometry diff --git a/ui/bootstrap/src/main/kotlin/hep/dataforge/vision/bootstrap/outputConfig.kt b/ui/bootstrap/src/main/kotlin/hep/dataforge/vision/bootstrap/outputConfig.kt index 9c17a6cd..69585627 100644 --- a/ui/bootstrap/src/main/kotlin/hep/dataforge/vision/bootstrap/outputConfig.kt +++ b/ui/bootstrap/src/main/kotlin/hep/dataforge/vision/bootstrap/outputConfig.kt @@ -44,7 +44,7 @@ public external interface CanvasControlsProps : RProps { public val CanvasControls: FunctionalComponent = functionalComponent("CanvasControls") { props -> val visionManager = useMemo( - { props.canvas.three.solidManager.visionManager }, + { props.canvas.three.solids.visionManager }, arrayOf(props.canvas) ) flexColumn { diff --git a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionForge.kt b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionForge.kt index ae42bf02..00ce9715 100644 --- a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionForge.kt +++ b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionForge.kt @@ -2,11 +2,16 @@ package hep.dataforge.vision import hep.dataforge.context.Context import hep.dataforge.context.PluginManager +import hep.dataforge.misc.DFExperimental +@DFExperimental public expect object VisionForge +@DFExperimental public expect val VisionForge.context: Context +@DFExperimental public val VisionForge.plugins: PluginManager get() = context.plugins +@DFExperimental public val VisionForge.visionManager: VisionManager get() = plugins.fetch(VisionManager) \ No newline at end of file diff --git a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/html/VisionTagConsumer.kt b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/html/VisionTagConsumer.kt index 4a279f3b..c1c26818 100644 --- a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/html/VisionTagConsumer.kt +++ b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/html/VisionTagConsumer.kt @@ -20,6 +20,8 @@ import kotlin.collections.set public class VisionOutput @PublishedApi internal constructor() { public var meta: Meta = Meta.EMPTY + //TODO expose a way to define required plugins. + public inline fun meta(block: MetaBuilder.() -> Unit) { this.meta = Meta(block) } diff --git a/visionforge-core/src/jsMain/kotlin/hep/dataforge/vision/visionForgeJs.kt b/visionforge-core/src/jsMain/kotlin/hep/dataforge/vision/visionForgeJs.kt index 119bfe50..f66bbc84 100644 --- a/visionforge-core/src/jsMain/kotlin/hep/dataforge/vision/visionForgeJs.kt +++ b/visionforge-core/src/jsMain/kotlin/hep/dataforge/vision/visionForgeJs.kt @@ -1,10 +1,12 @@ package hep.dataforge.vision import hep.dataforge.context.Context +import hep.dataforge.misc.DFExperimental import kotlinx.browser.document import kotlinx.browser.window @JsExport +@DFExperimental public actual object VisionForge{ /** * Render all visions in this [window] using current global state of [VisionForge] @@ -32,6 +34,8 @@ private val visionForgeContext = Context("VisionForge"){ plugin(VisionClient) } +@DFExperimental public actual val VisionForge.context: Context get() = visionForgeContext +@DFExperimental public val VisionForge.visionClient: VisionClient get() = plugins.fetch(VisionClient) \ No newline at end of file diff --git a/visionforge-core/src/jvmMain/kotlin/hep/dataforge/vision/visionForgeJvm.kt b/visionforge-core/src/jvmMain/kotlin/hep/dataforge/vision/visionForgeJvm.kt index 42c1d67b..ba71e312 100644 --- a/visionforge-core/src/jvmMain/kotlin/hep/dataforge/vision/visionForgeJvm.kt +++ b/visionforge-core/src/jvmMain/kotlin/hep/dataforge/vision/visionForgeJvm.kt @@ -1,13 +1,22 @@ package hep.dataforge.vision import hep.dataforge.context.Context +import hep.dataforge.context.PluginFactory import hep.dataforge.misc.DFExperimental +@DFExperimental public actual object VisionForge +@DFExperimental private val visionForgeContext = Context("VisionForge") +@DFExperimental public actual val VisionForge.context: Context get() = visionForgeContext @DFExperimental -public operator fun VisionForge.invoke(block: VisionForge.() -> Unit): Unit = run(block) \ No newline at end of file +public operator fun VisionForge.invoke(vararg modules: PluginFactory, block: VisionForge.() -> Unit): Unit { + modules.forEach { + plugins.fetch(it) + } + run(block) +} \ No newline at end of file diff --git a/visionforge-gdml/src/jvmTest/kotlin/hep/dataforge/vision/gdml/TestConvertor.kt b/visionforge-gdml/src/jvmTest/kotlin/hep/dataforge/vision/gdml/TestConvertor.kt index 16a1e202..76929d46 100644 --- a/visionforge-gdml/src/jvmTest/kotlin/hep/dataforge/vision/gdml/TestConvertor.kt +++ b/visionforge-gdml/src/jvmTest/kotlin/hep/dataforge/vision/gdml/TestConvertor.kt @@ -1,7 +1,7 @@ package hep.dataforge.vision.gdml import hep.dataforge.names.toName -import hep.dataforge.vision.solid.SolidManager +import hep.dataforge.vision.solid.Solids import org.junit.jupiter.api.Test import space.kscience.gdml.Gdml import space.kscience.gdml.decodeFromStream @@ -23,7 +23,7 @@ class TestConvertor { val gdml = Gdml.decodeFromStream(stream) val vision = gdml.toVision() assertNotNull(vision.getPrototype("solids.box".toName())) - println(SolidManager.encodeToString(vision)) + println(Solids.encodeToString(vision)) } @Test diff --git a/visionforge-gdml/src/jvmTest/kotlin/hep/dataforge/vision/gdml/TestCubes.kt b/visionforge-gdml/src/jvmTest/kotlin/hep/dataforge/vision/gdml/TestCubes.kt index 4d451ddb..d0d0f054 100644 --- a/visionforge-gdml/src/jvmTest/kotlin/hep/dataforge/vision/gdml/TestCubes.kt +++ b/visionforge-gdml/src/jvmTest/kotlin/hep/dataforge/vision/gdml/TestCubes.kt @@ -4,8 +4,8 @@ import hep.dataforge.context.Context import hep.dataforge.names.toName import hep.dataforge.vision.Vision import hep.dataforge.vision.solid.SolidGroup -import hep.dataforge.vision.solid.SolidManager import hep.dataforge.vision.solid.SolidReference +import hep.dataforge.vision.solid.Solids import hep.dataforge.vision.visionManager import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertDoesNotThrow @@ -13,7 +13,7 @@ import space.kscience.gdml.* import kotlin.test.assertNotNull internal val testContext = Context("TEST"){ - plugin(SolidManager) + plugin(Solids) } class TestCubes { @@ -77,7 +77,7 @@ class TestCubes { @Test fun testCubesReSerialize(){ val vision = cubes.toVision() - val serialized = SolidManager.encodeToString(vision) + val serialized = Solids.encodeToString(vision) val deserialized = testContext.visionManager.decodeFromString(serialized) as SolidGroup assertNotNull(deserialized.getPrototype("solids.smallBox".toName())) //println(testContext.visionManager.encodeToString(deserialized)) diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidGroup.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidGroup.kt index 28e0e4e3..55dfd65b 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidGroup.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidGroup.kt @@ -28,7 +28,7 @@ public interface PrototypeHolder { @Serializable @SerialName("group.solid") public class SolidGroup( - @Serializable(Prototypes.Companion::class) @SerialName("prototypes") internal var prototypes: MutableVisionGroup? = null, + @Serializable(PrototypeSerializer::class) internal var prototypes: MutableVisionGroup? = null, ) : VisionGroupBase(), Solid, PrototypeHolder { init { @@ -98,17 +98,9 @@ public fun VisionContainerBuilder.group(name: String, action: SolidGroup /** * A special class which works as a holder for prototypes */ -@Serializable(Prototypes.Companion::class) internal class Prototypes( - children: Map = emptyMap(), -) : VisionGroupBase(children as? MutableMap ?: children.toMutableMap()), PrototypeHolder { - - init { - //used during deserialization only - children.values.forEach { - it.parent = parent - } - } + children: MutableMap = hashMapOf(), +) : VisionGroupBase(children), PrototypeHolder { override fun getProperty( name: Name, @@ -118,34 +110,34 @@ internal class Prototypes( ): MetaItem? = null override fun setProperty(name: Name, item: MetaItem?, notify: Boolean) { - error("Can't ser property of prototypes container") + error("Can't set property of a prototypes container") } override val descriptor: NodeDescriptor? = null - companion object : KSerializer { - - private val mapSerializer: KSerializer> = - MapSerializer( - NameToken.serializer(), - PolymorphicSerializer(Vision::class) - ) - - override val descriptor: SerialDescriptor get() = mapSerializer.descriptor - - override fun deserialize(decoder: Decoder): MutableVisionGroup { - val map = mapSerializer.deserialize(decoder) - return Prototypes(map) - } - - override fun serialize(encoder: Encoder, value: MutableVisionGroup) { - mapSerializer.serialize(encoder, value.children) - } - } - override fun prototypes(builder: VisionContainerBuilder.() -> Unit) { apply(builder) } override fun getPrototype(name: Name): Solid? = get(name) as? Solid } + +internal class PrototypeSerializer : KSerializer { + + private val mapSerializer: KSerializer> = + MapSerializer( + NameToken.serializer(), + PolymorphicSerializer(Vision::class) + ) + + override val descriptor: SerialDescriptor get() = mapSerializer.descriptor + + override fun deserialize(decoder: Decoder): MutableVisionGroup { + val map = mapSerializer.deserialize(decoder) + return Prototypes(map.toMutableMap()) + } + + override fun serialize(encoder: Encoder, value: MutableVisionGroup) { + mapSerializer.serialize(encoder, value.children) + } +} \ No newline at end of file diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidManager.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Solids.kt similarity index 88% rename from visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidManager.kt rename to visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Solids.kt index d716442b..02c88bcc 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidManager.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/Solids.kt @@ -16,16 +16,16 @@ import kotlinx.serialization.modules.subclass import kotlin.reflect.KClass -public class SolidManager(meta: Meta) : VisionPlugin(meta) { +public class Solids(meta: Meta) : VisionPlugin(meta) { override val tag: PluginTag get() = Companion.tag override val visionSerializersModule: SerializersModule get() = serializersModuleForSolids - public companion object : PluginFactory { + public companion object : PluginFactory { override val tag: PluginTag = PluginTag(name = "vision.solid", group = PluginTag.DATAFORGE_GROUP) - override val type: KClass = SolidManager::class - override fun invoke(meta: Meta, context: Context): SolidManager = SolidManager(meta) + override val type: KClass = Solids::class + override fun invoke(meta: Meta, context: Context): Solids = Solids(meta) private fun PolymorphicModuleBuilder.solids() { subclass(SolidGroup.serializer()) @@ -69,8 +69,3 @@ public class SolidManager(meta: Meta) : VisionPlugin(meta) { @VisionBuilder @DFExperimental public inline fun VisionOutput.solid(block: SolidGroup.() -> Unit): SolidGroup = SolidGroup().apply(block) - -@DFExperimental -public fun VisionForge.useSolids(): Unit{ - plugins.fetch(SolidManager) -} diff --git a/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/ConvexTest.kt b/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/ConvexTest.kt index 20062686..16b4c6d6 100644 --- a/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/ConvexTest.kt +++ b/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/ConvexTest.kt @@ -28,7 +28,7 @@ class ConvexTest { val convex = group.children.values.first() as Convex - val json = SolidManager.jsonForSolids.encodeToJsonElement(Convex.serializer(), convex) + val json = Solids.jsonForSolids.encodeToJsonElement(Convex.serializer(), convex) val meta = json.toMetaItem().node!! val points = meta.getIndexed("points").values.map { (it as MetaItemNode<*>).node.point3D() } diff --git a/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/SerializationTest.kt b/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/SerializationTest.kt index bafe16a8..90a158de 100644 --- a/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/SerializationTest.kt +++ b/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/SerializationTest.kt @@ -30,9 +30,9 @@ class SerializationTest { x = 100 z = -100 } - val string = SolidManager.encodeToString(cube) + val string = Solids.encodeToString(cube) println(string) - val newCube = SolidManager.decodeFromString(string) + val newCube = Solids.decodeFromString(string) assertEquals(cube.meta, newCube.meta) } @@ -51,9 +51,9 @@ class SerializationTest { } } } - val string = SolidManager.encodeToString(group) + val string = Solids.encodeToString(group) println(string) - val reconstructed = SolidManager.decodeFromString(string) as SolidGroup + val reconstructed = Solids.decodeFromString(string) as SolidGroup assertEquals(group["cube"]?.meta, reconstructed["cube"]?.meta) } diff --git a/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/SolidPluginTest.kt b/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/SolidPluginTest.kt index 1da06a02..9613fb9b 100644 --- a/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/SolidPluginTest.kt +++ b/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/SolidPluginTest.kt @@ -18,7 +18,7 @@ class SolidPluginTest { @DFExperimental @Test fun testPluginConverter() { - val visionManager = Global.plugins.fetch(SolidManager).visionManager + val visionManager = Global.plugins.fetch(Solids).visionManager val meta = visionManager.encodeToMeta(vision) val reconstructed = visionManager.decodeFromMeta(meta) as SolidGroup diff --git a/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/SolidReferenceTest.kt b/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/SolidReferenceTest.kt index bb38316e..04bb38e5 100644 --- a/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/SolidReferenceTest.kt +++ b/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/SolidReferenceTest.kt @@ -3,7 +3,6 @@ package hep.dataforge.vision.solid import hep.dataforge.vision.get import hep.dataforge.vision.style import hep.dataforge.vision.useStyle -import kotlinx.serialization.json.Json import kotlinx.serialization.json.encodeToJsonElement import kotlin.test.Test import kotlin.test.assertEquals @@ -27,8 +26,8 @@ class SolidReferenceTest { @Test fun testReferenceSerialization(){ - val serialized = SolidManager.jsonForSolids.encodeToJsonElement(groupWithReference) - val deserialized = SolidManager.jsonForSolids.decodeFromJsonElement(SolidGroup.serializer(), serialized) + val serialized = Solids.jsonForSolids.encodeToJsonElement(groupWithReference) + val deserialized = Solids.jsonForSolids.decodeFromJsonElement(SolidGroup.serializer(), serialized) assertEquals("blue", (deserialized["test"] as Solid).color.string) } } \ No newline at end of file diff --git a/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/VisionUpdateTest.kt b/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/VisionUpdateTest.kt index 17babd41..2b8502e5 100644 --- a/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/VisionUpdateTest.kt +++ b/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/VisionUpdateTest.kt @@ -10,7 +10,7 @@ import kotlin.test.assertEquals import kotlin.test.assertTrue class VisionUpdateTest { - val solidManager = Global.plugins.fetch(SolidManager) + val solidManager = Global.plugins.fetch(Solids) val visionManager = solidManager.visionManager @Test diff --git a/visionforge-threejs/src/main/kotlin/hep/dataforge/vision/solid/three/ThreePlugin.kt b/visionforge-threejs/src/main/kotlin/hep/dataforge/vision/solid/three/ThreePlugin.kt index 6baab0ba..1e8427bd 100644 --- a/visionforge-threejs/src/main/kotlin/hep/dataforge/vision/solid/three/ThreePlugin.kt +++ b/visionforge-threejs/src/main/kotlin/hep/dataforge/vision/solid/three/ThreePlugin.kt @@ -6,7 +6,6 @@ import hep.dataforge.names.* import hep.dataforge.vision.* import hep.dataforge.vision.solid.* import hep.dataforge.vision.solid.specifications.Canvas3DOptions -import hep.dataforge.vision.solid.three.set import info.laht.threekt.core.Object3D import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.launchIn @@ -20,7 +19,7 @@ import info.laht.threekt.objects.Group as ThreeGroup public class ThreePlugin : AbstractPlugin(), ElementVisionRenderer { override val tag: PluginTag get() = Companion.tag - public val solidManager: SolidManager by require(SolidManager) + public val solids: Solids by require(Solids) private val objectFactories = HashMap, ThreeFactory<*>>() private val compositeFactory = ThreeCompositeFactory(this)