From a095b2e22257a4a0b2f1ec7384914b1a14836f9c Mon Sep 17 00:00:00 2001 From: Grigorii Kirgizov Date: Tue, 15 Oct 2019 21:17:24 +0300 Subject: [PATCH] Provide better (hopefully) name & explanation, undeprecate and provide missing impl for 'forgetSeen' method --- .../mps/logic/reactor/core/Dispatcher.kt | 13 ++++++++----- .../mps/logic/reactor/core/RuleMatchingProbe.kt | 9 +++++---- .../reactor/core/internal/ProcessingStateImpl.kt | 2 +- .../reactor/core/internal/ReteRuleMatcherImpl.kt | 16 ++++++++++++++-- .../reactor/core/internal/RuleMatcherImpl.kt | 2 +- 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/Dispatcher.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/Dispatcher.kt index d2d3516b..37d4e8b1 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/Dispatcher.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/Dispatcher.kt @@ -103,13 +103,16 @@ class Dispatcher (val ruleIndex: RuleIndex) { .map { probe -> probe.contract(discarded) }) /** - * Returns a [DispatchingFront] instance which "forgot" that it has seen occurrence. - * Needed for incremental reactivations to discern them from reactivations due to logicals. + * Returns a [DispatchingFront] instance which "forgot" that it already expanded the occurrence. + * After that the occurrence can be expanded again without triggering observer reactivation logic. + * Needed to discern incremental reactivation from observers reactivation. + * In other words, the state of [DispatchingFront] connected with this occurrence + * transitions from "fully-expanded" to "partly-expanded". */ - internal fun forgetSeen(dropped: Occurrence): DispatchingFront = DispatchingFront(this, + internal fun forgetExpanded(dropped: Occurrence): DispatchingFront = DispatchingFront(this, ruleIndex.forOccurrence(dropped) .mapNotNull { rule -> ruletag2probe[rule.uniqueTag()] } - .map { probe -> probe.forgetSeen(dropped) } + .map { probe -> probe.forgetExpanded(dropped) } ) /** @@ -120,7 +123,7 @@ class Dispatcher (val ruleIndex: RuleIndex) { internal fun forget(dropped: Occurrence): DispatchingFront = DispatchingFront(this, ruleIndex.forOccurrence(dropped) .mapNotNull { rule -> ruletag2probe[rule.uniqueTag()] } - .map { probe -> probe.contract(dropped).forgetSeen(dropped).forgetConsumed(dropped) }) + .map { probe -> probe.contract(dropped).forgetExpanded(dropped).forgetConsumed(dropped) }) /** * Serves to indicate that the specified [RuleMatchEx] has been processed (consumed) and has to diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/RuleMatchingProbe.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/RuleMatchingProbe.kt index 9e587a44..acc7370b 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/RuleMatchingProbe.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/RuleMatchingProbe.kt @@ -44,11 +44,12 @@ interface RuleMatchingProbe : RuleMatchingProbeState { fun contract(occ: Occurrence): RuleMatchingProbe /** - * The purpose and usages of this method are obscure. - * One of the implementations is a NOP. + * Turns the Probe's processing-related state linked with + * this occurrence from "fully-expanded" to "partly-expanded". + * Doesn't modify internal matches-related state. + * Instead modifies how the next "expansion" of this occurrence will be processed. */ - @Deprecated("hacky stuff") - fun forgetSeen(occ: Occurrence): RuleMatchingProbe + fun forgetExpanded(occ: Occurrence): RuleMatchingProbe fun forgetConsumed(occ: Occurrence): RuleMatchingProbe diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ProcessingStateImpl.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ProcessingStateImpl.kt index 5783e380..20fb019a 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ProcessingStateImpl.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ProcessingStateImpl.kt @@ -64,7 +64,7 @@ internal class ProcessingStateImpl(private var dispatchingFront: Dispatcher.Disp // Forget that occ was seen. Otherwise it will be // processed as with reactivation through observers. // Incremental reactivation should proceed more like usual activation. - this.dispatchingFront = dispatchingFront.forgetSeen(activeOcc) + this.dispatchingFront = dispatchingFront.forgetExpanded(activeOcc) trace.reactivateIncremental(activeOcc) return controller.reactivate(activeOcc) } diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ReteRuleMatcherImpl.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ReteRuleMatcherImpl.kt index 235c29be..0c3f694c 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ReteRuleMatcherImpl.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ReteRuleMatcherImpl.kt @@ -197,6 +197,10 @@ internal class ReteRuleMatcherImpl(private val ruleLookup: RuleLookup, return footprint.contains(occ.identity) } + fun forgetContains(occ: Occurrence): Boolean { + return footprint.remove(occ.identity) + } + fun ownNodes() : Iterable { complete() return nodesList.subList(startIdx, nodesList.size) @@ -293,7 +297,7 @@ internal class ReteRuleMatcherImpl(private val ruleLookup: RuleLookup, } fun introduce(occurrence: Occurrence, headPosMask: BitSet): Generation { - val reactivated = layers.first().containsOccurrence(occurrence) + val reactivated = layers.first().containsOccurrence(occurrence) // propagation history if (propagation && reactivated) { @@ -332,6 +336,13 @@ internal class ReteRuleMatcherImpl(private val ruleLookup: RuleLookup, return Generation(newLayers) } + /* + * Allows to avoid triggering reactivation logic in the next call of "introduce" for this occurrence. + */ + fun forgetIntroduced(occurrence: Occurrence) { + layers.first().forgetContains(occurrence) + } + fun matches(): Collection { val topLayer = layers.first() if (topLayer.final) { @@ -377,7 +388,8 @@ internal class ReteRuleMatcherImpl(private val ruleLookup: RuleLookup, return this } - override fun forgetSeen(occ: Occurrence): ReteNetwork { + override fun forgetExpanded(occ: Occurrence): ReteNetwork { + this.lastGeneration.forgetIntroduced(occ) return this } diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/RuleMatcherImpl.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/RuleMatcherImpl.kt index daf49a9d..7846d17e 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/RuleMatcherImpl.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/RuleMatcherImpl.kt @@ -154,7 +154,7 @@ internal class RuleMatcherImpl(private val ruleLookup: RuleLookup, return RuleMatchFront(trunkNodes, leafNodes, leafSignatures, seenOccurrences, newConsumed) // NB: not modifying genId } - override fun forgetSeen(occ: Occurrence): RuleMatchFront { + override fun forgetExpanded(occ: Occurrence): RuleMatchFront { val newSeen = seenOccurrences.without(occ.identity) return RuleMatchFront(trunkNodes, leafNodes, leafSignatures, newSeen, consumedSignatures) // NB: not modifying genId }