From 27fe492ab73e1a0ed58e46a7a2682efadb98900c Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 24 Feb 2021 18:26:15 +0300 Subject: [PATCH] Complete playfround jupyter demo --- .../VisionForgePlayGroundForJupyter.kt | 60 ++++++++++--------- .../src/jvmMain/kotlin/gdmlCubes.kt | 9 ++- .../src/jvmMain/kotlin/serverExtensions.kt | 13 +++- .../vision/html/VisionTagConsumer.kt | 2 +- .../dataforge/vision/gdml/GdmlTransformer.kt | 2 +- .../dataforge/vision/gdml/TestConvertor.kt | 10 ++-- .../hep/dataforge/vision/gdml/TestCubes.kt | 24 +++++++- .../dataforge/vision/plotly/VisionOfPlotly.kt | 2 + .../dataforge/vision/solid/SolidReference.kt | 10 +++- 9 files changed, 86 insertions(+), 46 deletions(-) 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 42bc2bc1..cd485580 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 @@ -1,13 +1,17 @@ package hep.dataforge.playground -import hep.dataforge.context.Context +import hep.dataforge.misc.DFExperimental +import hep.dataforge.vision.Vision +import hep.dataforge.vision.VisionForge import hep.dataforge.vision.VisionManager -import hep.dataforge.vision.gdml.gdml -import hep.dataforge.vision.html.* -import hep.dataforge.vision.plotly.PlotlyPlugin -import hep.dataforge.vision.plotly.VisionOfPlotly -import hep.dataforge.vision.solid.SolidManager -import hep.dataforge.vision.solid.solid +import hep.dataforge.vision.gdml.toVision +import hep.dataforge.vision.html.HtmlVisionFragment +import hep.dataforge.vision.html.Page +import hep.dataforge.vision.html.embedVisionFragment +import hep.dataforge.vision.html.fragment +import hep.dataforge.vision.plotly.toVision +import hep.dataforge.vision.plotly.withPlotly +import hep.dataforge.vision.solid.withSolids import hep.dataforge.vision.visionManager import kotlinx.html.div import kotlinx.html.id @@ -15,22 +19,16 @@ import kotlinx.html.script import kotlinx.html.stream.createHTML import kotlinx.html.unsafe import kscience.plotly.Plot -import kscience.plotly.PlotlyFragment import org.jetbrains.kotlinx.jupyter.api.HTML import org.jetbrains.kotlinx.jupyter.api.Notebook import org.jetbrains.kotlinx.jupyter.api.annotations.JupyterLibrary import org.jetbrains.kotlinx.jupyter.api.libraries.* -import org.jetbrains.kotlinx.jupyter.api.libraries.ResourceLocation import space.kscience.gdml.Gdml @JupyterLibrary +@DFExperimental internal class VisionForgePlayGroundForJupyter : JupyterIntegration() { - private val context = Context("Playground") { - plugin(SolidManager) - plugin(PlotlyPlugin) - } - val jsBundle = ResourceFallbacksBundle(listOf(ResourceLocation("js/visionforge-playground.js", ResourcePathType.CLASSPATH_PATH))) val jsResource = LibraryResource(name = "VisionForge", type = ResourceType.JS, bundles = listOf(jsBundle)) @@ -41,7 +39,7 @@ internal class VisionForgePlayGroundForJupyter : JupyterIntegration() { val id = "visionforge.vision[${counter++}]" div { this.id = id - embedVisionFragment(context.visionManager, fragment = fragment) + embedVisionFragment(VisionForge.visionManager, fragment = fragment) } script { type = "text/javascript" @@ -52,15 +50,27 @@ internal class VisionForgePlayGroundForJupyter : JupyterIntegration() { override fun Builder.onLoaded(notebook: Notebook?) { resource(jsResource) - import("space.kscience.gdml.*", "kscience.plotly.*", "kscience.plotly.models.*") + onLoaded { + VisionForge.withPlotly().withSolids() + } + import("space.kscience.gdml.*", + "kscience.plotly.*", + "kscience.plotly.models.*", + "kotlinx.html.*", + "hep.dataforge.vision.solid.*") + import("hep.dataforge.vision.VisionForge") render { gdmlModel -> val fragment = VisionManager.fragment { - vision { - solid { - gdml(gdmlModel) - } - } + vision(gdmlModel.toVision()) + } + + HTML(produceHtmlVisionString(fragment)) + } + + render { vision -> + val fragment = VisionManager.fragment { + vision(vision) } HTML(produceHtmlVisionString(fragment)) @@ -68,12 +78,10 @@ internal class VisionForgePlayGroundForJupyter : JupyterIntegration() { render { plot -> val fragment = VisionManager.fragment { - vision { - VisionOfPlotly(plot.config) - } + vision(plot.toVision()) } - HTML( produceHtmlVisionString(fragment)) + HTML(produceHtmlVisionString(fragment)) } render { fragment -> @@ -83,8 +91,6 @@ internal class VisionForgePlayGroundForJupyter : JupyterIntegration() { render { page -> HTML(page.render(createHTML()), true) } - - } } diff --git a/demo/playground/src/jvmMain/kotlin/gdmlCubes.kt b/demo/playground/src/jvmMain/kotlin/gdmlCubes.kt index 8a2d609f..a3d27b5f 100644 --- a/demo/playground/src/jvmMain/kotlin/gdmlCubes.kt +++ b/demo/playground/src/jvmMain/kotlin/gdmlCubes.kt @@ -3,10 +3,9 @@ package hep.dataforge.vision.examples import hep.dataforge.misc.DFExperimental import hep.dataforge.vision.VisionForge import hep.dataforge.vision.VisionManager -import hep.dataforge.vision.gdml.gdml +import hep.dataforge.vision.gdml.toVision import hep.dataforge.vision.html.ResourceLocation import hep.dataforge.vision.html.fragment -import hep.dataforge.vision.solid.solid import hep.dataforge.vision.solid.withSolids import space.kscience.gdml.* @@ -62,11 +61,11 @@ internal val cubes = Gdml { @DFExperimental fun main() { + println(cubes.encodeToString()) + val content = VisionManager.fragment { vision("canvas") { - solid { - gdml(cubes) - } + cubes.toVision() } } diff --git a/demo/playground/src/jvmMain/kotlin/serverExtensions.kt b/demo/playground/src/jvmMain/kotlin/serverExtensions.kt index 7008e4f3..eb26d5e4 100644 --- a/demo/playground/src/jvmMain/kotlin/serverExtensions.kt +++ b/demo/playground/src/jvmMain/kotlin/serverExtensions.kt @@ -2,6 +2,7 @@ package hep.dataforge.vision.examples import hep.dataforge.context.Context import hep.dataforge.misc.DFExperimental +import hep.dataforge.vision.Vision import hep.dataforge.vision.html.HtmlVisionFragment import hep.dataforge.vision.html.ResourceLocation import hep.dataforge.vision.html.page @@ -30,6 +31,12 @@ public fun Context.makeVisionFile( } if (show) Desktop.getDesktop().browse(actualPath.toFile().toURI()) } -// makeVisionFile(fragment, path = path, title = title, show = show) { actualPath -> -// scriptHeader("js/visionforge-playground.js", actualPath, resourceLocation) -//} + +@DFExperimental +public fun Context.makeVisionFile( + vision: Vision, + path: Path? = null, + title: String = "VisionForge page", + resourceLocation: ResourceLocation = ResourceLocation.SYSTEM, + show: Boolean = true, +): Unit = makeVisionFile({ vision(vision) }, path, title, resourceLocation, show) 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 9f3db8c1..4a279f3b 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 @@ -11,7 +11,6 @@ import hep.dataforge.vision.Vision import hep.dataforge.vision.VisionManager import kotlinx.html.* import kotlin.collections.set -import kotlin.collections.setOf /** @@ -46,6 +45,7 @@ public abstract class VisionTagConsumer( /** * Create a placeholder for a vision output with optional [Vision] in it + * TODO with multi-receivers could be replaced by [VisionTagConsumer, TagConsumer] extension */ public fun TagConsumer.vision( name: Name, diff --git a/visionforge-gdml/src/commonMain/kotlin/hep/dataforge/vision/gdml/GdmlTransformer.kt b/visionforge-gdml/src/commonMain/kotlin/hep/dataforge/vision/gdml/GdmlTransformer.kt index 098edded..c1fef40b 100644 --- a/visionforge-gdml/src/commonMain/kotlin/hep/dataforge/vision/gdml/GdmlTransformer.kt +++ b/visionforge-gdml/src/commonMain/kotlin/hep/dataforge/vision/gdml/GdmlTransformer.kt @@ -332,7 +332,7 @@ private class GdmlTransformer(val settings: GdmlTransformerSettings) { private fun finalize(final: SolidGroup): SolidGroup { //final.prototypes = proto - final.useStyle("Gdml") { + final.useStyle("gdml") { Solid.ROTATION_ORDER_KEY put RotationOrder.ZXY } 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 d599cf07..16a1e202 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 @@ -2,8 +2,8 @@ package hep.dataforge.vision.gdml import hep.dataforge.names.toName import hep.dataforge.vision.solid.SolidManager -import space.kscience.gdml.Gdml import org.junit.jupiter.api.Test +import space.kscience.gdml.Gdml import space.kscience.gdml.decodeFromStream import kotlin.test.assertNotNull @@ -11,7 +11,7 @@ class TestConvertor { @Test fun testBMNGeometry() { - val stream = javaClass.getResourceAsStream("/gdml/BM@N.gdml") + val stream = javaClass.getResourceAsStream("/gdml/BM@N.gdml")!! val gdml = Gdml.decodeFromStream(stream) val vision = gdml.toVision() //println(SolidManager.encodeToString(vision)) @@ -19,16 +19,16 @@ class TestConvertor { @Test fun testCubes() { - val stream = javaClass.getResourceAsStream("/gdml/cubes.gdml") + val stream = javaClass.getResourceAsStream("/gdml/cubes.gdml")!! val gdml = Gdml.decodeFromStream(stream) val vision = gdml.toVision() assertNotNull(vision.getPrototype("solids.box".toName())) - //println(SolidManager.encodeToString(vision)) + println(SolidManager.encodeToString(vision)) } @Test fun testSimple() { - val stream = javaClass.getResourceAsStream("/gdml/simple1.gdml") + val stream = javaClass.getResourceAsStream("/gdml/simple1.gdml")!! val gdml = Gdml.decodeFromStream(stream) val vision = gdml.toVision() //println(SolidManager.encodeToString(vision)) 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 adaca24a..4d451ddb 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 @@ -1,13 +1,23 @@ package hep.dataforge.vision.gdml +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.visionManager import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertDoesNotThrow import space.kscience.gdml.* import kotlin.test.assertNotNull +internal val testContext = Context("TEST"){ + plugin(SolidManager) +} + class TestCubes { + internal val cubes = Gdml { val center = define.position("center") structure { @@ -68,7 +78,19 @@ class TestCubes { fun testCubesReSerialize(){ val vision = cubes.toVision() val serialized = SolidManager.encodeToString(vision) - val deserialized = SolidManager.decodeFromString(serialized) as SolidGroup + val deserialized = testContext.visionManager.decodeFromString(serialized) as SolidGroup assertNotNull(deserialized.getPrototype("solids.smallBox".toName())) + //println(testContext.visionManager.encodeToString(deserialized)) + fun Vision.checkPrototypes(){ + if(this is SolidReference){ + assertDoesNotThrow { this.prototype } + } + if(this is SolidGroup){ + children.forEach { + it.value.checkPrototypes() + } + } + } + deserialized.checkPrototypes() } } \ No newline at end of file diff --git a/visionforge-plotly/src/commonMain/kotlin/hep/dataforge/vision/plotly/VisionOfPlotly.kt b/visionforge-plotly/src/commonMain/kotlin/hep/dataforge/vision/plotly/VisionOfPlotly.kt index 3fd82a3a..7c6f527b 100644 --- a/visionforge-plotly/src/commonMain/kotlin/hep/dataforge/vision/plotly/VisionOfPlotly.kt +++ b/visionforge-plotly/src/commonMain/kotlin/hep/dataforge/vision/plotly/VisionOfPlotly.kt @@ -13,5 +13,7 @@ public class VisionOfPlotly(private val plotConfig: Config) : VisionBase(plotCon public val plot: Plot get() = Plot(plotConfig) } +public fun Plot.toVision(): VisionOfPlotly = VisionOfPlotly(config) + @DFExperimental public inline fun VisionOutput.plotly(block: Plot.() -> Unit): VisionOfPlotly = VisionOfPlotly(Plotly.plot(block).config) \ No newline at end of file diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidReference.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidReference.kt index fd6ddbfa..c70db5f8 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidReference.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidReference.kt @@ -44,8 +44,12 @@ public class SolidReferenceGroup( * Recursively search for defined template in the parent */ override val prototype: Solid - get() = (parent as? SolidGroup)?.getPrototype(refName) - ?: error("Prototype with name $refName not found in $parent") + get() { + if(parent == null) error("No parent is present for SolidReferenceGroup") + if(parent !is SolidGroup) error("Reference parent is not a group") + return (parent as? SolidGroup)?.getPrototype(refName) + ?: error("Prototype with name $refName not found") + } override val children: Map get() = (prototype as? VisionGroup)?.children @@ -71,7 +75,7 @@ public class SolidReferenceGroup( private fun prototypeFor(name: Name): Solid { return if (name.isEmpty()) prototype else { (prototype as? SolidGroup)?.get(name) as? Solid - ?: error("Prototype with name $name not found in $this") + ?: error("Prototype with name $name not found in SolidReferenceGroup $refName") } }