From 945088f72e00d349b695b8d83dee964f3677d5c4 Mon Sep 17 00:00:00 2001 From: Fedor Isakov Date: Mon, 7 Mar 2022 19:16:05 +0100 Subject: [PATCH] Drop obsolete deprecated class, simplify code --- .../core/internal/ConstraintsProcessing.kt | 2 - .../reactor/core/internal/IncrSpecHolder.kt | 2 + .../reactor/core/internal/LogicalState.kt | 148 +++--------------- .../reactor/core/internal/MatchJournal.kt | 2 + .../reactor/core/internal/MatchJournalImpl.kt | 1 + .../core/internal/StoreAwareJournal.kt | 13 +- reactor/Test/test/TestStoreAwareJournal.kt | 4 - 7 files changed, 27 insertions(+), 145 deletions(-) diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ConstraintsProcessing.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ConstraintsProcessing.kt index 4d988723..f70a99d2 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ConstraintsProcessing.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ConstraintsProcessing.kt @@ -80,8 +80,6 @@ internal class ConstraintsProcessing( * This method may be called at most once for a fresh state frame. */ fun processActivated(controller: Controller, active: Occurrence, parent: MatchJournal.MatchChunk, inStatus: FeedbackStatus) : FeedbackStatus { - push() - if (!active.stored) { active.stored = true logActivation(active) diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/IncrSpecHolder.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/IncrSpecHolder.kt index ef696501..b6a4d3f0 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/IncrSpecHolder.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/IncrSpecHolder.kt @@ -29,9 +29,11 @@ interface IncrSpecHolder { val Occurrence.isPrincipal get() = ispec.isPrincipal(this.constraint()) val RuleMatch.isPrincipal get() = ispec.isPrincipal(this.rule()) + val Rule.isPrincipal get() = ispec.isPrincipal(this) val RuleMatch.isWeakPrincipal get() = ispec.isWeakPrincipal(this.rule()) + val Rule.isWeakPrincipal get() = ispec.isWeakPrincipal(this) } diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/LogicalState.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/LogicalState.kt index 3cb026c0..50d1426e 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/LogicalState.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/LogicalState.kt @@ -26,30 +26,14 @@ import java.util.Collections.singleton * Serves as a middle layer between logical variables and observers, the latter being notified of changes in the * state in context of the currenly active controller. * Keeps a reference to [Controller] instance, which is used to notify the observers. - * - * NOTE: the following is no longer relevant, as the "stack frame" functionality has been removed. - * - * Internally [LogicalState] maintains a stack of [LogicalStateFrame]s capturing processing - * state related to logical variable observers. The top [LogicalStateFrame] contains - * the current relevant set of [LogicalObserver]s. Events corresponding to - * adding and removing observers are forwarded to the top frame. - * - * Frames are added on events which could require reverting processing state to - * the point before them, which can be achieved by simply dropping a frame and - * thus dropping all added observers. */ class LogicalState : LogicalStateObservable, LogicalObserver { - // invariant: never empty - private val stateFrames = LinkedList() + private val observers = IdentityHashMap, ArrayList>() private var controller: Controller? = null - init { - stateFrames.push(LogicalStateFrame()) - } - internal fun setController(controller: Controller) { assert(this.controller === null) this.controller = controller @@ -61,113 +45,21 @@ class LogicalState : LogicalStateObservable, LogicalObserver return this } - fun currentFrame() : LogicalStateFrame = - stateFrames.first - - fun push() : LogicalStateFrame { - return currentFrame() -// val newFrame = StateFrame(stateFrames.first) -// stateFrames.push(newFrame) -// return newFrame - } - - fun reset() { - // Clear logicals from this forwarding observer on full reset -// currentFrame().observed().forEach { -// it.removeObserver(this) -// } - reset(stateFrames.last) - } - - fun reset(frame: LogicalStateFrame) { - val it = stateFrames.iterator() - while (it.hasNext()) { - val next = it.next() - if (frame === next) { // referential equality - return - } - it.remove() - } - throw IllegalStateException("invalid frame") - } - override fun addForwardingObserver(logical: Logical<*>, observer: ForwardingLogicalObserver) { - if (!currentFrame().isObserving(logical)) { + if (!observers.containsKey(logical)) { logical.addObserver(this) } - currentFrame().addForwardingObserver(logical, observer) - } - - override fun removeForwardingObserver(logical: Logical<*>, observer: ForwardingLogicalObserver) { - currentFrame().removeForwardingObserver(logical, observer) - if (!currentFrame().isObserving(logical)) { - logical.removeObserver(this) - } - } - - override fun removeForwardingObserversWhere(logical: Logical<*>, where: (ForwardingLogicalObserver) -> Boolean) { - currentFrame().removeForwardingObserversWhere(logical, where) - if (!currentFrame().isObserving(logical)) { - logical.removeObserver(this) - } - } - - override fun valueUpdated(logical: Logical<*>) { - // forward to the top frame with added transient state (Controller) - currentFrame().valueUpdated(logical, controller!!) - } - - override fun parentUpdated(logical: Logical<*>) { - // forward to the top frame with added transient state (Controller) - currentFrame().parentUpdated(logical, controller!!) - } - -} - -/** - * NOTE: this class is deprecated and the "stack of frames" functionality is no longer functioning. - * To be removed soon. - * - * A [LogicalStateFrame] captures a logical state corresponding to currently relevant - * observers of changes of logical variables. It roughly corresponds to a bunch of event - * that update a set of such observers. By dropping a frame the processing state - * can be reverted. - * - * The main (and only) example of such event is an occurrence activation and the - * corresponding observer reactivates occurrence on changes of logical variables - * used as its arguments. - * - * @author Fedor Isakov - */ - -@Deprecated("to be removed") -class LogicalStateFrame : LogicalStateObservable, ForwardingLogicalObserver -{ - // FIXME use Vector instead of ConsList -// private var observers: PersMap>, PersList> = Maps.of() - - private val observers = IdentityHashMap, ArrayList>() - -// constructor(prototype: StateFrame) : this() { -// this.observers = prototype.observers -// } - - override fun addForwardingObserver(logical: Logical<*>, observer: ForwardingLogicalObserver) { -// val logicalId = Id(logical) -// this.observers = observers.assoc(logicalId, -// observers[logicalId]?.prepend(observer) ?: Lists.of(observer)) observers.getOrPut(logical) { arrayListOf() }.add(observer) } override fun removeForwardingObserver(logical: Logical<*>, observer: ForwardingLogicalObserver) { -// val logicalId = Id(logical) -// observers[logicalId]?.let { -// this.observers = observers.assoc(logicalId, it.remove(observer)!!) -// } observers[logical]?.apply { removeAll(singleton(observer)) if (isEmpty()) observers.remove(logical) } + if (!observers.containsKey(logical)) { + logical.removeObserver(this) + } } override fun removeForwardingObserversWhere(logical: Logical<*>, where: (ForwardingLogicalObserver) -> Boolean) { @@ -175,27 +67,27 @@ class LogicalStateFrame : LogicalStateObservable, ForwardingLogicalObserver removeIf(where) if (isEmpty()) observers.remove(logical) } - } - - fun observed(): Sequence> = observers.keys.asSequence() -// observers.keys().asSequence().map { it.wrapped } - - fun isObserving(logical: Logical<*>) = observers.containsKey(logical) - - override fun valueUpdated(logical: Logical<*>, controller: Controller) { - observers[logical]?.let { list -> - for (observer in ArrayList(list)) { - observer.valueUpdated(logical, controller) - } + if (!observers.containsKey(logical)) { + logical.removeObserver(this) } } - override fun parentUpdated(logical: Logical<*>, controller: Controller) { + override fun valueUpdated(logical: Logical<*>) { observers[logical]?.let { list -> for (observer in ArrayList(list)) { - observer.parentUpdated(logical, controller) + observer.valueUpdated(logical, controller!!) } } + } -} \ No newline at end of file + override fun parentUpdated(logical: Logical<*>) { + observers[logical]?.let { list -> + for (observer in ArrayList(list)) { + observer.parentUpdated(logical, controller!!) + } + } + + } + +} 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 468ed2f8..97c3e69f 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 @@ -81,12 +81,14 @@ interface MatchJournal : EvidenceSource { /** * Same as [resetCursor], moves [cursor] before [initialChunk]. */ + @Deprecated("Obsolete TBR") fun resetCursor() = resetCursor(Pos(initialChunk(), 0)) /** * Moves [cursor] before [pastPos], so that [ChunkReader.next] is [pastPos]. * Doesn't modify journal contents, as opposed to [reset]. */ + @Deprecated("Obsolete TBR") fun resetCursor(pastPos: Pos) /** 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 408c4d7f..1250d5fa 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 @@ -207,6 +207,7 @@ internal open class MatchJournalImpl( reset(pastPos, false) } + // TBR private fun reset(pastPos: MatchJournal.Pos, removing: Boolean) { __cursor.moveToPastRemoving(pastPos) { popAncestor() diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/StoreAwareJournal.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/StoreAwareJournal.kt index 20a49d00..32e9eb6b 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/StoreAwareJournal.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/StoreAwareJournal.kt @@ -26,9 +26,7 @@ import java.lang.IllegalArgumentException */ interface StoreAwareJournal : MatchJournal, LogicalStateObservable { - // Only for testing push() in Impl - fun testPush() - + @Deprecated("Obsolete") fun resetStore() // for tests @@ -47,31 +45,24 @@ internal open class StoreAwareJournalImpl(private val journal: MatchJournal, { private class FramePos( - val frame: LogicalStateFrame, chunk: MatchJournal.Chunk, entriesCount: Int = 0 ) : MatchJournal.Pos(chunk, entriesCount) - fun push() = logicalState.push() - - override fun testPush() { logicalState.push() } - // Reset only store & history position, don't modify history override fun resetStore() { - logicalState.reset() this.resetCursor() } override fun currentPos(): MatchJournal.Pos = - FramePos(logicalState.currentFrame(), journal.currentPos().chunk, journal.currentPos().entriesCount) + FramePos(journal.currentPos().chunk, journal.currentPos().entriesCount) // Throw away recently added chunks and reset store accordingly // NB: not checking that chunks are actually recently added, from this exec session override fun reset(pastPos: MatchJournal.Pos) { if (pastPos is FramePos) { - logicalState.reset(pastPos.frame) journal.reset(pastPos) } else { throw IllegalArgumentException() diff --git a/reactor/Test/test/TestStoreAwareJournal.kt b/reactor/Test/test/TestStoreAwareJournal.kt index efbe247b..2a00c233 100644 --- a/reactor/Test/test/TestStoreAwareJournal.kt +++ b/reactor/Test/test/TestStoreAwareJournal.kt @@ -357,7 +357,6 @@ class TestStoreAwareJournal { // execute program - hist.testPush() logExpand("foo") logFirstMatch() @@ -433,8 +432,6 @@ class TestStoreAwareJournal { hist.currentPos().chunk shouldBeSame curChunk - // push happens before constraints in body are activated - hist.testPush() // last production from rule2 logExpand(quxOcc) @@ -450,7 +447,6 @@ class TestStoreAwareJournal { val oldStore = hist.storeView().allOccurrences() val savedPos = hist.currentPos() - hist.testPush() logExpandJustified("last") hist.view().chunks.size shouldBe 5 + initialJournalSize