From a0b2af95c942b87ef7555108cc071756ef2fa5cc Mon Sep 17 00:00:00 2001 From: Fedor Isakov Date: Mon, 4 Nov 2019 15:58:54 +0100 Subject: [PATCH] Revert "MPSCR-4: Upd refs to new RuleIndex on Dispatcher construction in incremental case." This reverts commit 0dd7f8c70f7f94b70ecec5d496ae88aeff022ee0. --- .../generator/template/main@generator.mps | 40 +++++++------------ reactor/.idea/misc.xml | 2 +- .../mps/logic/reactor/core/Dispatcher.kt | 23 +++++++---- .../mps/logic/reactor/core/RuleMatcher.kt | 7 +--- .../core/internal/EvaluationSessionImpl.kt | 5 ++- .../core/internal/ReteRuleMatcherImpl.kt | 10 +---- .../reactor/core/internal/RuleMatcherImpl.kt | 11 +---- .../reactor/core/internal/SessionTokenImpl.kt | 2 +- .../reactor/evaluation/SessionToken.java | 2 + 9 files changed, 41 insertions(+), 61 deletions(-) diff --git a/coderules/languages/jetbrains.mps.lang.coderules/generator/template/main@generator.mps b/coderules/languages/jetbrains.mps.lang.coderules/generator/template/main@generator.mps index d3e807b6..aeebecee 100644 --- a/coderules/languages/jetbrains.mps.lang.coderules/generator/template/main@generator.mps +++ b/coderules/languages/jetbrains.mps.lang.coderules/generator/template/main@generator.mps @@ -5067,26 +5067,22 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + @@ -5094,14 +5090,6 @@ - - - - - - - - diff --git a/reactor/.idea/misc.xml b/reactor/.idea/misc.xml index 8f4acde0..e28f1b3a 100644 --- a/reactor/.idea/misc.xml +++ b/reactor/.idea/misc.xml @@ -45,7 +45,7 @@ - + \ No newline at end of file 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 4b79cb2e..37d4e8b1 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/Dispatcher.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/Dispatcher.kt @@ -17,24 +17,21 @@ package jetbrains.mps.logic.reactor.core -typealias DispatchingFrontState = Map +typealias DispatchingFrontState = Map /** * A front-end interface to [RuleMatcher]. * * @author Fedor Isakov */ -class Dispatcher (val ruleIndex: RuleIndex, prevState: DispatchingFrontState = emptyMap()) { +class Dispatcher (val ruleIndex: RuleIndex) { private val ruletag2matcher = HashMap() init { ruleIndex.forEach { rule -> - val ruletag = rule.uniqueTag() - // reset references to stale RuleIndex instances - val matcher = prevState[ruletag]?.apply { setRuleLookup(ruleIndex) } - ?: createRuleMatcher(ruleIndex, ruletag) - ruletag2matcher.put(ruletag, matcher); + val matcher = createRuleMatcher(ruleIndex, rule.uniqueTag()) + ruletag2matcher.put(rule.uniqueTag(), matcher); } } @@ -43,6 +40,8 @@ class Dispatcher (val ruleIndex: RuleIndex, prevState: DispatchingFrontState = e */ fun front() = DispatchingFront() + fun frontFromState(predState: DispatchingFrontState) = DispatchingFront(predState) + inner class DispatchingFront { private val ruletag2probe : HashMap @@ -57,6 +56,14 @@ class Dispatcher (val ruleIndex: RuleIndex, prevState: DispatchingFrontState = e } } + constructor(predState: DispatchingFrontState) { + this.ruletag2probe = hashMapOf() + this.matching = null + ruletag2matcher.entries.forEach { e -> + ruletag2probe[e.key] = predState[e.key] ?: e.value.probe() + } + } + private constructor(pred: DispatchingFront, matching: Iterable? = null) { this.ruletag2probe = pred.ruletag2probe this.matching = matching @@ -76,7 +83,7 @@ class Dispatcher (val ruleIndex: RuleIndex, prevState: DispatchingFrontState = e return allMatches } - fun state() : DispatchingFrontState = ruletag2matcher //.asMap() + fun state() : DispatchingFrontState = ruletag2probe //.asMap() /** * Returns a [DispatchingFront] instance that is "expanded" with matches corresponding to the diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/RuleMatcher.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/RuleMatcher.kt index 856d14e3..35f7737a 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/RuleMatcher.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/RuleMatcher.kt @@ -26,19 +26,14 @@ import jetbrains.mps.logic.reactor.program.Rule * Abstracts a rule matching algorithm. * * A RuleMatcher corresponds to a single rule. - * The method [newProbe] provides a factory of [RuleMatchingProbe], which is the main handler of match operations. - * The method [probe] provides access to a last created instance of [RuleMatchingProbe] or creates a first one. + * The method [probe] provides a factory of [RuleMatchingProbe], which is the main handler of match operations. * * @author Fedor Isakov */ interface RuleMatcher { - fun newProbe(): RuleMatchingProbe - fun probe(): RuleMatchingProbe - fun setRuleLookup(ruleLookup: RuleLookup): Unit - } fun createRuleMatcher(lookup: RuleLookup, tag: Any): RuleMatcher = ReteRuleMatcherImpl(lookup, tag) diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/EvaluationSessionImpl.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/EvaluationSessionImpl.kt index 10d1e982..b200fd5a 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/EvaluationSessionImpl.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/EvaluationSessionImpl.kt @@ -47,10 +47,11 @@ internal class EvaluationSessionImpl private constructor ( ) : FeedbackStatus { val ruleIndex = RuleIndex(program.handlers()) + val dispatcher = Dispatcher(ruleIndex) if (ispec is IncrementalProgramSpec.NonIncrSpec || token == null) { val state = ProcessingStateImpl( - Dispatcher(ruleIndex).front(), + dispatcher.front(), MatchJournalImpl(ispec), ruleIndex, ispec, trace, profiler ) @@ -61,7 +62,7 @@ internal class EvaluationSessionImpl private constructor ( } else { val tkn = token as SessionTokenImpl val state = ProcessingStateImpl( - Dispatcher(ruleIndex, tkn.getFrontState()).front(), + dispatcher.frontFromState(tkn.frontState), MatchJournalImpl(ispec, tkn.journalView), ruleIndex, ispec, trace, profiler ) 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 d2f3a28e..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 @@ -41,7 +41,7 @@ fun footprintOf(): Footprint = TIntHashSet() fun Signature.toFootprint() = TIntHashSet(this) -internal class ReteRuleMatcherImpl(private var ruleLookup: RuleLookup, +internal class ReteRuleMatcherImpl(private val ruleLookup: RuleLookup, private val tag: Any) : RuleMatcher { @@ -51,13 +51,7 @@ internal class ReteRuleMatcherImpl(private var ruleLookup: RuleLookup, fun lookupRule(): Rule = ruleLookup.lookupRuleByTag(tag) ?: throw IllegalStateException("can't lookup rule by tag: '${tag}'") - override fun setRuleLookup(ruleLookup: RuleLookup) { this.ruleLookup = ruleLookup } - - override fun newProbe(): RuleMatchingProbe = ReteNetwork(head.size).also { probe = it } - - override fun probe(): RuleMatchingProbe = probe ?: newProbe() - - private var probe: RuleMatchingProbe? = null + override fun probe(): ReteNetwork = ReteNetwork(head.size) inner class ReteNetwork(val headSize: Int) : RuleMatchingProbe { 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 f8904475..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 @@ -33,7 +33,7 @@ import kotlin.collections.ArrayList * * @author Fedor Isakov */ -internal class RuleMatcherImpl(private var ruleLookup: RuleLookup, +internal class RuleMatcherImpl(private val ruleLookup: RuleLookup, private val tag: Any) : RuleMatcher { @@ -43,19 +43,12 @@ internal class RuleMatcherImpl(private var ruleLookup: RuleLookup, fun lookupRule(): Rule = ruleLookup.lookupRuleByTag(tag) ?: throw IllegalStateException("can't lookup rule by tag: '${tag}'") - override fun setRuleLookup(ruleLookup: RuleLookup) { this.ruleLookup = ruleLookup } - - override fun newProbe(): RuleMatchingProbe = + override fun probe(): RuleMatchingProbe = RuleMatchFront(emptyList(), emptyList(), Sets.of(IntArray(head.size).toSignature()), Sets.of(), Sets.of()) - .also { probe = it } - - override fun probe(): RuleMatchingProbe = probe ?: newProbe() - - private var probe: RuleMatchingProbe? = null inner class RuleMatchFront(private val trunkNodes: List, private val leafNodes: List, diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/SessionTokenImpl.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/SessionTokenImpl.kt index d3b90555..c7da708e 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/SessionTokenImpl.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/SessionTokenImpl.kt @@ -27,5 +27,5 @@ data class SessionTokenImpl( ) : SessionToken { override fun getJournalView(): MatchJournalView = journalView override fun getRuleTags(): Iterable = ruleTags - fun getFrontState(): DispatchingFrontState = frontState + override fun getFrontState(): DispatchingFrontState = frontState } diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/SessionToken.java b/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/SessionToken.java index b4b7f0f5..159e6b47 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/SessionToken.java +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/SessionToken.java @@ -24,4 +24,6 @@ public interface SessionToken { MatchJournalView getJournalView(); @NotNull() Iterable getRuleTags(); + @NotNull() + Map getFrontState(); }