Implement storeView() method to MatchHistory, remove it from ProcessingImpl (but not its single usage yet) and stored occurrences from StateFrame

This commit is contained in:
Grigorii Kirgizov 2019-05-16 20:06:30 +03:00
parent cbddd108bb
commit b2f65e1510
3 changed files with 29 additions and 44 deletions

View File

@ -19,8 +19,11 @@ package jetbrains.mps.logic.reactor.core.internal
import gnu.trove.set.TIntSet
import gnu.trove.set.hash.TIntHashSet
import jetbrains.mps.logic.reactor.core.Occurrence
import jetbrains.mps.logic.reactor.evaluation.ConstraintOccurrence
import jetbrains.mps.logic.reactor.evaluation.RuleMatch
import jetbrains.mps.logic.reactor.evaluation.StoreView
import jetbrains.mps.logic.reactor.program.ConstraintSymbol
import jetbrains.mps.logic.reactor.util.Id
import java.util.*
import kotlin.collections.ArrayList
@ -28,6 +31,7 @@ import kotlin.collections.ArrayList
interface MatchHistory {
data class View(val chunks: List<Chunk>, val nextChunkId: Int)
data class Chunk(val match: RuleMatch, val id: Int, val justifications: TIntSet) {
data class Entry(val occ: Occurrence, val isDiscarded: Boolean = false) {
override fun toString() = (if (isDiscarded) '-' else '+') + occ.toString()
@ -106,7 +110,15 @@ internal class MatchHistoryImpl(val state: ProcessingStateImpl, view: MatchHisto
}
// TODO:
override fun storeView(): StoreView = state.storeView()
override fun storeView(): StoreView = StoreViewImpl(allOccurrences())
private fun allOccurrences(): Sequence<Occurrence> {
val set = HashSet<Id<Occurrence>>()
hist.flatMap { it.occurrences } .forEach {
if (it.isDiscarded) set.add(Id(it.occ)) else set.remove(Id(it.occ))
}
return set.map { it.wrapped }.asSequence()
}
override fun view() = MatchHistory.View(ArrayList(hist), nextChunkId)
@ -192,6 +204,22 @@ internal class MatchHistoryImpl(val state: ProcessingStateImpl, view: MatchHisto
// fixme: does belong to here?
// fun resetOrder() { order = hist.mapToIndex() }
private class StoreViewImpl(occurrences: Sequence<Occurrence>) : StoreView {
val allOccurrences = occurrences.toSet()
val allSymbols = allOccurrences.map { co -> co.constraint().symbol() }.toSet()
override fun constraintSymbols(): Iterable<ConstraintSymbol> = allSymbols
override fun allOccurrences(): Iterable<ConstraintOccurrence> = allOccurrences
override fun occurrences(symbol: ConstraintSymbol): Iterable<ConstraintOccurrence> =
allOccurrences.filter { co -> co.constraint().symbol() == symbol }.toSet()
}
}
private fun RuleMatch.headJustifications(): TIntSet {

View File

@ -106,35 +106,4 @@ internal class ProcessingStateImpl private constructor(val trace: EvaluationTrac
// forward to the top frame
currentFrame().parentUpdated(logical)
}
fun storeView(): StoreView =
StoreViewImpl(allOccurrences())
private fun allOccurrences(): Sequence<Occurrence> {
val dit = stateFrames.descendingIterator()
val set = HashSet<Id<Occurrence>>()
while (dit.hasNext()) {
val stateFrame = dit.next()
stateFrame.allActivated().forEach { set.add(Id(it)) }
stateFrame.allDeactivated().forEach { set.remove(Id(it)) }
}
return set.map { it.wrapped }.asSequence()
}
private class StoreViewImpl(occurrences: Sequence<Occurrence>) : StoreView {
val allOccurrences = occurrences.toSet()
val allSymbols = allOccurrences.map { co -> co.constraint().symbol() }.toSet()
override fun constraintSymbols(): Iterable<ConstraintSymbol> = allSymbols
override fun allOccurrences(): Iterable<ConstraintOccurrence> = allOccurrences
override fun occurrences(symbol: ConstraintSymbol): Iterable<ConstraintOccurrence> =
allOccurrences.filter { co -> co.constraint().symbol() == symbol }.toSet()
}
}

View File

@ -45,10 +45,6 @@ internal class StateFrame private constructor(val state: ProcessingStateImpl) :
private var observers: PersMap<Id<Logical<*>>, ConsList<LogicalObserver>> = Maps.of()
private val activatedOccurrences = ArrayList<Occurrence>(4)
private val deactivatedOccurrences = ArrayList<Occurrence>(4)
constructor(state: ProcessingStateImpl, dispatcher: Dispatcher) : this(state) {
this.dispatchingFront = dispatcher.front()
}
@ -95,12 +91,6 @@ internal class StateFrame private constructor(val state: ProcessingStateImpl) :
}
}
fun allActivated() : Sequence<Occurrence> =
activatedOccurrences.asSequence()
fun allDeactivated() : Sequence<Occurrence> =
deactivatedOccurrences.asSequence()
/**
* Called to update the state with the currently active constraint occurrence.
* Calls the controller to process matches (if any) that were triggered.
@ -119,7 +109,6 @@ internal class StateFrame private constructor(val state: ProcessingStateImpl) :
state.trace.reactivate(active)
}
activatedOccurrences += active
this.dispatchingFront = dispatchingFront.expand(active)
val outStatus = dispatchingFront.matches().toList().fold(inStatus) { status, match ->
@ -167,7 +156,6 @@ internal class StateFrame private constructor(val state: ProcessingStateImpl) :
match.forEachReplaced { occ ->
this.dispatchingFront = dispatchingFront.contract(occ)
deactivatedOccurrences += occ
occ.stored = false
occ.terminate()