Simplified task builder.

This commit is contained in:
Alexander Nozik 2019-03-30 10:36:02 +03:00
parent 55db973cd6
commit 58e65ba71e
3 changed files with 63 additions and 6 deletions

View File

@ -48,6 +48,14 @@ class JoinGroupBuilder<T : Any, R : Any>(val actionMeta: Meta) {
} }
} }
fun group(groupName: String, filter: (Name, Data<T>) -> Boolean, action: JoinGroup<T, R>.() -> Unit) {
groupRules += { node ->
listOf(
JoinGroup<T, R>(groupName, node.filter(filter)).apply(action)
)
}
}
/** /**
* Apply transformation to the whole node * Apply transformation to the whole node
*/ */
@ -99,3 +107,5 @@ class JoinAction<T : Any, R : Any>(
} }
} }
} }
operator fun <T> Map<Name,T>.get(name:String) = get(name.toName())

View File

@ -127,7 +127,10 @@ class KTaskBuilder(val name: String) {
class TaskEnv(val name: Name, val meta: Meta, val context: Context) class TaskEnv(val name: Name, val meta: Meta, val context: Context)
inline fun <reified T : Any, reified R : Any> pipeAction( /**
* A customized pipe action with ability to change meta and name
*/
inline fun <reified T : Any, reified R : Any> customPipe(
from: String = "", from: String = "",
to: String = "", to: String = "",
crossinline block: PipeBuilder<T, R>.(Context) -> Unit crossinline block: PipeBuilder<T, R>.(Context) -> Unit
@ -141,6 +144,9 @@ class KTaskBuilder(val name: String) {
} }
} }
/**
* A simple pipe action without changing meta or name
*/
inline fun <reified T : Any, reified R : Any> pipe( inline fun <reified T : Any, reified R : Any> pipe(
from: String = "", from: String = "",
to: String = "", to: String = "",
@ -152,6 +158,7 @@ class KTaskBuilder(val name: String) {
inputType = T::class, inputType = T::class,
outputType = R::class outputType = R::class
) { ) {
//TODO automatically append task meta
result = { data -> result = { data ->
TaskEnv(name, meta, context).block(data) TaskEnv(name, meta, context).block(data)
} }
@ -159,7 +166,10 @@ class KTaskBuilder(val name: String) {
} }
} }
inline fun <reified T : Any, reified R : Any> joinAction( /**
* Join elements in gathered data by multiple groups
*/
inline fun <reified T : Any, reified R : Any> joinByGroup(
from: String = "", from: String = "",
to: String = "", to: String = "",
crossinline block: JoinGroupBuilder<T, R>.(Context) -> Unit crossinline block: JoinGroupBuilder<T, R>.(Context) -> Unit
@ -172,6 +182,9 @@ class KTaskBuilder(val name: String) {
} }
} }
/**
* Join all elemlents in gathered data matching input type
*/
inline fun <reified T : Any, reified R : Any> join( inline fun <reified T : Any, reified R : Any> join(
from: String = "", from: String = "",
to: String = "", to: String = "",
@ -183,7 +196,7 @@ class KTaskBuilder(val name: String) {
inputType = T::class, inputType = T::class,
outputType = R::class, outputType = R::class,
action = { action = {
result(actionMeta[TaskModel.MODEL_TARGET_KEY]?.string ?: "@anonimous") { data -> result(actionMeta[TaskModel.MODEL_TARGET_KEY]?.string ?: "@anonymous") { data ->
TaskEnv(name, meta, context).block(data) TaskEnv(name, meta, context).block(data)
} }
} }
@ -191,7 +204,10 @@ class KTaskBuilder(val name: String) {
} }
} }
inline fun <reified T : Any, reified R : Any> splitAction( /**
* Split each element in gathered data into fixed number of fragments
*/
inline fun <reified T : Any, reified R : Any> split(
from: String = "", from: String = "",
to: String = "", to: String = "",
crossinline block: SplitBuilder<T, R>.(Context) -> Unit crossinline block: SplitBuilder<T, R>.(Context) -> Unit
@ -211,7 +227,7 @@ class KTaskBuilder(val name: String) {
this.descriptor = NodeDescriptor.build(transform) this.descriptor = NodeDescriptor.build(transform)
} }
fun build(): GenericTask<Any> = internal fun build(): GenericTask<Any> =
GenericTask(name, Any::class, descriptor ?: NodeDescriptor.empty(), modelTransform) { GenericTask(name, Any::class, descriptor ?: NodeDescriptor.empty(), modelTransform) {
val workspace = this val workspace = this
{ data -> { data ->
@ -245,3 +261,5 @@ fun task(name: String, builder: KTaskBuilder.() -> Unit): GenericTask<Any> {
fun WorkspaceBuilder.task(name: String, builder: KTaskBuilder.() -> Unit) { fun WorkspaceBuilder.task(name: String, builder: KTaskBuilder.() -> Unit) {
task(KTaskBuilder(name).apply(builder).build()) task(KTaskBuilder(name).apply(builder).build())
} }
//TODO add delegates to build gradle-like tasks

View File

@ -33,6 +33,35 @@ class SimpleWorkspaceTest {
data.values.sum() data.values.sum()
} }
} }
task("average") {
model {
allData()
}
joinByGroup<Int, Double> { context ->
group("even", filter = { name, data -> name.toString().toInt() % 2 == 0 }) {
result { data ->
context.logger.info { "Starting even" }
data.values.average()
}
}
group("odd", filter = { name, data -> name.toString().toInt() % 2 == 1 }) {
result { data ->
context.logger.info { "Starting odd" }
data.values.average()
}
}
}
}
task("delta"){
model{
dependsOn("average")
}
join<Double,Double> {data->
data["even"]!! - data["odd"]!!
}
}
} }
@Test @Test