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

commonMain.it.unibo.tuprolog.solve.problog.lib.knowledge.ProbExplanationTerm.kt Maven / Gradle / Ivy

There is a newer version: 1.0.4
Show newest version
package it.unibo.tuprolog.solve.problog.lib.knowledge

import it.unibo.tuprolog.core.Atom
import it.unibo.tuprolog.core.Constant
import it.unibo.tuprolog.core.Scope
import it.unibo.tuprolog.core.Substitution

/**
 * This class wraps a [ProbExplanation] instance into a Prolog term
 * so that it can be used inside Prolog resolutions. This allows for proper leveraging
 * of the multi-paradigm nature of tuProlog.
 *
 * @author Jason Dellaluce
 */
internal class ProbExplanationTerm(
    val explanation: ProbExplanation,
) : Atom by Atom.of("") {
    override val isConstant: Boolean
        get() = false

    override fun asConstant(): Constant = this

    override fun freshCopy(): ProbExplanationTerm {
        return ProbExplanationTerm(explanation.apply { it.freshCopy() })
    }

    override fun freshCopy(scope: Scope): ProbExplanationTerm {
        return ProbExplanationTerm(explanation.apply { it.freshCopy(scope) })
    }

    override fun get(
        substitution: Substitution,
        vararg substitutions: Substitution,
    ): ProbExplanationTerm {
        return this.apply(substitution, *substitutions)
    }

    override fun apply(
        substitution: Substitution,
        vararg substitutions: Substitution,
    ): ProbExplanationTerm {
        return this.apply(Substitution.of(substitution, *substitutions))
    }

    override fun apply(substitution: Substitution): ProbExplanationTerm {
        return if (substitution.isEmpty() || isGround) {
            this
        } else {
            ProbExplanationTerm(explanation.apply { it.apply(substitution) })
        }
    }

    override val isGround: Boolean
        get() = !explanation.containsAnyNotGroundTerm
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy