From bfcee33ec98f5bded35d8ca26a882d1b73e455b2 Mon Sep 17 00:00:00 2001 From: Grigorii Kirgizov Date: Wed, 6 Nov 2019 17:40:04 +0300 Subject: [PATCH] Modify SessionToken to preserve old rules instead of tags & RulesDiff to also distinguish preserved rules --- .../mps/logic/reactor/core/RulesDiff.kt | 29 ++++++++++++------- .../core/internal/EvaluationSessionImpl.kt | 11 ++----- .../core/internal/ProcessingStateImpl.kt | 3 +- .../reactor/core/internal/SessionTokenImpl.kt | 7 +++-- .../reactor/evaluation/EvaluationSession.java | 2 -- .../reactor/evaluation/SessionToken.java | 6 ++-- .../mps/logic/reactor/program/Program.java | 6 ++++ reactor/Test/src/program/MockProgram.kt | 9 ++++++ reactor/Test/test/TestIncrementalProgram.kt | 6 ++-- 9 files changed, 48 insertions(+), 31 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 9f076ecf..87c58a99 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/RulesDiff.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/RulesDiff.kt @@ -20,37 +20,46 @@ import jetbrains.mps.logic.reactor.program.DependentRulesSpec import jetbrains.mps.logic.reactor.program.Rule -data class RulesDiff( +class RulesDiff( + preserved: Iterable, val added: Iterable, val removed: Set, val removedDependent: Set ) { + private val preserved: Map = HashMap().apply { + preserved.forEach { put(it.uniqueTag(), it) } + } + + fun getPreservedRule(utag: Any): Rule? = preserved[utag] + companion object { @JvmStatic - fun emptyDiff() = RulesDiff(emptyList(), emptySet(), emptySet()) + fun emptyDiff() = RulesDiff(emptyList(), emptyList(), emptySet(), emptySet()) @JvmStatic - fun findDiff(old: Iterable, new: Iterable, ruleDeps: DependentRulesSpec): RulesDiff { - val oldSet = old.toHashSet() - val newSet = new.toHashSet() + fun findDiff(old: Iterable, new: Iterable, ruleDeps: DependentRulesSpec): RulesDiff { + val oldTagsSet = old.map { it.uniqueTag() }.toHashSet() + val newTagsSet = new.map { it.uniqueTag() }.toHashSet() - val added: List = new.filter { !oldSet.contains(it.uniqueTag()) } - val removed: Set = oldSet.minus(newSet.map { it.uniqueTag() }) + val added = new.filter { !oldTagsSet.contains(it.uniqueTag()) } + val (preserved, removed) = old.partition { newTagsSet.contains(it.uniqueTag()) } + val removedTags: Set = removed.map { it.uniqueTag() }.toSet() + //fixme: remove val removedDeps = HashSet() for (rule in added) { for (depRule in ruleDeps.getDependentRules(rule)) { - if (!removed.contains(depRule)) { + if (!removedTags.contains(depRule)) { removedDeps.add(depRule) } } } - return RulesDiff(added, removed, removedDeps) + return RulesDiff(preserved, added, removedTags, removedDeps) } @JvmStatic - fun findDiff(old: Iterable, new: Iterable): RulesDiff = + fun findDiff(old: Iterable, new: Iterable): RulesDiff = findDiff(old, new, DependentRulesSpec.EmptySpec) } } \ No newline at end of file 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 3cb36dbc..36c1eac5 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 @@ -82,8 +82,6 @@ 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,16 +91,11 @@ internal class EvaluationSessionImpl private constructor ( return this } - override fun withSessionToken(token: SessionToken): EvaluationSession.Config { + override fun withSessionToken(token: SessionToken?): EvaluationSession.Config { 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 @@ -127,7 +120,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.launch(main, profiler, token, rulesDiff, ispec) + val status = session.launch(main, profiler, token, program.incrementalDiff(), 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 304b8fcb..3bc48f39 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 @@ -211,7 +211,8 @@ internal class ProcessingStateImpl(private var dispatchingFront: Dispatcher.Disp fun endSession(): SessionToken { val histView = view() resetStore() // clear observers - return SessionTokenImpl(histView, ruleOrdering.ruleTags, dispatchingFront.state()) + val rules = ArrayList().apply { ruleIndex.forEach { add(it) } } + return SessionTokenImpl(histView, rules, dispatchingFront.state()) } /** 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 c7da708e..af6bdc9a 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 @@ -19,13 +19,14 @@ package jetbrains.mps.logic.reactor.core.internal import jetbrains.mps.logic.reactor.core.DispatchingFrontState import jetbrains.mps.logic.reactor.evaluation.MatchJournalView import jetbrains.mps.logic.reactor.evaluation.SessionToken +import jetbrains.mps.logic.reactor.program.Rule data class SessionTokenImpl( private val journalView: MatchJournal.View, - private val ruleTags: Iterable, + private val rules: Iterable, private val frontState: DispatchingFrontState ) : SessionToken { override fun getJournalView(): MatchJournalView = journalView - override fun getRuleTags(): Iterable = ruleTags - override fun getFrontState(): DispatchingFrontState = frontState + override fun getRules(): Iterable = rules + fun getFrontState(): DispatchingFrontState = frontState } 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 84741277..6ce652ba 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/EvaluationSession.java +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/EvaluationSession.java @@ -88,8 +88,6 @@ 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/evaluation/SessionToken.java b/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/SessionToken.java index 159e6b47..2beb05b1 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/SessionToken.java +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/evaluation/SessionToken.java @@ -16,14 +16,12 @@ package jetbrains.mps.logic.reactor.evaluation; +import jetbrains.mps.logic.reactor.program.Rule; import org.jetbrains.annotations.NotNull; -import java.util.Map; public interface SessionToken { @NotNull() MatchJournalView getJournalView(); @NotNull() - Iterable getRuleTags(); - @NotNull() - Map getFrontState(); + Iterable getRules(); } 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 a6b3d4b8..4675b4e5 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,8 @@ package jetbrains.mps.logic.reactor.program; +import jetbrains.mps.logic.reactor.core.RulesDiff; + import java.util.ArrayList; /** @@ -30,6 +32,10 @@ public abstract class Program { public abstract Iterable handlers(); + public Program withRulesDiff(RulesDiff diff) { return this; }; + + public RulesDiff incrementalDiff() { return RulesDiff.emptyDiff(); }; + public Iterable rules() { ArrayList allRules = new ArrayList(); for (RulesList rulesList : handlers()) { diff --git a/reactor/Test/src/program/MockProgram.kt b/reactor/Test/src/program/MockProgram.kt index 7525ea21..a04a1642 100644 --- a/reactor/Test/src/program/MockProgram.kt +++ b/reactor/Test/src/program/MockProgram.kt @@ -2,6 +2,7 @@ * @author Fedor Isakov */ +import jetbrains.mps.logic.reactor.core.RulesDiff import jetbrains.mps.logic.reactor.evaluation.InvocationContext import jetbrains.mps.logic.reactor.evaluation.Supervisor import jetbrains.mps.logic.reactor.logical.LogicalContext @@ -104,6 +105,14 @@ class MockRule( } class MockProgram(val name: String, val rulesLists: List, val registry: MockConstraintRegistry) : Program() { + private var rulesDiff: RulesDiff = RulesDiff.emptyDiff() + + override fun incrementalDiff(): RulesDiff = rulesDiff + + override fun withRulesDiff(diff: RulesDiff): MockProgram { + this.rulesDiff = diff + return this + } override fun name(): String = name diff --git a/reactor/Test/test/TestIncrementalProgram.kt b/reactor/Test/test/TestIncrementalProgram.kt index 37c40ff5..81cde2dc 100644 --- a/reactor/Test/test/TestIncrementalProgram.kt +++ b/reactor/Test/test/TestIncrementalProgram.kt @@ -63,11 +63,13 @@ class TestIncrementalProgram { private fun Builder.relaunch(name: String, incrSpec: IncrementalProgramSpec, sessionToken: SessionToken, resultHandler: (EvaluationResult) -> Unit ) : Pair { - val result = EvaluationSession.newSession(program(name)) + val prog = program(name).withRulesDiff( + RulesDiff.findDiff(sessionToken.rules, rules) + ) + val result = EvaluationSession.newSession(prog) .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)