Drop obsolete deprecated class, simplify code

This commit is contained in:
Fedor Isakov 2022-03-07 19:16:05 +01:00
parent cf4e9b2b33
commit 945088f72e
7 changed files with 27 additions and 145 deletions

View File

@ -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)

View File

@ -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)
}

View File

@ -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<LogicalStateFrame>()
private val observers = IdentityHashMap<Logical<*>, ArrayList<ForwardingLogicalObserver>>()
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<Id<Logical<*>>, PersList<LogicalObserver>> = Maps.of()
private val observers = IdentityHashMap<Logical<*>, ArrayList<ForwardingLogicalObserver>>()
// 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<Logical<*>> = 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!!)
}
}
}
}
override fun parentUpdated(logical: Logical<*>) {
observers[logical]?.let { list ->
for (observer in ArrayList(list)) {
observer.parentUpdated(logical, controller!!)
}
}
}
}

View File

@ -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)
/**

View File

@ -207,6 +207,7 @@ internal open class MatchJournalImpl(
reset(pastPos, false)
}
// TBR
private fun reset(pastPos: MatchJournal.Pos, removing: Boolean) {
__cursor.moveToPastRemoving(pastPos) {
popAncestor()

View File

@ -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()

View File

@ -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