Module loading refactor

This commit is contained in:
Alexander Nozik 2021-02-27 21:40:58 +03:00
parent bc15d9241b
commit 7c9315b029
28 changed files with 90 additions and 91 deletions

View File

@ -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<GDMLAppProps>("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)

View File

@ -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

View File

@ -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<String>) {
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)

View File

@ -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()]
}
}

View File

@ -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()
}

View File

@ -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"))
}
}
}

View File

@ -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)

View File

@ -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)
}

View File

@ -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

View File

@ -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)
}

View File

@ -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"),

View File

@ -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)
}

View File

@ -16,7 +16,7 @@ import kotlin.random.Random
fun main() {
val satContext = Global.context("sat") {
plugin(SolidManager)
plugin(Solids)
}
//Create a geometry

View File

@ -44,7 +44,7 @@ public external interface CanvasControlsProps : RProps {
public val CanvasControls: FunctionalComponent<CanvasControlsProps> = functionalComponent("CanvasControls") { props ->
val visionManager = useMemo(
{ props.canvas.three.solidManager.visionManager },
{ props.canvas.three.solids.visionManager },
arrayOf(props.canvas)
)
flexColumn {

View File

@ -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)

View File

@ -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)
}

View File

@ -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)

View File

@ -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)
public operator fun VisionForge.invoke(vararg modules: PluginFactory<out VisionPlugin>, block: VisionForge.() -> Unit): Unit {
modules.forEach {
plugins.fetch(it)
}
run(block)
}

View File

@ -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

View File

@ -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))

View File

@ -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<Vision>.group(name: String, action: SolidGroup
/**
* A special class which works as a holder for prototypes
*/
@Serializable(Prototypes.Companion::class)
internal class Prototypes(
children: Map<NameToken, Vision> = emptyMap(),
) : VisionGroupBase(children as? MutableMap<NameToken, Vision> ?: children.toMutableMap()), PrototypeHolder {
init {
//used during deserialization only
children.values.forEach {
it.parent = parent
}
}
children: MutableMap<NameToken, Vision> = hashMapOf(),
) : VisionGroupBase(children), PrototypeHolder {
override fun getProperty(
name: Name,
@ -118,12 +110,19 @@ 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<MutableVisionGroup> {
override fun prototypes(builder: VisionContainerBuilder<Solid>.() -> Unit) {
apply(builder)
}
override fun getPrototype(name: Name): Solid? = get(name) as? Solid
}
internal class PrototypeSerializer : KSerializer<MutableVisionGroup> {
private val mapSerializer: KSerializer<Map<NameToken, Vision>> =
MapSerializer(
@ -135,17 +134,10 @@ internal class Prototypes(
override fun deserialize(decoder: Decoder): MutableVisionGroup {
val map = mapSerializer.deserialize(decoder)
return Prototypes(map)
return Prototypes(map.toMutableMap())
}
override fun serialize(encoder: Encoder, value: MutableVisionGroup) {
mapSerializer.serialize(encoder, value.children)
}
}
override fun prototypes(builder: VisionContainerBuilder<Solid>.() -> Unit) {
apply(builder)
}
override fun getPrototype(name: Name): Solid? = get(name) as? Solid
}

View File

@ -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<SolidManager> {
public companion object : PluginFactory<Solids> {
override val tag: PluginTag = PluginTag(name = "vision.solid", group = PluginTag.DATAFORGE_GROUP)
override val type: KClass<out SolidManager> = SolidManager::class
override fun invoke(meta: Meta, context: Context): SolidManager = SolidManager(meta)
override val type: KClass<out Solids> = Solids::class
override fun invoke(meta: Meta, context: Context): Solids = Solids(meta)
private fun PolymorphicModuleBuilder<Solid>.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)
}

View File

@ -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() }

View File

@ -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)
}

View File

@ -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

View File

@ -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)
}
}

View File

@ -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

View File

@ -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<KClass<out Solid>, ThreeFactory<*>>()
private val compositeFactory = ThreeCompositeFactory(this)