Introduce support for undo activate/discard trace events

In case evaluation fails and the state of activated occurrences is
reset to some previously stored state, the trace view does not
get updated and contains some stale information. This patch fixes it.
This commit is contained in:
Fedor Isakov 2022-03-14 18:35:17 +01:00
parent c90382ec8f
commit bcad14d2a5
3 changed files with 26 additions and 14 deletions

View File

@ -105,7 +105,7 @@ internal class EvaluationSessionImpl private constructor (
?.also { it.updateIndexFromRules(program.rules()) }
?: RuleIndex(program.rules())
val journal = MatchJournalImpl(incrementality)
val journal = MatchJournalImpl(incrementality, trace)
val logicalState = LogicalState()
val dispatchingFront = Dispatcher(ruleIndex).front()

View File

@ -175,6 +175,27 @@ internal open class MatchJournalImpl(
return added
}
internal fun resetOccurrences(occSpecs: List<Chunk.Entry>) =
// assume occSpecs are ordered in order of processing
// so, iterate over reversed list
occSpecs.asReversed().forEach {
if (it.discarded) {
trace.undoDiscard(it.occ)
it.occ.alive = true
it.occ.stored = true
} else {
trace.undoActivate(it.occ)
it.occ.alive = false
it.occ.stored = false
}
}
override fun initialChunk(): MatchChunk = initialChunk
override fun parentChunk(): MatchChunk = ancestorChunksStack.peek()!!
@ -379,19 +400,6 @@ internal fun RuleMatch.collectJustifications(vararg withEvidence: Evidence): Jus
this.allHeads().forEach { allJss.addAll(it.justifications()) }
}
internal fun resetOccurrences(occSpecs: List<Chunk.Entry>) =
// assume occSpecs are ordered in order of processing
// so, iterate over reversed list
occSpecs.asReversed().forEach {
if (it.discarded) {
it.occ.alive = true
it.occ.stored = true
} else {
it.occ.alive = false
it.occ.stored = false
}
}
internal fun <E> MutableList<E>.push(element: E) = this.add(element)
internal fun <E> MutableList<E>.pop() = this.removeAt(this.size - 1)
internal fun <E> MutableList<E>.peek() = this.last()

View File

@ -31,6 +31,8 @@ public interface EvaluationTrace {
default void activate(ConstraintOccurrence occurrence) {}
default void undoActivate(ConstraintOccurrence occurrence) {}
default void reactivate(ConstraintOccurrence occurrence) {}
default void activateContinue(ConstraintOccurrence occurrence) {}
@ -39,6 +41,8 @@ public interface EvaluationTrace {
default void discard(ConstraintOccurrence occurrence) {}
default void undoDiscard(ConstraintOccurrence occurrence) {}
default void trying(RuleMatch ruleMatch) {}
default void reject(RuleMatch ruleMatch) {}