From dce9199b7888b967175b74a29f0f5e00b693109d Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 29 Oct 2019 20:21:38 +0300 Subject: [PATCH] #18 --- .../kotlin/hep/dataforge/data/Data.kt | 12 +++++----- .../kotlin/hep/dataforge/data/Goal.kt | 8 +++---- .../data/{PipeAction.kt => MapAction.kt} | 16 ++++++------- .../data/{JoinAction.kt => ReduceAction.kt} | 10 ++++---- .../kotlin/hep/dataforge/data/SplitAction.kt | 2 +- .../hep/dataforge/workspace/TaskBuilder.kt | 24 +++++++++---------- .../workspace/SimpleWorkspaceTest.kt | 16 ++++++------- 7 files changed, 44 insertions(+), 44 deletions(-) rename dataforge-data/src/commonMain/kotlin/hep/dataforge/data/{PipeAction.kt => MapAction.kt} (70%) rename dataforge-data/src/commonMain/kotlin/hep/dataforge/data/{JoinAction.kt => ReduceAction.kt} (87%) diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Data.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Data.kt index bd93ddfc..e15a5210 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Data.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Data.kt @@ -85,7 +85,7 @@ class StaticData( class NamedData(val name: String, data: Data) : Data by data -fun Data.pipe( +fun Data.map( outputType: KClass, coroutineContext: CoroutineContext = EmptyCoroutineContext, meta: Meta = this.meta, @@ -98,7 +98,7 @@ fun Data.pipe( /** * Create a data pipe */ -inline fun Data.pipe( +inline fun Data.map( coroutineContext: CoroutineContext = EmptyCoroutineContext, meta: Meta = this.meta, noinline block: suspend CoroutineScope.(T) -> R @@ -109,7 +109,7 @@ inline fun Data.pipe( /** * Create a joined data. */ -inline fun Collection>.join( +inline fun Collection>.reduce( coroutineContext: CoroutineContext = EmptyCoroutineContext, meta: Meta, noinline block: suspend CoroutineScope.(Collection) -> R @@ -119,10 +119,10 @@ inline fun Collection>.join( coroutineContext, this ) { - block(map { this.run { it.await(this) } }) + block(map { run { it.await(this) } }) } -fun Map>.join( +fun Map>.reduce( outputType: KClass, coroutineContext: CoroutineContext = EmptyCoroutineContext, meta: Meta, @@ -143,7 +143,7 @@ fun Map>.join( * @param T type of the input goal * @param R type of the result goal */ -inline fun Map>.join( +inline fun Map>.reduce( coroutineContext: CoroutineContext = EmptyCoroutineContext, meta: Meta, noinline block: suspend CoroutineScope.(Map) -> R diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Goal.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Goal.kt index 54bb743e..8275d31e 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Goal.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Goal.kt @@ -85,7 +85,7 @@ open class DynamicGoal( /** * Create a one-to-one goal based on existing goal */ -fun Goal.pipe( +fun Goal.map( coroutineContext: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.(T) -> R ): Goal = DynamicGoal(coroutineContext, listOf(this)) { @@ -95,11 +95,11 @@ fun Goal.pipe( /** * Create a joining goal. */ -fun Collection>.join( +fun Collection>.reduce( coroutineContext: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.(Collection) -> R ): Goal = DynamicGoal(coroutineContext, this) { - block(map { this.run { it.await(this) } }) + block(map { run { it.await(this) } }) } /** @@ -108,7 +108,7 @@ fun Collection>.join( * @param T type of the input goal * @param R type of the result goal */ -fun Map>.join( +fun Map>.reduce( coroutineContext: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.(Map) -> R ): Goal = DynamicGoal(coroutineContext, this.values) { diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/PipeAction.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/MapAction.kt similarity index 70% rename from dataforge-data/src/commonMain/kotlin/hep/dataforge/data/PipeAction.kt rename to dataforge-data/src/commonMain/kotlin/hep/dataforge/data/MapAction.kt index 91a204d8..b184a2a8 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/PipeAction.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/MapAction.kt @@ -10,7 +10,7 @@ class ActionEnv(val name: Name, val meta: Meta) /** * Action environment */ -class PipeBuilder(var name: Name, var meta: MetaBuilder) { +class MapActionBuilder(var name: Name, var meta: MetaBuilder) { lateinit var result: suspend ActionEnv.(T) -> R /** @@ -22,10 +22,10 @@ class PipeBuilder(var name: Name, var meta: MetaBuilder) { } -class PipeAction( +class MapAction( val inputType: KClass, val outputType: KClass, - private val block: PipeBuilder.() -> Unit + private val block: MapActionBuilder.() -> Unit ) : Action { override fun invoke(node: DataNode, meta: Meta): DataNode { @@ -38,12 +38,12 @@ class PipeAction( // creating environment from old meta and name val env = ActionEnv(name, oldMeta) //applying transformation from builder - val builder = PipeBuilder(name, oldMeta).apply(block) + val builder = MapActionBuilder(name, oldMeta).apply(block) //getting new name val newName = builder.name //getting new meta val newMeta = builder.meta.seal() - val newData = data.pipe(outputType, meta = newMeta) { builder.result(env, it) } + val newData = data.map(outputType, meta = newMeta) { builder.result(env, it) } //setting the data node this[newName] = newData } @@ -51,10 +51,10 @@ class PipeAction( } } -inline fun DataNode.pipe( +inline fun DataNode.map( meta: Meta, - noinline action: PipeBuilder.() -> Unit -): DataNode = PipeAction(T::class, R::class, action).invoke(this, meta) + noinline action: MapActionBuilder.() -> Unit +): DataNode = MapAction(T::class, R::class, action).invoke(this, meta) diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/JoinAction.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/ReduceAction.kt similarity index 87% rename from dataforge-data/src/commonMain/kotlin/hep/dataforge/data/JoinAction.kt rename to dataforge-data/src/commonMain/kotlin/hep/dataforge/data/ReduceAction.kt index 7ad4055e..48738452 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/JoinAction.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/ReduceAction.kt @@ -21,7 +21,7 @@ class JoinGroup(var name: String, internal val node: DataNode< } -class JoinGroupBuilder(val actionMeta: Meta) { +class ReduceGroupBuilder(val actionMeta: Meta) { private val groupRules: MutableList<(DataNode) -> List>> = ArrayList(); /** @@ -73,16 +73,16 @@ class JoinGroupBuilder(val actionMeta: Meta) { /** * The same rules as for KPipe */ -class JoinAction( +class ReduceAction( val inputType: KClass, val outputType: KClass, - private val action: JoinGroupBuilder.() -> Unit + private val action: ReduceGroupBuilder.() -> Unit ) : Action { override fun invoke(node: DataNode, meta: Meta): DataNode { node.ensureType(inputType) return DataNode.invoke(outputType) { - JoinGroupBuilder(meta).apply(action).buildGroups(node).forEach { group -> + ReduceGroupBuilder(meta).apply(action).buildGroups(node).forEach { group -> val laminate = Laminate(group.meta, meta) @@ -92,7 +92,7 @@ class JoinAction( val env = ActionEnv(groupName.toName(), laminate.builder()) - val res: DynamicData = dataMap.join(outputType, meta = laminate) { group.result.invoke(env, it) } + val res: DynamicData = dataMap.reduce(outputType, meta = laminate) { group.result.invoke(env, it) } set(env.name, res) } diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/SplitAction.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/SplitAction.kt index 637baeeb..a4f82931 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/SplitAction.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/SplitAction.kt @@ -55,7 +55,7 @@ class SplitAction( rule(env) - val res = data.pipe(outputType, meta = env.meta) { env.result(it) } + val res = data.map(outputType, meta = env.meta) { env.result(it) } set(env.name, res) } } diff --git a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/TaskBuilder.kt b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/TaskBuilder.kt index 150d56a6..80d89e24 100644 --- a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/TaskBuilder.kt +++ b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/TaskBuilder.kt @@ -105,15 +105,15 @@ class TaskBuilder(val name: Name, val type: KClass) { } /** - * A customized pipe action with ability to change meta and name + * A customized map action with ability to change meta and name */ - inline fun customPipe( + inline fun mapAction( from: String = "", to: String = "", - crossinline block: PipeBuilder.(TaskEnv) -> Unit + crossinline block: MapActionBuilder.(TaskEnv) -> Unit ) { action(from, to) { - PipeAction( + MapAction( inputType = T::class, outputType = type ) { block(this@action) } @@ -121,15 +121,15 @@ class TaskBuilder(val name: Name, val type: KClass) { } /** - * A simple pipe action without changing meta or name + * A simple map action without changing meta or name */ - inline fun pipe( + inline fun map( from: String = "", to: String = "", crossinline block: suspend TaskEnv.(T) -> R ) { action(from, to) { - PipeAction( + MapAction( inputType = T::class, outputType = type ) { @@ -144,13 +144,13 @@ class TaskBuilder(val name: Name, val type: KClass) { /** * Join elements in gathered data by multiple groups */ - inline fun joinByGroup( + inline fun reduceByGroup( from: String = "", to: String = "", - crossinline block: JoinGroupBuilder.(TaskEnv) -> Unit //TODO needs KEEP-176 + crossinline block: ReduceGroupBuilder.(TaskEnv) -> Unit //TODO needs KEEP-176 ) { action(from, to) { - JoinAction( + ReduceAction( inputType = T::class, outputType = type ) { block(this@action) } @@ -160,13 +160,13 @@ class TaskBuilder(val name: Name, val type: KClass) { /** * Join all elemlents in gathered data matching input type */ - inline fun join( + inline fun reduce( from: String = "", to: String = "", crossinline block: suspend TaskEnv.(Map) -> R ) { action(from, to) { - JoinAction( + ReduceAction( inputType = T::class, outputType = type, action = { diff --git a/dataforge-workspace/src/jvmTest/kotlin/hep/dataforge/workspace/SimpleWorkspaceTest.kt b/dataforge-workspace/src/jvmTest/kotlin/hep/dataforge/workspace/SimpleWorkspaceTest.kt index 3059cb29..4d37f5d9 100644 --- a/dataforge-workspace/src/jvmTest/kotlin/hep/dataforge/workspace/SimpleWorkspaceTest.kt +++ b/dataforge-workspace/src/jvmTest/kotlin/hep/dataforge/workspace/SimpleWorkspaceTest.kt @@ -17,7 +17,7 @@ class SimpleWorkspaceTest { override val tag: PluginTag = PluginTag("test") val contextTask = task("test", Any::class) { - pipe { + map { context.logger.info { "Test: $it" } } } @@ -39,13 +39,13 @@ class SimpleWorkspaceTest { model { data("myData\\[12\\]") } - pipe{ + map{ it } } val square = task("square") { - pipe { data -> + map { data -> if (meta["testFlag"].boolean == true) { println("flag") } @@ -55,7 +55,7 @@ class SimpleWorkspaceTest { } val linear = task("linear") { - pipe { data -> + map { data -> context.logger.info { "Starting linear on $data" } data * 2 + 1 } @@ -86,14 +86,14 @@ class SimpleWorkspaceTest { model { dependsOn(square) } - join { data -> + reduce { data -> context.logger.info { "Starting sum" } data.values.sum() } } val average = task("average") { - joinByGroup { env -> + reduceByGroup { env -> group("even", filter = { name, _ -> name.toString().toInt() % 2 == 0 }) { result { data -> env.context.logger.info { "Starting even" } @@ -113,13 +113,13 @@ class SimpleWorkspaceTest { model { dependsOn(average) } - join { data -> + reduce { data -> data["even"]!! - data["odd"]!! } } val customPipeTask = task("custom") { - customPipe { + mapAction { meta = meta.builder().apply { "newValue" to 22 }