Modify SessionToken to preserve old rules instead of tags & RulesDiff to also distinguish preserved rules

This commit is contained in:
Grigorii Kirgizov 2019-11-06 17:40:04 +03:00
parent 9b61c6a2d3
commit bfcee33ec9
9 changed files with 48 additions and 31 deletions

View File

@ -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<Rule>,
val added: Iterable<Rule>,
val removed: Set<Any>,
val removedDependent: Set<Any>
) {
private val preserved: Map<Any, Rule> = HashMap<Any, Rule>().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<Any>, new: Iterable<Rule>, ruleDeps: DependentRulesSpec): RulesDiff {
val oldSet = old.toHashSet()
val newSet = new.toHashSet()
fun findDiff(old: Iterable<Rule>, new: Iterable<Rule>, ruleDeps: DependentRulesSpec): RulesDiff {
val oldTagsSet = old.map { it.uniqueTag() }.toHashSet()
val newTagsSet = new.map { it.uniqueTag() }.toHashSet()
val added: List<Rule> = new.filter { !oldSet.contains(it.uniqueTag()) }
val removed: Set<Any> = 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<Any> = removed.map { it.uniqueTag() }.toSet()
//fixme: remove
val removedDeps = HashSet<Any>()
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<Any>, new: Iterable<Rule>): RulesDiff =
fun findDiff(old: Iterable<Rule>, new: Iterable<Rule>): RulesDiff =
findDiff(old, new, DependentRulesSpec.EmptySpec)
}
}

View File

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

View File

@ -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<Rule>().apply { ruleIndex.forEach { add(it) } }
return SessionTokenImpl(histView, rules, dispatchingFront.state())
}
/**

View File

@ -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<Any>,
private val rules: Iterable<Rule>,
private val frontState: DispatchingFrontState
) : SessionToken {
override fun getJournalView(): MatchJournalView = journalView
override fun getRuleTags(): Iterable<Any> = ruleTags
override fun getFrontState(): DispatchingFrontState = frontState
override fun getRules(): Iterable<Rule> = rules
fun getFrontState(): DispatchingFrontState = frontState
}

View File

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

View File

@ -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<Object> getRuleTags();
@NotNull()
Map<Object, RuleMatchingProbeState> getFrontState();
Iterable<Rule> getRules();
}

View File

@ -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<RulesList> handlers();
public Program withRulesDiff(RulesDiff diff) { return this; };
public RulesDiff incrementalDiff() { return RulesDiff.emptyDiff(); };
public Iterable<Rule> rules() {
ArrayList<Rule> allRules = new ArrayList<Rule>();
for (RulesList rulesList : handlers()) {

View File

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

View File

@ -63,11 +63,13 @@ class TestIncrementalProgram {
private fun Builder.relaunch(name: String, incrSpec: IncrementalProgramSpec, sessionToken: SessionToken, resultHandler: (EvaluationResult) -> Unit )
: Pair<Builder, EvaluationResult>
{
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)