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 c2e16df0..7f8c47d7 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 @@ -48,11 +48,7 @@ internal class ProcessingStateImpl(private var dispatchingFront: Dispatcher.Disp val profiler: Profiler? = null) : StoreAwareJournalImpl(journal) { - private val ruleOrder: Map = HashMap().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 -> diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/RuleOrdering.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/RuleOrdering.kt new file mode 100644 index 00000000..9960c62c --- /dev/null +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/RuleOrdering.kt @@ -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): Comparator { + + private val ruleOrder: Map = HashMap().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 = 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) + } + +} \ No newline at end of file