From 88067571d07b2d3ea20d1710dce49e9da4c96f90 Mon Sep 17 00:00:00 2001 From: Grigorii Kirgizov Date: Tue, 24 Nov 2020 11:43:44 +0300 Subject: [PATCH] Provide as eval result tags of invalidated rules. for MPSCR-82 Relevant for Incremental strategy. Allows to more precisely track invalidated types of nodes. Type of node is invalidated if rules with this origin were invalidated during incremental processing. --- .../core/internal/EvaluationSessionImpl.kt | 6 ++- .../reactor/core/internal/IncrementalStage.kt | 38 +++++++++++-------- .../core/internal/ProcessingStrategy.kt | 15 +++++++- .../reactor/evaluation/EvaluationResult.java | 2 + 4 files changed, 43 insertions(+), 18 deletions(-) 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 ccfb6bb7..65bffc43 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 @@ -129,7 +129,7 @@ internal class EvaluationSessionImpl private constructor ( val status = run(main) controller.shutDown() val newToken = endSession(session) - return EvaluationResultImpl(newToken, status, strategy.invalidatedFeedback()) + return EvaluationResultImpl(newToken, status, strategy.invalidatedFeedback(), strategy.invalidatedRules()) } private fun SessionParts.run(main: Constraint): FeedbackStatus = strategy.run(processing, controller, main) @@ -329,12 +329,14 @@ internal class EvaluationSessionImpl private constructor ( private class EvaluationResultImpl( val token: SessionToken, val status: FeedbackStatus, - val invalidFeedbackKeys: FeedbackKeySet + val invalidFeedbackKeys: FeedbackKeySet, + val invalidRules: Collection ): EvaluationResult { override fun token() = token override fun storeView(): StoreView = token.journalView.storeView override fun feedback(): EvaluationFeedback? = if (status is FAILED) status.failure else null override fun invalidFeedbackKeys(): Collection = invalidFeedbackKeys + override fun invalidRules(): Collection = invalidRules } diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/IncrementalStage.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/IncrementalStage.kt index 9dffb368..20b9cbfb 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/IncrementalStage.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/IncrementalStage.kt @@ -55,7 +55,12 @@ internal class InvalidationStage( private val invalidFeedbackKeys: MutableSet = mutableSetOf() - fun invalidatedFeedback(): FeedbackKeySet = HashSet(invalidFeedbackKeys) + private val invalidRuleIdsAll: MutableList = mutableListOf() + + + fun invalidatedFeedback(): FeedbackKeySet = invalidFeedbackKeys.toHashSet() + + fun invalidatedRules(): List = invalidRuleIdsAll.toList() fun receive(invalid: Iterable) { invalidJustifications.addAll(invalid) } @@ -89,28 +94,31 @@ internal class InvalidationStage( val validOccs: Sequence if (chunk is MatchJournal.MatchChunk) { - trace.invalidate(chunk.match) + with (chunk.match) { + trace.invalidate(this) - invalidFeedbackKeys.add(chunk.match.feedbackKey) - stateCleaner.erase(chunk.match as RuleMatchEx) + invalidFeedbackKeys.add(feedbackKey) + invalidRuleIdsAll.add(rule().uniqueTag()) - // Valid head occurrences could match more rules - // without this match, so need to reactivate them. - // E.g. occurrences discarded in this match on - // previous run but revived here can match more rules. - validOccs = chunk.match.allHeads().filter { occ -> - !occ.justifiedByAny(invalidJustifications) + stateCleaner.erase(this as RuleMatchEx) + + // Valid head occurrences could match more rules + // without this match, so need to reactivate them. + // E.g. occurrences discarded in this match on + // previous run but revived here can match more rules. + validOccs = allHeads().filter { occ -> + !occ.justifiedByAny(invalidJustifications) + } + // By definition of Chunk and principal rule, + // all occurrences from the head are principal. + assert(allHeads().all { it.isPrincipal }) } - // By definition of Chunk and principal rule, - // all occurrences from the head are principal. - assert(chunk.match.allHeads().all { it.isPrincipal }) - } else validOccs = emptySequence() return validOccs.asIterable() } private fun MatchJournal.MatchChunk.dependsOnAny(utags: Iterable): Boolean = - utags.contains(this.ruleUniqueTag) || utags.any { utag -> dependsOnRule(utag) } + utags.contains(ruleUniqueTag) || utags.any(::dependsOnRule) } diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ProcessingStrategy.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ProcessingStrategy.kt index 42510687..13fe6e5d 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ProcessingStrategy.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ProcessingStrategy.kt @@ -34,6 +34,11 @@ internal interface ProcessingStrategy { */ fun invalidatedFeedback(): FeedbackKeySet + /** + * Unique tags of principal rules which matches where invalidated. + */ + fun invalidatedRules(): List + /** * Entry point for processing session. */ @@ -59,6 +64,8 @@ internal class NonIncrementalProcessing: ProcessingStrategy { override fun invalidatedFeedback(): FeedbackKeySet = emptySet() + override fun invalidatedRules(): List = emptyList() + /** * Simply redirects evaluation to [Controller]. */ @@ -108,7 +115,11 @@ internal class IncrementalProcessing( private val postponer = PostponeMatchesStage(ispec, journal, journalIndex, ruleOrdering) - override fun invalidatedFeedback(): FeedbackKeySet = invalidator.invalidatedFeedback() + override fun invalidatedFeedback(): FeedbackKeySet = + invalidator.invalidatedFeedback() + + override fun invalidatedRules(): List = + invalidator.invalidatedRules() override fun processMatch(match: RuleMatchEx) = continueReplacedHeadsImpl(match) @@ -196,6 +207,8 @@ internal class PreambleProcessing( override fun invalidatedFeedback(): FeedbackKeySet = emptySet() + override fun invalidatedRules(): List = emptyList() + override fun processMatch(match: RuleMatchEx) = Unit override fun processOccurrenceMatches(active: Occurrence, matches: List) = matches diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/EvaluationResult.java b/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/EvaluationResult.java index afb88fc9..d30d4a78 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/EvaluationResult.java +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/EvaluationResult.java @@ -31,4 +31,6 @@ public interface EvaluationResult { public Collection invalidFeedbackKeys(); + public Collection invalidRules(); + }