From f6123273d482dde0532bc6fd065d3d2fd77b8061 Mon Sep 17 00:00:00 2001 From: Fedor Isakov Date: Tue, 1 Dec 2020 21:55:17 +0100 Subject: [PATCH] Minor refactoring: drop usages of Program.rulesLists() method. --- .../mps/logic/reactor/core/RuleIndex.kt | 31 ++++++++++++------- .../core/internal/EvaluationSessionImpl.kt | 10 +++--- reactor/Test/src/program/MockProgram.kt | 2 ++ reactor/Test/test/RulesHelper.kt | 2 +- reactor/Test/test/TestController.kt | 4 +-- reactor/Test/test/TestProgramBuilder.kt | 2 +- reactor/Test/test/TestRuleIndex.kt | 20 ++++++------ reactor/Test/test/TestRuleMatcher.kt | 12 +++---- reactor/Test/test/TestStoreAwareJournal.kt | 14 ++++----- 9 files changed, 54 insertions(+), 43 deletions(-) diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/RuleIndex.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/RuleIndex.kt index 2514bbc8..331ea4fd 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/RuleIndex.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/RuleIndex.kt @@ -33,7 +33,7 @@ import kotlin.collections.HashMap * * @author Fedor Isakov */ -class RuleIndex(ruleLists: Iterable) : Iterable, RuleLookup +class RuleIndex(): Iterable, RuleLookup { private class IndexedRule { @@ -69,8 +69,8 @@ class RuleIndex(ruleLists: Iterable) : Iterable, RuleLookup /** the bit set to be used for temporary processing within [select]*/ private val andRuleIndices = BitSet(allRules.size) - init { - buildIndex(ruleLists) + constructor(rules: Iterable) : this() { + buildIndexFromRules(rules) } override fun lookupRuleByTag(tag: Any): Rule? = tag2rule[tag] @@ -111,13 +111,25 @@ class RuleIndex(ruleLists: Iterable) : Iterable, RuleLookup override fun iterator(): Iterator = allRules.map { it.rule }.iterator() + fun buildIndexFromRules(rules: Iterable) { + rules.forEachIndexed { idx, rule -> + val irule = IndexedRule(idx, rule) + addRuleToIndex(irule) + allRules.add(irule) + } + } + fun updateIndex(ruleLists: Iterable) { + updateIndexFromRules(ruleLists.flatMap { it.rules() }) + } + + fun updateIndexFromRules(rules: Iterable) { val removedTags = allRules.map { it.rule.uniqueTag() }.toHashSet() - ruleLists.flatMap { it.rules() }.map { it.uniqueTag() }.forEach{ removedTags.remove(it) } + rules.map { it.uniqueTag() }.forEach{ removedTags.remove(it) } val allRulesIt = allRules.listIterator() var nextIdx = 0 - ruleLists.flatMap { it.rules() }.forEach { rule -> + rules.forEach { rule -> var skip = false while (allRulesIt.hasNext()) { val irule = allRulesIt.next() @@ -128,7 +140,7 @@ class RuleIndex(ruleLists: Iterable) : Iterable, RuleLookup } else if (!irule.rule.uniqueTag().equals(rule.uniqueTag())) { allRulesIt.previous() break - + } else { irule.idx = nextIdx skip = true @@ -151,11 +163,8 @@ class RuleIndex(ruleLists: Iterable) : Iterable, RuleLookup } private fun buildIndex(ruleLists: Iterable) { - ruleLists.flatMap { it.rules() }.forEachIndexed { idx, rule -> - val irule = IndexedRule(idx, rule) - addRuleToIndex(irule) - allRules.add(irule) - } + val rules = ruleLists.flatMap { it.rules() } + buildIndexFromRules(rules) } private fun removeRuleFromIndex(irule: IndexedRule) { 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 74361d7b..f46adf29 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 @@ -99,8 +99,8 @@ internal class EvaluationSessionImpl private constructor ( private fun getSession(token: SessionToken?): SessionParts { val ruleIndex = token ?.let { (it as SessionTokenImpl).ruleIndex } - ?.also { it.updateIndex(program.rulesLists()) } - ?: RuleIndex(program.rulesLists()) + ?.also { it.updateIndexFromRules(program.rules()) } + ?: RuleIndex(program.rules()) val journal = MatchJournalImpl(incrementality) val logicalState = LogicalState() @@ -141,7 +141,7 @@ internal class EvaluationSessionImpl private constructor ( val tkn = token as SessionTokenImpl val logicalState = tkn.logicalState - val ruleIndex = tkn.ruleIndex.apply { updateIndex(program.rulesLists()) } + val ruleIndex = tkn.ruleIndex.apply { updateIndexFromRules(program.rules()) } val journal = MatchJournalImpl(incrementality, tkn.journalView as MatchJournal.View) val front = Dispatcher(ruleIndex, tkn.getFrontState()).front() val processing = ConstraintsProcessing(front, journal, logicalState, incrementality, trace, profiler) @@ -181,7 +181,7 @@ internal class EvaluationSessionImpl private constructor ( val tkn = token as SessionTokenImpl val logicalState = LogicalState() - val ruleIndex = RuleIndex(program.rulesLists()) + val ruleIndex = RuleIndex(program.rules()) val journal = MatchJournalImpl(incrementality, tkn.journalView as MatchJournal.View) val front = Dispatcher(ruleIndex, tkn.getFrontState()).front() val processing = ConstraintsProcessing(front, journal, logicalState, incrementality, trace, profiler) @@ -338,6 +338,6 @@ internal class EvaluationSessionImpl private constructor ( } - private fun RuleIndex.toRules() = ArrayList().also { l -> forEach { l.add(it) }} + private fun RuleIndex.toRules() = ArrayList().also { l -> this.forEach { l.add(it) }} } diff --git a/reactor/Test/src/program/MockProgram.kt b/reactor/Test/src/program/MockProgram.kt index 8111f6eb..3336227f 100644 --- a/reactor/Test/src/program/MockProgram.kt +++ b/reactor/Test/src/program/MockProgram.kt @@ -117,6 +117,8 @@ class MockProgram(val name: String, val rulesLists: List, val registr override fun name(): String = name override fun rulesLists(): Iterable = unmodifiableCollection(rulesLists) + + override fun rules(): MutableIterable = unmodifiableCollection(rulesLists.flatMap { it.rules() }) } diff --git a/reactor/Test/test/RulesHelper.kt b/reactor/Test/test/RulesHelper.kt index 66263b65..f568cb5d 100644 --- a/reactor/Test/test/RulesHelper.kt +++ b/reactor/Test/test/RulesHelper.kt @@ -17,7 +17,7 @@ import kotlin.collections.HashMap class Builder(var rulesLists: List) : RuleLookup { - val tag2rule = HashMap() + val tag2rule = LinkedHashMap() val programBuilder = ProgramBuilder(MockConstraintRegistry()) diff --git a/reactor/Test/test/TestController.kt b/reactor/Test/test/TestController.kt index cf09278d..6cb366a4 100644 --- a/reactor/Test/test/TestController.kt +++ b/reactor/Test/test/TestController.kt @@ -59,7 +59,7 @@ class TestController { private fun Builder.controller(vararg occurrences: ConstraintOccurrence): Controller { val program = MockProgram("test", rulesLists, registry = MockConstraintRegistry()) MockSession.init(program, MockSupervisor()) - val controller = createController(MockSupervisor(), RuleIndex(program.rulesLists)) + val controller = createController(MockSupervisor(), RuleIndex(program.rules())) MockSession.ourBackend.session.controller = controller return controller } @@ -72,7 +72,7 @@ class TestController { feedbackHandler(ruleMatch, feedback) } MockSession.init(program, supervisor) - val controller = createController(supervisor, RuleIndex(program.rulesLists)) + val controller = createController(supervisor, RuleIndex(program.rules())) MockSession.ourBackend.session.controller = controller return controller } diff --git a/reactor/Test/test/TestProgramBuilder.kt b/reactor/Test/test/TestProgramBuilder.kt index 627a0161..9dc92c27 100644 --- a/reactor/Test/test/TestProgramBuilder.kt +++ b/reactor/Test/test/TestProgramBuilder.kt @@ -44,7 +44,7 @@ class TestProgramBuilder { ) ) ).run { - assertEquals(program("test").rulesLists().flatMap { it.rules() }.count(), 2) + assertEquals(program("test").rules().count(), 2) } } diff --git a/reactor/Test/test/TestRuleIndex.kt b/reactor/Test/test/TestRuleIndex.kt index 1c81fad4..ff8df650 100644 --- a/reactor/Test/test/TestRuleIndex.kt +++ b/reactor/Test/test/TestRuleIndex.kt @@ -52,7 +52,7 @@ class TestRuleIndex { )) { - val ruleIndex = RuleIndex(first.rulesLists) + val ruleIndex = RuleIndex(first.rules) with (ruleIndex.forOccurrence(occurrence("foo"))) { map { it.uniqueTag() }.toList() shouldBe listOf("rule0") } @@ -60,7 +60,7 @@ class TestRuleIndex { iterator().hasNext() shouldBe false } - ruleIndex.updateIndex(second.rulesLists) + ruleIndex.updateIndexFromRules(second.rules) with (ruleIndex.forOccurrence(occurrence("foo"))) { iterator().hasNext() shouldBe false } @@ -87,12 +87,12 @@ class TestRuleIndex { )) { - val ruleIndex = RuleIndex(first.rulesLists) + val ruleIndex = RuleIndex(first.rules) with (ruleIndex.forOccurrence(occurrence("foo"))) { map { it.uniqueTag() }.toList() shouldBe listOf("rule0") } - ruleIndex.updateIndex(second.rulesLists) + ruleIndex.updateIndexFromRules(second.rules) with (ruleIndex.forOccurrence(occurrence("foo"))) { map { it.uniqueTag() }.toList() shouldBe listOf("rule1","rule0") } @@ -120,7 +120,7 @@ class TestRuleIndex { )) { - val ruleIndex = RuleIndex(first.rulesLists) + val ruleIndex = RuleIndex(first.rules) with (ruleIndex.forOccurrence(occurrence("foo"))) { map { it.uniqueTag() }.toList() shouldBe listOf("rule1","rule0") } @@ -128,7 +128,7 @@ class TestRuleIndex { map { it.uniqueTag() }.toList() shouldBe listOf("rule1","rule2") } - ruleIndex.updateIndex(second.rulesLists) + ruleIndex.updateIndexFromRules(second.rules) with (ruleIndex.forOccurrence(occurrence("foo"))) { map { it.uniqueTag() }.toList() shouldBe listOf("rule1") } @@ -151,7 +151,7 @@ class TestRuleIndex { )) { - val ruleIndex = RuleIndex(first.rulesLists) + val ruleIndex = RuleIndex(first.rules) with (ruleIndex.forOccurrence(occurrence("foo"))) { map { it.uniqueTag() }.toList() shouldBe listOf("rule1","rule0") } @@ -159,7 +159,7 @@ class TestRuleIndex { map { it.uniqueTag() }.toList() shouldBe listOf("rule1","rule2") } - ruleIndex.updateIndex(second.rulesLists) + ruleIndex.updateIndexFromRules(second.rules) with (ruleIndex.forOccurrence(occurrence("foo"))) { map { it.uniqueTag() }.toList() shouldBe listOf("rule0") } @@ -195,7 +195,7 @@ class TestRuleIndex { )) { - val ruleIndex = RuleIndex(first.rulesLists) + val ruleIndex = RuleIndex(first.rules) with (ruleIndex.forOccurrence(occurrence("foo"))) { map { it.uniqueTag() }.toList() shouldBe listOf("rule0", "rule3") } @@ -209,7 +209,7 @@ class TestRuleIndex { map { it.uniqueTag() }.toList() shouldBe listOf("rule4") } - ruleIndex.updateIndex(second.rulesLists) + ruleIndex.updateIndexFromRules(second.rules) with (ruleIndex.forOccurrence(occurrence("foo"))) { map { it.uniqueTag() }.toList() shouldBe listOf("rule5", "rule0") } diff --git a/reactor/Test/test/TestRuleMatcher.kt b/reactor/Test/test/TestRuleMatcher.kt index d74cfb71..a7140125 100644 --- a/reactor/Test/test/TestRuleMatcher.kt +++ b/reactor/Test/test/TestRuleMatcher.kt @@ -369,7 +369,7 @@ class TestRuleMatcher { val bar0 = occurrence("bar") // with(ruleMatcher().probe()) { - with(Dispatcher(RuleIndex(rulesLists)).front()) { + with(Dispatcher(RuleIndex(rules)).front()) { expand(foo1) }.apply { matches().count() shouldBe 0 }.run { @@ -796,7 +796,7 @@ class TestRuleMatcher { constraint("qux") )))) { - with(Dispatcher(RuleIndex(rulesLists)).front()) { + with(Dispatcher(RuleIndex(rules)).front()) { expand(occurrence("foo")) }.apply { matches().count() shouldBe 0 }.run { @@ -838,7 +838,7 @@ class TestRuleMatcher { constraint("qux") )))) { - with(Dispatcher(RuleIndex(rulesLists)).front()) { + with(Dispatcher(RuleIndex(rules)).front()) { expand(occurrence("foo")) }.apply { matches().count() shouldBe 0 }.run { @@ -908,7 +908,7 @@ class TestRuleMatcher { constraint("qux") )))) { - with(Dispatcher(RuleIndex(rulesLists)).front()) { + with(Dispatcher(RuleIndex(rules)).front()) { expand(occurrence("blin")) }.apply { matches().count() shouldBe 0 }.run { @@ -958,7 +958,7 @@ class TestRuleMatcher { constraint("qux") )))) { - with(Dispatcher(RuleIndex(rulesLists)).front()) { + with(Dispatcher(RuleIndex(rules)).front()) { expand(occurrence("foo")) }.apply { matches().count() shouldBe 0 }.run { @@ -1010,7 +1010,7 @@ class TestRuleMatcher { )))) { val bar = occurrence("bar") - with(Dispatcher(RuleIndex(rulesLists)).front()) { + with(Dispatcher(RuleIndex(rules)).front()) { expand(occurrence("foo")) }.apply { matches().count() shouldBe 0 }.run { diff --git a/reactor/Test/test/TestStoreAwareJournal.kt b/reactor/Test/test/TestStoreAwareJournal.kt index 14f36d79..e0b5c440 100644 --- a/reactor/Test/test/TestStoreAwareJournal.kt +++ b/reactor/Test/test/TestStoreAwareJournal.kt @@ -83,7 +83,7 @@ class TestStoreAwareJournal { body() ))) { - with(JournalDispatcherHelper(Dispatcher(RuleIndex(rulesLists)))) { + with(JournalDispatcherHelper(Dispatcher(RuleIndex(rules)))) { hist.justifications() shouldBe justsOf(0) // initial chunk @@ -142,7 +142,7 @@ class TestStoreAwareJournal { )) )) { - with(JournalDispatcherHelper(Dispatcher(RuleIndex(rulesLists)))) { + with(JournalDispatcherHelper(Dispatcher(RuleIndex(rules)))) { val initPos = hist.currentPos() @@ -224,7 +224,7 @@ class TestStoreAwareJournal { )) )) { - with(JournalDispatcherHelper(Dispatcher(RuleIndex(rulesLists)))) { + with(JournalDispatcherHelper(Dispatcher(RuleIndex(rules)))) { val initPos = hist.currentPos() @@ -313,7 +313,7 @@ class TestStoreAwareJournal { )) )) { - with(JournalDispatcherHelper(Dispatcher(RuleIndex(rulesLists)))) { + with(JournalDispatcherHelper(Dispatcher(RuleIndex(rules)))) { val initPos = hist.currentPos() @@ -394,7 +394,7 @@ class TestStoreAwareJournal { )) )) { - with(JournalDispatcherHelper(Dispatcher(RuleIndex(rulesLists)))) { + with(JournalDispatcherHelper(Dispatcher(RuleIndex(rules)))) { logExpand(principalOccurrenceInit("foo")) @@ -444,7 +444,7 @@ class TestStoreAwareJournal { )) )) { - with(JournalDispatcherHelper(Dispatcher(RuleIndex(rulesLists)))) { + with(JournalDispatcherHelper(Dispatcher(RuleIndex(rules)))) { with(hist) { view().chunks.size shouldBe initialJournalSize // only initial chunk @@ -510,7 +510,7 @@ class TestStoreAwareJournal { )) )) { - with(JournalDispatcherHelper(Dispatcher(RuleIndex(rulesLists)))) { + with(JournalDispatcherHelper(Dispatcher(RuleIndex(rules)))) { logExpand(principalOccurrenceInit("foo"))