Test for plugin task loading

This commit is contained in:
Alexander Nozik 2019-05-19 10:05:21 +03:00
parent f1692297b8
commit 2a395f8064
7 changed files with 58 additions and 10 deletions

View File

@ -116,11 +116,11 @@ open class Context(final override val name: String, val parent: Context? = Globa
} }
} }
fun Context.content(target: String): Map<Name, Any> = content<Any>(target)
/** /**
* A sequences of all objects provided by plugins with given target and type * A sequences of all objects provided by plugins with given target and type
*/ */
fun Context.content(target: String): Map<Name, Any> = content<Any>(target)
@JvmName("typedContent") @JvmName("typedContent")
inline fun <reified T : Any> Context.content(target: String): Map<Name, T> = inline fun <reified T : Any> Context.content(target: String): Map<Name, T> =
plugins.flatMap { plugin -> plugins.flatMap { plugin ->

View File

@ -85,11 +85,12 @@ inline fun <reified T : Any> Provider.provide(target: String, name: Name): T? {
inline fun <reified T : Any> Provider.provide(target: String, name: String): T? = inline fun <reified T : Any> Provider.provide(target: String, name: String): T? =
provide(target, name.toName()) provide(target, name.toName())
fun Provider.top(target: String): Map<Name, Any> = top<Any>(target)
/** /**
* A top level content with names * A top level content with names
*/ */
fun Provider.top(target: String): Map<Name, Any> = top<Any>(target)
@JvmName("typedTop") @JvmName("typedTop")
inline fun <reified T : Any> Provider.top(target: String): Map<Name, T> { inline fun <reified T : Any> Provider.top(target: String): Map<Name, T> {
return listNames(target).associate { return listNames(target).associate {

View File

@ -104,7 +104,7 @@ class NodeDescriptor(override val config: Config) : Specific {
*/ */
val nodes: Map<String, NodeDescriptor> val nodes: Map<String, NodeDescriptor>
get() = config.getAll("node".toName()).entries.associate { (name, node) -> 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) { fun node(name: String, block: NodeDescriptor.() -> Unit) {
node(name, NodeDescriptor.build { this.name = name }.apply(block)) node(name, build { this.name = name }.apply(block))
} }

View File

@ -2,6 +2,7 @@ package hep.dataforge.workspace
import hep.dataforge.context.Context import hep.dataforge.context.Context
import hep.dataforge.context.Global import hep.dataforge.context.Global
import hep.dataforge.context.content
import hep.dataforge.data.DataNode import hep.dataforge.data.DataNode
import hep.dataforge.meta.Meta import hep.dataforge.meta.Meta
import hep.dataforge.names.Name import hep.dataforge.names.Name
@ -18,8 +19,9 @@ class SimpleWorkspace(
override val targets: Map<String, Meta>, override val targets: Map<String, Meta>,
tasks: Collection<Task<Any>> tasks: Collection<Task<Any>>
) : Workspace { ) : Workspace {
override val tasks: Map<Name, Task<*>> by lazy { override val tasks: Map<Name, Task<*>> by lazy {
context.top<Task<*>>(Task.TYPE) + tasks.associate { it.name.toName() to it } context.content<Task<*>>(Task.TYPE) + tasks.associate { it.name.toName() to it }
} }
companion object { companion object {

View File

@ -26,7 +26,7 @@ interface WorkspaceBuilder {
/** /**
* Set the context for future workspcace * 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() context = ContextBuilder(name, parentContext).apply(block).build()
} }

View File

@ -207,7 +207,7 @@ class TaskBuilder(val name: String) {
} }
} }
fun task(name: String, builder: TaskBuilder.() -> Unit): GenericTask<Any> { fun Workspace.Companion.task(name: String, builder: TaskBuilder.() -> Unit): GenericTask<Any> {
return TaskBuilder(name).apply(builder).build() return TaskBuilder(name).apply(builder).build()
} }

View File

@ -1,16 +1,52 @@
package hep.dataforge.workspace package hep.dataforge.workspace
import hep.dataforge.context.AbstractPlugin
import hep.dataforge.context.PluginTag
import hep.dataforge.data.first import hep.dataforge.data.first
import hep.dataforge.data.get import hep.dataforge.data.get
import hep.dataforge.meta.boolean import hep.dataforge.meta.boolean
import hep.dataforge.meta.get import hep.dataforge.meta.get
import hep.dataforge.names.Name
import hep.dataforge.names.toName
import org.junit.Test import org.junit.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertTrue
class SimpleWorkspaceTest { class SimpleWorkspaceTest {
val testPlugin = object : AbstractPlugin() {
override val tag: PluginTag = PluginTag("test")
val contextTask = Workspace.task("test") {
pipe<Any, Unit> {
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<Name> {
return if(target== Task.TYPE){
sequenceOf(contextTask.name.toName())
} else{
emptySequence()
}
}
}
val workspace = SimpleWorkspace.build { val workspace = SimpleWorkspace.build {
context{
plugin(testPlugin)
}
repeat(100) { repeat(100) {
static("myData[$it]", it) static("myData[$it]", it)
} }
@ -67,6 +103,8 @@ class SimpleWorkspaceTest {
data["even"]!! - data["odd"]!! data["even"]!! - data["odd"]!!
} }
} }
target("empty") {}
} }
@Test @Test
@ -78,7 +116,14 @@ class SimpleWorkspaceTest {
@Test @Test
fun testMetaPropagation() { fun testMetaPropagation() {
val node = workspace.run("sum"){"testFlag" to true} val node = workspace.run("sum") { "testFlag" to true }
val res = node.first().get() val res = node.first().get()
} }
@Test
fun testPluginTask() {
val tasks = workspace.tasks
assertTrue { tasks["test.test"] != null }
//val node = workspace.run("test.test", "empty")
}
} }