945807b375
Fixed #74 : Keyword parameters are no longer recognized when a concept that redefines equality is created Fixed #118 : RecursionError: maximum recursion depth exceeded Fixed #119 : PreventCircularReferenceEvaluator Fixed #121 : Plural are not updated when new elements are added Fixed #123 : BaseCache : Values in cache can be evicted before being committed Fixed #105 : TOO_MANY_ERROR is not the relevant error when results are filtered
159 lines
4.3 KiB
Python
159 lines
4.3 KiB
Python
import ast
|
|
|
|
from core.builtin_concepts import BuiltinConcepts, ParserResultConcept, ReturnValueConcept
|
|
from core.concept import Concept
|
|
from evaluators.BaseEvaluator import BaseEvaluator
|
|
from parsers.BaseNodeParser import ConceptNode
|
|
from parsers.ExactConceptParser import ExactConceptParser
|
|
from parsers.PythonParser import PythonNode, PythonParser
|
|
from parsers.SequenceNodeParser import SequenceNodeParser
|
|
from parsers.SyaNodeParser import SyaNodeParser
|
|
|
|
reduced_requested = ReturnValueConcept("Sheerka", True, Concept(name=BuiltinConcepts.REDUCE_REQUESTED,
|
|
key=BuiltinConcepts.REDUCE_REQUESTED))
|
|
|
|
sequence = SequenceNodeParser()
|
|
sya = SyaNodeParser()
|
|
exact = ExactConceptParser()
|
|
python = PythonParser()
|
|
|
|
|
|
def ret_val(value="value", who="who", status=True):
|
|
"""
|
|
ReturnValueConcept
|
|
:param value:
|
|
:param who:
|
|
:param status:
|
|
:return:
|
|
"""
|
|
return ReturnValueConcept(who, status, value)
|
|
|
|
|
|
def p_ret_val(value="value", parser=exact, status=True):
|
|
"""
|
|
ReturnValueConcept from parser
|
|
:param value:
|
|
:param parser:
|
|
:param status:
|
|
:return:
|
|
"""
|
|
return ReturnValueConcept(parser.name, status, value)
|
|
|
|
|
|
def e_ret_val(value="value", evaluator="evaluator", status=True):
|
|
"""
|
|
ReturnValueConcept from evaluator
|
|
:param value:
|
|
:param evaluator:
|
|
:param status:
|
|
:return:
|
|
"""
|
|
return ReturnValueConcept(BaseEvaluator.PREFIX + evaluator, status, value)
|
|
|
|
|
|
def p_ret_val_false(value="value", parser=exact):
|
|
"""
|
|
Failed ReturnValueConcept from parser
|
|
:param value:
|
|
:param parser:
|
|
:return:
|
|
"""
|
|
return p_ret_val(value, parser, status=False)
|
|
|
|
|
|
def p_ret_val_true(value="value", parser=exact):
|
|
"""
|
|
Successful ReturnValueConcept from parser
|
|
:param value:
|
|
:param parser:
|
|
:return:
|
|
"""
|
|
return p_ret_val(value, parser, status=True)
|
|
|
|
|
|
def e_ret_val_false(value="value", evaluator="evaluator"):
|
|
"""
|
|
Failed ReturnValueConcept from evaluator
|
|
:param value:
|
|
:param evaluator:
|
|
:return:
|
|
"""
|
|
return e_ret_val(value, evaluator, status=False)
|
|
|
|
|
|
def e_ret_val_true(value="value", evaluator="evaluator"):
|
|
"""
|
|
Successful ReturnValueConcept from evaluator
|
|
:param value:
|
|
:param evaluator:
|
|
:return:
|
|
"""
|
|
return e_ret_val(value, evaluator, status=True)
|
|
|
|
|
|
def e_ret_val_new(key, evaluator="evaluator", status=True, **kwargs):
|
|
"""
|
|
Successful ReturnValueConcept from evaluator that returns a concept
|
|
:param key:
|
|
:param evaluator:
|
|
:param status:
|
|
:param kwargs:
|
|
:return:
|
|
"""
|
|
body = new_concept(key, **kwargs)
|
|
return e_ret_val(body, evaluator, status)
|
|
|
|
|
|
def pr_ret_val(value, parser=exact, source=None, status=True):
|
|
"""
|
|
ParserResult ReturnValue
|
|
eg: ReturnValue with a ParserResult
|
|
:param value:
|
|
:param parser:
|
|
:param source:
|
|
:param status:
|
|
:return:
|
|
"""
|
|
source = source or (value.name if isinstance(value, Concept) else "source")
|
|
parser_result = ParserResultConcept(parser, source=source, value=value)
|
|
return p_ret_val(value=parser_result, parser=parser, status=status)
|
|
|
|
|
|
def python_ret_val(source):
|
|
"""
|
|
ReturnValueConcept with a PythonNode
|
|
:param source:
|
|
:return:
|
|
"""
|
|
python_node = PythonNode(source.lstrip(), ast.parse(source.strip(), f"<source>", 'eval'))
|
|
return pr_ret_val(python_node, parser=python, source=source)
|
|
|
|
|
|
def cnode_ret_val(concept, source=None, parser=sya):
|
|
source = source or concept.name
|
|
cnode = ConceptNode(concept, 0, 0, source=source)
|
|
return pr_ret_val([cnode], parser=parser, source=source)
|
|
|
|
|
|
def concept_ret_val(concept, source=None, parser=exact):
|
|
source = source or concept.name
|
|
return pr_ret_val(concept, parser=parser, source=source)
|
|
|
|
|
|
def new_concept(key, **kwargs):
|
|
res = Concept(key=key, name=key, is_builtin=False, is_unique=False)
|
|
for k, v in kwargs.items():
|
|
to_use = "#" + k + "#" if k in ("body", "pre", "post", "ret") else k
|
|
res.set_value(to_use, v)
|
|
|
|
res.get_hints().is_evaluated = True
|
|
return res
|
|
|
|
|
|
def new_plural(name):
|
|
name_stripped_s = name.lstrip("s")
|
|
single = Concept(name_stripped_s)
|
|
concept = Concept(key=name, name=name, id=f"{name}:{BuiltinConcepts.PLURAL}", is_builtin=False, is_unique=False)
|
|
concept.set_prop(BuiltinConcepts.PLURAL, single)
|
|
return concept
|