From 7074112b7c7a17190d3e9e4c548cfe369218cc1b Mon Sep 17 00:00:00 2001 From: Grigorii Kirgizov Date: Mon, 3 Feb 2020 17:57:41 +0300 Subject: [PATCH] Revert "Refine logic for finding activation Pos to correctly handle discarding rules" This reverts commit 746c4808 --- .../reactor/core/internal/ExecutionQueue.kt | 11 ++-- .../reactor/core/internal/MatchJournal.kt | 10 +--- .../reactor/core/internal/MatchJournalImpl.kt | 60 ++++--------------- 3 files changed, 19 insertions(+), 62 deletions(-) diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ExecutionQueue.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ExecutionQueue.kt index 72bcc453..4b2ea94a 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ExecutionQueue.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ExecutionQueue.kt @@ -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) } diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/MatchJournal.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/MatchJournal.kt index e1d2a8f5..8d584e55 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/MatchJournal.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/MatchJournal.kt @@ -108,20 +108,12 @@ interface MatchJournal : MutableIterable { */ fun activatingChunkOf(occId: Id): 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? + fun activationPos(match: RuleMatchEx): OccChunk? /** * Length of the indexed [MatchJournal] diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/MatchJournalImpl.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/MatchJournalImpl.kt index d6006ab8..4278c93e 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/MatchJournalImpl.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/MatchJournalImpl.kt @@ -225,35 +225,18 @@ internal open class MatchJournalImpl( val last: E get() = if (l is LinkedList) l.last else l.last() } - private class IndexImpl(chunks: Collection): MatchJournal.Index + private class IndexImpl(chunks: Iterable): MatchJournal.Index { - private val chunkOrder = HashMap() // chunk.id to its index + private val chunkOrder = HashMap() private val occChunks = HashMap, OccChunk>() - private val occChunk2LastDescendant = HashMap() init { - if (chunks.isNotEmpty()) { - val occChunksTree = LinkedList() + 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) = occChunks[occId] - override fun lastDescendantOf(occChunk: OccChunk): Chunk? = occChunk2LastDescendant[occChunk.id] - - override fun activationPos(match: RuleMatchEx): Pair? { - // The latest matched occurrence from match's head is - // (by definition) the occurrence which activated this match. - - val parentChunks: List = - 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 =