Test for node builder. Fixed bug with node content resolve by name
This commit is contained in:
parent
530b1c1b76
commit
74b5a1ac50
@ -1,8 +1,6 @@
|
|||||||
package hep.dataforge.data
|
package hep.dataforge.data
|
||||||
|
|
||||||
import hep.dataforge.meta.EmptyMeta
|
import hep.dataforge.meta.*
|
||||||
import hep.dataforge.meta.Meta
|
|
||||||
import hep.dataforge.meta.MetaRepr
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlin.coroutines.CoroutineContext
|
import kotlin.coroutines.CoroutineContext
|
||||||
import kotlin.coroutines.EmptyCoroutineContext
|
import kotlin.coroutines.EmptyCoroutineContext
|
||||||
@ -21,7 +19,12 @@ interface Data<out T : Any> : Goal<T>, MetaRepr {
|
|||||||
*/
|
*/
|
||||||
val meta: Meta
|
val meta: Meta
|
||||||
|
|
||||||
override fun toMeta(): Meta = meta
|
override fun toMeta(): Meta = buildMeta {
|
||||||
|
"type" to (type.simpleName?:"undefined")
|
||||||
|
if(!meta.isEmpty()) {
|
||||||
|
"meta" to meta
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val TYPE = "data"
|
const val TYPE = "data"
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
package hep.dataforge.data
|
package hep.dataforge.data
|
||||||
|
|
||||||
|
import hep.dataforge.meta.Meta
|
||||||
|
import hep.dataforge.meta.MetaRepr
|
||||||
|
import hep.dataforge.meta.buildMeta
|
||||||
import hep.dataforge.names.*
|
import hep.dataforge.names.*
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
@ -9,22 +12,26 @@ import kotlin.collections.component2
|
|||||||
import kotlin.collections.set
|
import kotlin.collections.set
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
sealed class DataItem<out T : Any> {
|
sealed class DataItem<out T : Any> : MetaRepr {
|
||||||
abstract val type: KClass<out T>
|
abstract val type: KClass<out T>
|
||||||
|
|
||||||
class Node<out T : Any>(val value: DataNode<T>) : DataItem<T>() {
|
class Node<out T : Any>(val value: DataNode<T>) : DataItem<T>() {
|
||||||
override val type: KClass<out T> get() = value.type
|
override val type: KClass<out T> get() = value.type
|
||||||
|
|
||||||
|
override fun toMeta(): Meta = value.toMeta()
|
||||||
}
|
}
|
||||||
|
|
||||||
class Leaf<out T : Any>(val value: Data<T>) : DataItem<T>() {
|
class Leaf<out T : Any>(val value: Data<T>) : DataItem<T>() {
|
||||||
override val type: KClass<out T> get() = value.type
|
override val type: KClass<out T> get() = value.type
|
||||||
|
|
||||||
|
override fun toMeta(): Meta = value.toMeta()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A tree-like data structure grouped into the node. All data inside the node must inherit its type
|
* A tree-like data structure grouped into the node. All data inside the node must inherit its type
|
||||||
*/
|
*/
|
||||||
interface DataNode<out T : Any> {
|
interface DataNode<out T : Any>: MetaRepr {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The minimal common ancestor to all data in the node
|
* The minimal common ancestor to all data in the node
|
||||||
@ -33,6 +40,15 @@ interface DataNode<out T : Any> {
|
|||||||
|
|
||||||
val items: Map<NameToken, DataItem<T>>
|
val items: Map<NameToken, DataItem<T>>
|
||||||
|
|
||||||
|
override fun toMeta(): Meta = buildMeta {
|
||||||
|
"type" to (type.simpleName?:"undefined")
|
||||||
|
"items" to {
|
||||||
|
this@DataNode.items.forEach {
|
||||||
|
it.key.toString() to it.value.toMeta()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val TYPE = "dataNode"
|
const val TYPE = "dataNode"
|
||||||
|
|
||||||
@ -68,7 +84,7 @@ fun DataNode<*>.joinAll(scope: CoroutineScope): Job = scope.launch {
|
|||||||
|
|
||||||
operator fun <T : Any> DataNode<T>.get(name: Name): DataItem<T>? = when (name.length) {
|
operator fun <T : Any> DataNode<T>.get(name: Name): DataItem<T>? = when (name.length) {
|
||||||
0 -> error("Empty name")
|
0 -> error("Empty name")
|
||||||
1 -> (items[name.first()] as? DataItem.Leaf)
|
1 -> items[name.first()]
|
||||||
else -> get(name.first()!!.asName()).node?.get(name.cutFirst())
|
else -> get(name.first()!!.asName()).node?.get(name.cutFirst())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,7 +126,9 @@ class DataTree<out T : Any> internal constructor(
|
|||||||
override val type: KClass<out T>,
|
override val type: KClass<out T>,
|
||||||
override val items: Map<NameToken, DataItem<T>>
|
override val items: Map<NameToken, DataItem<T>>
|
||||||
) : DataNode<T> {
|
) : DataNode<T> {
|
||||||
//TODO add node-level meta?
|
override fun toString(): String {
|
||||||
|
return super.toString()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private sealed class DataTreeBuilderItem<out T : Any> {
|
private sealed class DataTreeBuilderItem<out T : Any> {
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
package hep.dataforge.data
|
||||||
|
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
|
|
||||||
|
internal class DataTreeBuilderTest{
|
||||||
|
@Test
|
||||||
|
fun testDataUpdate(){
|
||||||
|
val updateData = DataNode.build(Any::class){
|
||||||
|
"update" to {
|
||||||
|
"a" to Data.static("a")
|
||||||
|
"b" to Data.static("b")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val node = DataNode.build(Any::class){
|
||||||
|
"primary" to {
|
||||||
|
"a" to Data.static("a")
|
||||||
|
"b" to Data.static("b")
|
||||||
|
}
|
||||||
|
"root" to Data.static("root")
|
||||||
|
update(updateData)
|
||||||
|
}
|
||||||
|
|
||||||
|
println(node.toMeta())
|
||||||
|
|
||||||
|
assertTrue { node["update.a"] != null }
|
||||||
|
assertTrue { node["primary.a"] != null }
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user