65 lines
2.6 KiB
Python
65 lines
2.6 KiB
Python
from core.builtin_concepts import ParserResultConcept, BuiltinConcepts
|
|
from core.concept import Concept, ConceptParts
|
|
from evaluators.BaseEvaluator import OneReturnValueEvaluator
|
|
|
|
|
|
class ConceptEvaluator(OneReturnValueEvaluator):
|
|
"""
|
|
The concept evaluator is the main class that know what to do with a concept
|
|
It verifies the PRE
|
|
If ok, can execute or not the BODY
|
|
Then checks the POST conditions
|
|
"""
|
|
NAME = "Concept"
|
|
|
|
def __init__(self, return_body=False):
|
|
super().__init__(self.NAME, [BuiltinConcepts.EVALUATION], 50)
|
|
self.return_body = return_body
|
|
|
|
# def init_evaluator(self, context, return_values):
|
|
# if BuiltinConcepts.EVAL_BODY_REQUESTED in context.local_hints:
|
|
# self.evaluate_body = True
|
|
#
|
|
# for r in return_values:
|
|
# if r.status and context.sheerka.isinstance(r.body, BuiltinConcepts.CONCEPT_VALUE_REQUESTED):
|
|
# self.evaluate_body = True
|
|
# break
|
|
#
|
|
# self.is_initialized = True
|
|
|
|
def matches(self, context, return_value):
|
|
return return_value.status and \
|
|
isinstance(return_value.value, ParserResultConcept) and \
|
|
isinstance(return_value.value.value, Concept)
|
|
|
|
def eval(self, context, return_value):
|
|
sheerka = context.sheerka
|
|
concept = return_value.value.value
|
|
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 ?
|
|
# If we evaluate Concept("foo", body="a").set_prop("a", "'property_a'")
|
|
# 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(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)
|
|
|
|
if evaluated.key != concept.key:
|
|
# evaluated.key != concept.key means that we have transformed the concept
|
|
# When you successfully evaluate an error, the status should not be false
|
|
return sheerka.ret(
|
|
self.name,
|
|
False,
|
|
evaluated,
|
|
parents=[return_value])
|
|
|
|
if not self.return_body or ConceptParts.BODY not in evaluated.compiled:
|
|
return sheerka.ret(self.name, True, evaluated, parents=[return_value])
|
|
else:
|
|
return sheerka.ret(self.name, True, evaluated.body, parents=[return_value])
|