Revert "Refine logic for finding activation Pos to correctly handle discarding rules"

This reverts commit 746c4808
This commit is contained in:
Grigorii Kirgizov 2020-02-03 17:57:41 +03:00
parent 3b0008ad22
commit 7074112b7c
3 changed files with 19 additions and 62 deletions

View File

@ -67,7 +67,7 @@ internal class ExecutionQueue(
// Handles the case when several matches are added to the same position.
// Then shouldn't replay, because currentPos is valid and more recent (!) than execPos.
if (execPos.pos != prevPos) {
processing.replayDescendants(processing.logicalState, execPos.ancestor, execPos.pos)
processing.replay(processing.logicalState, execPos.pos)
lastIncrementalRootPos = execPos.pos
}
prevPos = execPos.pos
@ -101,13 +101,14 @@ internal class ExecutionQueue(
// Returns null for matches with occurrences only from this session
// because journalIndex indexes only previous session.
val pos2occ = journalIndex.activationPos(m)
val occChunk = journalIndex.activationPos(m)
val pos = occChunk?.toPos()
// if it is a future match
if (pos2occ != null && journalIndex.compare(lastIncrementalRootPos, pos2occ.first) < 0) {
val idOcc = Id(pos2occ.second.occ)
if (pos != null && journalIndex.compare(lastIncrementalRootPos, pos) < 0) {
val idOcc = Id(occChunk.occ)
postponedMatches[idOcc] = (postponedMatches[idOcc] ?: emptyList()) + listOf(m)
offer(pos2occ.first, pos2occ.second)
offer(pos, occChunk)
} else {
currentMatches.add(m)
}

View File

@ -108,20 +108,12 @@ interface MatchJournal : MutableIterable<MatchJournal.Chunk> {
*/
fun activatingChunkOf(occId: Id<Occurrence>): OccChunk?
/**
* Returns [Chunk] which is the last descendant of the [occChunk]
* according to justifications. May return activation chunk of
* the occurrence if it has no descendants.
* Returns null for non-principal occurrences.
*/
fun lastDescendantOf(occChunk: OccChunk): Chunk?
/**
* Returns [Pos] at which provided [RuleMatch] is triggered
* according to its indexed head [Occurrence]s.
* May return null for matches of non-principal rules.
*/
fun activationPos(match: RuleMatchEx): Pair<Pos, OccChunk>?
fun activationPos(match: RuleMatchEx): OccChunk?
/**
* Length of the indexed [MatchJournal]

View File

@ -225,35 +225,18 @@ internal open class MatchJournalImpl(
val last: E get() = if (l is LinkedList<E>) l.last else l.last()
}
private class IndexImpl(chunks: Collection<Chunk>): MatchJournal.Index
private class IndexImpl(chunks: Iterable<Chunk>): MatchJournal.Index
{
private val chunkOrder = HashMap<Int, Int>() // chunk.id to its index
private val chunkOrder = HashMap<Int, Int>()
private val occChunks = HashMap<Id<Occurrence>, OccChunk>()
private val occChunk2LastDescendant = HashMap<Int, Chunk>()
init {
if (chunks.isNotEmpty()) {
val occChunksTree = LinkedList<OccChunk>()
chunks.forEachIndexed { index, chunk ->
chunkOrder[chunk.id] = index
chunks.forEachIndexed { index, chunk ->
chunkOrder[chunk.id] = index
if (chunk is OccChunk) {
occChunks[Id(chunk.occ)] = chunk
occChunksTree.push(chunk)
}
occChunksTree.peek()?.let {
if (chunk.isDescendantOf(it.id)) {
// after iteration ends will store the last descendant
occChunk2LastDescendant[it.id] = chunk
} else {
// means descendant chunks ended
occChunksTree.pop()
}
}
if (chunk is OccChunk) {
occChunks[Id(chunk.occ)] = chunk
}
}
}
@ -261,31 +244,12 @@ internal open class MatchJournalImpl(
override fun activatingChunkOf(occId: Id<Occurrence>) = occChunks[occId]
override fun lastDescendantOf(occChunk: OccChunk): Chunk? = occChunk2LastDescendant[occChunk.id]
override fun activationPos(match: RuleMatchEx): Pair<Pos, OccChunk>? {
// The latest matched occurrence from match's head is
// (by definition) the occurrence which activated this match.
val parentChunks: List<OccChunk> =
match.signature().mapNotNull { occSig ->
occSig?.let { activatingChunkOf(it) }
}
val activatingChunk: OccChunk = parentChunks.maxBy { chunkOrder[it.id]!! }
?: return null
// If rule discards its occurrences then it must be inserted after
// all existing matches which are descendants of activating occurrence.
val discardingRule = match.rule().headReplaced().count() > 0
val posChunk =
if (discardingRule)
// not null if activatingChunk is not null
lastDescendantOf(activatingChunk)!!
else
activatingChunk
return posChunk.toPos() to activatingChunk
}
override fun activationPos(match: RuleMatchEx): OccChunk? =
// The latest matched occurrence from match's head is (by definition)
// the occurrence which activated this match.
match.signature().mapNotNull { occSig ->
occSig?.let { activatingChunkOf(it) }
}.maxBy { chunkOrder[it.id]!! } // compare positions: find latest
// todo: throw for invalid positions?
override fun compare(lhs: Pos, rhs: Pos): Int =