From 6298d741ffbf4299d7f7ae7abf7f59e8e3c9365a Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 8 Apr 2019 14:39:40 +0300 Subject: [PATCH] Moved GenericTask to common --- .../hep/dataforge/workspace/GenericTask.kt | 61 +++++++++++++++ .../{GenericTask.kt => TaskBuilder.kt} | 75 ++++--------------- 2 files changed, 75 insertions(+), 61 deletions(-) create mode 100644 dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/GenericTask.kt rename dataforge-workspace/src/jvmMain/kotlin/hep/dataforge/workspace/{GenericTask.kt => TaskBuilder.kt} (72%) diff --git a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/GenericTask.kt b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/GenericTask.kt new file mode 100644 index 00000000..278098d1 --- /dev/null +++ b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/GenericTask.kt @@ -0,0 +1,61 @@ +package hep.dataforge.workspace + +import hep.dataforge.data.* +import hep.dataforge.descriptors.NodeDescriptor +import hep.dataforge.meta.Meta +import hep.dataforge.meta.get +import hep.dataforge.meta.node +import kotlin.reflect.KClass + +//data class TaskEnv(val workspace: Workspace, val model: TaskModel) + + +class GenericTask( + override val name: String, + override val type: KClass, + override val descriptor: NodeDescriptor, + private val modelTransform: TaskModelBuilder.(Meta) -> Unit, + private val dataTransform: Workspace.() -> TaskModel.(DataNode) -> DataNode +) : Task { + + private fun gather(workspace: Workspace, model: TaskModel): DataNode { + return DataNode.build(Any::class) { + model.dependencies.forEach { dep -> + update(dep.apply(workspace)) + } + } + } + + override fun run(workspace: Workspace, model: TaskModel): DataNode { + //validate model + validate(model) + + // gather data + val input = gather(workspace, model) + + //execute + workspace.context.logger.info{"Starting task '$name' on ${model.target} with meta: \n${model.meta}"} + val output = dataTransform(workspace).invoke(model, input) + + //handle result + //output.handle(model.context.dispatcher) { this.handle(it) } + + return output + } + + /** + * Build new TaskModel and apply specific model transformation for this + * task. By default model uses the meta node with the same node as the name of the task. + * + * @param workspace + * @param taskConfig + * @return + */ + override fun build(workspace: Workspace, taskConfig: Meta): TaskModel { + val taskMeta = taskConfig[name]?.node ?: taskConfig + val builder = TaskModelBuilder(name, taskMeta) + modelTransform.invoke(builder, taskMeta) + return builder.build() + } + //TODO add validation +} \ No newline at end of file diff --git a/dataforge-workspace/src/jvmMain/kotlin/hep/dataforge/workspace/GenericTask.kt b/dataforge-workspace/src/jvmMain/kotlin/hep/dataforge/workspace/TaskBuilder.kt similarity index 72% rename from dataforge-workspace/src/jvmMain/kotlin/hep/dataforge/workspace/GenericTask.kt rename to dataforge-workspace/src/jvmMain/kotlin/hep/dataforge/workspace/TaskBuilder.kt index 4d3ec219..761dc8f3 100644 --- a/dataforge-workspace/src/jvmMain/kotlin/hep/dataforge/workspace/GenericTask.kt +++ b/dataforge-workspace/src/jvmMain/kotlin/hep/dataforge/workspace/TaskBuilder.kt @@ -5,67 +5,13 @@ import hep.dataforge.data.* import hep.dataforge.descriptors.NodeDescriptor import hep.dataforge.meta.Meta import hep.dataforge.meta.get -import hep.dataforge.meta.node import hep.dataforge.meta.string import hep.dataforge.names.Name import hep.dataforge.names.toName import kotlin.reflect.KClass -//data class TaskEnv(val workspace: Workspace, val model: TaskModel) - - -class GenericTask( - override val name: String, - override val type: KClass, - override val descriptor: NodeDescriptor, - private val modelTransform: TaskModelBuilder.(Meta) -> Unit, - private val dataTransform: Workspace.() -> TaskModel.(DataNode) -> DataNode -) : Task { - - private fun gather(workspace: Workspace, model: TaskModel): DataNode { - return DataNode.build(Any::class) { - model.dependencies.forEach { dep -> - update(dep.apply(workspace)) - } - } - } - - override fun run(workspace: Workspace, model: TaskModel): DataNode { - //validate model - validate(model) - - // gather data - val input = gather(workspace, model) - - //execute - workspace.context.logger.info("Starting task '$name' on ${model.target} with meta: \n${model.meta}") - val output = dataTransform(workspace).invoke(model, input) - - //handle result - //output.handle(model.context.dispatcher) { this.handle(it) } - - return output - } - - /** - * Build new TaskModel and apply specific model transformation for this - * task. By default model uses the meta node with the same node as the name of the task. - * - * @param workspace - * @param taskConfig - * @return - */ - override fun build(workspace: Workspace, taskConfig: Meta): TaskModel { - val taskMeta = taskConfig[name]?.node ?: taskConfig - val builder = TaskModelBuilder(name, taskMeta) - modelTransform.invoke(builder, taskMeta) - return builder.build() - } - //TODO add validation -} - @TaskBuildScope -class KTaskBuilder(val name: String) { +class TaskBuilder(val name: String) { private var modelTransform: TaskModelBuilder.(Meta) -> Unit = { data("*") } var descriptor: NodeDescriptor? = null @@ -196,7 +142,9 @@ class KTaskBuilder(val name: String) { inputType = T::class, outputType = R::class, action = { - result(actionMeta[TaskModel.MODEL_TARGET_KEY]?.string ?: "@anonymous") { data -> + result( + actionMeta[TaskModel.MODEL_TARGET_KEY]?.string ?: "@anonymous" + ) { data -> TaskEnv(name, meta, context).block(data) } } @@ -228,7 +176,12 @@ class KTaskBuilder(val name: String) { } internal fun build(): GenericTask = - GenericTask(name, Any::class, descriptor ?: NodeDescriptor.empty(), modelTransform) { + GenericTask( + name, + Any::class, + descriptor ?: NodeDescriptor.empty(), + modelTransform + ) { val workspace = this { data -> val model = this @@ -254,12 +207,12 @@ class KTaskBuilder(val name: String) { } } -fun task(name: String, builder: KTaskBuilder.() -> Unit): GenericTask { - return KTaskBuilder(name).apply(builder).build() +fun task(name: String, builder: TaskBuilder.() -> Unit): GenericTask { + return TaskBuilder(name).apply(builder).build() } -fun WorkspaceBuilder.task(name: String, builder: KTaskBuilder.() -> Unit) { - task(KTaskBuilder(name).apply(builder).build()) +fun WorkspaceBuilder.task(name: String, builder: TaskBuilder.() -> Unit) { + task(TaskBuilder(name).apply(builder).build()) } //TODO add delegates to build gradle-like tasks \ No newline at end of file