Optimize Dispatcher: ensure allMatches are calculated lazily on request.

This commit is contained in:
Fedor Isakov 2019-10-08 13:20:40 +02:00
parent 445108da74
commit 17f5bd64b0
1 changed files with 15 additions and 13 deletions

View File

@ -46,10 +46,11 @@ class Dispatcher (val ruleIndex: RuleIndex) {
private val ruletag2probe : HashMap<Any, RuleMatchingProbe>
private val allMatches = arrayListOf<RuleMatchEx>()
private val matching: Iterable<RuleMatchingProbe>?
constructor() {
this.ruletag2probe = hashMapOf()
this.matching = null
ruletag2matcher.entries.forEach { e ->
ruletag2probe.put(e.key, e.value.probe())
}
@ -57,29 +58,30 @@ class Dispatcher (val ruleIndex: RuleIndex) {
constructor(predState: DispatchingFrontState) {
this.ruletag2probe = hashMapOf()
this.matching = null
ruletag2matcher.entries.forEach { e ->
ruletag2probe[e.key] = predState[e.key] ?: e.value.probe()
}
}
private constructor(pred: DispatchingFront) {
private constructor(pred: DispatchingFront, matching: Iterable<RuleMatchingProbe>? = null) {
this.ruletag2probe = pred.ruletag2probe
}
private constructor(pred: DispatchingFront, matching: Iterable<RuleMatchingProbe>) {
this.ruletag2probe = pred.ruletag2probe
matching.forEach { probe ->
if (RULE_MATCHER_PROBE_PERSISTENT) {
ruletag2probe[probe.rule().uniqueTag()] = probe
}
allMatches.addAll(probe.matches())
}
this.matching = matching
}
/**
* Returns all matches satisfied by the constraint occurrences received so far.
*/
fun matches() : Iterable<RuleMatchEx> = allMatches
fun matches() : Iterable<RuleMatchEx> {
val allMatches = arrayListOf<RuleMatchEx>()
matching?.forEach { probe ->
if (RULE_MATCHER_PROBE_PERSISTENT) {
ruletag2probe[probe.rule().uniqueTag()] = probe
}
allMatches.addAll(probe.matches())
}
return allMatches
}
fun state() : DispatchingFrontState = ruletag2probe //.asMap()