EvalEvaluator is called only if in root context. Added action and action_context to ExecutionContext

This commit is contained in:
2020-06-12 17:47:29 +02:00
parent c43a3ef946
commit 912455c343
27 changed files with 292 additions and 117 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ class BaseTest:
pass
def get_context(self, sheerka=None, eval_body=False, eval_where=False):
context = ExecutionContext("test", Event(), sheerka or self.get_sheerka())
context = ExecutionContext("test", Event(), sheerka or self.get_sheerka(), BuiltinConcepts.NOP, None)
if eval_body:
context.local_hints.add(BuiltinConcepts.EVAL_BODY_REQUESTED)
if eval_where:
+22 -19
View File
@@ -7,11 +7,11 @@ from sdp.sheerkaDataProvider import Event
def test_id_is_incremented_by_event_digest():
a = ExecutionContext("foo", Event("event_1"), None)
b = ExecutionContext("foo", Event("event_1"), None)
c = ExecutionContext("foo", Event("event_2"), None)
d = b.push()
e = c.push()
a = ExecutionContext("foo", Event("event_1"), None, BuiltinConcepts.NOP, None)
b = ExecutionContext("foo", Event("event_1"), None, BuiltinConcepts.NOP, None)
c = ExecutionContext("foo", Event("event_2"), None, BuiltinConcepts.NOP, None)
d = b.push(BuiltinConcepts.NOP, None)
e = c.push(BuiltinConcepts.NOP, None)
assert a.id == 0
assert b.id == 1
@@ -21,13 +21,14 @@ def test_id_is_incremented_by_event_digest():
def test_i_can_use_with_statement():
with ExecutionContext("who_", Event("event"), "fake_sheerka") as e:
with ExecutionContext("who_", Event("event"), "fake_sheerka", BuiltinConcepts.NOP, None) as e:
pass
assert e.elapsed > 0
def test_i_can_push():
a = ExecutionContext("foo", Event("event_1"), "fake_sheerka", "some description",
a = ExecutionContext("foo", Event("event_1"), "fake_sheerka", BuiltinConcepts.NOP, None,
desc="some description",
obj=Concept("foo"),
step=BuiltinConcepts.EVALUATION,
iteration=15,
@@ -37,13 +38,15 @@ def test_i_can_push():
a.local_hints.add(BuiltinConcepts.EVAL_BODY_REQUESTED)
a.global_hints.add(BuiltinConcepts.EVAL_BODY_REQUESTED)
b = a.push()
b = a.push(BuiltinConcepts.EVALUATION, "sub action context", desc="sub description")
assert b._parent == a
assert b.who == a.who
assert b.event == a.event
assert b.sheerka == a.sheerka
assert b.desc is None
assert b.action == BuiltinConcepts.EVALUATION
assert b.action_context == "sub action context"
assert b.desc == "sub description"
assert b.obj == a.obj
assert b.step == a.step
assert b.iteration == a.iteration
@@ -56,10 +59,10 @@ def test_i_can_push():
def test_children_i_created_when_i_push():
e = ExecutionContext("who_", Event("event"), "fake_sheerka")
e.push("a", desc="I do something")
e.push("b", desc="oups! I did a again")
e.push("c", desc="I do something else")
e = ExecutionContext("who_", Event("event"), "fake_sheerka", BuiltinConcepts.NOP, None)
e.push(BuiltinConcepts.NOP, None, who="a", desc="I do something")
e.push(BuiltinConcepts.NOP, None, who="b", desc="oups! I did a again")
e.push(BuiltinConcepts.NOP, None, who="c", desc="I do something else")
assert len(e.children) == 3
assert e.children[0].who, e.children[0].who == ("a", "I do something")
@@ -68,8 +71,8 @@ def test_children_i_created_when_i_push():
def test_i_can_add_variable_when_i_push():
e = ExecutionContext("who_", Event("event"), "fake_sheerka")
sub_e = e.push("a", my_new_var="new var value")
e = ExecutionContext("who_", Event("event"), "fake_sheerka", BuiltinConcepts.NOP, None)
sub_e = e.push(BuiltinConcepts.NOP, None, who="a", my_new_var="new var value")
assert sub_e.my_new_var == "new var value"
with pytest.raises(AttributeError):
@@ -77,11 +80,11 @@ def test_i_can_add_variable_when_i_push():
def test_local_hints_are_local_and_global_hints_are_global():
a = ExecutionContext("foo", Event("event_1"), "fake_sheerka")
a = ExecutionContext("foo", Event("event_1"), "fake_sheerka", BuiltinConcepts.NOP, None)
a.local_hints.add("local hint 1")
a.global_hints.add("global hint 1")
b = a.push()
b = a.push(BuiltinConcepts.NOP, None)
b.local_hints.add("local hint 2")
b.global_hints.add("global hint 2")
@@ -93,9 +96,9 @@ def test_local_hints_are_local_and_global_hints_are_global():
def test_global_hits_are_global_even_when_empty():
a = ExecutionContext("foo", Event("event_1"), "fake_sheerka")
a = ExecutionContext("foo", Event("event_1"), "fake_sheerka", BuiltinConcepts.NOP, None)
b = a.push()
b = a.push(BuiltinConcepts.NOP, None)
b.global_hints.add("global hint 2")
assert a.global_hints == {"global hint 2"}
+1 -1
View File
@@ -68,7 +68,7 @@ class TestSheerkaFilter(TestUsingMemoryBasedSheerka):
def test_i_can_pipe_explanation_concept(self):
sheerka, context = self.init_concepts()
execution_contexts = [context.push(desc=f"desc_{i}") for i in range(4)]
execution_contexts = [context.push(BuiltinConcepts.NOP, None, desc=f"desc_{i}") for i in range(4)]
explanation_node = sheerka.new(BuiltinConcepts.EXPLANATION, body=execution_contexts)
@Pipe
+4 -1
View File
@@ -219,7 +219,10 @@ class TestSheerkaPrinter(TestUsingMemoryBasedSheerka):
sheerka = self.get_sheerka()
context = self.get_context(sheerka)
execution_context = context.push("test_sheerka_printer", "Testing Execution Context Printing")
execution_context = context.push(BuiltinConcepts.NOP,
None,
who="test_sheerka_printer",
desc="Testing Execution Context Printing")
ret_val = sheerka.ret("test_sheerka_printer", True, sheerka.new(BuiltinConcepts.SUCCESS))
execution_context.add_values(return_value=ret_val)
ec = execution_context.as_bag()
+22 -3
View File
@@ -1,9 +1,9 @@
import pytest
from core.builtin_concepts import ReturnValueConcept, BuiltinConcepts
from core.concept import Concept
from core.sheerka.services.SheerkaSetsManager import SheerkaSetsManager
from evaluators.EvalEvaluator import EvalEvaluator
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
@@ -22,8 +22,8 @@ class TestEvalEvaluator(TestUsingMemoryBasedSheerka):
return_values = [
ReturnValueConcept("some_name", True, "not to eval"),
ReturnValueConcept("some_name", True, Concept(name="not to eval")),
ReturnValueConcept("some_name", False, Concept(name="1", body="'not to eval'")),
ReturnValueConcept("some_name", True, Concept(name="not to eval")), # no body
ReturnValueConcept("some_name", False, Concept(name="1", body="'not to eval'")), # status is false
to_eval1,
to_eval2,
]
@@ -52,6 +52,25 @@ class TestEvalEvaluator(TestUsingMemoryBasedSheerka):
context.global_hints.add(BuiltinConcepts.CONCEPT_VALUE_REQUESTED)
assert EvalEvaluator().matches(context, [return_value])
def test_i_can_match_depending_on_builtin_concept_processing(self):
context = self.get_context()
context.global_hints.add(BuiltinConcepts.CONCEPT_VALUE_REQUESTED)
return_values = [ReturnValueConcept("some_name", True, Concept(name="2", body="to eval").auto_init())]
evaluator = EvalEvaluator()
# i match when no BuiltinConcepts.PROCESSING is found
assert evaluator.matches(context, return_values)
# i match when one BuiltinConcepts.PROCESSING is found
root_processing = context.push(BuiltinConcepts.PROCESSING, {"step": BuiltinConcepts.EVALUATION}). \
push(BuiltinConcepts.NOP, None)
assert evaluator.matches(root_processing, return_values)
# otherwise, i cannot match
sub_root = root_processing.push(BuiltinConcepts.PROCESSING, {"step": BuiltinConcepts.EVALUATION}). \
push(BuiltinConcepts.NOP, None)
assert not evaluator.matches(sub_root, return_values)
def test_concept_eval_requested_is_reduced_when_nothing_to_reduce(self):
context = self.get_context()
+27 -2
View File
@@ -1,7 +1,8 @@
import pytest
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept, PROPERTIES_TO_SERIALIZE, simplec, CMV
from core.concept import Concept, PROPERTIES_TO_SERIALIZE, simplec, CMV, CB, CC, CV
from evaluators.MutipleSameSuccessEvaluator import MultipleSameSuccessEvaluator
from evaluators.PythonEvaluator import PythonEvalError
from parsers.BaseNodeParser import SyaAssociativity
from parsers.BnfNodeParser import Sequence, StrMatch, OrderedChoice, Optional, ConceptExpression
@@ -916,10 +917,34 @@ as:
assert sheerka.isinstance(res[0].body, BuiltinConcepts.SUCCESS)
assert sheerka.get_concepts_weights("some_prop") == {'1001': 1, '1002': 2}
# it now also works using the concepts names
expression = "eval one < two"
res = sheerka.evaluate_user_input(expression)
assert not res[0].status
assert res[0].status
assert sheerka.isinstance(res[0].body, BuiltinConcepts.SUCCESS)
assert sheerka.get_concepts_weights("some_prop") == {'1001': 1, '1002': 2}
def test_i_can_detect_multiple_errors_when_evaluating_a_concept(self):
sheerka, context, foo, plus_one = self.init_concepts(
Concept("foo", body="'string'"),
Concept("a plus one", body="a + 1").def_var("a")
)
res = sheerka.evaluate_user_input("eval foo plus one")
assert not res[0].status
assert context.sheerka.isinstance(res[0].body, BuiltinConcepts.CONCEPT_EVAL_ERROR)
assert context.sheerka.isinstance(res[0].body.body, BuiltinConcepts.TOO_MANY_ERRORS)
error0 = res[0].body.body.body[0]
assert isinstance(error0, PythonEvalError)
assert isinstance(error0.error, TypeError)
assert error0.error.args[0] == 'can only concatenate str (not "int") to str'
error1 = res[0].body.body.body[1]
assert isinstance(error1, PythonEvalError)
assert isinstance(error1.error, TypeError)
assert error1.error.args[0] == "unsupported operand type(s) for +: 'Concept' and 'int'"
class TestSheerkaNonRegFile(TestUsingFileBasedSheerka):
+8 -8
View File
@@ -201,7 +201,7 @@ class TestSheerkaPickleHandler(TestUsingMemoryBasedSheerka):
to_string = sheerkapickle.encode(sheerka, user_input)
decoded = sheerkapickle.decode(sheerka, to_string)
assert decoded == user_input
assert to_string == '{"_sheerka/obj": "core.builtin_concepts.UserInputConcept", "concept/id": ["__USER_INPUT", "11"], "user_name": "my_user_name", "text": "my_text"}'
assert to_string == '{"_sheerka/obj": "core.builtin_concepts.UserInputConcept", "concept/id": ["__USER_INPUT", "22"], "user_name": "my_user_name", "text": "my_text"}'
def test_i_can_encode_decode_user_input_when_tokens(self):
sheerka = self.get_sheerka()
@@ -213,7 +213,7 @@ class TestSheerkaPickleHandler(TestUsingMemoryBasedSheerka):
to_string = sheerkapickle.encode(sheerka, user_input)
decoded = sheerkapickle.decode(sheerka, to_string)
assert decoded == sheerka.new(BuiltinConcepts.USER_INPUT, body=text, user_name="my_user_name")
assert to_string == '{' + f'"_sheerka/obj": "core.builtin_concepts.UserInputConcept", "concept/id": ["__USER_INPUT", "11"], "user_name": "my_user_name", "text": "{text}"' + '}'
assert to_string == '{' + f'"_sheerka/obj": "core.builtin_concepts.UserInputConcept", "concept/id": ["__USER_INPUT", "22"], "user_name": "my_user_name", "text": "{text}"' + '}'
def test_i_can_encode_decode_return_value(self):
sheerka = self.get_sheerka()
@@ -223,7 +223,7 @@ class TestSheerkaPickleHandler(TestUsingMemoryBasedSheerka):
to_string = sheerkapickle.encode(sheerka, ret_val)
decoded = sheerkapickle.decode(sheerka, to_string)
assert decoded == ret_val
assert to_string == '{"_sheerka/obj": "core.builtin_concepts.ReturnValueConcept", "concept/id": ["__RETURN_VALUE", "16"], "who": "who", "status": true, "value": 10}'
assert to_string == '{"_sheerka/obj": "core.builtin_concepts.ReturnValueConcept", "concept/id": ["__RETURN_VALUE", "27"], "who": "who", "status": true, "value": 10}'
def test_i_can_encode_decode_return_value_with_parent(self):
sheerka = self.get_sheerka()
@@ -236,7 +236,7 @@ class TestSheerkaPickleHandler(TestUsingMemoryBasedSheerka):
decoded = sheerkapickle.decode(sheerka, to_string)
assert decoded == ret_val
assert decoded.parents == ret_val.parents
id_str = ', "concept/id": ["__RETURN_VALUE", "16"]'
id_str = ', "concept/id": ["__RETURN_VALUE", "27"]'
parents_str = '[{"_sheerka/obj": "core.builtin_concepts.ReturnValueConcept"' + id_str + ', "who": "parent_who", "status": true, "value": "10"}, {"_sheerka/id": 1}]'
assert to_string == '{"_sheerka/obj": "core.builtin_concepts.ReturnValueConcept"' + id_str + ', "who": "who", "status": true, "value": 10, "parents": ' + parents_str + '}'
@@ -272,13 +272,13 @@ class TestSheerkaPickleHandler(TestUsingMemoryBasedSheerka):
def test_i_can_encode_decode_execution_context(self):
sheerka = self.get_sheerka()
context = ExecutionContext("who", Event("xxx"), sheerka, "my desc")
context = ExecutionContext("who", Event("xxx"), sheerka, BuiltinConcepts.NOP, None, "my desc")
input_list = [ReturnValueConcept("who", True, 10), ReturnValueConcept("who2", False, 20)]
context.inputs = {"a": input_list, "b": set_full_serialization(Concept("foo"))}
context.values = {"c": input_list, "d": set_full_serialization(Concept("bar"))}
context.obj = set_full_serialization(Concept("baz"))
context.push("who3", "sub_child1")
context.push("who4", "sub_child2")
context.push(BuiltinConcepts.NOP, None, who="who3", desc="sub_child1")
context.push(BuiltinConcepts.NOP, None, who="who4", desc="sub_child2")
to_string = sheerkapickle.encode(sheerka, context)
decoded = sheerkapickle.decode(sheerka, to_string)
@@ -288,7 +288,7 @@ class TestSheerkaPickleHandler(TestUsingMemoryBasedSheerka):
sheerka = self.get_sheerka(skip_builtins_in_db=False)
text = "def concept one as 1"
execution_context = ExecutionContext("s", Event(), sheerka, f"Evaluating '{text}'")
execution_context = ExecutionContext("s", Event(), sheerka, BuiltinConcepts.NOP, None, f"Evaluating '{text}'")
user_input = sheerka.ret("s", True, sheerka.new(BuiltinConcepts.USER_INPUT, body=text, user_name="n"))
reduce_requested = sheerka.ret("s", True, sheerka.new(BuiltinConcepts.REDUCE_REQUESTED))