commonMain.it.unibo.tuprolog.solve.ClassicSolver.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solve-classic-metadata Show documentation
Show all versions of solve-classic-metadata Show documentation
Stable, state-machine-based implementation of Prolog's SLDNF resolution principle
package it.unibo.tuprolog.solve
import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.operators.OperatorSet
import it.unibo.tuprolog.solve.fsm.State
import it.unibo.tuprolog.solve.fsm.StateInit
import it.unibo.tuprolog.solve.fsm.clone
import it.unibo.tuprolog.solve.library.Libraries
import it.unibo.tuprolog.theory.Theory
internal open class ClassicSolver(
libraries: Libraries = Libraries.empty(),
flags: FlagStore = FlagStore.empty(),
staticKb: Theory = Theory.empty(),
dynamicKb: Theory = Theory.empty(),
inputChannels: InputStore<*> = ExecutionContextAware.defaultInputChannels(),
outputChannels: OutputStore<*> = ExecutionContextAware.defaultOutputChannels()
) : Solver {
private var state: State = StateInit(
ClassicExecutionContext(
libraries = libraries,
flags = flags,
staticKb = staticKb,
dynamicKb = dynamicKb,
operators = getAllOperators(libraries, staticKb, dynamicKb).toOperatorSet(),
inputChannels = inputChannels,
outputChannels = outputChannels
)
)
protected fun updateContext(contextMapper: ClassicExecutionContext.() -> ClassicExecutionContext) {
val ctx = state.context
val newCtx = ctx.contextMapper()
if (newCtx != ctx) {
state = state.clone(newCtx)
}
}
override fun solve(goal: Struct, maxDuration: TimeDuration): Sequence {
val initialContext = ClassicExecutionContext(
query = goal,
libraries = libraries,
flags = flags,
staticKb = staticKb,
dynamicKb = dynamicKb,
operators = operators,
inputChannels = inputChannels,
outputChannels = outputChannels,
maxDuration = maxDuration,
startTime = currentTimeInstant()
)
state = StateInit(initialContext)
return SolutionIterator(state) { newState, newStep ->
require(newState.context.step == newStep)
state = newState
}.asSequence()
}
override val libraries: Libraries
get() = state.context.libraries
override val flags: FlagStore
get() = state.context.flags
override val staticKb: Theory
get() = state.context.staticKb
override val dynamicKb: Theory
get() = state.context.dynamicKb
override val inputChannels: InputStore<*>
get() = state.context.inputChannels
override val outputChannels: OutputStore<*>
get() = state.context.outputChannels
override val operators: OperatorSet
get() = state.context.operators
}