commonMain.it.unibo.tuprolog.solve.solver.fsm.impl.StateInit.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solve-streams-jvm Show documentation
Show all versions of solve-streams-jvm Show documentation
Experimental, functional-programming-based implementation of Prolog's SLDNF resolution principle
package it.unibo.tuprolog.solve.solver.fsm.impl
import it.unibo.tuprolog.solve.extractSignature
import it.unibo.tuprolog.solve.primitive.Solve
import it.unibo.tuprolog.solve.solver.StreamsExecutionContext
import it.unibo.tuprolog.solve.solver.fsm.State
import it.unibo.tuprolog.solve.solver.isWellFormed
import it.unibo.tuprolog.solve.solver.prepareForExecutionAsGoal
/**
* The state responsible of initializing the state machine with the goal that has to be executed
*
* @author Enrico
*/
internal class StateInit(
override val solve: Solve.Request
) : AbstractTimedState(solve) {
override fun behaveTimed(): Sequence = sequence {
val initializedSideEffectsManager = with(solve.context) { sideEffectManager.stateInitInitialize(this) }
val currentGoal = solve.query
when {
with(solve.context) { solverStrategies.successCheckStrategy(currentGoal, this) } ->
// current goal is already demonstrated
yield(
ifTimeIsNotOver(
stateEndTrue(
solve.context.substitution,
sideEffectManager = initializedSideEffectsManager
)
)
)
else -> when {
currentGoal.isWellFormed() -> currentGoal.prepareForExecutionAsGoal().also { preparedGoal ->
// a primitive call or well-formed goal
yield(StateGoalEvaluation(
solve.copy(
signature = preparedGoal.extractSignature(),
arguments = preparedGoal.argsList,
context = with(solve.context) { copy(sideEffectManager = initializedSideEffectsManager) }
)
))
}
// goal non well-formed
else -> yield(stateEndFalse(sideEffectManager = initializedSideEffectsManager))
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy