From 5414fe7e674e323cfce774d3d7ef283b7cbce615 Mon Sep 17 00:00:00 2001 From: Grigorii Kirgizov Date: Tue, 9 Jul 2019 16:09:48 +0300 Subject: [PATCH] Move RulesDiff construction from reactor to coderules runtime. --- .../mps/logic/reactor/core/RulesDiff.kt | 6 +++--- .../core/internal/EvaluationSessionImpl.kt | 17 ++++++++++++++--- .../core/internal/ProcessingStateImpl.kt | 2 +- .../reactor/evaluation/EvaluationSession.java | 3 +++ .../mps/logic/reactor/program/Program.java | 10 ++++++++++ reactor/Test/test/TestIncrementalProgram.kt | 2 ++ 6 files changed, 33 insertions(+), 7 deletions(-) diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/RulesDiff.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/RulesDiff.kt index 83f57fe1..9f076ecf 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/RulesDiff.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/RulesDiff.kt @@ -34,11 +34,11 @@ data class RulesDiff( val oldSet = old.toHashSet() val newSet = new.toHashSet() - val added = new.filter { !oldSet.contains(it.uniqueTag()) } - val removed = oldSet.minus(newSet.map { it.uniqueTag() }) + val added: List = new.filter { !oldSet.contains(it.uniqueTag()) } + val removed: Set = oldSet.minus(newSet.map { it.uniqueTag() }) val removedDeps = HashSet() - for (rule in new) { + for (rule in added) { for (depRule in ruleDeps.getDependentRules(rule)) { if (!removed.contains(depRule)) { removedDeps.add(depRule) 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 8a74d548..d3eb9c5d 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 @@ -42,7 +42,11 @@ internal class EvaluationSessionImpl private constructor ( override fun controller() = controller - private fun incrLaunch(main: Constraint, profiler: Profiler?, token: SessionToken?, ispec: IncrementalProgramSpec) : FeedbackStatus { + private fun launch( + main: Constraint, profiler: Profiler?, + token: SessionToken?, rulesDiff: RulesDiff, ispec: IncrementalProgramSpec + ) : FeedbackStatus { + val ruleIndex = RuleIndex(program().handlers()) val dispatcher = Dispatcher(ruleIndex) @@ -64,7 +68,6 @@ internal class EvaluationSessionImpl private constructor ( ruleIndex, ispec, trace, profiler ) - val rulesDiff = RulesDiff.findDiff(tkn.ruleTags, ruleIndex) this.controller = ControllerImpl(supervisor, state, ispec, trace, profiler) return controller.incrLaunch(main, rulesDiff) } @@ -80,6 +83,8 @@ internal class EvaluationSessionImpl private constructor ( var token: SessionToken? = null + var rulesDiff: RulesDiff = RulesDiff.emptyDiff() + override fun withTrace(computingTracer: EvaluationTrace): EvaluationSession.Config { this.evaluationTrace = computingTracer return this @@ -93,6 +98,12 @@ internal class EvaluationSessionImpl private constructor ( this.token = token return this } + + override fun withRulesDiff(rulesDiff: RulesDiff): EvaluationSession.Config { + this.rulesDiff = rulesDiff + return this + } + override fun withIncrSpec(ispec: IncrementalProgramSpec): EvaluationSession.Config { this.ispec = ispec return this @@ -117,7 +128,7 @@ internal class EvaluationSessionImpl private constructor ( var failure: Feedback? = null try { val main = parameters[ParameterKey.of("main", Constraint::class.java)] as Constraint - val status = session.incrLaunch(main, profiler, token, ispec) + val status = session.launch(main, profiler, token, rulesDiff, ispec) if (status is FAILED) { failure = status.failure } 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 a858bd47..2c08b758 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 @@ -71,12 +71,12 @@ internal class ProcessingStateImpl(private var dispatchingFront: Dispatcher.Disp private data class MatchCandidate(val rule: Rule, val occChunk: MatchJournal.OccChunk) - fun invalidateDependentRules(ruleIds: Set) { val it = this.iterator() while (it.hasNext()) { val chunk = it.next() if (chunk is MatchJournal.MatchChunk && ruleIds.contains(chunk.match.rule().uniqueTag())) { + trace.invalidate(chunk.match) it.remove() } } diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/EvaluationSession.java b/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/EvaluationSession.java index 87e5e5ab..e7c7a9cc 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/EvaluationSession.java +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/EvaluationSession.java @@ -17,6 +17,7 @@ package jetbrains.mps.logic.reactor.evaluation; +import jetbrains.mps.logic.reactor.core.RulesDiff; import jetbrains.mps.logic.reactor.program.IncrementalProgramSpec; import jetbrains.mps.logic.reactor.program.Program; @@ -97,6 +98,8 @@ public abstract class EvaluationSession { public Config withSessionToken(SessionToken token) { return this; } + public Config withRulesDiff(RulesDiff rulesDiff) { return this; } + public Config withIncrSpec(IncrementalProgramSpec ispec) { return this; } public abstract EvaluationResult start(Supervisor supervisor); diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/program/Program.java b/reactor/Core/src/jetbrains/mps/logic/reactor/program/Program.java index 7dae7eb4..a6b3d4b8 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/program/Program.java +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/program/Program.java @@ -16,6 +16,7 @@ package jetbrains.mps.logic.reactor.program; +import java.util.ArrayList; /** * A collection of handlers that constitute a constraint rules program. @@ -29,4 +30,13 @@ public abstract class Program { public abstract Iterable handlers(); + public Iterable rules() { + ArrayList allRules = new ArrayList(); + for (RulesList rulesList : handlers()) { + for (Rule rule : rulesList.rules()) { + allRules.add(rule); + } + } + return allRules; + }; } diff --git a/reactor/Test/test/TestIncrementalProgram.kt b/reactor/Test/test/TestIncrementalProgram.kt index 1a7ae655..37c40ff5 100644 --- a/reactor/Test/test/TestIncrementalProgram.kt +++ b/reactor/Test/test/TestIncrementalProgram.kt @@ -1,6 +1,7 @@ import jetbrains.mps.logic.reactor.program.IncrementalProgramSpec import jetbrains.mps.logic.reactor.core.Occurrence import jetbrains.mps.logic.reactor.core.ReactorLifecycle +import jetbrains.mps.logic.reactor.core.RulesDiff import jetbrains.mps.logic.reactor.core.internal.MatchJournal import jetbrains.mps.logic.reactor.evaluation.SessionToken import jetbrains.mps.logic.reactor.evaluation.EvaluationResult @@ -66,6 +67,7 @@ class TestIncrementalProgram { .withParameter(EvaluationSession.ParameterKey.of("main", Constraint::class.java), MockConstraint(ConstraintSymbol("main", 0))) .withIncrSpec(incrSpec) .withSessionToken(sessionToken) + .withRulesDiff(RulesDiff.findDiff(sessionToken.ruleTags, this.rules)) .start(MockSupervisor()) result.feedback()?.let { if (it.isFailure) throw it.failureCause() } resultHandler(result)