From 121aecc4a8485571bb9337919372b903510afb25 Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Thu, 18 Jun 2020 16:01:38 +0200 Subject: [PATCH] improve backtracking --- .../tuprolog/solve/ChoicePointContext.kt | 28 ++++++++++++++----- .../tuprolog/solve/fsm/StateBacktracking.kt | 2 +- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/solve-classic/src/commonMain/kotlin/it/unibo/tuprolog/solve/ChoicePointContext.kt b/solve-classic/src/commonMain/kotlin/it/unibo/tuprolog/solve/ChoicePointContext.kt index 07533ec87..198395e6b 100644 --- a/solve-classic/src/commonMain/kotlin/it/unibo/tuprolog/solve/ChoicePointContext.kt +++ b/solve-classic/src/commonMain/kotlin/it/unibo/tuprolog/solve/ChoicePointContext.kt @@ -56,7 +56,7 @@ sealed class ChoicePointContext( protected abstract val typeName: String - abstract fun backtrack(nextStep: Long, startTime: Long): ClassicExecutionContext + abstract fun backtrack(context: ClassicExecutionContext): ClassicExecutionContext data class Primitives( override val alternatives: Cursor, @@ -72,11 +72,18 @@ sealed class ChoicePointContext( override val typeName: String get() = "Primitives" - override fun backtrack(nextStep: Long, startTime: Long): ClassicExecutionContext { + override fun backtrack(context: ClassicExecutionContext): ClassicExecutionContext { val tempContext = executionContext!!.copy( primitives = alternatives, - step = nextStep, - startTime = startTime + step = context.step + 1, + startTime = context.startTime, + flags = context.flags, + dynamicKb = context.dynamicKb, + staticKb = context.staticKb, + operators = context.operators, + inputChannels = context.inputChannels, + outputChannels = context.outputChannels, + libraries = context.libraries ) val nextChoicePointContext = copy( @@ -102,11 +109,18 @@ sealed class ChoicePointContext( override val typeName: String get() = "Rules" - override fun backtrack(nextStep: Long, startTime: Long): ClassicExecutionContext { + override fun backtrack(context: ClassicExecutionContext): ClassicExecutionContext { val tempContext = executionContext!!.copy( rules = alternatives, - step = nextStep, - startTime = startTime + step = context.step + 1, + startTime = context.startTime, + flags = context.flags, + dynamicKb = context.dynamicKb, + staticKb = context.staticKb, + operators = context.operators, + inputChannels = context.inputChannels, + outputChannels = context.outputChannels, + libraries = context.libraries ) val nextChoicePointContext = copy( diff --git a/solve-classic/src/commonMain/kotlin/it/unibo/tuprolog/solve/fsm/StateBacktracking.kt b/solve-classic/src/commonMain/kotlin/it/unibo/tuprolog/solve/fsm/StateBacktracking.kt index c32248482..5b9732016 100644 --- a/solve-classic/src/commonMain/kotlin/it/unibo/tuprolog/solve/fsm/StateBacktracking.kt +++ b/solve-classic/src/commonMain/kotlin/it/unibo/tuprolog/solve/fsm/StateBacktracking.kt @@ -13,7 +13,7 @@ internal data class StateBacktracking(override val context: ClassicExecutionCont ) } else { val choicePointContext = choicePoints!!.pathToRoot.first { it.alternatives.hasNext } - val nextContext = choicePointContext.backtrack(nextStep(), context.startTime) + val nextContext = choicePointContext.backtrack(context) if (nextContext.primitives.hasNext) { StatePrimitiveExecution(nextContext) } else {