Skip to content

Commit

Permalink
My version of first three broken commits (more clean, less modificati…
Browse files Browse the repository at this point in the history
…ons)
  • Loading branch information
siboxd committed Jul 1, 2020
1 parent 070b085 commit d0f9ba4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 547 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ internal fun Solve.Request<StreamsExecutionContext>.newSolveRequest(
fun Solve.Request<ExecutionContext>.replyWith(otherResponse: Solve.Response): Solve.Response =
with(otherResponse) {
replyWith(
solution, libraries, flags, staticKb, dynamicKb,
solution,
sideEffectManager ?: this@replyWith.context.getSideEffectManager(),
operators
*sideEffects.toTypedArray()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ internal data class StreamsExecutionContext(
val sideEffectManager: SideEffectManagerImpl = SideEffectManagerImpl()
) : ExecutionContext {

constructor(context: ExecutionContext, newCurrentSubstitution: Substitution.Unifier) : this( // to be tested
context.libraries,
context.flags,
context.staticKb,
context.dynamicKb,
context.operators,
context.inputChannels,
context.outputChannels,
newCurrentSubstitution,
(context as? StreamsExecutionContext)?.solverStrategies ?: SolverStrategies.prologStandard,
(context as? StreamsExecutionContext)?.sideEffectManager ?: SideEffectManagerImpl()
)

override val procedure: Struct?
get() = sideEffectManager.logicalParentRequests.map { it.query }.firstOrNull()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package it.unibo.tuprolog.solve.solver.fsm.impl

import it.unibo.tuprolog.core.Substitution
import it.unibo.tuprolog.core.operators.OperatorSet
import it.unibo.tuprolog.solve.*
import it.unibo.tuprolog.solve.exception.HaltException
import it.unibo.tuprolog.solve.exception.TuPrologRuntimeException
import it.unibo.tuprolog.solve.library.Libraries
import it.unibo.tuprolog.solve.solver.StreamsExecutionContext
import it.unibo.tuprolog.solve.solver.fsm.AbstractState
import it.unibo.tuprolog.solve.solver.fsm.FinalState
import it.unibo.tuprolog.solve.solver.fsm.IntermediateState
import it.unibo.tuprolog.solve.solver.fsm.State
import it.unibo.tuprolog.solve.solver.getSideEffectManager
import it.unibo.tuprolog.theory.Theory

/**
* Base class of states representing the computation end
Expand All @@ -21,40 +18,45 @@ import it.unibo.tuprolog.theory.Theory
*
* @author Enrico
*/
internal sealed class StateEnd(override val solve: Solve.Response) : AbstractState(solve), FinalState {
internal sealed class StateEnd(
private val sourceContext: ExecutionContext,
override val solve: Solve.Response
) : AbstractState(solve), FinalState {

override fun behave(): Sequence<State> = emptySequence()

override val context: ExecutionContext
get() = with(solve) {
StreamsExecutionContext(
libraries ?: Libraries(),
flags ?: emptyMap(),
staticKb ?: Theory.empty(),
dynamicKb ?: Theory.empty(),
operators ?: OperatorSet.EMPTY,
inputChannels ?: ExecutionContextAware.defaultInputChannels(),
outputChannels ?: ExecutionContextAware.defaultOutputChannels(),
solution.substitution as? Substitution.Unifier ?: Substitution.empty()
)
}
override val context: ExecutionContext by lazy {
StreamsExecutionContext(
sourceContext.apply(solve.sideEffects),
newCurrentSubstitution = solve.solution.substitution as? Substitution.Unifier ?: Substitution.empty()
)
}

/** The *True* state is reached when a successful computational path has ended */
internal data class True(override val solve: Solve.Response) : StateEnd(solve), FinalState {
internal data class True(
private val sourceContext: ExecutionContext,
override val solve: Solve.Response
) : StateEnd(sourceContext, solve), FinalState {
init {
require(solve.solution is Solution.Yes) { "True end state can be created only with Solution.Yes. Current: `${solve.solution}`" }
}
}

/** The *False* state is reached when a failed computational path has ended */
internal data class False(override val solve: Solve.Response) : StateEnd(solve), FinalState {
internal data class False(
private val sourceContext: ExecutionContext,
override val solve: Solve.Response
) : StateEnd(sourceContext, solve), FinalState {
init {
require(solve.solution is Solution.No) { "False end state can be created only with Solution.No. Current: `${solve.solution}`" }
}
}

/** The *Halt* state is reached when an [HaltException] is caught, terminating the computation */
internal data class Halt(override val solve: Solve.Response) : StateEnd(solve), FinalState {
internal data class Halt(
private val sourceContext: ExecutionContext,
override val solve: Solve.Response
) : StateEnd(sourceContext, solve), FinalState {
init {
require(solve.solution is Solution.Halt) { "Halt end state can be created only with Solution.Halt. Current: `${solve.solution}`" }
}
Expand All @@ -67,129 +69,62 @@ internal sealed class StateEnd(override val solve: Solve.Response) : AbstractSta
/** Transition from this intermediate state to [StateEnd.True], creating a [Solve.Response] with given data */
internal fun IntermediateState.stateEndTrue(
substitution: Substitution.Unifier = Substitution.empty(),
libraries: Libraries? = null,
flags: PrologFlags? = null,
staticKb: Theory? = null,
dynamicKb: Theory? = null,
sideEffectManager: SideEffectManager? = null,
operators: OperatorSet? = null,
inputChannels: PrologInputChannels<*>? = null,
outputChannels: PrologOutputChannels<*>? = null
vararg sideEffects: SideEffect
) = StateEnd.True(
context,
solve.replySuccess(
substitution,
libraries ?: solve.context.libraries,
flags ?: solve.context.flags,
staticKb ?: solve.context.staticKb,
dynamicKb ?: solve.context.dynamicKb,
sideEffectManager ?: solve.context.getSideEffectManager(),
operators ?: solve.context.operators,
inputChannels ?: solve.context.inputChannels,
outputChannels ?: solve.context.outputChannels
*sideEffects
)
)

/** Transition from this intermediate state to [StateEnd.False], creating a [Solve.Response] with given data */
internal fun IntermediateState.stateEndFalse(
libraries: Libraries? = null,
flags: PrologFlags? = null,
staticKb: Theory? = null,
dynamicKb: Theory? = null,
sideEffectManager: SideEffectManager? = null,
operators: OperatorSet? = null,
inputChannels: PrologInputChannels<*>? = null,
outputChannels: PrologOutputChannels<*>? = null
vararg sideEffects: SideEffect
) = StateEnd.False(
context,
solve.replyFail(
libraries ?: solve.context.libraries,
flags ?: solve.context.flags,
staticKb ?: solve.context.staticKb,
dynamicKb ?: solve.context.dynamicKb,
sideEffectManager ?: solve.context.getSideEffectManager(),
operators ?: solve.context.operators,
inputChannels ?: solve.context.inputChannels,
outputChannels ?: solve.context.outputChannels
*sideEffects
)
)

/** Transition from this intermediate state to [StateEnd.Halt], creating a [Solve.Response] with given data */
internal fun IntermediateState.stateEndHalt(
exception: TuPrologRuntimeException,
libraries: Libraries? = null,
flags: PrologFlags? = null,
staticKb: Theory? = null,
dynamicKb: Theory? = null,
sideEffectManager: SideEffectManager? = null,
operators: OperatorSet? = null,
inputChannels: PrologInputChannels<*>? = null,
outputChannels: PrologOutputChannels<*>? = null
vararg sideEffects: SideEffect
) = StateEnd.Halt(
context,
solve.replyException(
exception,
libraries ?: solve.context.libraries,
flags ?: solve.context.flags,
staticKb ?: solve.context.staticKb,
dynamicKb ?: solve.context.dynamicKb,
sideEffectManager
?: exception.context.getSideEffectManager()
?: solve.context.getSideEffectManager(),
operators ?: solve.context.operators,
inputChannels ?: solve.context.inputChannels,
outputChannels ?: solve.context.outputChannels
*sideEffects
)
)

/** Transition from this intermediate state to the correct [StateEnd] depending on provided [solution] */
internal fun IntermediateState.stateEnd(
solution: Solution,
libraries: Libraries? = null,
flags: PrologFlags? = null,
staticKb: Theory? = null,
dynamicKb: Theory? = null,
sideEffectManager: SideEffectManager? = null,
operators: OperatorSet? = null,
inputChannels: PrologInputChannels<*>? = null,
outputChannels: PrologOutputChannels<*>? = null
vararg sideEffects: SideEffect
): StateEnd = when (solution) {
is Solution.Yes ->
stateEndTrue(
solution.substitution.takeUnless { it.isEmpty() } ?: solve.context.substitution,
libraries,
flags,
staticKb,
dynamicKb,
sideEffectManager,
operators,
inputChannels,
outputChannels
)
is Solution.No ->
stateEndFalse(libraries, flags, staticKb, dynamicKb, sideEffectManager, operators, inputChannels, outputChannels)
is Solution.Halt ->
stateEndHalt(
solution.exception,
libraries,
flags,
staticKb,
dynamicKb,
sideEffectManager,
operators,
inputChannels,
outputChannels
*sideEffects
)
is Solution.No -> stateEndFalse(sideEffectManager, *sideEffects)
is Solution.Halt -> stateEndHalt(solution.exception, sideEffectManager, *sideEffects)
}

/** Transition from this intermediate state to a [StateEnd] containing provided [response] data */
internal fun IntermediateState.stateEnd(response: Solve.Response) = with(response) {
stateEnd(
solution,
libraries,
flags,
staticKb,
dynamicKb,
sideEffectManager,
operators,
inputChannels,
outputChannels
)
stateEnd(solution, sideEffectManager, *sideEffects.toTypedArray())
}
Loading

0 comments on commit d0f9ba4

Please sign in to comment.