Introduced a WorkspacePlugin to access tasks from plugin easily
This commit is contained in:
parent
2a395f8064
commit
343ba84118
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
@ -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,28 +19,12 @@ 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 {
|
||||
|
||||
context{
|
||||
context {
|
||||
plugin(testPlugin)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user