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

commonMain.it.unibo.tuprolog.collections.rete.generic.AbstractLeafReteNode.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: 1.0.4
Show newest version
package it.unibo.tuprolog.collections.rete.generic

import it.unibo.tuprolog.core.Clause
import kotlin.math.min

/** A leaf Rete Node */
internal abstract class AbstractLeafReteNode(
    override val children: MutableMap> = mutableMapOf(),
) : AbstractReteNode(children) {
    /** Internal data structure to store leaf elements */
    protected abstract val leafElements: MutableList

    override val indexedElements: Sequence
        get() = leafElements.asSequence()

    override fun put(
        element: E,
        beforeOthers: Boolean,
    ) {
        if (beforeOthers) {
            leafElements.add(0, element)
        } else {
            leafElements.add(element)
        }
    }

    override fun removeWithLimit(
        element: E,
        limit: Int,
    ): Sequence =
        get(element)
            .take(if (limit > 0) min(limit, leafElements.count()) else leafElements.count())
            .toList()
            .asSequence()
            .also { leafElements.removeAll(it) }

    override fun removeAll(element: E): Sequence =
        get(element)
            .toList()
            .asSequence()
            .also { leafElements.removeAll(it) }

    override fun toString(treefy: Boolean): String =
        if (treefy) {
            "$header {${leafElements.joinToString(".\n\t", "\n\t", ".\n")}}"
        } else {
            toString()
        }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy