Modify logging methods of MatchJournal to return logged Chunks

This commit is contained in:
Grigorii Kirgizov 2020-02-03 20:22:12 +03:00
parent 7074112b7c
commit 18fc4592f0
2 changed files with 20 additions and 6 deletions

View File

@ -28,13 +28,15 @@ interface MatchJournal : MutableIterable<MatchJournal.Chunk> {
/** /**
* Add new [Chunk] for matches of principal rules. * Add new [Chunk] for matches of principal rules.
* Log occurrences discarded by match in current [Chunk]. * Log occurrences discarded by match in current [Chunk].
* Returns added [Chunk], null for non-principal rules.
*/ */
fun logMatch(match: RuleMatch) fun logMatch(match: RuleMatch): MatchChunk?
/** /**
* Log occurrence activation in current [Chunk]. * Log occurrence activation in current [Chunk].
* Returns added [Chunk], null for non-principal occurrences.
*/ */
fun logActivation(occ: Occurrence) fun logActivation(occ: Occurrence): OccChunk?
/** /**
* Returns nearest MatchChunk which justifies current chunk. * Returns nearest MatchChunk which justifies current chunk.

View File

@ -66,23 +66,33 @@ internal open class MatchJournalImpl(
override fun iterator(): MutableIterator<Chunk> = hist.iterator() override fun iterator(): MutableIterator<Chunk> = hist.iterator()
override fun logMatch(match: RuleMatch) { override fun logMatch(match: RuleMatch): MatchChunk? {
var added: MatchChunk? = null
if (ispec.isPrincipal(match.rule())) { if (ispec.isPrincipal(match.rule())) {
current = MatchChunk(nextChunkId++, match) added = MatchChunk(nextChunkId++, match)
current = added
posPtr.add(current) posPtr.add(current)
} }
// Log discarded occurrences // Log discarded occurrences
(match as RuleMatchImpl).forEachReplaced { occ -> (match as RuleMatchImpl).forEachReplaced { occ ->
current.entries.add(Chunk.Entry(occ, true)) current.entries.add(Chunk.Entry(occ, true))
} }
return added
} }
override fun logActivation(occ: Occurrence) { override fun logActivation(occ: Occurrence): OccChunk? {
var added: OccChunk? = null
if (ispec.isPrincipal(occ.constraint)) { if (ispec.isPrincipal(occ.constraint)) {
current = OccChunk(nextChunkId++, occ) added = OccChunk(nextChunkId++, occ)
current = added
posPtr.add(current) posPtr.add(current)
} }
current.entries.add(Chunk.Entry(occ)) current.entries.add(Chunk.Entry(occ))
return added
} }
override fun ancestorMatch(): MatchChunk { override fun ancestorMatch(): MatchChunk {
@ -118,11 +128,13 @@ internal open class MatchJournalImpl(
while (posPtr.hasPrevious()) { while (posPtr.hasPrevious()) {
current = posPtr.previous() current = posPtr.previous()
if (current === pastPos.chunk) { if (current === pastPos.chunk) {
// todo: need reversed?
resetOccurrences(current.entries.drop(pastPos.entriesCount)) resetOccurrences(current.entries.drop(pastPos.entriesCount))
current.entries = current.entries.subList(0, pastPos.entriesCount) current.entries = current.entries.subList(0, pastPos.entriesCount)
posPtr.next() // make 'posPtr' to always point right after 'current' posPtr.next() // make 'posPtr' to always point right after 'current'
return return
} }
// todo: need reversed?
resetOccurrences(current.entries) resetOccurrences(current.entries)
posPtr.remove() posPtr.remove()
} }