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

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

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

internal abstract class AbstractReteClauseCollection> protected constructor(
    rete: ReteTree,
) : AbstractClauseCollection(rete) {
    protected abstract fun newCollectionBuilder(rete: ReteTree): Self

    override fun add(clause: Clause): Self =
        newCollectionBuilder(
            rete.deepCopy().apply {
                assertZ(TheoryUtils.checkClauseCorrect(clause))
            },
        )

    override fun addAll(clauses: Iterable): Self =
        newCollectionBuilder(
            rete.deepCopy().apply {
                clauses.forEach {
                    assertZ(TheoryUtils.checkClauseCorrect(it))
                }
            },
        )

    override fun retrieve(clause: Clause): RetrieveResult {
        val newTheory = rete.deepCopy()
        val retracted = newTheory.retractFirst(clause)
        return when {
            retracted.none() -> RetrieveResult.Failure(self)
            else -> RetrieveResult.Success(newCollectionBuilder(newTheory), retracted.toList())
        }
    }

    override fun retrieveAll(clause: Clause): RetrieveResult {
        val newTheory = rete.deepCopy()
        val retracted = newTheory.retractAll(clause)
        return when {
            retracted.none() -> RetrieveResult.Failure(self)
            else -> RetrieveResult.Success(newCollectionBuilder(newTheory), retracted.toList())
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy