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
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() {
val testPlugin = object : WorkspacePlugin() {
override val tag: PluginTag = PluginTag("test")
val contextTask = Workspace.task("test") {
@ -22,23 +19,7 @@ class SimpleWorkspaceTest {
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()
}
}
override val tasks: Collection<Task<*>> = listOf(contextTask)
}
val workspace = SimpleWorkspace.build {