All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.it.unibo.tuprolog.collections.rete.generic.AbstractReteNode.kt Maven / Gradle / Ivy

Go to download

In-memory storage and indexing facilities for ordered and unordered knowledge bases composed by logic clauses

There is a newer version: 0.17.4
Show newest version
package it.unibo.tuprolog.collections.rete.generic

import kotlin.jvm.JvmStatic

/** Abstract base class for Rete Tree nodes */
internal abstract class AbstractReteNode(override val children: MutableMap> = mutableMapOf()) :
    ReteNode {

    /** Description for current Rete Tree Node */
    protected abstract val header: String

    override fun remove(element: E, limit: Int): Sequence = when (limit) {
        0 -> emptySequence()
        else -> removeWithLimit(element, limit)
    }

    /** Called when a non-zero-limit removal is required inside a node */
    protected abstract fun removeWithLimit(element: E, limit: Int): Sequence

    override fun removeAll(element: E): Sequence = remove(element, Int.MAX_VALUE)

    override fun toString(treefy: Boolean): String =
        if (treefy)
            "$header {" +
                    children.values.joinToString(",\n\t", "\n\t", "\n") {
                        it.toString(treefy).replace("\n", "\n\t")
                    } + "}"
        else
            "$header {${children.values.joinToString(",") {
                it.toString()
            }}"

    override fun toString(): String {
        return toString(false)
    }

    companion object {

        /** Utility function to deeply copy a MutableMap */
        @JvmStatic
        protected inline fun  MutableMap.deepCopy(
            deepCopyKey: (K) -> K,
            deepCopyValue: (V) -> V
        ): MutableMap =
            entries.map { deepCopyKey(it.key) to deepCopyValue(it.value) }.toMap(mutableMapOf())
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy