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

commonMain.it.unibo.tuprolog.collections.AbstractMutableReteClauseCollection.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

import it.unibo.tuprolog.collections.rete.custom.ReteTree
import it.unibo.tuprolog.core.Clause
import it.unibo.tuprolog.theory.TheoryUtils

internal abstract class AbstractMutableReteClauseCollection>
protected constructor(
    rete: ReteTree
) : MutableClauseCollection, AbstractClauseCollection(rete) {

    override fun add(clause: Clause): Self {
        rete.assertZ(clause)
        @Suppress("UNCHECKED_CAST")
        return this as Self
    }

    override fun addAll(clauses: Iterable): Self {
        clauses.forEach { rete.assertZ(it) }
        @Suppress("UNCHECKED_CAST")
        return this as Self
    }

    override fun retrieve(clause: Clause): RetrieveResult {
        val retracted = rete.retractFirst(clause)

        @Suppress("UNCHECKED_CAST")
        return when {
            retracted.none() ->
                RetrieveResult.Failure(this as Self)
            else ->
                RetrieveResult.Success(
                    this as Self,
                    retracted.toList()
                )
        }
    }

    override fun retrieveAll(clause: Clause): RetrieveResult {
        val retracted = rete.retractAll(clause)

        @Suppress("UNCHECKED_CAST")
        return when {
            retracted.none() ->
                RetrieveResult.Failure(this as Self)
            else ->
                RetrieveResult.Success(
                    this as Self,
                    retracted.toList()
                )
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy