minor data refactor

This commit is contained in:
Alexander Nozik 2021-02-02 12:09:44 +03:00
parent 11ba116a89
commit 6a0bfae931
12 changed files with 89 additions and 89 deletions

View File

@ -1,6 +1,6 @@
name: Gradle build name: Gradle build
on: [push] on: [ push ]
jobs: jobs:
build: build:

View File

@ -39,7 +39,7 @@ public class ReduceGroupBuilder<T : Any, R : Any>(
*/ */
public fun byValue(tag: String, defaultTag: String = "@default", action: JoinGroup<T, R>.() -> Unit) { public fun byValue(tag: String, defaultTag: String = "@default", action: JoinGroup<T, R>.() -> Unit) {
groupRules += { node -> groupRules += { node ->
GroupRule.byValue(scope, tag, defaultTag).gather(inputType, node).map { GroupRule.byMetaValue(scope, tag, defaultTag).gather(inputType, node).map {
JoinGroup<T, R>(it.key, it.value).apply(action) JoinGroup<T, R>(it.key, it.value).apply(action)
} }
} }

View File

@ -88,7 +88,7 @@ public suspend fun <T : Any> DataSetBuilder<T>.emit(data: NamedData<T>) {
/** /**
* Produce lazy [Data] and emit it into the [DataSetBuilder] * Produce lazy [Data] and emit it into the [DataSetBuilder]
*/ */
public suspend inline fun <reified T : Any> DataSetBuilder<T>.emitLazy( public suspend inline fun <reified T : Any> DataSetBuilder<T>.produce(
name: String, name: String,
meta: Meta = Meta.EMPTY, meta: Meta = Meta.EMPTY,
noinline producer: suspend () -> T, noinline producer: suspend () -> T,
@ -97,7 +97,7 @@ public suspend inline fun <reified T : Any> DataSetBuilder<T>.emitLazy(
emit(name, data) emit(name, data)
} }
public suspend inline fun <reified T : Any> DataSetBuilder<T>.emitLazy( public suspend inline fun <reified T : Any> DataSetBuilder<T>.produce(
name: Name, name: Name,
meta: Meta = Meta.EMPTY, meta: Meta = Meta.EMPTY,
noinline producer: suspend () -> T, noinline producer: suspend () -> T,
@ -109,13 +109,13 @@ public suspend inline fun <reified T : Any> DataSetBuilder<T>.emitLazy(
/** /**
* Emit a static data with the fixed value * Emit a static data with the fixed value
*/ */
public suspend fun <T : Any> DataSetBuilder<T>.emitStatic(name: String, data: T, meta: Meta = Meta.EMPTY): Unit = public suspend fun <T : Any> DataSetBuilder<T>.static(name: String, data: T, meta: Meta = Meta.EMPTY): Unit =
emit(name, Data.static(data, meta)) emit(name, Data.static(data, meta))
public suspend fun <T : Any> DataSetBuilder<T>.emitStatic(name: Name, data: T, meta: Meta = Meta.EMPTY): Unit = public suspend fun <T : Any> DataSetBuilder<T>.static(name: Name, data: T, meta: Meta = Meta.EMPTY): Unit =
emit(name, Data.static(data, meta)) emit(name, Data.static(data, meta))
public suspend fun <T : Any> DataSetBuilder<T>.emitStatic( public suspend fun <T : Any> DataSetBuilder<T>.static(
name: String, name: String,
data: T, data: T,
metaBuilder: MetaBuilder.() -> Unit, metaBuilder: MetaBuilder.() -> Unit,

View File

@ -19,6 +19,7 @@ import hep.dataforge.meta.get
import hep.dataforge.meta.string import hep.dataforge.meta.string
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlin.reflect.KClass import kotlin.reflect.KClass
public interface GroupRule { public interface GroupRule {
@ -33,7 +34,7 @@ public interface GroupRule {
* @param defaultTagValue * @param defaultTagValue
* @return * @return
*/ */
public fun byValue( public fun byMetaValue(
scope: CoroutineScope, scope: CoroutineScope,
key: String, key: String,
defaultTagValue: String, defaultTagValue: String,
@ -50,31 +51,16 @@ public interface GroupRule {
map.getOrPut(tagValue) { ActiveDataTree(dataType) }.emit(data.name, data.data) map.getOrPut(tagValue) { ActiveDataTree(dataType) }.emit(data.name, data.data)
} }
scope.launch {
set.updates.collect { name ->
val data = set.getData(name)
val tagValue = data?.meta[key]?.string ?: defaultTagValue
map.getOrPut(tagValue) { ActiveDataTree(dataType) }.emit(name, data)
}
}
return map return map
} }
} }
// @ValueDef(key = "byValue", required = true, info = "The name of annotation value by which grouping should be made")
// @ValueDef(
// key = "defaultValue",
// def = "default",
// info = "Default value which should be used for content in which the grouping value is not presented"
// )
// public fun byMeta(scope: CoroutineScope, config: Meta): GroupRule {
// //TODO expand grouping options
// return config["byValue"]?.string?.let {
// byValue(
// scope,
// it,
// config["defaultValue"]?.string ?: "default"
// )
// } ?: object : GroupRule {
// override suspend fun <T : Any> gather(
// dataType: KClass<T>,
// source: DataSource<T>,
// ): Map<String, DataSource<T>> = mapOf("" to source)
// }
// }
} }
} }

View File

@ -2,7 +2,7 @@ package hep.dataforge.data
import hep.dataforge.names.* import hep.dataforge.names.*
import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.collect
import kotlin.reflect.KClass import kotlin.reflect.KClass
@PublishedApi @PublishedApi
@ -22,7 +22,7 @@ internal class StaticDataTree<T : Any>(
} }
} }
fun getOrCreateNode(name: Name): StaticDataTree<T> = when (name.length) { private fun getOrCreateNode(name: Name): StaticDataTree<T> = when (name.length) {
0 -> this 0 -> this
1 -> { 1 -> {
val itemName = name.firstOrNull()!! val itemName = name.firstOrNull()!!

View File

@ -32,7 +32,6 @@ public fun <T : Any> DataSet<T>.filter(
} }
} }
/** /**
* Generate a wrapper data set with a given name prefix appended to all names * Generate a wrapper data set with a given name prefix appended to all names
*/ */
@ -48,7 +47,6 @@ else object : ActiveDataSet<T> {
override val updates: Flow<Name> get() = this@withNamePrefix.updates.map { prefix + it } override val updates: Flow<Name> get() = this@withNamePrefix.updates.map { prefix + it }
} }
/** /**
* Get a subset of data starting with a given [branchName] * Get a subset of data starting with a given [branchName]
*/ */

View File

@ -14,7 +14,7 @@ class ActionsTest {
val data: DataTree<Int> = runBlocking { val data: DataTree<Int> = runBlocking {
DataTree { DataTree {
repeat(10) { repeat(10) {
emitStatic(it.toString(), it) static(it.toString(), it)
} }
} }
} }

View File

@ -8,6 +8,22 @@ import kotlin.test.assertEquals
internal class DataTreeBuilderTest { internal class DataTreeBuilderTest {
@Test
fun testTreeBuild() = runBlocking {
val node = DataTree<Any> {
"primary" put {
static("a", "a")
static("b", "b")
}
static("c.d", "c.d")
static("c.f", "c.f")
}
assertEquals("a", node.getData("primary.a")?.value())
assertEquals("b", node.getData("primary.b")?.value())
assertEquals("c.d", node.getData("c.d")?.value())
assertEquals("c.f", node.getData("c.f")?.value())
}
@Test @Test
fun testDataUpdate() = runBlocking { fun testDataUpdate() = runBlocking {
val updateData: DataTree<Any> = DataTree { val updateData: DataTree<Any> = DataTree {
@ -18,11 +34,11 @@ internal class DataTreeBuilderTest {
} }
val node = DataTree<Any> { val node = DataTree<Any> {
emit("primary") { "primary" put {
emitStatic("a", "a") static("a", "a")
emitStatic("b", "b") static("b", "b")
} }
emitStatic("root", "root") static("root", "root")
populate(updateData) populate(updateData)
} }
@ -40,7 +56,7 @@ internal class DataTreeBuilderTest {
updateJob = launch { updateJob = launch {
repeat(10) { repeat(10) {
delay(10) delay(10)
emitStatic("value", it) static("value", it)
} }
delay(10) delay(10)
} }

View File

@ -49,7 +49,7 @@ class DataPropagationTest {
runBlocking { runBlocking {
data { data {
repeat(100) { repeat(100) {
emitStatic("myData[$it]", it) static("myData[$it]", it)
} }
} }
} }

View File

@ -23,11 +23,11 @@ class FileDataTest {
val dataNode = runBlocking { val dataNode = runBlocking {
DataTree<String> { DataTree<String> {
emit("dir") { emit("dir") {
emitStatic("a", "Some string") { static("a", "Some string") {
"content" put "Some string" "content" put "Some string"
} }
} }
emitStatic("b", "root data") static("b", "root data")
meta { meta {
"content" put "This is root meta node" "content" put "This is root meta node"
} }

View File

@ -56,7 +56,7 @@ class SimpleWorkspaceTest {
data { data {
repeat(100) { repeat(100) {
emitStatic("myData[$it]", it) static("myData[$it]", it)
} }
} }