Extract rule ordering logic from ProcessingStateImpl
This commit is contained in:
parent
175d9f7623
commit
db80dc7ab6
|
|
@ -48,11 +48,7 @@ internal class ProcessingStateImpl(private var dispatchingFront: Dispatcher.Disp
|
|||
val profiler: Profiler? = null)
|
||||
: StoreAwareJournalImpl(journal)
|
||||
{
|
||||
private val ruleOrder: Map<Any, Int> = HashMap<Any, Int>().apply {
|
||||
put(MatchJournalImpl.InitRuleMatch.rule().uniqueTag(), -1)
|
||||
ruleIndex.forEachIndexed { index, rule -> put(rule.uniqueTag(), index) }
|
||||
}
|
||||
|
||||
private val ruleOrdering: RuleOrdering = RuleOrdering(ruleIndex)
|
||||
private val journalIndex: MatchJournal.Index = journal.index()
|
||||
|
||||
// It is a position in Journal from previous session,
|
||||
|
|
@ -182,7 +178,7 @@ internal class ProcessingStateImpl(private var dispatchingFront: Dispatcher.Disp
|
|||
// Place to activate candidate:
|
||||
// either as the last one, after all existing activations
|
||||
// or according to the ordering between rules.
|
||||
val placeToInsertFound = compareRuleOrders(chunk.match.rule(), candRule) > 0
|
||||
val placeToInsertFound = ruleOrdering.compare(chunk.match.rule(), candRule) > 0
|
||||
val childChunksEnded = !chunk.isDescendantOf(parentId)
|
||||
|
||||
val pos =
|
||||
|
|
@ -203,16 +199,6 @@ internal class ProcessingStateImpl(private var dispatchingFront: Dispatcher.Disp
|
|||
}
|
||||
}
|
||||
|
||||
private fun compareRuleOrders(lhs: Rule, rhs: Rule): Int {
|
||||
val lhsRuleOrder = ruleOrder[lhs.uniqueTag()]
|
||||
val rhsRuleOrder = ruleOrder[rhs.uniqueTag()]
|
||||
|
||||
if (lhsRuleOrder == null || rhsRuleOrder == null) {
|
||||
throw(IllegalStateException("Rules compared must be present in the rule index!"))
|
||||
}
|
||||
return lhsRuleOrder.compareTo(rhsRuleOrder)
|
||||
}
|
||||
|
||||
fun launchQueue(controller: Controller): FeedbackStatus.NORMAL {
|
||||
if (execQueue.isNotEmpty()) {
|
||||
resetStore()
|
||||
|
|
@ -240,7 +226,7 @@ internal class ProcessingStateImpl(private var dispatchingFront: Dispatcher.Disp
|
|||
}
|
||||
|
||||
fun snapshot(): SessionToken {
|
||||
return SessionToken(view(), ruleOrder.keys, dispatchingFront.state())
|
||||
return SessionToken(view(), ruleOrdering.ruleTags(), dispatchingFront.state())
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -277,7 +263,7 @@ internal class ProcessingStateImpl(private var dispatchingFront: Dispatcher.Disp
|
|||
val newCurrentMatches = handleFutureMatches(matches)
|
||||
val allMatches = newCurrentMatches + (postponedMatches.remove(Id(active)) ?: emptyList())
|
||||
// Sort according to rule priorities
|
||||
allMatches.sortedBy { ruleOrder[it.rule().uniqueTag()] }
|
||||
allMatches.sortedBy { ruleOrdering.orderOf(it.rule()) }
|
||||
}
|
||||
|
||||
val outStatus = currentMatches.fold(inStatus) { status, match ->
|
||||
|
|
|
|||
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Copyright 2014-2019 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package jetbrains.mps.logic.reactor.core.internal
|
||||
|
||||
import jetbrains.mps.logic.reactor.program.Rule
|
||||
|
||||
|
||||
internal class RuleOrdering(order: Iterable<Rule>): Comparator<Rule> {
|
||||
|
||||
private val ruleOrder: Map<Any, Int> = HashMap<Any, Int>().apply {
|
||||
put(MatchJournalImpl.InitRuleMatch.rule().uniqueTag(), -1)
|
||||
order.forEachIndexed { index, rule -> put(rule.uniqueTag(), index) }
|
||||
}
|
||||
|
||||
fun orderOf(rule: Rule): Int? = ruleOrder[rule.uniqueTag()]
|
||||
|
||||
fun ruleTags(): Set<Any> = ruleOrder.keys
|
||||
|
||||
override fun compare(lhs: Rule, rhs: Rule): Int {
|
||||
val lhsRuleOrder = orderOf(lhs)
|
||||
val rhsRuleOrder = orderOf(rhs)
|
||||
|
||||
if (lhsRuleOrder == null || rhsRuleOrder == null) {
|
||||
throw(IllegalStateException("Rules compared must be present in the rule index!"))
|
||||
}
|
||||
return lhsRuleOrder.compareTo(rhsRuleOrder)
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue