Minor refactoring: drop usages of Program.rulesLists() method.

This commit is contained in:
Fedor Isakov 2020-12-01 21:55:17 +01:00
parent c6152abdc2
commit f6123273d4
9 changed files with 54 additions and 43 deletions

View File

@ -33,7 +33,7 @@ import kotlin.collections.HashMap
*
* @author Fedor Isakov
*/
class RuleIndex(ruleLists: Iterable<RulesList>) : Iterable<Rule>, RuleLookup
class RuleIndex(): Iterable<Rule>, RuleLookup
{
private class IndexedRule {
@ -69,8 +69,8 @@ class RuleIndex(ruleLists: Iterable<RulesList>) : Iterable<Rule>, RuleLookup
/** the bit set to be used for temporary processing within [select]*/
private val andRuleIndices = BitSet(allRules.size)
init {
buildIndex(ruleLists)
constructor(rules: Iterable<Rule>) : this() {
buildIndexFromRules(rules)
}
override fun lookupRuleByTag(tag: Any): Rule? = tag2rule[tag]
@ -111,13 +111,25 @@ class RuleIndex(ruleLists: Iterable<RulesList>) : Iterable<Rule>, RuleLookup
override fun iterator(): Iterator<Rule> = allRules.map { it.rule }.iterator()
fun buildIndexFromRules(rules: Iterable<Rule>) {
rules.forEachIndexed { idx, rule ->
val irule = IndexedRule(idx, rule)
addRuleToIndex(irule)
allRules.add(irule)
}
}
fun updateIndex(ruleLists: Iterable<RulesList>) {
updateIndexFromRules(ruleLists.flatMap { it.rules() })
}
fun updateIndexFromRules(rules: Iterable<Rule>) {
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<RulesList>) : Iterable<Rule>, 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<RulesList>) : Iterable<Rule>, RuleLookup
}
private fun buildIndex(ruleLists: Iterable<RulesList>) {
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) {

View File

@ -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<Rule>().also { l -> forEach { l.add(it) }}
private fun RuleIndex.toRules() = ArrayList<Rule>().also { l -> this.forEach { l.add(it) }}
}

View File

@ -117,6 +117,8 @@ class MockProgram(val name: String, val rulesLists: List<RulesList>, val registr
override fun name(): String = name
override fun rulesLists(): Iterable<RulesList> = unmodifiableCollection(rulesLists)
override fun rules(): MutableIterable<Rule> = unmodifiableCollection(rulesLists.flatMap { it.rules() })
}

View File

@ -17,7 +17,7 @@ import kotlin.collections.HashMap
class Builder(var rulesLists: List<RulesList>) : RuleLookup {
val tag2rule = HashMap<Any, Rule>()
val tag2rule = LinkedHashMap<Any, Rule>()
val programBuilder = ProgramBuilder(MockConstraintRegistry())

View File

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

View File

@ -44,7 +44,7 @@ class TestProgramBuilder {
)
)
).run {
assertEquals(program("test").rulesLists().flatMap { it.rules() }.count(), 2)
assertEquals(program("test").rules().count(), 2)
}
}

View File

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

View File

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

View File

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