From e5942d2c77942fbb92d70d01592bc7ccd31d8290 Mon Sep 17 00:00:00 2001 From: Grigorii Kirgizov Date: Mon, 20 May 2019 16:15:55 +0300 Subject: [PATCH] Fix MatchHistory tests: fix impl of storeView, taking into account current pos in history (not all of it) --- .../reactor/core/internal/ControllerImpl.kt | 3 ++- .../reactor/core/internal/MatchHistory.kt | 17 ++++++++---- reactor/Test/test/RulesHelper.kt | 2 +- reactor/Test/test/TestMatchHistory.kt | 26 +++++++++---------- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ControllerImpl.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ControllerImpl.kt index 4b79ecd8..6d1455f5 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ControllerImpl.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/ControllerImpl.kt @@ -36,7 +36,8 @@ internal class ControllerImpl ( { /** For tests only */ - override fun storeView(): StoreView = state.storeView() +// override fun storeView(): StoreView = state.storeView() + override fun storeView(): StoreView = TODO() /** For tests only */ override fun evaluate(occ: Occurrence): StoreView { diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/MatchHistory.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/MatchHistory.kt index cd9b94e3..499a0786 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/MatchHistory.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/MatchHistory.kt @@ -18,6 +18,7 @@ package jetbrains.mps.logic.reactor.core.internal import gnu.trove.set.TIntSet import gnu.trove.set.hash.TIntHashSet +import jetbrains.mps.logic.reactor.core.Dispatcher import jetbrains.mps.logic.reactor.core.Occurrence import jetbrains.mps.logic.reactor.evaluation.ConstraintOccurrence import jetbrains.mps.logic.reactor.evaluation.RuleMatch @@ -69,8 +70,8 @@ interface MatchHistory { fun rollTo(chunk: Chunk) companion object { - fun fromSeed(chunkIdSeed: Int = 0): MatchHistory = MatchHistoryImpl(ProcessingStateImpl(), chunkIdSeed) - fun fromView(view: MatchHistory.View): MatchHistory = MatchHistoryImpl(ProcessingStateImpl(), view) + fun fromSeed(disp: Dispatcher, chunkIdSeed: Int = 0): MatchHistory = MatchHistoryImpl(ProcessingStateImpl(disp), chunkIdSeed) + fun fromView(disp: Dispatcher, view: MatchHistory.View): MatchHistory = MatchHistoryImpl(ProcessingStateImpl(disp), view) } } @@ -109,13 +110,19 @@ internal class MatchHistoryImpl(val state: ProcessingStateImpl, view: MatchHisto pos = hist.listIterator() } - // TODO: override fun storeView(): StoreView = StoreViewImpl(allOccurrences()) private fun allOccurrences(): Sequence { + // the following lop doesn't handle this case of starting pos, when 'current' isn't valid + if (!pos.hasPrevious()) + return emptySequence() + val set = HashSet>() - hist.flatMap { it.occurrences } .forEach { - if (it.isDiscarded) set.add(Id(it.occ)) else set.remove(Id(it.occ)) + for (chunk in hist) { + chunk.occurrences.forEach { + if (it.isDiscarded) set.remove(Id(it.occ)) else set.add(Id(it.occ)) + } + if (chunk == current) break } return set.map { it.wrapped }.asSequence() } diff --git a/reactor/Test/test/RulesHelper.kt b/reactor/Test/test/RulesHelper.kt index e520f4b2..59aaacbe 100644 --- a/reactor/Test/test/RulesHelper.kt +++ b/reactor/Test/test/RulesHelper.kt @@ -124,7 +124,7 @@ fun occurrence(id: String, vararg args: Any): Occurrence = MockConstraint(ConstraintSymbol.symbol(id, args.size)).occurrence(MockController(), listOf(* args)) fun justifiedOccurrence(id: String, justs: TIntSet, vararg args: Any): Occurrence = - MockConstraint(ConstraintSymbol.symbol(id, args.size), true).occurrence(listOf(* args), { fooObservable }, justs) + MockConstraint(ConstraintSymbol.symbol(id, args.size), true).occurrence(MockController(), listOf(* args), justs) fun justifiedOccurrence(id: String, justs: Set, vararg args: Any): Occurrence = justifiedOccurrence(id, TIntHashSet(justs), * args) fun sym0(id: String): ConstraintSymbol = diff --git a/reactor/Test/test/TestMatchHistory.kt b/reactor/Test/test/TestMatchHistory.kt index ffdd9896..b42f53f7 100644 --- a/reactor/Test/test/TestMatchHistory.kt +++ b/reactor/Test/test/TestMatchHistory.kt @@ -26,8 +26,6 @@ class TestMatchHistory { @Test fun testJustificationTracking() { - val hist = MatchHistory.fromSeed() - with(programWithRules( rule("rule1", headKept( @@ -60,7 +58,9 @@ class TestMatchHistory { )))) { - var d = Dispatcher(RuleIndex(rulesLists)).front() + val disp = Dispatcher(RuleIndex(rulesLists)) + var d = disp.front() + val hist = MatchHistory.fromSeed(disp) val mainOcc = justifiedOccurrence("main", setOf(0)) // hist.logOccurrence(mainOcc) // plays a role of the initial constraint, with no preceding RuleMatch d = d.expand(mainOcc) @@ -122,8 +122,6 @@ class TestMatchHistory { @Test fun testResetThenRoll() { - val hist = MatchHistory.fromSeed() - with(programWithRules( rule("rule1", headKept( @@ -150,7 +148,9 @@ class TestMatchHistory { )) { - var d = Dispatcher(RuleIndex(rulesLists)).front() + val disp = Dispatcher(RuleIndex(rulesLists)) + var d = disp.front() + val hist = MatchHistory.fromSeed(disp) val mainOcc = justifiedOccurrence("main", setOf(0)) // hist.logOccurrence(mainOcc) // plays a role of the initial constraint, with no preceding RuleMatch d = d.expand(mainOcc) @@ -195,8 +195,6 @@ class TestMatchHistory { @Test fun testPushExecReset() { - val hist = MatchHistory.fromSeed() - with(programWithRules( rule("rule1", headKept( @@ -233,7 +231,9 @@ class TestMatchHistory { )) { - var d = Dispatcher(RuleIndex(rulesLists)).front() + val disp = Dispatcher(RuleIndex(rulesLists)) + var d = disp.front() + val hist = MatchHistory.fromSeed(disp) val mainOcc = justifiedOccurrence("main", setOf(0)) d = d.expand(mainOcc) @@ -305,8 +305,6 @@ class TestMatchHistory { @Test fun testRmAddInMiddle() { - val hist = MatchHistory.fromSeed() - with(programWithRules( rule("rule0", headKept( @@ -370,7 +368,9 @@ class TestMatchHistory { // fst exec: rule1 -> rule2a -> rule3 -> ruleMakeP // snd exec: rule1 -> rule2b -> ruleMakeP -> rule3 -> ruleMakeP - var d = Dispatcher(RuleIndex(rulesLists)).front() + val disp = Dispatcher(RuleIndex(rulesLists)) + var d = disp.front() + val hist = MatchHistory.fromSeed(disp) val mainOcc = justifiedOccurrence("main", setOf(0)) d = d.expand(mainOcc) @@ -471,7 +471,7 @@ class TestMatchHistory { val pOcc2 = justifiedOccurrence("p", hist.current().justifications) hist.logOccurrence(pOcc2); d = d.expand(pOcc2) - + // only pOcc2 should be in the store val pStoredBeforeRoll = hist.storeView().occurrences(ConstraintSymbol.symbol("p", 0)) pStoredBeforeRoll.count() shouldBe 1 // todo?: check history before roll