Remove attachChildren. Move group children to constructor

This commit is contained in:
Alexander Nozik 2020-12-21 09:49:42 +03:00
parent 0259d4eb15
commit eefc036dcb
9 changed files with 28 additions and 67 deletions

View File

@ -62,6 +62,7 @@ public interface Vision : Described {
* Flow of property invalidation events. It does not contain property values after invalidation since it is not clear * Flow of property invalidation events. It does not contain property values after invalidation since it is not clear
* if it should include inherited properties etc. * if it should include inherited properties etc.
*/ */
@OptIn(ExperimentalCoroutinesApi::class)
public val propertyChanges: Flow<Name> get() = callbackFlow<Name> { public val propertyChanges: Flow<Name> get() = callbackFlow<Name> {
coroutineScope { coroutineScope {
onPropertyChange(this) { onPropertyChange(this) {

View File

@ -66,12 +66,7 @@ public class VisionChange(
public val vision: Vision? = null, public val vision: Vision? = null,
@Serializable(MetaSerializer::class) public val properties: Meta? = null, @Serializable(MetaSerializer::class) public val properties: Meta? = null,
public val children: Map<Name, VisionChange>? = null, public val children: Map<Name, VisionChange>? = null,
) { )
init {
(vision as? VisionGroup)?.attachChildren()
}
}
public inline fun VisionChange(manager: VisionManager, block: VisionChangeBuilder.() -> Unit): VisionChange = public inline fun VisionChange(manager: VisionManager, block: VisionChangeBuilder.() -> Unit): VisionChange =
VisionChangeBuilder().apply(block).isolate(manager) VisionChangeBuilder().apply(block).isolate(manager)

View File

@ -45,16 +45,6 @@ public interface VisionGroup : Provider, Vision, VisionContainer<Vision> {
} }
} }
/**
* A fix for serialization bug that writes all proper parents inside the tree after deserialization
*/
public fun attachChildren() {
children.values.forEach {
it.parent = this
(it as? VisionGroup)?.attachChildren()
}
}
public companion object { public companion object {
public const val STYLE_TARGET: String = "style" public const val STYLE_TARGET: String = "style"
} }

View File

@ -11,23 +11,26 @@ import kotlinx.serialization.Transient
/** /**
* Abstract implementation of mutable group of [Vision] * Abstract implementation of mutable group of [Vision]
*
* @param childrenInternal Internal mutable container for group children
*/ */
@Serializable @Serializable
@SerialName("vision.group") @SerialName("vision.group")
public open class VisionGroupBase : VisionBase(), MutableVisionGroup { public open class VisionGroupBase(
@SerialName("children") internal val childrenInternal: MutableMap<NameToken, Vision> = LinkedHashMap(),
/** ) : VisionBase(), MutableVisionGroup {
* Internal mutable container for group children
* TODO made protected due to [https://github.com/Kotlin/kotlinx.serialization/issues/1200]
*/
@SerialName("children")
protected val childrenInternal: MutableMap<NameToken, Vision> = LinkedHashMap()
/** /**
* A map of top level named children * A map of top level named children
*/ */
override val children: Map<NameToken, Vision> get() = childrenInternal override val children: Map<NameToken, Vision> get() = childrenInternal
init {
childrenInternal.values.forEach {
it.parent = this
}
}
override suspend fun notifyPropertyChanged(propertyName: Name) { override suspend fun notifyPropertyChanged(propertyName: Name) {
super.notifyPropertyChanged(propertyName) super.notifyPropertyChanged(propertyName)
for (obj in this) { for (obj in this) {

View File

@ -30,16 +30,11 @@ public class VisionManager(meta: Meta) : AbstractPlugin(meta) {
serializersModule = this@VisionManager.serializersModule serializersModule = this@VisionManager.serializersModule
} }
public fun decodeFromString(string: String): Vision = jsonFormat.decodeFromString(visionSerializer, string).also { public fun decodeFromString(string: String): Vision = jsonFormat.decodeFromString(visionSerializer, string)
(it as? VisionGroup)?.attachChildren()
}
public fun encodeToString(vision: Vision): String = jsonFormat.encodeToString(visionSerializer, vision) public fun encodeToString(vision: Vision): String = jsonFormat.encodeToString(visionSerializer, vision)
public fun decodeFromJson(json: JsonElement): Vision = public fun decodeFromJson(json: JsonElement): Vision = jsonFormat.decodeFromJsonElement(visionSerializer, json)
jsonFormat.decodeFromJsonElement(visionSerializer, json).also {
(it as? VisionGroup)?.attachChildren()
}
public fun encodeToJsonElement(vision: Vision): JsonElement = public fun encodeToJsonElement(vision: Vision): JsonElement =
jsonFormat.encodeToJsonElement(visionSerializer, vision) jsonFormat.encodeToJsonElement(visionSerializer, vision)

View File

@ -23,21 +23,17 @@ public interface PrototypeHolder {
/** /**
* Represents 3-dimensional Visual Group * Represents 3-dimensional Visual Group
* @param prototypes A container for templates visible inside this group
*/ */
@Serializable @Serializable
@SerialName("group.solid") @SerialName("group.solid")
public class SolidGroup : VisionGroupBase(), Solid, PrototypeHolder { public class SolidGroup(
@Serializable(Prototypes.Companion::class) @SerialName("prototypes") private var prototypes: MutableVisionGroup? = null,
) : VisionGroupBase(), Solid, PrototypeHolder {
override val descriptor: NodeDescriptor get() = Solid.descriptor override val descriptor: NodeDescriptor get() = Solid.descriptor
/**
* A container for templates visible inside this group
*/
@Serializable(Prototypes.Companion::class)
@SerialName("prototypes")
internal var prototypes: MutableVisionGroup? = null
/** /**
* Ger a prototype redirecting the request to the parent if prototype is not found * Ger a prototype redirecting the request to the parent if prototype is not found
*/ */
@ -60,13 +56,6 @@ public class SolidGroup : VisionGroupBase(), Solid, PrototypeHolder {
override var scale: Point3D? = null override var scale: Point3D? = null
override fun attachChildren() {
prototypes?.parent = this
prototypes?.attachChildren()
super.attachChildren()
}
// /** // /**
// * TODO add special static group to hold statics without propagation // * TODO add special static group to hold statics without propagation
// */ // */
@ -108,16 +97,11 @@ public fun VisionContainerBuilder<Vision>.group(name: String, action: SolidGroup
@Serializable(Prototypes.Companion::class) @Serializable(Prototypes.Companion::class)
internal class Prototypes( internal class Prototypes(
children: Map<NameToken, Vision> = emptyMap(), children: Map<NameToken, Vision> = emptyMap(),
) : VisionGroupBase(), PrototypeHolder { ) : VisionGroupBase(children as? MutableMap<NameToken, Vision> ?: children.toMutableMap()), PrototypeHolder {
init { init {
childrenInternal.putAll(children)
}
override fun attachChildren() {
children.values.forEach { children.values.forEach {
it.parent = parent it.parent = parent
(it as? VisionGroup)?.attachChildren()
} }
} }

View File

@ -7,7 +7,10 @@ import hep.dataforge.context.PluginTag
import hep.dataforge.meta.Meta import hep.dataforge.meta.Meta
import hep.dataforge.names.Name import hep.dataforge.names.Name
import hep.dataforge.names.toName import hep.dataforge.names.toName
import hep.dataforge.vision.* import hep.dataforge.vision.Vision
import hep.dataforge.vision.VisionBase
import hep.dataforge.vision.VisionGroupBase
import hep.dataforge.vision.VisionManager
import hep.dataforge.vision.VisionManager.Companion.VISION_SERIALIZER_MODULE_TARGET import hep.dataforge.vision.VisionManager.Companion.VISION_SERIALIZER_MODULE_TARGET
import kotlinx.serialization.PolymorphicSerializer import kotlinx.serialization.PolymorphicSerializer
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
@ -67,10 +70,6 @@ public class SolidManager(meta: Meta) : AbstractPlugin(meta) {
public fun encodeToString(solid: Solid): String = jsonForSolids.encodeToString(PolymorphicSerializer(Vision::class), solid) public fun encodeToString(solid: Solid): String = jsonForSolids.encodeToString(PolymorphicSerializer(Vision::class), solid)
fun decodeFromString(str: String): Solid = jsonForSolids.decodeFromString(PolymorphicSerializer(Solid::class), str).also { public fun decodeFromString(str: String): Solid = jsonForSolids.decodeFromString(PolymorphicSerializer(Solid::class), str)
if(it is VisionGroup){
it.attachChildren()
}
}
} }
} }

View File

@ -82,10 +82,6 @@ public class SolidReferenceGroup(
includeDefaults: Boolean, includeDefaults: Boolean,
): MetaItem<*>? = getRefProperty(name, inherit, includeStyles, includeDefaults) ): MetaItem<*>? = getRefProperty(name, inherit, includeStyles, includeDefaults)
override fun attachChildren() {
//do nothing
}
override val descriptor: NodeDescriptor get() = prototype.descriptor override val descriptor: NodeDescriptor get() = prototype.descriptor
@ -142,10 +138,6 @@ public class SolidReferenceGroup(
TODO("Not yet implemented") TODO("Not yet implemented")
} }
override fun attachChildren() {
//do nothing
}
override val descriptor: NodeDescriptor get() = prototype.descriptor override val descriptor: NodeDescriptor get() = prototype.descriptor
} }

View File

@ -26,7 +26,9 @@ internal object UnRef : VisualTreeTransform<SolidGroup>() {
} }
private fun MutableVisionGroup.unref(name: Name) { private fun MutableVisionGroup.unref(name: Name) {
(this as? SolidGroup)?.prototypes?.set(name, null) (this as? SolidGroup)?.prototypes{
set(name, null)
}
children.filter { (it.value as? SolidReferenceGroup)?.templateName == name }.forEach { (key, value) -> children.filter { (it.value as? SolidReferenceGroup)?.templateName == name }.forEach { (key, value) ->
val reference = value as SolidReferenceGroup val reference = value as SolidReferenceGroup
val newChild = mergeChild(reference, reference.prototype) val newChild = mergeChild(reference, reference.prototype)