58 lines
2.1 KiB
Python
58 lines
2.1 KiB
Python
import logging
|
|
|
|
import core.builtin_helpers
|
|
from core.builtin_concepts import BuiltinConcepts
|
|
from evaluators.BaseEvaluator import AllReturnValuesEvaluator
|
|
from parsers.BaseParser import BaseParser
|
|
|
|
|
|
class TooManySuccessEvaluator(AllReturnValuesEvaluator):
|
|
"""
|
|
Used to filter the responses
|
|
It has a low priority to let other evaluators try to resolve the errors
|
|
|
|
Raises an error when that are several successful answers, with different values
|
|
"""
|
|
|
|
NAME = "TooManySuccess"
|
|
|
|
def __init__(self):
|
|
super().__init__(self.NAME, [BuiltinConcepts.AFTER_EVALUATION], 60)
|
|
self.success = []
|
|
|
|
def matches(self, context, return_values):
|
|
to_process = False
|
|
|
|
for ret in return_values:
|
|
if ret.status and ret.who.startswith(BaseParser.PREFIX):
|
|
return False
|
|
elif ret.status and context.sheerka.isinstance(ret.body, BuiltinConcepts.REDUCE_REQUESTED):
|
|
to_process = True
|
|
self.eaten.append(ret)
|
|
elif ret.status and ret.who.startswith(self.PREFIX):
|
|
self.success.append(ret)
|
|
self.eaten.append(ret)
|
|
elif not ret.status:
|
|
self.eaten.append(ret)
|
|
|
|
return to_process and len(self.success) > 1
|
|
|
|
def eval(self, context, return_values):
|
|
sheerka = context.sheerka
|
|
if self.verbose_log.isEnabledFor(logging.DEBUG):
|
|
for s in self.success:
|
|
context.log(s, self.name)
|
|
context.log(f"value={sheerka.value(s.value)}", self.name)
|
|
|
|
same_success = core.builtin_helpers.is_same_success(context, self.success)
|
|
if same_success is None:
|
|
return None
|
|
|
|
if not same_success:
|
|
context.log(f"Values are different. Raising {BuiltinConcepts.TOO_MANY_SUCCESS}.", self.name)
|
|
too_many_success = sheerka.new(BuiltinConcepts.TOO_MANY_SUCCESS, body=self.success)
|
|
return sheerka.ret(self.name, False, too_many_success, parents=self.eaten)
|
|
|
|
context.log(f"Values are the same. Nothing to do.", self.name)
|
|
return None
|