From 2a395f80644bc4ada7471f4320536227ff8555b2 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 19 May 2019 10:05:21 +0300 Subject: [PATCH] Test for plugin task loading --- .../kotlin/hep/dataforge/context/Context.kt | 4 +- .../kotlin/hep/dataforge/provider/Provider.kt | 5 +- .../dataforge/descriptors/NodeDescriptor.kt | 4 +- .../dataforge/workspace/SimpleWorkspace.kt | 4 +- .../dataforge/workspace/WorkspaceBuilder.kt | 2 +- .../hep/dataforge/workspace/TaskBuilder.kt | 2 +- .../workspace/SimpleWorkspaceTest.kt | 47 ++++++++++++++++++- 7 files changed, 58 insertions(+), 10 deletions(-) diff --git a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/Context.kt b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/Context.kt index cf6bb662..8d355173 100644 --- a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/Context.kt +++ b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/Context.kt @@ -116,11 +116,11 @@ open class Context(final override val name: String, val parent: Context? = Globa } } +fun Context.content(target: String): Map = content(target) + /** * A sequences of all objects provided by plugins with given target and type */ -fun Context.content(target: String): Map = content(target) - @JvmName("typedContent") inline fun Context.content(target: String): Map = plugins.flatMap { plugin -> diff --git a/dataforge-context/src/commonMain/kotlin/hep/dataforge/provider/Provider.kt b/dataforge-context/src/commonMain/kotlin/hep/dataforge/provider/Provider.kt index 657f272d..35f1aca0 100644 --- a/dataforge-context/src/commonMain/kotlin/hep/dataforge/provider/Provider.kt +++ b/dataforge-context/src/commonMain/kotlin/hep/dataforge/provider/Provider.kt @@ -85,11 +85,12 @@ inline fun Provider.provide(target: String, name: Name): T? { inline fun Provider.provide(target: String, name: String): T? = provide(target, name.toName()) + +fun Provider.top(target: String): Map = top(target) + /** * A top level content with names */ -fun Provider.top(target: String): Map = top(target) - @JvmName("typedTop") inline fun Provider.top(target: String): Map { return listNames(target).associate { diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/descriptors/NodeDescriptor.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/descriptors/NodeDescriptor.kt index 902c81a3..238cc865 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/descriptors/NodeDescriptor.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/descriptors/NodeDescriptor.kt @@ -104,7 +104,7 @@ class NodeDescriptor(override val config: Config) : Specific { */ val nodes: Map get() = config.getAll("node".toName()).entries.associate { (name, node) -> - name to NodeDescriptor.wrap(node.node ?: error("Node descriptor must be a node")) + name to wrap(node.node ?: error("Node descriptor must be a node")) } @@ -114,7 +114,7 @@ class NodeDescriptor(override val config: Config) : Specific { } fun node(name: String, block: NodeDescriptor.() -> Unit) { - node(name, NodeDescriptor.build { this.name = name }.apply(block)) + node(name, build { this.name = name }.apply(block)) } diff --git a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/SimpleWorkspace.kt b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/SimpleWorkspace.kt index c7a54f3d..d707545d 100644 --- a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/SimpleWorkspace.kt +++ b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/SimpleWorkspace.kt @@ -2,6 +2,7 @@ package hep.dataforge.workspace import hep.dataforge.context.Context import hep.dataforge.context.Global +import hep.dataforge.context.content import hep.dataforge.data.DataNode import hep.dataforge.meta.Meta import hep.dataforge.names.Name @@ -18,8 +19,9 @@ class SimpleWorkspace( override val targets: Map, tasks: Collection> ) : Workspace { + override val tasks: Map> by lazy { - context.top>(Task.TYPE) + tasks.associate { it.name.toName() to it } + context.content>(Task.TYPE) + tasks.associate { it.name.toName() to it } } companion object { diff --git a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/WorkspaceBuilder.kt b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/WorkspaceBuilder.kt index f62753a2..b8df2fef 100644 --- a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/WorkspaceBuilder.kt +++ b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/WorkspaceBuilder.kt @@ -26,7 +26,7 @@ interface WorkspaceBuilder { /** * Set the context for future workspcace */ -fun WorkspaceBuilder.context(name: String, block: ContextBuilder.() -> Unit = {}) { +fun WorkspaceBuilder.context(name: String = "WORKSPACE", block: ContextBuilder.() -> Unit = {}) { context = ContextBuilder(name, parentContext).apply(block).build() } diff --git a/dataforge-workspace/src/jvmMain/kotlin/hep/dataforge/workspace/TaskBuilder.kt b/dataforge-workspace/src/jvmMain/kotlin/hep/dataforge/workspace/TaskBuilder.kt index 761dc8f3..d2e1b864 100644 --- a/dataforge-workspace/src/jvmMain/kotlin/hep/dataforge/workspace/TaskBuilder.kt +++ b/dataforge-workspace/src/jvmMain/kotlin/hep/dataforge/workspace/TaskBuilder.kt @@ -207,7 +207,7 @@ class TaskBuilder(val name: String) { } } -fun task(name: String, builder: TaskBuilder.() -> Unit): GenericTask { +fun Workspace.Companion.task(name: String, builder: TaskBuilder.() -> Unit): GenericTask { return TaskBuilder(name).apply(builder).build() } diff --git a/dataforge-workspace/src/jvmTest/kotlin/hep/dataforge/workspace/SimpleWorkspaceTest.kt b/dataforge-workspace/src/jvmTest/kotlin/hep/dataforge/workspace/SimpleWorkspaceTest.kt index 82bb2eb7..ff57c53b 100644 --- a/dataforge-workspace/src/jvmTest/kotlin/hep/dataforge/workspace/SimpleWorkspaceTest.kt +++ b/dataforge-workspace/src/jvmTest/kotlin/hep/dataforge/workspace/SimpleWorkspaceTest.kt @@ -1,16 +1,52 @@ package hep.dataforge.workspace +import hep.dataforge.context.AbstractPlugin +import hep.dataforge.context.PluginTag import hep.dataforge.data.first import hep.dataforge.data.get import hep.dataforge.meta.boolean import hep.dataforge.meta.get +import hep.dataforge.names.Name +import hep.dataforge.names.toName import org.junit.Test import kotlin.test.assertEquals +import kotlin.test.assertTrue class SimpleWorkspaceTest { + val testPlugin = object : AbstractPlugin() { + override val tag: PluginTag = PluginTag("test") + + val contextTask = Workspace.task("test") { + pipe { + context.logger.info { "Test: $it" } + } + } + + override fun provideTop(target: String, name: Name): Any? { + return if (target == Task.TYPE && name == "test".toName()) { + contextTask + } else { + null + } + } + + override fun listNames(target: String): Sequence { + return if(target== Task.TYPE){ + sequenceOf(contextTask.name.toName()) + } else{ + emptySequence() + } + } + + } + val workspace = SimpleWorkspace.build { + context{ + plugin(testPlugin) + } + repeat(100) { static("myData[$it]", it) } @@ -67,6 +103,8 @@ class SimpleWorkspaceTest { data["even"]!! - data["odd"]!! } } + + target("empty") {} } @Test @@ -78,7 +116,14 @@ class SimpleWorkspaceTest { @Test fun testMetaPropagation() { - val node = workspace.run("sum"){"testFlag" to true} + val node = workspace.run("sum") { "testFlag" to true } val res = node.first().get() } + + @Test + fun testPluginTask() { + val tasks = workspace.tasks + assertTrue { tasks["test.test"] != null } + //val node = workspace.run("test.test", "empty") + } } \ No newline at end of file