Change signature of Supervisor to handle RuleMatch instead of Rule

This commit is contained in:
Grigorii Kirgizov 2019-07-17 14:00:25 +03:00 committed by Fedor Isakov
parent 78c0cc1559
commit 8ed7eb3c02
6 changed files with 28 additions and 24 deletions

View File

@ -17,8 +17,8 @@
package jetbrains.mps.logic.reactor.core
import jetbrains.mps.logic.reactor.evaluation.EvaluationFeedback
import jetbrains.mps.logic.reactor.evaluation.RuleMatch
import jetbrains.mps.logic.reactor.evaluation.Supervisor
import jetbrains.mps.logic.reactor.program.Rule
import java.util.ArrayList
import java.util.Arrays
@ -33,12 +33,12 @@ class CompositeFeedback private constructor(private val elements: List<Feedback>
this.severity = maxSeverity(elements)
}
override fun handle(rule: Rule, supervisor: Supervisor): Boolean {
override fun handle(ruleMatch: RuleMatch, supervisor: Supervisor): Boolean {
var unhandled = 0
for (feedback in elements) {
if (!feedback.alreadyHandled()) {
unhandled += 1
if (supervisor.handleFeedback(rule, feedback)) {
if (supervisor.handleFeedback(ruleMatch, feedback)) {
feedback.setHandled()
unhandled -= 1
}
@ -47,7 +47,7 @@ class CompositeFeedback private constructor(private val elements: List<Feedback>
return unhandled == 0
}
override fun handle(rule: Rule) {
override fun handle(ruleMatch: RuleMatch) {
for (feedback in elements) {
if (!feedback.alreadyHandled()) {
feedback.setHandled()

View File

@ -17,8 +17,8 @@
package jetbrains.mps.logic.reactor.core
import jetbrains.mps.logic.reactor.evaluation.EvaluationFeedback
import jetbrains.mps.logic.reactor.evaluation.RuleMatch
import jetbrains.mps.logic.reactor.evaluation.Supervisor
import jetbrains.mps.logic.reactor.program.Rule
/**
* @author Fedor Isakov
@ -40,14 +40,14 @@ abstract class Feedback : EvaluationFeedback() {
/**
* Returns true if the feedback has been handled.
*/
open fun handle(rule: Rule, supervisor: Supervisor): Boolean {
if (!alreadyHandled() && supervisor.handleFeedback(rule, this)) {
open fun handle(ruleMatch: RuleMatch, supervisor: Supervisor): Boolean {
if (!alreadyHandled() && supervisor.handleFeedback(ruleMatch, this)) {
setHandled()
}
return alreadyHandled()
}
open fun handle(rule: Rule) {
open fun handle(ruleMatch: RuleMatch) {
if (!alreadyHandled()) {
setHandled()
}

View File

@ -154,7 +154,7 @@ internal class ControllerImpl (
if (itemOk) {
context.withStatus { status ->
if (status.feedback?.alreadyHandled() == false) {
status.feedback.handle(match.rule(), supervisor)
status.feedback.handle(match, supervisor)
}
}
@ -168,15 +168,18 @@ internal class ControllerImpl (
if (status is FAILED) {
trace.feedback(status.failure)
// if there's alternative body branch then try it
if (altIt.hasNext()) {
// clear the failure handled status
// the supervisor is NOT notified here
status.failure.handle(match.rule())
status.failure.handle(match)
status
} else if (status.feedback?.alreadyHandled() == false && status.failure.handle(match.rule(), supervisor)) {
// if failure can be handled here then recover
} else if (status.feedback?.alreadyHandled() == false && status.failure.handle(match, supervisor)) {
status.recover()
// else propagate further up the stack
} else {
status
}
@ -244,6 +247,7 @@ internal class ControllerImpl (
private fun RuleMatch.allStored() = (matchHeadKept() + matchHeadReplaced()).all { co -> (co as Occurrence).stored }
private class Context(inStatus: FeedbackStatus,
val logicalContext: LogicalContext) : InvocationContext
{

View File

@ -130,7 +130,7 @@ internal class ProcessingStateImpl(private var dispatchingFront: Dispatcher.Disp
val validOccs = matchedOccs.filter { occ ->
!occ.justifications().intersects(justificationRoots)
}
assert(matchedOccs.all { it.isPrincipal() })
// assert(matchedOccs.all { it.isPrincipal() })
execQueue.offerAll(validOccs)
}

View File

@ -17,7 +17,6 @@
package jetbrains.mps.logic.reactor.evaluation;
import jetbrains.mps.logic.reactor.logical.LogicalContext;
import jetbrains.mps.logic.reactor.program.Rule;
import java.util.List;
@ -35,7 +34,7 @@ public interface Supervisor {
* Override this method in order to "handle" the feedback.
* Returns true if the method has handled (consumed) the feedback.
*/
default boolean handleFeedback(Rule rule, EvaluationFeedback feedback) {
default boolean handleFeedback(RuleMatch ruleMatch, EvaluationFeedback feedback) {
return false;
}

View File

@ -65,12 +65,12 @@ class TestController {
return controller
}
private fun Builder.controllerWithFeedback(feedbackHandler: (Rule, EvaluationFeedback) -> Boolean,
private fun Builder.controllerWithFeedback(feedbackHandler: (RuleMatch, EvaluationFeedback) -> Boolean,
vararg occurrences: ConstraintOccurrence): Controller {
val program = MockProgram("test", rulesLists, registry = MockConstraintRegistry())
val supervisor = object : MockSupervisor() {
override fun handleFeedback(rule: Rule, feedback: EvaluationFeedback): Boolean =
feedbackHandler(rule, feedback)
override fun handleFeedback(ruleMatch: RuleMatch, feedback: EvaluationFeedback): Boolean =
feedbackHandler(ruleMatch, feedback)
}
MockSession.init(program, supervisor)
val controller = createController(supervisor, RuleIndex(program.rulesLists))
@ -821,9 +821,9 @@ class TestController {
@Test(expected = EvaluationFailureException::class)
fun failureHandler() {
val failures = ArrayList<Pair<EvaluationFailure, Any>>()
val failureHandler = { rule: Rule, feedback: EvaluationFeedback ->
val failureHandler = { ruleMatch: RuleMatch, feedback: EvaluationFeedback ->
if (feedback is EvaluationFailure) {
failures.add(feedback to rule.uniqueTag())
failures.add(feedback to ruleMatch.rule().uniqueTag())
}
false
}
@ -863,10 +863,11 @@ class TestController {
@Test
fun failureHandlerRecover() {
val failures = ArrayList<Pair<EvaluationFailure, Any>>()
val failureHandler = { rule: Rule, feedback: EvaluationFeedback ->
val failureHandler = { ruleMatch: RuleMatch, feedback: EvaluationFeedback ->
if (feedback is EvaluationFailure) {
failures.add(feedback to rule.uniqueTag())
(rule.uniqueTag().toString().startsWith("recoverable"))
val utag = ruleMatch.rule().uniqueTag()
failures.add(feedback to utag)
(utag.toString().startsWith("recoverable"))
} else false
}
@ -917,8 +918,8 @@ class TestController {
@Test
fun detailsFeedbackHandler() {
val feedbacks = arrayListOf<Pair<EvaluationFeedback, Any>>()
val feedbackHandler = { rule: Rule, feedback: EvaluationFeedback ->
feedbacks.add(feedback to rule.uniqueTag())
val feedbackHandler = { ruleMatch: RuleMatch, feedback: EvaluationFeedback ->
feedbacks.add(feedback to ruleMatch.rule().uniqueTag())
feedback.message.startsWith("catchme")
}