minor refactoring

This commit is contained in:
Alexander Nozik 2019-03-19 20:13:57 +03:00
parent 2283aaffad
commit 00c9e70c09
8 changed files with 31 additions and 20 deletions

View File

@ -4,6 +4,7 @@ import hep.dataforge.names.Name
import hep.dataforge.names.NameToken import hep.dataforge.names.NameToken
import hep.dataforge.names.plus import hep.dataforge.names.plus
import hep.dataforge.names.toName import hep.dataforge.names.toName
import hep.dataforge.names.asName
/** /**
* 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
@ -50,23 +51,23 @@ class DataTree<out T : Any> internal constructor(private val items: Map<NameToke
override fun get(name: Name): Data<T>? = when (name.length) { override fun get(name: Name): Data<T>? = when (name.length) {
0 -> error("Empty name") 0 -> error("Empty name")
1 -> (items[name.first()] as? DataTreeItem.Value)?.value 1 -> (items[name.first()] as? DataTreeItem.Value)?.value
else -> getNode(name.first()!!.toName())?.get(name.cutFirst()) else -> getNode(name.first()!!.asName())?.get(name.cutFirst())
} }
override fun getNode(name: Name): DataTree<T>? = when (name.length) { override fun getNode(name: Name): DataTree<T>? = when (name.length) {
0 -> this 0 -> this
1 -> (items[name.first()] as? DataTreeItem.Node)?.tree 1 -> (items[name.first()] as? DataTreeItem.Node)?.tree
else -> getNode(name.first()!!.toName())?.getNode(name.cutFirst()) else -> getNode(name.first()!!.asName())?.getNode(name.cutFirst())
} }
override fun dataSequence(): Sequence<Pair<Name, Data<T>>> { override fun dataSequence(): Sequence<Pair<Name, Data<T>>> {
return sequence { return sequence {
items.forEach { (head, tree) -> items.forEach { (head, tree) ->
when (tree) { when (tree) {
is DataTreeItem.Value -> yield(head.toName() to tree.value) is DataTreeItem.Value -> yield(head.asName() to tree.value)
is DataTreeItem.Node -> { is DataTreeItem.Node -> {
val subSequence = val subSequence =
tree.tree.dataSequence().map { (name, data) -> (head.toName() + name) to data } tree.tree.dataSequence().map { (name, data) -> (head.asName() + name) to data }
yieldAll(subSequence) yieldAll(subSequence)
} }
} }
@ -78,9 +79,9 @@ class DataTree<out T : Any> internal constructor(private val items: Map<NameToke
return sequence { return sequence {
items.forEach { (head, tree) -> items.forEach { (head, tree) ->
if (tree is DataTreeItem.Node) { if (tree is DataTreeItem.Node) {
yield(head.toName() to tree.tree) yield(head.asName() to tree.tree)
val subSequence = val subSequence =
tree.tree.nodeSequence().map { (name, node) -> (head.toName() + name) to node } tree.tree.nodeSequence().map { (name, node) -> (head.asName() + name) to node }
yieldAll(subSequence) yieldAll(subSequence)
} }
} }

View File

@ -2,7 +2,7 @@ package hep.dataforge.meta
import hep.dataforge.names.Name import hep.dataforge.names.Name
import hep.dataforge.names.NameToken import hep.dataforge.names.NameToken
import hep.dataforge.names.toName import hep.dataforge.names.asName
//TODO add validator to configuration //TODO add validator to configuration
@ -28,7 +28,7 @@ operator fun Config.get(token: NameToken): MetaItem<Config>? = items[token]
fun Meta.toConfig(): Config = this as? Config ?: Config().also { builder -> fun Meta.toConfig(): Config = this as? Config ?: Config().also { builder ->
this.items.mapValues { entry -> this.items.mapValues { entry ->
val item = entry.value val item = entry.value
builder[entry.key.toName()] = when (item) { builder[entry.key.asName()] = when (item) {
is MetaItem.ValueItem -> MetaItem.ValueItem(item.value) is MetaItem.ValueItem -> MetaItem.ValueItem(item.value)
is MetaItem.NodeItem -> MetaItem.NodeItem(item.node.toConfig()) is MetaItem.NodeItem -> MetaItem.NodeItem(item.node.toConfig())
} }

View File

@ -7,6 +7,7 @@ import hep.dataforge.names.Name
import hep.dataforge.names.NameToken import hep.dataforge.names.NameToken
import hep.dataforge.names.plus import hep.dataforge.names.plus
import hep.dataforge.names.toName import hep.dataforge.names.toName
import hep.dataforge.names.asName
import hep.dataforge.values.EnumValue import hep.dataforge.values.EnumValue
import hep.dataforge.values.Value import hep.dataforge.values.Value
import hep.dataforge.values.boolean import hep.dataforge.values.boolean
@ -93,8 +94,8 @@ fun Meta.asValueSequence(): Sequence<Pair<Name, Value>> {
return items.asSequence().flatMap { entry -> return items.asSequence().flatMap { entry ->
val item = entry.value val item = entry.value
when (item) { when (item) {
is ValueItem -> sequenceOf(entry.key.toName() to item.value) is ValueItem -> sequenceOf(entry.key.asName() to item.value)
is NodeItem -> item.node.asValueSequence().map { pair -> (entry.key.toName() + pair.first) to pair.second } is NodeItem -> item.node.asValueSequence().map { pair -> (entry.key.asName() + pair.first) to pair.second }
} }
} }
} }

View File

@ -1,7 +1,7 @@
package hep.dataforge.meta package hep.dataforge.meta
import hep.dataforge.names.Name import hep.dataforge.names.Name
import hep.dataforge.names.toName import hep.dataforge.names.asName
import hep.dataforge.values.Value import hep.dataforge.values.Value
/** /**
@ -38,7 +38,7 @@ fun Meta.builder(): MetaBuilder {
return MetaBuilder().also { builder -> return MetaBuilder().also { builder ->
items.mapValues { entry -> items.mapValues { entry ->
val item = entry.value val item = entry.value
builder[entry.key.toName()] = when (item) { builder[entry.key.asName()] = when (item) {
is MetaItem.ValueItem -> MetaItem.ValueItem<MetaBuilder>(item.value) is MetaItem.ValueItem -> MetaItem.ValueItem<MetaBuilder>(item.value)
is MetaItem.NodeItem -> MetaItem.NodeItem(item.node.builder()) is MetaItem.NodeItem -> MetaItem.NodeItem(item.node.builder())
} }

View File

@ -4,6 +4,7 @@ import hep.dataforge.names.Name
import hep.dataforge.names.NameToken import hep.dataforge.names.NameToken
import hep.dataforge.names.plus import hep.dataforge.names.plus
import hep.dataforge.names.toName import hep.dataforge.names.toName
import hep.dataforge.names.asName
import hep.dataforge.values.Value import hep.dataforge.values.Value
internal data class MetaListener( internal data class MetaListener(
@ -62,7 +63,7 @@ abstract class MutableMetaNode<M : MutableMetaNode<M>> : AbstractMetaNode<M>(),
} }
} }
} }
itemChanged(key.toName(), oldItem, newItem) itemChanged(key.asName(), oldItem, newItem)
} }
/** /**
@ -103,7 +104,7 @@ fun <M : MutableMeta<M>> MutableMeta<M>.setItem(name: String, item: MetaItem<M>)
fun <M : MutableMeta<M>> MutableMeta<M>.setValue(name: String, value: Value) = fun <M : MutableMeta<M>> MutableMeta<M>.setValue(name: String, value: Value) =
set(name.toName(), MetaItem.ValueItem(value)) set(name.toName(), MetaItem.ValueItem(value))
fun <M : MutableMeta<M>> MutableMeta<M>.setItem(token: NameToken, item: MetaItem<M>?) = set(token.toName(), item) fun <M : MutableMeta<M>> MutableMeta<M>.setItem(token: NameToken, item: MetaItem<M>?) = set(token.asName(), item)
fun <M : MutableMetaNode<M>> MutableMetaNode<M>.setNode(name: Name, node: Meta) = fun <M : MutableMetaNode<M>> MutableMetaNode<M>.setNode(name: Name, node: Meta) =
set(name, MetaItem.NodeItem(wrap(name, node))) set(name, MetaItem.NodeItem(wrap(name, node)))
@ -125,7 +126,7 @@ operator fun <M : MutableMetaNode<M>> M.set(name: Name, value: Any?) {
} }
} }
operator fun <M : MutableMetaNode<M>> M.set(name: NameToken, value: Any?) = set(Name(key), value) operator fun <M : MutableMetaNode<M>> M.set(name: NameToken, value: Any?) = set(name.asName(), value)
operator fun <M : MutableMetaNode<M>> M.set(key: String, value: Any?) = set(key.toName(), value) operator fun <M : MutableMetaNode<M>> M.set(key: String, value: Any?) = set(key.toName(), value)
@ -139,9 +140,9 @@ fun <M : MutableMetaNode<M>> M.update(meta: Meta) {
meta.items.forEach { entry -> meta.items.forEach { entry ->
val value = entry.value val value = entry.value
when (value) { when (value) {
is MetaItem.ValueItem -> setValue(entry.key.toName(), value.value) is MetaItem.ValueItem -> setValue(entry.key.asName(), value.value)
is MetaItem.NodeItem -> (this[entry.key.toName()] as? MetaItem.NodeItem)?.node?.update(value.node) is MetaItem.NodeItem -> (this[entry.key.asName()] as? MetaItem.NodeItem)?.node?.update(value.node)
?: run { setNode(entry.key.toName(), value.node) } ?: run { setNode(entry.key.asName(), value.node) }
} }
} }
} }

View File

@ -101,7 +101,7 @@ operator fun Name.plus(other: Name): Name = Name(this.tokens + other.tokens)
operator fun Name.plus(other: String): Name = this + other.toName() operator fun Name.plus(other: String): Name = this + other.toName()
fun NameToken.toName() = Name(listOf(this)) fun NameToken.asName() = Name(listOf(this))
val EmptyName = Name(emptyList()) val EmptyName = Name(emptyList())

View File

@ -1,7 +1,14 @@
package hep.dataforge.meta package hep.dataforge.meta
//TODO add Meta wrapper for dynamic
/**
* Represent or copy this [Meta] to dynamic object to be passed to JS libraries
*/
fun Meta.toDynamic(): dynamic { fun Meta.toDynamic(): dynamic {
//if(this is DynamicMeta) return this.obj
fun MetaItem<*>.toDynamic(): dynamic = when (this) { fun MetaItem<*>.toDynamic(): dynamic = when (this) {
is MetaItem.ValueItem -> this.value.value.asDynamic() is MetaItem.ValueItem -> this.value.value.asDynamic()
is MetaItem.NodeItem -> this.node.toDynamic() is MetaItem.NodeItem -> this.node.toDynamic()

View File

@ -8,6 +8,7 @@ import hep.dataforge.output.TextRenderer.Companion.TEXT_RENDERER_TYPE
import hep.dataforge.output.html.HtmlBuilder.Companion.HTML_CONVERTER_TYPE import hep.dataforge.output.html.HtmlBuilder.Companion.HTML_CONVERTER_TYPE
import hep.dataforge.provider.Type import hep.dataforge.provider.Type
import hep.dataforge.provider.provideAll import hep.dataforge.provider.provideAll
import hep.dataforge.provider.provideAllByType
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.html.TagConsumer import kotlinx.html.TagConsumer
@ -28,7 +29,7 @@ class HtmlOutput<T : Any>(override val context: Context, private val consumer: T
} else { } else {
val value = cache[obj::class] val value = cache[obj::class]
if (value == null) { if (value == null) {
val answer = context.provideAll(HTML_CONVERTER_TYPE).filterIsInstance<HtmlBuilder<*>>() val answer = context.provideAllByType<HtmlBuilder<*>>()
.filter { it.type.isInstance(obj) }.firstOrNull() .filter { it.type.isInstance(obj) }.firstOrNull()
if (answer != null) { if (answer != null) {
cache[obj::class] = answer cache[obj::class] = answer