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

commonMain.it.unibo.tuprolog.collections.rete.generic.ReteNode.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 it.unibo.tuprolog.core.Clause
import kotlin.jvm.JvmStatic
import it.unibo.tuprolog.collections.rete.generic.set.RootNode as SetNode

/** A class modeling a Rete Tree Node */
internal interface ReteNode {

    /** All direct children of this node */
    val children: MutableMap>

    /** All indexed elements by this node and its children */
    val indexedElements: Sequence

    /** Indexes a new element under this node */
    fun put(element: E, beforeOthers: Boolean)

    /** Indexes a new element under this node, after the others */
    fun put(element: E) =
        put(element, false)

    /** Indexes all the element given as input, after the others**/
    fun put(clauses: Iterable) =
        clauses.forEach {
            put(it)
        }

    /** Gets a sequence of matching elements starting from give one */
    fun get(element: @UnsafeVariance E): Sequence

    /**
     * Removes elements matching given [element], respecting provided [limit], and returns them.
     * A negative limit will be considered as if there's no limit, i.e. all matching elements will be removed
     */
    fun remove(element: @UnsafeVariance E, limit: Int): Sequence

    /**
     * Removes one element matching given [element], and returns it;
     */
    fun remove(element: @UnsafeVariance E): Sequence =
        remove(element, 1)

    /** Removes all elements matching given one */
    fun removeAll(element: @UnsafeVariance E): Sequence

    /** Creates a deep copy of the tree index; it doesn't copy indexed elements */
    fun deepCopy(): ReteNode

    /** An enhanced toString that can print nodes in Tree fashion */
    fun toString(treefy: Boolean): String

    override fun toString(): String

    companion object {
        /** Creates a ReteTree from give clauses */
        @JvmStatic
        fun ofSet(clauses: Iterable): ReteNode<*, Clause> =
            SetNode().apply { clauses.forEach { put(it) } }

        /** Creates a ReteTree from give clauses */
        @JvmStatic
        fun ofSet(vararg clauses: Clause): ReteNode<*, Clause> =
            ofSet(listOf(*clauses))
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy