Introduce tellPattern event for better tracing

This commit is contained in:
Fedor Isakov 2024-07-22 13:31:26 +02:00
parent f2202726a9
commit 8262a880a2
3 changed files with 24 additions and 4 deletions

View File

@ -35,6 +35,8 @@ interface Controller {
fun tryTell(invocation: PredicateInvocation): Solver.Result
fun tryTellPattern(invocation: PredicateInvocation): Solver.Result
fun activate(constraint: Constraint): FeedbackStatus
fun reactivate(occ: Occurrence): FeedbackStatus

View File

@ -89,6 +89,12 @@ internal class ControllerImpl (
return solver.tryTell(invocation)
}
override fun tryTellPattern(invocation: PredicateInvocation): Solver.Result {
val solver = invocation.predicate().symbol().solver(supervisor)
trace.tellPattern(invocation)
return solver.tryTell(invocation)
}
override fun reactivate(occ: Occurrence): FeedbackStatus =
profiler.profile<FeedbackStatus>("reactivate_${occ.constraint.symbol()}") {
@ -99,8 +105,8 @@ internal class ControllerImpl (
override fun offerMatch(match: RuleMatchEx, inStatus: FeedbackStatus) : FeedbackStatus =
inStatus
.then { checkMatchPreconditions(match, it) }
.also { trace.trying(match) }
.then { checkMatchPreconditions(match, it) }
.then { processGuard(match, it) }
private fun checkMatchPreconditions(match: RuleMatchEx, inStatus: FeedbackStatus) : FeedbackStatus {
@ -109,7 +115,7 @@ internal class ControllerImpl (
// invoke matched pattern predicates
for (prd in match.patternPredicates()) {
// normally these are safe, but unification can fail
if (!tellPredicate(prd, context)) break
if (!tellPatternPredicate(prd, context)) break
}
return context.currentStatus()
@ -257,7 +263,17 @@ internal class ControllerImpl (
context.runSafe {
val args = supervisor.instantiateArguments(predicate.arguments(), context.logicalContext, context)
tryTell(predicate.invocation(args, context.logicalContext, context))
tryTellPattern(predicate.invocation(args, context.logicalContext, context))
}
}
private fun tellPatternPredicate(predicate: Predicate, context: Context) : Boolean =
profiler.profile<Boolean>("tell_${predicate.symbol()}") {
context.runSafe {
val args = supervisor.instantiateArguments(predicate.arguments(), context.logicalContext, context)
tryTellPattern(predicate.invocation(args, context.logicalContext, context))
}
}

View File

@ -52,9 +52,11 @@ public interface EvaluationTrace {
default void finish(RuleMatch ruleMatch) {}
default void ask(boolean result, PredicateInvocation invocation) {}
default void tell(PredicateInvocation invocation) {}
default void ask(boolean result, PredicateInvocation invocation) {}
default void tellPattern(PredicateInvocation invocation) {}
default void feedback(EvaluationFeedback feedback) {}