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

commonMain.it.unibo.tuprolog.collections.rete.custom.Utils.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.custom

import it.unibo.tuprolog.collections.rete.custom.clause.SituatedIndexedClause
import it.unibo.tuprolog.collections.rete.takeFirstAfterSkipping
import it.unibo.tuprolog.core.Clause
import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.utils.mergeSequences

internal object Utils {
    /**Calculate the arity of the first argument of any [Struct], at the given nesting level.
     * No checks are performed upon the validity of the Struct this extension method is called upon. */
    fun Struct.arityOfNestedFirstArgument(nestingLevel: Int): Int =
        this.firstArguments().takeFirstAfterSkipping(nestingLevel).let { it.castToStruct().arity }

    /**Calculate the functor of the first argument of any [Struct], at the given nesting level.
     * No checks are performed upon the validity of the Struct this extension method is called upon. */
    fun Struct.functorOfNestedFirstArgument(nestingLevel: Int): String =
        this.firstArguments().takeFirstAfterSkipping(nestingLevel).let { it.castToStruct().functor }

    /**Calculate the [Term] representing the first argument of any [Struct], at the given nesting level.
     * No checks are performed upon the validity of the Struct this extension method is called upon. */
    fun Struct.nestedFirstArgument(nestingLevel: Int): Term = this.firstArguments().takeFirstAfterSkipping(nestingLevel)

    private fun Struct.firstArguments(): Sequence =
        sequence {
            var currentTerm: Term = this@firstArguments
            while (currentTerm.isStruct) {
                yield(currentTerm)
                currentTerm = currentTerm.castToStruct().getArgAt(0)
            }
            yield(currentTerm)
        }

    /**Sorts all the given [Sequence] of [SituatedIndexedClause]*/
    fun merge(vararg args: Sequence): Sequence = merge(listOf(*args))

    /**Sorts all the given [Sequence] of [SituatedIndexedClause]*/
    fun merge(sequence: Sequence>): Sequence =
        merge(sequence.asIterable())

    /**Sorts all the given [Sequence] of [SituatedIndexedClause]*/
    fun merge(iterable: Iterable>): Sequence =
        mergeSequences(iterable, SituatedIndexedClause::compareTo)

    /**Composes all the given [Sequence] of [SituatedIndexedClause], disregarding order*/
    fun flattenIndexed(vararg args: Sequence): Sequence =
        flattenIndexed(sequenceOf(*args))

    /**Composes all the given [Sequence] of [SituatedIndexedClause], disregarding order*/
    fun flattenIndexed(sequence: Sequence>): Sequence =
        sequence.flatten()

    /**Composes all the given [Sequence] of [SituatedIndexedClause], disregarding order*/
    fun flattenIndexed(iterable: Iterable>): Sequence =
        flattenIndexed(iterable.asSequence())

    /**Composes all the given [Sequence] of [Clause], disregarding order*/
    fun flatten(vararg args: Sequence): Sequence = flatten(sequenceOf(*args))

    /**Composes all the given [Sequence] of [Clause], disregarding order*/
    fun flatten(sequence: Sequence>): Sequence = sequence.flatten()

    /**Composes all the given [Sequence] of [Clause], disregarding order*/
    fun flatten(iterable: Iterable>): Sequence = flatten(iterable.asSequence())

    /**Compares two nullable [SituatedIndexedClause]. If both are null, null is returned*/
    fun comparePriority(
        a: SituatedIndexedClause?,
        b: SituatedIndexedClause?,
    ): SituatedIndexedClause? =
        when {
            a == null && b == null -> null
            a == null -> b
            b == null -> a
            a < b -> a
            else -> b
        }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy