From 2291072e2638aadbbaf1f27138a96b2606d65f16 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 26 Jan 2021 16:13:31 +0300 Subject: [PATCH] Cleanup after task refactoring --- .../hep/dataforge/{data => actions}/Action.kt | 11 ++++- .../dataforge/{data => actions}/MapAction.kt | 3 +- .../dataforge/{data => actions}/NamedData.kt | 4 +- .../{data => actions}/ReduceAction.kt | 14 +------ .../{data => actions}/SplitAction.kt | 3 +- .../hep/dataforge/data/CachingAction.kt | 2 + .../kotlin/hep/dataforge/data/DataSet.kt | 2 + .../hep/dataforge/data/DataSetBuilder.kt | 1 + .../kotlin/hep/dataforge/data/DataTree.kt | 2 + .../kotlin/hep/dataforge/data/dataFilter.kt | 2 + .../hep/dataforge/data/dataTransform.kt | 41 +++++++++++-------- .../kotlin/hep/dataforge/data/select.kt | 2 + .../kotlin/hep/dataforge/data/ActionsTest.kt | 1 + .../kotlin/hep/dataforge/workspace/Task.kt | 10 ++--- .../hep/dataforge/workspace/TaskData.kt | 2 +- .../workspace/SimpleWorkspaceTest.kt | 1 + 16 files changed, 60 insertions(+), 41 deletions(-) rename dataforge-data/src/commonMain/kotlin/hep/dataforge/{data => actions}/Action.kt (79%) rename dataforge-data/src/commonMain/kotlin/hep/dataforge/{data => actions}/MapAction.kt (97%) rename dataforge-data/src/commonMain/kotlin/hep/dataforge/{data => actions}/NamedData.kt (89%) rename dataforge-data/src/commonMain/kotlin/hep/dataforge/{data => actions}/ReduceAction.kt (89%) rename dataforge-data/src/commonMain/kotlin/hep/dataforge/{data => actions}/SplitAction.kt (97%) diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Action.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/actions/Action.kt similarity index 79% rename from dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Action.kt rename to dataforge-data/src/commonMain/kotlin/hep/dataforge/actions/Action.kt index 3b72c718..a7c51e8f 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/Action.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/actions/Action.kt @@ -1,5 +1,7 @@ -package hep.dataforge.data +package hep.dataforge.actions +import hep.dataforge.data.DataSet +import hep.dataforge.meta.DFExperimental import hep.dataforge.meta.Meta import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.* @@ -31,3 +33,10 @@ public infix fun Action.then(action: Action DataSet.transformWith( + action: Action, + meta: Meta = Meta.EMPTY, + scope: CoroutineScope? = null, +): DataSet = action.execute(this, meta, scope) + diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/MapAction.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/actions/MapAction.kt similarity index 97% rename from dataforge-data/src/commonMain/kotlin/hep/dataforge/data/MapAction.kt rename to dataforge-data/src/commonMain/kotlin/hep/dataforge/actions/MapAction.kt index cfd37f0a..45632b43 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/MapAction.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/actions/MapAction.kt @@ -1,5 +1,6 @@ -package hep.dataforge.data +package hep.dataforge.actions +import hep.dataforge.data.* import hep.dataforge.meta.* import hep.dataforge.names.Name import kotlinx.coroutines.CoroutineScope diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/NamedData.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/actions/NamedData.kt similarity index 89% rename from dataforge-data/src/commonMain/kotlin/hep/dataforge/data/NamedData.kt rename to dataforge-data/src/commonMain/kotlin/hep/dataforge/actions/NamedData.kt index aa5afcdc..126c972b 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/NamedData.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/actions/NamedData.kt @@ -1,5 +1,7 @@ -package hep.dataforge.data +package hep.dataforge.actions +import hep.dataforge.data.Data +import hep.dataforge.data.StaticData import hep.dataforge.meta.isEmpty import hep.dataforge.misc.Named import hep.dataforge.names.Name diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/ReduceAction.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/actions/ReduceAction.kt similarity index 89% rename from dataforge-data/src/commonMain/kotlin/hep/dataforge/data/ReduceAction.kt rename to dataforge-data/src/commonMain/kotlin/hep/dataforge/actions/ReduceAction.kt index b326b344..7bc4ab07 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/ReduceAction.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/actions/ReduceAction.kt @@ -1,5 +1,6 @@ -package hep.dataforge.data +package hep.dataforge.actions +import hep.dataforge.data.* import hep.dataforge.meta.DFExperimental import hep.dataforge.meta.Meta import hep.dataforge.meta.MetaBuilder @@ -44,17 +45,6 @@ public class ReduceGroupBuilder( } } -// /** -// * Add a single fixed group to grouping rules -// */ -// public fun group(groupName: String, filter: DataMapper, action: JoinGroup.() -> Unit) { -// groupRules += { node -> -// listOf( -// JoinGroup(groupName, node.filter(filter)).apply(action) -// ) -// } -// } - public fun group( groupName: String, filter: suspend (Name, Data) -> Boolean, diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/SplitAction.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/actions/SplitAction.kt similarity index 97% rename from dataforge-data/src/commonMain/kotlin/hep/dataforge/data/SplitAction.kt rename to dataforge-data/src/commonMain/kotlin/hep/dataforge/actions/SplitAction.kt index 3d8fca5a..aaec3836 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/SplitAction.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/actions/SplitAction.kt @@ -1,5 +1,6 @@ -package hep.dataforge.data +package hep.dataforge.actions +import hep.dataforge.data.* import hep.dataforge.meta.Laminate import hep.dataforge.meta.Meta import hep.dataforge.meta.MetaBuilder diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/CachingAction.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/CachingAction.kt index 5f8f8704..0e4ddd97 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/CachingAction.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/CachingAction.kt @@ -1,5 +1,7 @@ package hep.dataforge.data +import hep.dataforge.actions.Action +import hep.dataforge.actions.NamedData import hep.dataforge.meta.Meta import hep.dataforge.names.Name import hep.dataforge.names.startsWith diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/DataSet.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/DataSet.kt index db20fb16..c401b275 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/DataSet.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/DataSet.kt @@ -1,5 +1,7 @@ package hep.dataforge.data +import hep.dataforge.actions.NamedData +import hep.dataforge.actions.named import hep.dataforge.meta.Meta import hep.dataforge.meta.set import hep.dataforge.names.* diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/DataSetBuilder.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/DataSetBuilder.kt index 5a0a3b55..6e3669b7 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/DataSetBuilder.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/DataSetBuilder.kt @@ -1,5 +1,6 @@ package hep.dataforge.data +import hep.dataforge.actions.NamedData import hep.dataforge.meta.DFExperimental import hep.dataforge.meta.Meta import hep.dataforge.meta.MetaBuilder diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/DataTree.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/DataTree.kt index 659ff029..0354b161 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/DataTree.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/DataTree.kt @@ -1,5 +1,7 @@ package hep.dataforge.data +import hep.dataforge.actions.NamedData +import hep.dataforge.actions.named import hep.dataforge.meta.* import hep.dataforge.misc.Type import hep.dataforge.names.* diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/dataFilter.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/dataFilter.kt index 5f2b2a8a..4d500587 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/dataFilter.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/dataFilter.kt @@ -1,5 +1,7 @@ package hep.dataforge.data +import hep.dataforge.actions.NamedData +import hep.dataforge.actions.named import hep.dataforge.meta.DFExperimental import hep.dataforge.names.* import kotlinx.coroutines.flow.Flow diff --git a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/dataTransform.kt b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/dataTransform.kt index 39f5f306..31e30fb1 100644 --- a/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/dataTransform.kt +++ b/dataforge-data/src/commonMain/kotlin/hep/dataforge/data/dataTransform.kt @@ -1,5 +1,7 @@ package hep.dataforge.data +import hep.dataforge.actions.NamedData +import hep.dataforge.actions.named import hep.dataforge.meta.Meta import hep.dataforge.meta.MetaBuilder import hep.dataforge.meta.seal @@ -11,36 +13,41 @@ import kotlin.coroutines.CoroutineContext import kotlin.coroutines.EmptyCoroutineContext import kotlin.reflect.KClass - +/** + * Lazily transform this data to another data. By convention [block] should not use external data (be pure). + * @param coroutineContext additional [CoroutineContext] elements used for data computation. + * @param meta for the resulting data. By default equals input data. + * @param block the transformation itself + */ public fun Data.map( outputType: KClass, coroutineContext: CoroutineContext = EmptyCoroutineContext, meta: Meta = this.meta, block: suspend (T) -> R, -): Data = LazyData(outputType, meta, coroutineContext, listOf(this)) { +): LazyData = LazyData(outputType, meta, coroutineContext, listOf(this)) { block(await()) } /** - * Create a data mapping + * See [map] */ public inline fun Data.map( coroutineContext: CoroutineContext = EmptyCoroutineContext, meta: Meta = this.meta, crossinline block: suspend (T) -> R, -): Data = LazyData(R::class, meta, coroutineContext, listOf(this)) { +): LazyData = LazyData(R::class, meta, coroutineContext, listOf(this)) { block(await()) } /** - * Combine this data with the other data using [block] + * Combine this data with the other data using [block]. See [map] for other details */ -public inline fun Data.combine( +public inline fun Data.combine( other: Data, coroutineContext: CoroutineContext = EmptyCoroutineContext, meta: Meta = this.meta, crossinline block: suspend (left: T1, right: T2) -> R, -): Data = LazyData(R::class, meta, coroutineContext, listOf(this,other)) { +): LazyData = LazyData(R::class, meta, coroutineContext, listOf(this, other)) { block(await(), other.await()) } @@ -48,19 +55,19 @@ public inline fun Data.combine( //data collection operations /** - * Create a joined data. + * Lazily reduce a collection of [Data] to a single data. */ public inline fun Collection>.reduceToData( coroutineContext: CoroutineContext = EmptyCoroutineContext, meta: Meta = Meta.EMPTY, - noinline block: suspend (Collection) -> R, -): Data = LazyData( + crossinline block: suspend (Collection) -> R, +): LazyData = LazyData( R::class, meta, coroutineContext, this ) { - block(map { run { it.await() } }) + block(map { it.await() }) } public fun Map>.reduceToData( @@ -79,7 +86,7 @@ public fun Map>.reduceToData( /** - * A joining of multiple data into a single one + * Lazily reduce a [Map] of [Data] with any static key. * @param K type of the map key * @param T type of the input goal * @param R type of the result goal @@ -97,8 +104,10 @@ public inline fun Map>.reduceToData( block(mapValues { it.value.await() }) } +//flow operations + /** - * Transform a [Flow] of [NamedData] to a single [Data]. Execution restrictions are removed for inner [Flow] + * Transform a [Flow] of [NamedData] to a single [Data]. */ public suspend fun Flow>.reduceToData( outputType: KClass, @@ -114,8 +123,6 @@ public suspend fun Flow>.reduceToData( transformation(this) } -//flow operations - public suspend inline fun Flow>.reduceToData( coroutineContext: CoroutineContext = EmptyCoroutineContext, meta: Meta = Meta.EMPTY, @@ -145,7 +152,7 @@ public suspend fun DataSet.map( coroutineContext: CoroutineContext = EmptyCoroutineContext, metaTransform: MetaBuilder.() -> Unit = {}, block: suspend (T) -> R, -): DataSet = DataTree(outputType) { +): DataTree = DataTree(outputType) { populate( flow().map { val newMeta = it.meta.toMutableMeta().apply(metaTransform).seal() @@ -158,7 +165,7 @@ public suspend inline fun DataSet.map( coroutineContext: CoroutineContext = EmptyCoroutineContext, noinline metaTransform: MetaBuilder.() -> Unit = {}, noinline block: suspend (T) -> R, -): DataSet = map(R::class, coroutineContext, metaTransform, block) +): DataTree = map(R::class, coroutineContext, metaTransform, block) public suspend fun DataSet.forEach(block: suspend (NamedData) -> Unit) { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } diff --git a/dataforge-data/src/jvmMain/kotlin/hep/dataforge/data/select.kt b/dataforge-data/src/jvmMain/kotlin/hep/dataforge/data/select.kt index 5b30a0d3..5217d1f0 100644 --- a/dataforge-data/src/jvmMain/kotlin/hep/dataforge/data/select.kt +++ b/dataforge-data/src/jvmMain/kotlin/hep/dataforge/data/select.kt @@ -1,5 +1,7 @@ package hep.dataforge.data +import hep.dataforge.actions.NamedData +import hep.dataforge.actions.named import hep.dataforge.meta.DFExperimental import hep.dataforge.names.* import kotlinx.coroutines.flow.Flow diff --git a/dataforge-data/src/jvmTest/kotlin/hep/dataforge/data/ActionsTest.kt b/dataforge-data/src/jvmTest/kotlin/hep/dataforge/data/ActionsTest.kt index 50240b28..686762d9 100644 --- a/dataforge-data/src/jvmTest/kotlin/hep/dataforge/data/ActionsTest.kt +++ b/dataforge-data/src/jvmTest/kotlin/hep/dataforge/data/ActionsTest.kt @@ -1,5 +1,6 @@ package hep.dataforge.data +import hep.dataforge.actions.MapAction import kotlinx.coroutines.runBlocking import org.junit.jupiter.api.Test import kotlin.test.assertEquals diff --git a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/Task.kt b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/Task.kt index f2808cd5..df3c16af 100644 --- a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/Task.kt +++ b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/Task.kt @@ -15,8 +15,6 @@ import kotlin.reflect.KClass @Type(TYPE) public interface Task : Described { - public val type: KClass - /** * Compute a [TaskResult] using given meta. In general, the result is lazy and represents both computation model * and a handler for actual result @@ -36,8 +34,8 @@ public class TaskResultBuilder( public val workspace: Workspace, public val taskName: Name, public val taskMeta: Meta, - private val dataSync: DataSetBuilder, -) : DataSetBuilder by dataSync + private val dataDrop: DataSetBuilder, +) : DataSetBuilder by dataDrop /** * Create a [Task] that composes a result using [builder]. Only data from the workspace could be used. @@ -50,8 +48,6 @@ public fun Task( builder: suspend TaskResultBuilder.() -> Unit, ): Task = object : Task { - override val type: KClass = resultType - override val descriptor: ItemDescriptor? = descriptor override suspend fun execute( @@ -60,7 +56,7 @@ public fun Task( taskMeta: Meta, ): TaskResult = withContext(GoalExecutionRestriction() + workspace.goalLogger) { //TODO use safe builder and check for external data on add and detects cycles - val dataset = DataTree(type) { + val dataset = DataTree(resultType) { TaskResultBuilder(workspace,taskName, taskMeta, this).apply { builder() } } workspace.internalize(dataset, taskName, taskMeta) diff --git a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/TaskData.kt b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/TaskData.kt index f317b00a..37862a48 100644 --- a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/TaskData.kt +++ b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/TaskData.kt @@ -1,7 +1,7 @@ package hep.dataforge.workspace +import hep.dataforge.actions.NamedData import hep.dataforge.data.Data -import hep.dataforge.data.NamedData import hep.dataforge.meta.Meta import hep.dataforge.names.Name 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 2147294b..3b290d4e 100644 --- a/dataforge-workspace/src/jvmTest/kotlin/hep/dataforge/workspace/SimpleWorkspaceTest.kt +++ b/dataforge-workspace/src/jvmTest/kotlin/hep/dataforge/workspace/SimpleWorkspaceTest.kt @@ -1,5 +1,6 @@ package hep.dataforge.workspace +import hep.dataforge.actions.get import hep.dataforge.context.* import hep.dataforge.data.* import hep.dataforge.meta.*