Provide better (hopefully) name & explanation, undeprecate and provide missing impl for 'forgetSeen' method

This commit is contained in:
Grigorii Kirgizov 2019-10-15 21:17:24 +03:00
parent bc06bf39cc
commit a095b2e222
5 changed files with 29 additions and 13 deletions

View File

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

View File

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

View File

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

View File

@ -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<ReteNode> {
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<RuleMatchImpl> {
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
}

View File

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