Introduced a WorkspacePlugin to access tasks from plugin easily

This commit is contained in:
Alexander Nozik 2019-05-19 10:14:49 +03:00
parent 2a395f8064
commit 343ba84118
2 changed files with 31 additions and 22 deletions

View File

@ -0,0 +1,28 @@
package hep.dataforge.workspace
import hep.dataforge.context.AbstractPlugin
import hep.dataforge.names.Name
import hep.dataforge.names.toName
/**
* An abstract plugin with some additional boilerplate to effectively work with workspace context
*/
abstract class WorkspacePlugin : AbstractPlugin() {
abstract val tasks: Collection<Task<*>>
override fun provideTop(target: String, name: Name): Any? {
return if (target == Task.TYPE) {
tasks.find { it.name == name.toString() }
} else {
super.provideTop(target, name)
}
}
override fun listNames(target: String): Sequence<Name> {
return if (target == Task.TYPE) {
tasks.asSequence().map { it.name.toName() }
} else {
return super.listNames(target)
}
}
}

View File

@ -1,20 +1,17 @@
package hep.dataforge.workspace package hep.dataforge.workspace
import hep.dataforge.context.AbstractPlugin
import hep.dataforge.context.PluginTag 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 import kotlin.test.assertTrue
class SimpleWorkspaceTest { class SimpleWorkspaceTest {
val testPlugin = object : AbstractPlugin() { val testPlugin = object : WorkspacePlugin() {
override val tag: PluginTag = PluginTag("test") override val tag: PluginTag = PluginTag("test")
val contextTask = Workspace.task("test") { val contextTask = Workspace.task("test") {
@ -22,28 +19,12 @@ class SimpleWorkspaceTest {
context.logger.info { "Test: $it" } context.logger.info { "Test: $it" }
} }
} }
override val tasks: Collection<Task<*>> = listOf(contextTask)
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{ context {
plugin(testPlugin) plugin(testPlugin)
} }