Logger is now an attribute of the execution context

This commit is contained in:
2020-02-18 16:31:55 +01:00
parent 86c2ff58d4
commit 87f232b527
27 changed files with 213 additions and 243 deletions
+3 -3
View File
@@ -47,7 +47,7 @@ class AddConceptEvaluator(OneReturnValueEvaluator):
isinstance(return_value.value.value, DefConceptNode)
def eval(self, context, return_value):
context.log(self.log, "Adding a new concept", self.name)
context.log("Adding a new concept", self.name)
def_concept_node = return_value.value.value
sheerka = context.sheerka
@@ -86,10 +86,10 @@ class AddConceptEvaluator(OneReturnValueEvaluator):
sheerka.is_success(def_concept_node.definition):
concept.bnf = def_concept_node.definition.value.value
ret = sheerka.create_new_concept(context, concept, self.verbose_log)
ret = sheerka.create_new_concept(context, concept)
if not ret.status:
error_cause = sheerka.value(ret.body)
context.log(self.log, f"Failed to add concept '{concept.name}'. Reason: {error_cause}", self.name)
context.log(f"Failed to add concept '{concept.name}'. Reason: {error_cause}", self.name)
return sheerka.ret(self.name, ret.status, ret.value, parents=[return_value])
@staticmethod
+5 -5
View File
@@ -34,14 +34,14 @@ class AddConceptInSetEvaluator(OneReturnValueEvaluator):
sheerka.new(BuiltinConcepts.USER_INPUT, body=name_node.tokens, user_name="N/A"))
with context.push(desc=f"Recognizing '{name_node}'") as sub_context:
r = sheerka.execute(sub_context, ret_val, ALL_STEPS, self.verbose_log)
r = sheerka.execute(sub_context, ret_val, ALL_STEPS)
one_r = core.builtin_helpers.expect_one(context, r)
sub_context.add_values(return_values=one_r)
return one_r
isa_node = return_value.value.value
sheerka = context.sheerka
context.log(self.log, f"Adding a concept {isa_node.concept} to set {isa_node.set}", self.name)
context.log(f"Adding concept '{isa_node.concept}' to set '{isa_node.set}'", self.name)
# Try to recognize the concept
res = _resolve(isa_node.concept)
@@ -62,11 +62,11 @@ class AddConceptInSetEvaluator(OneReturnValueEvaluator):
parents=[return_value])
concept_set = res.value
res = sheerka.add_concept_to_set(context, concept, concept_set, self.verbose_log)
res = sheerka.set_isa(context, concept, concept_set)
if not res.status:
context.log(self.log, f"Failed. Reason: {sheerka.value(res.body)}.", self.name)
context.log(f"Failed. Reason: {sheerka.value(res.body)}.", self.name)
else:
context.log(self.log, f"Concept added.", self.name)
context.log(f"Concept added.", self.name)
return sheerka.ret(
self.name,
+3 -3
View File
@@ -24,7 +24,7 @@ class ConceptEvaluator(OneReturnValueEvaluator):
def eval(self, context, return_value):
sheerka = context.sheerka
concept = return_value.value.value
context.log(self.verbose_log, f"Evaluating concept {concept}.", self.name)
context.log(f"Evaluating concept {concept}.", self.name)
# If the concept that is requested is in the context(at least its name), drop the call.
# Why ?
@@ -32,11 +32,11 @@ class ConceptEvaluator(OneReturnValueEvaluator):
# The body should be 'property_a', and not a concept called 'a'
if context.obj and concept.name in context.obj.props:
value = context.obj.props[concept.name].value
context.log(self.verbose_log, f"{concept.name} is a property. Returning value '{value}'.", self.name)
context.log(f"{concept.name} is a property. Returning value '{value}'.", self.name)
return sheerka.ret(self.name, True, value, parents=[return_value])
evaluated = sheerka.evaluate_concept(context, concept, self.verbose_log)
evaluated = sheerka.evaluate_concept(context, concept)
if evaluated.key != concept.key:
# evaluated.key != concept.key means that we have transformed the concept
+2 -2
View File
@@ -29,10 +29,10 @@ class EvalEvaluator(AllReturnValuesEvaluator):
for ret_val in return_values:
if ret_val.status and isinstance(ret_val.body, Concept) and ret_val.body.body:
context.log(self.verbose_log, f"Evaluating {ret_val.body}", who=self)
context.log(f"Evaluating {ret_val.body}", who=self)
result.append(sheerka.ret(self.name, True, ret_val.body.body, parents=[ret_val, self.eval_requested]))
elif ret_val.status and sheerka.isaset(context, ret_val.body):
context.log(self.verbose_log, f"Evaluating set {ret_val.body}", who=self)
context.log(f"Evaluating set {ret_val.body}", who=self)
result.append(sheerka.ret(
self.name,
True,
+1 -1
View File
@@ -44,7 +44,7 @@ class LexerNodeEvaluator(OneReturnValueEvaluator):
if not hasattr(nodes, "__iter__"):
nodes = [nodes]
context.log(self.verbose_log, f"{nodes=}", self.name)
context.log(f"{nodes=}", self.name)
for node in nodes:
if isinstance(node, SourceCodeNode):
@@ -45,9 +45,9 @@ class MultipleSameSuccessEvaluator(AllReturnValuesEvaluator):
def eval(self, context, return_values):
sheerka = context.sheerka
context.log(self.verbose_log, f"{len(self.success)} successful return value(s)", who=self)
context.log(f"{len(self.success)} successful return value(s)", who=self)
for s in self.success:
context.log(self.verbose_log, f"{s}", who=self)
context.log(f"{s}", who=self)
if not core.builtin_helpers.is_same_success(sheerka, self.success):
return None
+2 -2
View File
@@ -35,8 +35,8 @@ class OneErrorEvaluator(AllReturnValuesEvaluator):
return to_process and nb_evaluators_in_error == 1
def eval(self, context, return_values):
context.log(self.verbose_log, f"1 return value in error, {len(self.eaten)} item(s) eaten", who=self)
context.log(self.verbose_log, f"{self.return_value_in_error}", who=self)
context.log(f"1 return value in error, {len(self.eaten)} item(s) eaten", who=self)
context.log(f"{self.return_value_in_error}", who=self)
sheerka = context.sheerka
return sheerka.ret(self.name, False, self.return_value_in_error.value, parents=self.eaten)
+2 -2
View File
@@ -37,8 +37,8 @@ class OneSuccessEvaluator(AllReturnValuesEvaluator):
return to_process and nb_successful_evaluators == 1
def eval(self, context, return_values):
context.log(self.verbose_log, f"1 successful return value, {len(self.eaten)} item(s) eaten", who=self)
context.log(self.verbose_log, f"{self.successful_return_value}", who=self)
context.log(f"1 successful return value, {len(self.eaten)} item(s) eaten", who=self)
context.log(f"{self.successful_return_value}", who=self)
sheerka = context.sheerka
return sheerka.ret(self.name, True, self.successful_return_value.value, parents=self.eaten)
+16 -58
View File
@@ -31,35 +31,35 @@ class PythonEvaluator(OneReturnValueEvaluator):
sheerka = context.sheerka
node = return_value.value.value
try:
context.log(self.verbose_log, f"Evaluating python node {node}.", self.name)
context.log(f"Evaluating python node {node}.", self.name)
# Do not evaluate if the ast refers to a concept (leave it to ConceptEvaluator)
if isinstance(node.ast_, ast.Expression) and isinstance(node.ast_.body, ast.Name):
c = context.sheerka.get(node.ast_.body.id)
if not context.sheerka.isinstance(c, BuiltinConcepts.UNKNOWN_CONCEPT):
context.log(self.verbose_log, "It's a simple concept. Not for me.", self.name)
context.log("It's a simple concept. Not for me.", self.name)
not_for_me = context.sheerka.new(BuiltinConcepts.NOT_FOR_ME, body=node)
return sheerka.ret(self.name, False, not_for_me, parents=[return_value])
# get locals
my_globals = self.get_globals(context, node)
my_locals = {}
context.log(self.verbose_log, f"globals={my_globals}", self.name)
context.log(f"globals={my_globals}", self.name)
# eval
if isinstance(node.ast_, ast.Expression):
context.log(self.verbose_log, "Evaluating using 'eval'.", self.name)
context.log("Evaluating using 'eval'.", self.name)
compiled = compile(node.ast_, "<string>", "eval")
evaluated = eval(compiled, my_globals, my_locals)
else:
context.log(self.verbose_log, "Evaluating using 'exec'.", self.name)
context.log("Evaluating using 'exec'.", self.name)
evaluated = self.exec_with_return(node.ast_, my_globals, my_locals)
context.log(self.verbose_log, f"{evaluated=}", self.name)
context.log(f"{evaluated=}", self.name)
return sheerka.ret(self.name, True, evaluated, parents=[return_value])
except Exception as error:
context.log_error(self.verbose_log, error, self.name)
context.log_error(error, self.name)
error = sheerka.new(BuiltinConcepts.ERROR, body=error)
return sheerka.ret(self.name, False, error, parents=[return_value])
@@ -74,8 +74,7 @@ class PythonEvaluator(OneReturnValueEvaluator):
"Concept": core.concept.Concept
}
if context.obj:
context.log(self.verbose_log,
f"Concept '{context.obj}' is in context. Adding it and its properties to locals.", self.name)
context.log(f"Concept '{context.obj}' is in context. Adding it and its properties to locals.", self.name)
for prop_name, prop_value in context.obj.props.items():
if isinstance(prop_value.value, Concept):
@@ -90,10 +89,10 @@ class PythonEvaluator(OneReturnValueEvaluator):
unreferenced_names_visitor.visit(node_concept)
for name in unreferenced_names_visitor.names:
context.log(self.verbose_log, f"Resolving '{name}'.", self.name)
context.log(f"Resolving '{name}'.", self.name)
if name in node.concepts:
context.log(self.verbose_log, f"Using value from node.", self.name)
context.log(f"Using value from node.", self.name)
concept = node.concepts[name]
return_concept = False
@@ -101,20 +100,19 @@ class PythonEvaluator(OneReturnValueEvaluator):
c_key, c_id, return_concept = self.resolve_name(name)
if c_key in my_locals:
context.log(self.verbose_log, f"Using value from property.", self.name)
context.log(f"Using value from property.", self.name)
continue
context.log(self.verbose_log, f"Instantiating new concept with {c_key=}, {c_id=}.", self.name)
context.log(f"Instantiating new concept with {c_key=}, {c_id=}.", self.name)
new = context.sheerka.new
concept = new((None, c_id)) if c_id else new(c_key)
if context.sheerka.isinstance(concept, BuiltinConcepts.UNKNOWN_CONCEPT):
context.log(self.verbose_log, f"({c_key=}, {c_id=}) is not a concept. Skipping.", self.name)
context.log(f"({c_key=}, {c_id=}) is not a concept. Skipping.", self.name)
continue
context.log(self.verbose_log, f"Evaluating '{concept}'", self.name)
context.log(f"Evaluating '{concept}'", self.name)
with context.push(self.name, desc=f"Evaluating '{concept}'", obj=concept) as sub_context:
sub_context.log_new(self.verbose_log)
evaluated = context.sheerka.evaluate_concept(sub_context, concept, self.verbose_log)
evaluated = context.sheerka.evaluate_concept(sub_context, concept)
sub_context.add_values(return_values=evaluated)
if evaluated.key == concept.key:
@@ -141,47 +139,7 @@ class PythonEvaluator(OneReturnValueEvaluator):
return key, id_, use_concept
else:
return to_resolve, None, False
#
# if not to_resolve.startswith("__C__"):
# return to_resolve, None, False
#
# context.log(self.verbose_log, f"Resolving name '{to_resolve}'.", self.name)
#
# if len(to_resolve) >= 18 and to_resolve[:18] == "__C__USE_CONCEPT__":
# use_concept = True
# index = 18
# else:
# use_concept = False
# index = 5
#
# try:
# next_index = to_resolve.index("__", index)
# if next_index == index:
# context.log(self.verbose_log, f"Error: no key between '__'.", self.name)
# return None
# concept_key = to_resolve[index: next_index]
# except ValueError:
# context.log(self.verbose_log, f"Error: Missing trailing '__'.", self.name)
# return None
#
# if next_index == len(to_resolve) - 5:
# context.log(self.verbose_log, f"Recognized concept '{concept_key}'", self.name)
# return concept_key, None, use_concept
#
# index = next_index + 2
# try:
# next_index = to_resolve.index("__", index)
# if next_index == index:
# context.log(self.verbose_log, f"Error: no id between '__'.", self.name)
# return None
#
# concept_id = to_resolve[index: next_index]
# except ValueError:
# context.log(self.verbose_log, f"Recognized concept '{concept_key}'.", self.name)
# return concept_key, None, use_concept
#
# context.log(self.verbose_log, f"Recognized concept '{concept_key}' (id='{concept_id}').", self.name)
# return concept_key, concept_id, use_concept
@staticmethod
def expr_to_expression(expr):
+4 -5
View File
@@ -41,14 +41,13 @@ class TooManySuccessEvaluator(AllReturnValuesEvaluator):
sheerka = context.sheerka
if self.verbose_log.isEnabledFor(logging.DEBUG):
for s in self.success:
context.log(self.verbose_log, s, self.name)
context.log(self.verbose_log, f"value={sheerka.value(s.value)}", self.name)
context.log(s, self.name)
context.log(f"value={sheerka.value(s.value)}", self.name)
if not core.builtin_helpers.is_same_success(sheerka, self.success):
context.log(self.verbose_log,
f"Values are different. Raising {BuiltinConcepts.TOO_MANY_SUCCESS}.", self.name)
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(self.verbose_log, f"Values are the same. Nothing to do.", self.name)
context.log(f"Values are the same. Nothing to do.", self.name)
return None