From 44b20040919d2df504c7a22623170b3884369f31 Mon Sep 17 00:00:00 2001 From: Fedor Isakov Date: Wed, 1 Aug 2018 19:42:43 +0200 Subject: [PATCH] Experimentally switch to the new matching algorithm --- .../mps/logic/reactor/core/Controller.kt | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/Controller.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/Controller.kt index 5c2b7a99..8f441d06 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/Controller.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/Controller.kt @@ -43,6 +43,8 @@ class Controller( private val matcher = Matcher(ruleIndex, profiler) + private var dispatchFringe = Dispatcher(ruleIndex).fringe() + // persistent (functional) object. reassigned on update private var propHistory = PropagationHistory() @@ -86,35 +88,43 @@ class Controller( trace.reactivate(active) } - for (match in matcher.matches(active, frameStack.current.store)) { + val activatedDF = dispatchFringe.activated(active) + this.dispatchFringe = activatedDF + +// for (match in matcher.matches(active, frameStack.current.store)) { + for(match in dispatchFringe.allMatches.toList()) { + // TODO: paranoid check. should be isAlive() instead if (!active.isStored()) break - if (!match.successful) continue +// if (!match.successful) continue // FIXME: move this check elsewhere - if ((match.keptOccurrences + match.discardedOccurrences).any { co -> !co.isStored() }) continue - if (propHistory.isRecorded(match)) continue + if ((match.matchHeadKept() + match.matchHeadReplaced()).any { co -> !co.isStored() }) continue + + // TODO: prophistory +// if (propHistory.isRecorded(match)) continue trace.trying(match) - for (prd in match.patternPredicates) { - tellPredicate(prd, match.logicalContext, trace) + for (prd in match.patternPredicates()) { + tellPredicate(prd, match.logicalContext(), trace) } - if (!match.rule.checkGuard(match.logicalContext, trace)) { + if (!match.rule().checkGuard(match.logicalContext(), trace)) { trace.reject(match) continue } trace.trigger(match) - for (occ in match.discardedOccurrences) { + for (occ in match.matchHeadReplaced()) { + this.dispatchFringe = dispatchFringe.discarded(occ) frameStack.current.store.discard(occ) trace.discard(occ) } var failure: EvaluationFailureException? = null - for (body in match.rule.bodyAlternation()) { + for (body in match.rule().bodyAlternation()) { if (failure != null) { trace.failure(failure) trace.retry(match) @@ -125,7 +135,8 @@ class Controller( // we must reassign the field on every rule triggering // and store on the stack the last value before rule activation in order to undo in case of failure val savedPropHistory = propHistory - this.propHistory = propHistory.record(match) + // TODO: prophistory +// this.propHistory = propHistory.record(match) val savedFrame = frameStack.current frameStack.push() @@ -133,8 +144,8 @@ class Controller( try { for (item in body) { when (item) { - is Constraint -> process(session.occurrence(item, match.logicalContext)) - is Predicate -> tellPredicate(item, match.logicalContext, trace) + is Constraint -> process(session.occurrence(item, match.logicalContext())) + is Predicate -> tellPredicate(item, match.logicalContext(), trace) else -> throw IllegalArgumentException("unknown item ${item}") } } @@ -195,3 +206,8 @@ class Controller( private val noLogicalContext: LogicalContext = object: LogicalContext { override fun variable(metaLogical: MetaLogical): Logical = TODO() } + +fun MatchRule.patternPredicates() = + (rule().headKept() + rule().headReplaced()).zip(matchHeadKept() + matchHeadReplaced()).flatMap { + it.first.patternPredicates(it.second.arguments()) + }.toList()