Avoid duplicate subscription of Occurrences on LogicalState

Previously it was done twice: at Occurence creation
and in ConstraintsProcessing.processActivated.
Occurrences now constructed in 'dead' state (without observers).
This commit is contained in:
Grigorii Kirgizov 2020-04-22 15:00:30 +03:00
parent 17de7bf9fe
commit 49d8507eed
2 changed files with 13 additions and 9 deletions

View File

@ -39,14 +39,14 @@ class Occurrence (observable: LogicalStateObservable,
ConstraintOccurrence, ForwardingLogicalObserver, Justified
{
var alive = true
var alive = false
var stored = false
val identity = System.identityHashCode(this)
init {
revive(observable)
// revive(observable)
}
override fun constraint(): Constraint = constraint
@ -64,18 +64,22 @@ class Occurrence (observable: LogicalStateObservable,
override fun parentUpdated(logical: Logical<*>, controller: Controller) = doReactivate(controller)
fun terminate(observable: LogicalStateObservable) {
for (a in arguments) {
if (a is Logical<*>) {
observable.removeForwardingObserver(a, this)
if (alive) {
for (a in arguments) {
if (a is Logical<*>) {
observable.removeForwardingObserver(a, this)
}
}
}
alive = false
}
fun revive(observable: LogicalStateObservable) {
for (a in arguments) {
if (a is Logical<*>) {
observable.addForwardingObserver(a, this)
if (!alive) {
for (a in HashSet(arguments)) { // avoid duplicate subscriptions
if (a is Logical<*>) {
observable.addForwardingObserver(a, this)
}
}
}
alive = true

View File

@ -212,7 +212,6 @@ internal class ConstraintsProcessing(private var dispatchingFront: Dispatcher.Di
*/
fun processActivated(controller: Controller, active: Occurrence, parent: MatchJournal.MatchChunk, inStatus: FeedbackStatus) : FeedbackStatus {
push()
assert(active.alive)
val activationChunk: MatchJournal.OccChunk?
if (!active.stored) {
@ -223,6 +222,7 @@ internal class ConstraintsProcessing(private var dispatchingFront: Dispatcher.Di
// defined (not null) & needed only for incremental execution
activationChunk = journalIndex.activatingChunkOf(active)
}
assert(active.alive)
profiler.profile("dispatch_${active.constraint().symbol()}") {