Fixed memory() and RET usage

This commit is contained in:
2020-09-21 21:30:38 +02:00
parent 177a6b1d5f
commit dd520c1680
37 changed files with 816 additions and 353 deletions
+27
View File
@@ -0,0 +1,27 @@
from core.builtin_concepts import BuiltinConcepts
from evaluators.BaseEvaluator import OneReturnValueEvaluator
class AddToMemoryEvaluator(OneReturnValueEvaluator):
"""
Last chance to alter the return_value
This evaluator is supposed to be a generic evaluator for all rules that must be executed just before
the aggregations
"""
NAME = "AddToMemory"
def __init__(self):
super().__init__(self.NAME, [BuiltinConcepts.AFTER_EVALUATION], 10)
def matches(self, context, return_value):
evaluation_parents = context.get_parents(lambda c: c.action == BuiltinConcepts.PROCESSING)
if len(evaluation_parents) > 1:
return False # It must be executed only when the top level context
from core.sheerka.services.SheerkaMemory import SheerkaMemory
return len(context.sheerka.services[SheerkaMemory.NAME].registration) > 0
def eval(self, context, return_value):
context.sheerka.add_registered_objects(context)
return None # no need to have a second pass
+2 -1
View File
@@ -53,8 +53,9 @@ class ConceptEvaluator(OneReturnValueEvaluator):
evaluated = sheerka.evaluate_concept(context, concept)
if evaluated.key != concept.key:
if not sheerka.is_success(evaluated) and evaluated.key != concept.key:
# evaluated.key != concept.key means that we have transformed the concept
# not sheerka.is_success(evaluated) means that it was transformed into an error
# When you successfully evaluate an error, the status should not be false
return sheerka.ret(
self.name,
+1 -1
View File
@@ -22,7 +22,7 @@ class PostExecutionEvaluator(OneReturnValueEvaluator):
# only support the rule for the COMMANDS
value = return_value.body
return isinstance(value, Concept) and context.sheerka.isa(value, context.sheerka.new(BuiltinConcepts.COMMAND))
return isinstance(value, Concept) and context.sheerka.isa(value, context.sheerka.new(BuiltinConcepts.AUTO_EVAL))
def eval(self, context, return_value):
# only support the rule for the COMMANDS
+4 -1
View File
@@ -85,6 +85,9 @@ class PythonEvaluator(OneReturnValueEvaluator):
not_for_me = context.sheerka.new(BuiltinConcepts.NOT_FOR_ME, body=node)
return sheerka.ret(self.name, False, not_for_me, parents=[return_value])
# If we evaluate a Concept metadata which is NOT the body ex (pre, post, where...)
# We need to disable the function that may alter the state
# It's a poor way to have source code security check
attr_under_eval = context.get_parents(lambda ec: ec.action == BuiltinConcepts.EVALUATING_ATTRIBUTE)
if attr_under_eval:
attr_under_eval = attr_under_eval[0]
@@ -256,7 +259,7 @@ class PythonEvaluator(OneReturnValueEvaluator):
else:
context.log(f"Evaluating '{concept}'", self.name)
evaluated = context.sheerka.evaluate_concept(context, concept, eval_body=True)
if evaluated.key != concept.key:
if not context.sheerka.is_success(evaluated) and evaluated.key != concept.key:
context.log(f"Error while evaluating '{name}'. Skipping.", self.name)
continue
concept = evaluated
+41 -40
View File
@@ -1,40 +1,41 @@
from core.builtin_concepts import BuiltinConcepts
from core.builtin_helpers import ensure_evaluated
from core.concept import Concept, ConceptParts
from evaluators.BaseEvaluator import OneReturnValueEvaluator
class RetEvaluator(OneReturnValueEvaluator):
"""
The evaluator transforms the concept, using the RET metadata value
"""
NAME = "Ret"
def __init__(self):
super().__init__(self.NAME, [BuiltinConcepts.EVALUATION], 10)
def matches(self, context, return_value):
return return_value.status and \
isinstance(return_value.value, Concept) and \
return_value.value.metadata.ret is not None
def eval(self, context, return_value):
sheerka = context.sheerka
concept = return_value.value
context.log(f"Processing ret value for concept {concept}.", self.name)
if not concept.metadata.is_evaluated:
evaluated = ensure_evaluated(context, concept)
if evaluated.key != concept.key:
context.log(f"Failed to evaluate concept '{concept}'")
return None
ret = evaluated.get_value(ConceptParts.RET)
else:
ret = concept.get_value(ConceptParts.RET)
if isinstance(ret, Concept) and sheerka.is_known(ret):
return sheerka.ret(self.name, True, ret, parents=[return_value])
context.log(f"ret '{ret}' is not a concept!")
return None
# from core.builtin_concepts import BuiltinConcepts
# from core.builtin_helpers import ensure_evaluated
# from core.concept import Concept, ConceptParts
# from evaluators.BaseEvaluator import OneReturnValueEvaluator
#
#
# class RetEvaluator(OneReturnValueEvaluator):
# """
# The evaluator transforms the concept, using the RET metadata value
# """
#
# NAME = "Ret"
#
# def __init__(self):
# super().__init__(self.NAME, [BuiltinConcepts.EVALUATION], 10)
# self.enabled = False
#
# def matches(self, context, return_value):
# return return_value.status and \
# isinstance(return_value.value, Concept) and \
# return_value.value.metadata.ret is not None
#
# def eval(self, context, return_value):
# sheerka = context.sheerka
# concept = return_value.value
# context.log(f"Processing ret value for concept {concept}.", self.name)
#
# if not concept.metadata.is_evaluated:
# evaluated = ensure_evaluated(context, concept)
# if evaluated.key != concept.key:
# context.log(f"Failed to evaluate concept '{concept}'")
# return None
# ret = evaluated.get_value(ConceptParts.RET)
# else:
# ret = concept.get_value(ConceptParts.RET)
#
# if isinstance(ret, Concept) and sheerka.is_known(ret):
# return sheerka.ret(self.name, True, ret, parents=[return_value])
#
# context.log(f"ret '{ret}' is not a concept!")
# return None
+7 -3
View File
@@ -1,8 +1,8 @@
import logging
from core.builtin_concepts import BuiltinConcepts
import core.builtin_helpers
from evaluators.BaseEvaluator import AllReturnValuesEvaluator, BaseEvaluator
from core.builtin_concepts import BuiltinConcepts
from evaluators.BaseEvaluator import AllReturnValuesEvaluator
from parsers.BaseParser import BaseParser
@@ -44,7 +44,11 @@ class TooManySuccessEvaluator(AllReturnValuesEvaluator):
context.log(s, self.name)
context.log(f"value={sheerka.value(s.value)}", self.name)
if not core.builtin_helpers.is_same_success(context, self.success):
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)