commonMain.it.unibo.tuprolog.collections.rete.generic.AbstractReteNode.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of theory-metadata Show documentation
Show all versions of theory-metadata Show documentation
In-memory storage and indexing facilities for ordered and unordered knowledge bases composed by logic clauses
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())
}
}