commonMain.it.unibo.tuprolog.solve.TestSubstitutionsImpl.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of test-solve Show documentation
Show all versions of test-solve Show documentation
Common testing facilities for 2P-Kt-based logic solvers
package it.unibo.tuprolog.solve
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.dsl.theory.logicProgramming
import it.unibo.tuprolog.solve.flags.FlagStore
import it.unibo.tuprolog.solve.flags.TrackVariables
import it.unibo.tuprolog.solve.flags.invoke
import it.unibo.tuprolog.solve.primitive.Solve
import it.unibo.tuprolog.solve.primitive.UnaryPredicate
import kotlin.test.assertEquals
import kotlin.test.assertIs
class TestSubstitutionsImpl(private val solverFactory: SolverFactory) : TestSubstitutions {
private class Inspect(
private val inspections: MutableList>,
) : UnaryPredicate.Predicative("inspect") {
override fun Solve.Request.compute(first: Term): Boolean {
inspections +=
first.variables.distinct()
.map { context.substitution.getOriginal(it) }
.filterNotNull()
.map { it.name }
.toSet()
return true
}
}
override fun interestingVariablesAreNotObliterated() {
testVariablesTracking(false)
}
private fun testVariablesTracking(tracking: Boolean) {
val inspections = mutableListOf>()
logicProgramming {
val inspect = Inspect(inspections)
val solver =
solverFactory.solverOf(
staticKb = TestingClauseTheories.callsWithVariablesAndInspectorTheory("p", inspect.functor),
libraries = runtimeOf("default", inspect),
flags = FlagStore.EMPTY + TrackVariables { if (tracking) ON else OFF },
)
val query = "p"(A, B, C)
solver.solveOnce(query)
}
assertEquals(setOf("A", "B", "C"), inspections[0])
assertEquals(setOf("D"), inspections[1])
assertEquals(setOf("E"), inspections[2])
assertEquals(setOf("A", "B", "C", "E") + if (tracking) setOf("D") else emptySet(), inspections[3])
}
override fun interestingVariablesAreProperlyTracked() {
testVariablesTracking(true)
}
override fun uninterestingVariablesAreObliterated() {
logicProgramming {
val solver = solverFactory.solverOf(staticKb = TestingClauseTheories.callsWithVariablesTheory)
val query = "a"(X)
val solution = solver.solveOnce(query)
assertIs(solution)
assertEquals(1, solution.substitution.size)
assertEquals(intOf(1), solution.substitution[X])
}
}
}