Changed map and reduce action API to include both meta from data and meta from task/action

This commit is contained in:
Alexander Nozik 2019-11-01 14:41:16 +03:00
parent 6e98bc7408
commit 592f911db7
2 changed files with 28 additions and 14 deletions

View File

@ -1,16 +1,19 @@
package hep.dataforge.data package hep.dataforge.data
import hep.dataforge.meta.* import hep.dataforge.meta.Meta
import hep.dataforge.meta.MetaBuilder
import hep.dataforge.meta.builder
import hep.dataforge.meta.seal
import hep.dataforge.names.Name import hep.dataforge.names.Name
import kotlin.reflect.KClass import kotlin.reflect.KClass
class ActionEnv(val name: Name, val meta: Meta) data class ActionEnv(val name: Name, val actionMeta: Meta, val meta: Meta)
/** /**
* Action environment * Action environment
*/ */
class MapActionBuilder<T, R>(var name: Name, var meta: MetaBuilder) { class MapActionBuilder<T, R>(var name: Name, var meta: MetaBuilder, val actionMeta: Meta) {
lateinit var result: suspend ActionEnv.(T) -> R lateinit var result: suspend ActionEnv.(T) -> R
/** /**
@ -33,16 +36,24 @@ class MapAction<T : Any, out R : Any>(
return DataNode.invoke(outputType) { return DataNode.invoke(outputType) {
node.dataSequence().forEach { (name, data) -> node.dataSequence().forEach { (name, data) ->
//merging data meta with action meta (data meta is primary) /*
val oldMeta = meta.builder().apply { update(data.meta) } * Creating a new environment for action using **old** name, old meta and task meta
// creating environment from old meta and name */
val env = ActionEnv(name, oldMeta) val env = ActionEnv(name, meta, data.meta)
//applying transformation from builder //applying transformation from builder
val builder = MapActionBuilder<T, R>(name, oldMeta).apply(block) val builder = MapActionBuilder<T, R>(
name,
data.meta.builder(), // using data meta
meta
).apply(block)
//getting new name //getting new name
val newName = builder.name val newName = builder.name
//getting new meta //getting new meta
val newMeta = builder.meta.seal() val newMeta = builder.meta.seal()
val newData = data.map(outputType, meta = newMeta) { builder.result(env, it) } val newData = data.map(outputType, meta = newMeta) { builder.result(env, it) }
//setting the data node //setting the data node
this[newName] = newData this[newName] = newData

View File

@ -1,9 +1,7 @@
package hep.dataforge.data package hep.dataforge.data
import hep.dataforge.meta.Laminate
import hep.dataforge.meta.Meta import hep.dataforge.meta.Meta
import hep.dataforge.meta.MetaBuilder import hep.dataforge.meta.MetaBuilder
import hep.dataforge.meta.builder
import hep.dataforge.names.Name import hep.dataforge.names.Name
import hep.dataforge.names.toName import hep.dataforge.names.toName
import kotlin.reflect.KClass import kotlin.reflect.KClass
@ -84,15 +82,20 @@ class ReduceAction<T : Any, R : Any>(
return DataNode.invoke(outputType) { return DataNode.invoke(outputType) {
ReduceGroupBuilder<T, R>(meta).apply(action).buildGroups(node).forEach { group -> ReduceGroupBuilder<T, R>(meta).apply(action).buildGroups(node).forEach { group ->
val laminate = Laminate(group.meta, meta) //val laminate = Laminate(group.meta, meta)
val dataMap = group.node.dataSequence().associate { it } val dataMap = group.node.dataSequence().associate { it }
val groupName: String = group.name; val groupName: String = group.name
val env = ActionEnv(groupName.toName(), laminate.builder()) val groupMeta = group.meta
val res: DynamicData<R> = dataMap.reduce(outputType, meta = laminate) { group.result.invoke(env, it) } val env = ActionEnv(groupName.toName(), meta, groupMeta)
val res: DynamicData<R> = dataMap.reduce(
outputType,
meta = groupMeta
) { group.result.invoke(env, it) }
set(env.name, res) set(env.name, res)
} }