Fixed memory() and RET usage
This commit is contained in:
@@ -90,6 +90,7 @@ class BaseTest:
|
||||
instance = sheerka.new(concept.key if isinstance(concept, Concept) else concept)
|
||||
for i, var in enumerate(instance.metadata.variables):
|
||||
if var[0] in kwargs:
|
||||
assert isinstance(kwargs[var[0]], str), "variables definitions must be string"
|
||||
instance.metadata.variables[i] = (var[0], kwargs[var[0]])
|
||||
|
||||
return instance
|
||||
|
||||
@@ -182,7 +182,7 @@ class TestSheerkaCreateNewConcept(TestUsingMemoryBasedSheerka):
|
||||
assert sheerka.cache_manager.get(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "-") == [bnf_concept.id]
|
||||
assert sheerka.cache_manager.get(sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "-") == [bnf_concept.id]
|
||||
|
||||
def test_concept_references_are_updated(self):
|
||||
def test_concept_references_are_updated_1(self):
|
||||
sheerka, context, one, two, number, twenty, twenties = self.init_concepts(
|
||||
"one",
|
||||
"two",
|
||||
@@ -198,6 +198,23 @@ class TestSheerkaCreateNewConcept(TestUsingMemoryBasedSheerka):
|
||||
assert sheerka.cache_manager.get(sheerka.CONCEPTS_REFERENCES_ENTRY, twenty.id) == {twenties.id}
|
||||
assert sheerka.cache_manager.get(sheerka.CONCEPTS_REFERENCES_ENTRY, twenties.id) is None
|
||||
|
||||
def test_concept_references_are_updated_2(self):
|
||||
sheerka, context, one, two, number, twenty, twenties = self.init_concepts(
|
||||
"one",
|
||||
"two",
|
||||
"number",
|
||||
"twenty",
|
||||
Concept("twenties", definition="twenty number"),
|
||||
create_new=True
|
||||
)
|
||||
|
||||
assert sheerka.cache_manager.get(sheerka.CONCEPTS_REFERENCES_ENTRY, one.id) is None
|
||||
assert sheerka.cache_manager.get(sheerka.CONCEPTS_REFERENCES_ENTRY, two.id) is None
|
||||
assert sheerka.cache_manager.get(sheerka.CONCEPTS_REFERENCES_ENTRY, number.id) == {twenties.id}
|
||||
assert sheerka.cache_manager.get(sheerka.CONCEPTS_REFERENCES_ENTRY, twenty.id) == {twenties.id}
|
||||
assert sheerka.cache_manager.get(sheerka.CONCEPTS_REFERENCES_ENTRY, twenties.id) is None
|
||||
|
||||
|
||||
|
||||
class TestSheerkaCreateNewConceptFileBased(TestUsingFileBasedSheerka):
|
||||
def test_i_can_add_several_concepts(self):
|
||||
|
||||
@@ -2,6 +2,7 @@ import pytest
|
||||
from core.builtin_concepts import BuiltinConcepts, ReturnValueConcept, ParserResultConcept
|
||||
from core.concept import Concept, DoNotResolve, ConceptParts, Property, InfiniteRecursionResolved, CB, NotInit
|
||||
from core.sheerka.services.SheerkaEvaluateConcept import SheerkaEvaluateConcept
|
||||
from core.sheerka.services.SheerkaMemory import SheerkaMemory
|
||||
from parsers.PythonParser import PythonNode
|
||||
|
||||
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
@@ -34,6 +35,8 @@ class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
|
||||
assert evaluated.metadata.is_evaluated
|
||||
assert len(evaluated.values) == 0 if body is None else 1
|
||||
|
||||
assert "foo" in sheerka.services[SheerkaMemory.NAME].registration
|
||||
|
||||
@pytest.mark.parametrize("expr, expected", [
|
||||
("", ""),
|
||||
("1", 1),
|
||||
@@ -191,7 +194,13 @@ class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
|
||||
assert sheerka.objvalue(evaluated) == CB("a", BuiltinConcepts.NOT_INITIALIZED)
|
||||
assert evaluated.metadata.is_evaluated
|
||||
|
||||
def test_i_can_evaluate_concept_when_variables_reference_others_concepts(self):
|
||||
def test_i_can_evaluate_concept_when_variables_reference_others_concepts_1(self):
|
||||
"""
|
||||
The body references a variable.
|
||||
The variable reference a concept
|
||||
The variable name is also the name of a concept
|
||||
:return:
|
||||
"""
|
||||
sheerka, context, concept_a, concept = self.init_concepts(
|
||||
Concept("a"),
|
||||
Concept("foo", body="a").def_var("a", "a"),
|
||||
@@ -218,6 +227,35 @@ class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
|
||||
assert evaluated.key == concept.key
|
||||
assert evaluated.body == concept_a
|
||||
|
||||
def test_i_can_evaluate_concept_when_variables_reference_others_concepts_3(self):
|
||||
"""
|
||||
The body references a variable.
|
||||
The variable reference a concept
|
||||
The name of the variable is also the name of a concept, but the variable points to something else
|
||||
:return:
|
||||
"""
|
||||
sheerka, context, concept_a, concept_b = self.init_concepts("a", "b", eval_body=True)
|
||||
|
||||
concept = Concept("foo", body="a").def_var("a", "b")
|
||||
evaluated = sheerka.evaluate_concept(context, concept)
|
||||
|
||||
assert evaluated.key == concept.key
|
||||
assert evaluated.body == concept_b
|
||||
|
||||
def test_i_can_evaluate_concept_when_variables_reference_others_concepts_4(self):
|
||||
"""
|
||||
The body references a variable.
|
||||
The variable reference a concept
|
||||
:return:
|
||||
"""
|
||||
sheerka, context, concept_b = self.init_concepts("b", eval_body=True)
|
||||
|
||||
concept = Concept("foo", body="a").def_var("a", "b")
|
||||
evaluated = sheerka.evaluate_concept(context, concept)
|
||||
|
||||
assert evaluated.key == concept.key
|
||||
assert evaluated.body == concept_b
|
||||
|
||||
def test_i_can_evaluate_concept_when_variables_reference_others_concepts_with_body(self):
|
||||
sheerka, context, *concepts = self.init_concepts(
|
||||
Concept(name="a", body="1"),
|
||||
@@ -714,7 +752,7 @@ class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
|
||||
assert evaluated.key == command.key
|
||||
assert "a" not in sheerka.locals
|
||||
|
||||
sheerka.set_isa(context, command, sheerka.new(BuiltinConcepts.COMMAND))
|
||||
sheerka.set_isa(context, command, sheerka.new(BuiltinConcepts.AUTO_EVAL))
|
||||
evaluated = sheerka.evaluate_concept(context, sheerka.new("command"))
|
||||
assert evaluated.key == command.key
|
||||
assert "a" in sheerka.locals
|
||||
@@ -730,7 +768,7 @@ class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
|
||||
setattr(foo.metadata, metadata, "a=10; print('10')")
|
||||
foo.metadata.need_validation = True
|
||||
|
||||
evaluated = sheerka.evaluate_concept(context, foo)
|
||||
evaluated = sheerka.evaluate_concept(context, foo, eval_body=True)
|
||||
captured = capsys.readouterr()
|
||||
|
||||
assert sheerka.isinstance(evaluated, BuiltinConcepts.CONCEPT_EVAL_ERROR)
|
||||
@@ -773,19 +811,24 @@ class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out == ""
|
||||
|
||||
@pytest.mark.parametrize("concept, expected", [
|
||||
(Concept("foo"), []),
|
||||
(Concept("foo", pre="pre", post="post", ret="ret", where="where"), ["pre", "ret", "post"]),
|
||||
(Concept("foo", pre="a").def_var("a"), ["variables", "pre"]),
|
||||
(Concept("foo", pre="self"), ["body", "pre"]),
|
||||
(Concept("foo", pre="self + a").def_var("a"), ["variables", "body", "pre"]),
|
||||
(Concept("foo", pre="self + a", ret="ret").def_var("a"), ["variables", "body", "pre", "ret"]),
|
||||
(Concept("foo", body="body"), []) # only if eval_body_is_set
|
||||
@pytest.mark.parametrize("concept, eval_body, expected", [
|
||||
(Concept("foo"), False, []),
|
||||
(Concept("foo", pre="pre", post="post", ret="ret", where="where"), False, ["pre", "post"]),
|
||||
(Concept("foo", pre="pr", post="p", ret="r", where="w"), True, ["pre", "ret", "post", "variables", "body"]),
|
||||
(Concept("foo", pre="a").def_var("a"), False, ["variables", "pre"]),
|
||||
(Concept("foo", pre="self"), False, ["body", "pre"]),
|
||||
(Concept("foo", pre="self + a").def_var("a"), False, ["variables", "body", "pre"]),
|
||||
(Concept("foo", pre="self + a", ret="ret").def_var("a"), False, ["variables", "body", "pre"]),
|
||||
(Concept("foo", pre="self + a", ret="ret").def_var("a"), True, ["variables", "body", "pre", "ret"]),
|
||||
(Concept("foo", body="body"), False, [])
|
||||
])
|
||||
def test_i_can_compute_metadata_to_eval(self, concept, expected):
|
||||
def test_i_can_compute_metadata_to_eval(self, concept, eval_body, expected):
|
||||
sheerka, context, concept = self.init_concepts(concept)
|
||||
service = sheerka.services[SheerkaEvaluateConcept.NAME]
|
||||
|
||||
if eval_body:
|
||||
context.add_to_protected_hints(BuiltinConcepts.EVAL_BODY_REQUESTED)
|
||||
|
||||
service.initialize_concept_asts(context, concept)
|
||||
assert service.compute_metadata_to_eval(context, concept) == expected
|
||||
|
||||
@@ -839,6 +882,21 @@ class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
|
||||
evaluated = sheerka.evaluate_concept(context, concept, metadata=['pre'])
|
||||
assert evaluated.values == {"a": Property("a", NotInit), ConceptParts.PRE: Property(ConceptParts.PRE, 'pre')}
|
||||
|
||||
def test_i_can_manage_ret(self):
|
||||
sheerka, context, foo, bar = self.init_concepts("foo", Concept("bar", ret="foo"))
|
||||
|
||||
res = sheerka.evaluate_concept(context, bar)
|
||||
assert res.id == bar.id
|
||||
|
||||
res = sheerka.evaluate_concept(context, bar, eval_body=True)
|
||||
assert res.id == foo.id
|
||||
|
||||
def test_ret_is_evaluated_only_is_body_is_requested(self):
|
||||
sheerka, context, foo, bar = self.init_concepts("foo", Concept("bar", ret="__NOT_FOUND"))
|
||||
|
||||
res = sheerka.evaluate_concept(context, bar, eval_body=False)
|
||||
assert res.id == bar.id
|
||||
|
||||
# I cannot implement value cache for now
|
||||
# def test_values_when_no_variables_are_computed_only_once(self):
|
||||
# sheerka, context, foo = self.init_concepts(Concept("foo", body="10"))
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.concept import Concept
|
||||
from core.sheerka.ExecutionContext import ExecutionContext
|
||||
from core.sheerka.services.SheerkaMemory import SheerkaMemory, MemoryObject
|
||||
|
||||
from tests.TestUsingFileBasedSheerka import TestUsingFileBasedSheerka
|
||||
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
|
||||
|
||||
class TestSheerkaMemory(TestUsingMemoryBasedSheerka):
|
||||
def test_i_can_add_to_global_short_term_memory(self):
|
||||
sheerka = self.get_sheerka()
|
||||
service = sheerka.services[SheerkaMemory.NAME]
|
||||
|
||||
foo = Concept("foo")
|
||||
sheerka.add_to_short_term_memory(None, "a", foo)
|
||||
|
||||
assert service.short_term_objects.copy() == {":a": foo}
|
||||
assert id(sheerka.get_from_short_term_memory(None, "a")) == id(foo)
|
||||
|
||||
def test_i_can_add_context_short_term_memory(self):
|
||||
sheerka, context = self.init_concepts()
|
||||
service = sheerka.services[SheerkaMemory.NAME]
|
||||
|
||||
foo = Concept("foo")
|
||||
sheerka.add_to_short_term_memory(context, "a", foo)
|
||||
|
||||
context_id = ExecutionContext.ids[context.event.get_digest()]
|
||||
assert service.short_term_objects.copy() == {f"{context_id}:a": foo}
|
||||
assert id(sheerka.get_from_short_term_memory(context, "a")) == id(foo)
|
||||
assert sheerka.get_from_short_term_memory(None, "a") is None
|
||||
|
||||
def test_i_can_get_obj_from_parents(self):
|
||||
sheerka, context = self.init_concepts()
|
||||
service = sheerka.services[SheerkaMemory.NAME]
|
||||
foo = Concept("foo")
|
||||
sheerka.add_to_short_term_memory(None, "a", foo)
|
||||
|
||||
with context.push(BuiltinConcepts.TESTING, None) as sub_context:
|
||||
assert service.short_term_objects.copy() == {":a": foo}
|
||||
assert id(sheerka.get_from_short_term_memory(sub_context, "a")) == id(foo)
|
||||
assert id(sheerka.get_from_short_term_memory(context, "a")) == id(foo)
|
||||
assert id(sheerka.get_from_short_term_memory(None, "a")) == id(foo)
|
||||
|
||||
def test_entry_are_removed_on_context_exit(self):
|
||||
sheerka, context = self.init_concepts()
|
||||
|
||||
with context.push(BuiltinConcepts.TESTING, None) as sub_context:
|
||||
foo = Concept("foo")
|
||||
sheerka.add_to_short_term_memory(sub_context, "a", foo)
|
||||
assert id(sheerka.get_from_short_term_memory(sub_context, "a")) == id(foo)
|
||||
|
||||
assert sheerka.get_from_short_term_memory(sub_context, "a") is None
|
||||
|
||||
def test_i_can_add_and_retrieve_from_memory(self):
|
||||
sheerka, context = self.init_concepts()
|
||||
service = sheerka.services[SheerkaMemory.NAME]
|
||||
|
||||
assert sheerka.get_from_memory(context, "a") is None
|
||||
|
||||
foo = Concept("foo")
|
||||
sheerka.add_to_memory(context, "a", foo)
|
||||
|
||||
assert service.objects.copy() == {"a": MemoryObject(context.event.get_digest(), foo)}
|
||||
assert id(sheerka.get_from_memory(context, "a").obj) == id(foo)
|
||||
|
||||
def test_i_can_use_memory_to_get_the_list_of_all_objects(self):
|
||||
sheerka, context = self.init_concepts()
|
||||
foo = Concept("foo")
|
||||
bar = Concept("bar")
|
||||
|
||||
sheerka.add_to_memory(context, "foo", 'value that will not appear')
|
||||
sheerka.add_to_memory(context, "foo", foo)
|
||||
sheerka.add_to_memory(context, "bar", bar)
|
||||
|
||||
assert sheerka.memory(context) == {"foo": foo, "bar": bar}
|
||||
|
||||
def test_i_can_use_memory_with_a_string(self):
|
||||
sheerka, context = self.init_concepts()
|
||||
|
||||
foo = Concept("foo")
|
||||
sheerka.add_to_memory(context, "foo", foo)
|
||||
|
||||
assert sheerka.memory(context, "foo") == foo
|
||||
|
||||
def test_i_can_use_memory_with_a_concept(self):
|
||||
sheerka, context = self.init_concepts()
|
||||
|
||||
foo = Concept("foo")
|
||||
sheerka.add_to_memory(context, "foo", foo)
|
||||
|
||||
assert sheerka.memory(context, Concept("foo")) == foo
|
||||
|
||||
def test_concept_not_found_is_return_when_not_found(self):
|
||||
sheerka, context = self.init_concepts()
|
||||
|
||||
res = sheerka.memory(context, "foo")
|
||||
|
||||
assert sheerka.isinstance(res, BuiltinConcepts.NOT_FOUND)
|
||||
assert res.body == {"#name": "foo"}
|
||||
|
||||
def test_memory_only_returns_the_last_object(self):
|
||||
sheerka, context = self.init_concepts()
|
||||
|
||||
foo = Concept("foo")
|
||||
bar = Concept("bar")
|
||||
|
||||
sheerka.add_to_memory(context, "item", foo)
|
||||
sheerka.add_to_memory(context, "item", bar)
|
||||
|
||||
assert sheerka.memory(context, "item") == bar
|
||||
|
||||
|
||||
class TestSheerkaMemoryUsingFileBase(TestUsingFileBasedSheerka):
|
||||
def test_i_can_record_memory_objects(self):
|
||||
sheerka, context = self.init_concepts()
|
||||
|
||||
sheerka.add_to_memory(context, "item", Concept("foo"))
|
||||
sheerka.cache_manager.commit(context)
|
||||
|
||||
sheerka = self.get_sheerka()
|
||||
context = self.get_context(sheerka)
|
||||
assert sheerka.get_from_memory(context, "item").obj == Concept("foo")
|
||||
@@ -92,6 +92,18 @@ class TestSheerkaModifyConcept(TestUsingMemoryBasedSheerka):
|
||||
assert foo_from_sheerka[0].metadata.body == "1"
|
||||
assert foo_from_sheerka[1].metadata.body == "value"
|
||||
|
||||
def test_i_can_get_and_set_attribute(self):
|
||||
sheerka, context = self.init_concepts()
|
||||
foo = Concept("foo")
|
||||
prop = Concept("property")
|
||||
bar = Concept("bar")
|
||||
|
||||
res = sheerka.set_attr(foo, prop, bar)
|
||||
assert res.status
|
||||
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
|
||||
|
||||
assert sheerka.get_attr(foo, prop) == bar
|
||||
|
||||
|
||||
class TestSheerkaModifyConceptUsingFile(TestUsingFileBasedSheerka):
|
||||
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.concept import Concept
|
||||
from core.sheerka.ExecutionContext import ExecutionContext
|
||||
from core.sheerka.services.SheerkaShortTermMemory import SheerkaShortTermMemory
|
||||
|
||||
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
|
||||
|
||||
class TestSheerkaShortTermMemory(TestUsingMemoryBasedSheerka):
|
||||
def test_i_can_add_to_global_short_term_memory(self):
|
||||
sheerka = self.get_sheerka()
|
||||
service = sheerka.services[SheerkaShortTermMemory.NAME]
|
||||
|
||||
foo = Concept("foo")
|
||||
sheerka.add_to_short_term_memory(None, "a", foo)
|
||||
|
||||
assert service.objects.copy() == {":a": foo}
|
||||
assert id(sheerka.get_from_short_term_memory(None, "a")) == id(foo)
|
||||
|
||||
def test_i_can_add_context_short_term_memory(self):
|
||||
sheerka, context = self.init_concepts()
|
||||
service = sheerka.services[SheerkaShortTermMemory.NAME]
|
||||
|
||||
foo = Concept("foo")
|
||||
sheerka.add_to_short_term_memory(context, "a", foo)
|
||||
|
||||
context_id = ExecutionContext.ids[context.event.get_digest()]
|
||||
assert service.objects.copy() == {f"{context_id}:a": foo}
|
||||
assert id(sheerka.get_from_short_term_memory(context, "a")) == id(foo)
|
||||
assert sheerka.get_from_short_term_memory(None, "a") is None
|
||||
|
||||
def test_i_can_get_obj_from_parents(self):
|
||||
sheerka, context = self.init_concepts()
|
||||
service = sheerka.services[SheerkaShortTermMemory.NAME]
|
||||
foo = Concept("foo")
|
||||
sheerka.add_to_short_term_memory(None, "a", foo)
|
||||
|
||||
with context.push(BuiltinConcepts.TESTING, None) as sub_context:
|
||||
assert service.objects.copy() == {":a": foo}
|
||||
assert id(sheerka.get_from_short_term_memory(sub_context, "a")) == id(foo)
|
||||
assert id(sheerka.get_from_short_term_memory(context, "a")) == id(foo)
|
||||
assert id(sheerka.get_from_short_term_memory(None, "a")) == id(foo)
|
||||
|
||||
def test_entry_are_removed_on_context_exit(self):
|
||||
sheerka, context = self.init_concepts()
|
||||
|
||||
with context.push(BuiltinConcepts.TESTING, None) as sub_context:
|
||||
foo = Concept("foo")
|
||||
sheerka.add_to_short_term_memory(sub_context, "a", foo)
|
||||
assert id(sheerka.get_from_short_term_memory(sub_context, "a")) == id(foo)
|
||||
|
||||
assert sheerka.get_from_short_term_memory(sub_context, "a") is None
|
||||
@@ -24,7 +24,6 @@ class TestAddConceptEvaluator(TestUsingMemoryBasedSheerka):
|
||||
concept = Concept(name="foo",
|
||||
where="True",
|
||||
pre="2 > 1",
|
||||
ret="3",
|
||||
post="4").def_var("a", "5").def_var("b", "6")
|
||||
|
||||
evaluator = ConceptEvaluator()
|
||||
@@ -36,7 +35,6 @@ class TestAddConceptEvaluator(TestUsingMemoryBasedSheerka):
|
||||
assert result.value.name == "foo"
|
||||
assert result.value.get_value(ConceptParts.WHERE) == True
|
||||
assert result.value.get_value(ConceptParts.PRE) == True
|
||||
assert result.value.get_value(ConceptParts.RET) == 3
|
||||
assert result.value.get_value(ConceptParts.POST) == 4
|
||||
assert result.value.get_value("a") == 5
|
||||
assert result.value.get_value("b") == 6
|
||||
|
||||
@@ -8,6 +8,7 @@ from core.tokenizer import Tokenizer
|
||||
from evaluators.PythonEvaluator import PythonEvaluator, PythonEvalError
|
||||
from parsers.BaseNodeParser import SourceCodeNode, SourceCodeWithConceptNode
|
||||
from parsers.PythonParser import PythonNode, PythonParser
|
||||
from parsers.PythonWithConceptsParser import PythonWithConceptsParser
|
||||
|
||||
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
|
||||
@@ -17,6 +18,15 @@ def get_concept_name(concept):
|
||||
|
||||
|
||||
def get_source_code_node(source_code, concepts=None):
|
||||
if concepts:
|
||||
for concept_name, concept in sorted(concepts.items(), key=lambda kv: len(kv[0]), reverse=True):
|
||||
identifier = "__C__" + PythonWithConceptsParser.sanitize(concept.name)
|
||||
if concept.id:
|
||||
identifier += "__" + concept.id
|
||||
identifier += "__C__"
|
||||
source_code = source_code.replace(concept_name, identifier)
|
||||
concepts[identifier] = concept
|
||||
|
||||
if source_code:
|
||||
python_node = PythonNode(source_code, ast.parse(source_code, f"<source>", 'eval'))
|
||||
else:
|
||||
@@ -32,6 +42,11 @@ def get_source_code_node(source_code, concepts=None):
|
||||
return scwcn
|
||||
|
||||
|
||||
def get_ret_val_from_source_code(context, source_code, concepts):
|
||||
parsed = get_source_code_node(source_code, concepts)
|
||||
return context.sheerka.ret("parsers.??", True, ParserResultConcept(value=parsed))
|
||||
|
||||
|
||||
class TestPythonEvaluator(TestUsingMemoryBasedSheerka):
|
||||
|
||||
@pytest.mark.parametrize("ret_val, expected", [
|
||||
@@ -289,3 +304,14 @@ class TestPythonEvaluator(TestUsingMemoryBasedSheerka):
|
||||
|
||||
assert evaluated.status
|
||||
assert evaluated.value == 11
|
||||
|
||||
def test_i_can_eval_concept_with_ret(self):
|
||||
sheerka, context, one, the = self.init_concepts("one", Concept("the a", ret="a").def_var("a"))
|
||||
ret_val = get_ret_val_from_source_code(context, "test_using_context(one, the one)", {
|
||||
"the one": self.get_concept_instance(sheerka, the, a="one"),
|
||||
"one": self.get_concept_instance(sheerka, "one")
|
||||
})
|
||||
|
||||
evaluated = PythonEvaluator().eval(context, ret_val)
|
||||
assert evaluated.status
|
||||
assert evaluated.value.startswith("I have access to Sheerka ! param1=(1001)one, param2=(1001)one, event=")
|
||||
|
||||
@@ -1,47 +1,47 @@
|
||||
import pytest
|
||||
from core.builtin_concepts import ReturnValueConcept
|
||||
from core.concept import Concept
|
||||
from evaluators.RetEvaluator import RetEvaluator
|
||||
|
||||
from tests.BaseTest import BaseTest
|
||||
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
|
||||
|
||||
class TestRetEvaluator(TestUsingMemoryBasedSheerka):
|
||||
|
||||
@pytest.mark.parametrize("ret_val, expected", [
|
||||
(ReturnValueConcept("who", True, Concept("foo", ret="bar")), True),
|
||||
(ReturnValueConcept("who", False, Concept("foo", ret="bar")), False),
|
||||
(ReturnValueConcept("who", True, Concept("foo")), False),
|
||||
(BaseTest.pretval(Concept("foo", ret="bar")), False),
|
||||
(ReturnValueConcept("who", True, "not even a concept"), False),
|
||||
])
|
||||
def test_i_can_match(self, ret_val, expected):
|
||||
context = self.get_context()
|
||||
assert RetEvaluator().matches(context, ret_val) == expected
|
||||
|
||||
def test_i_can_evaluate_fully_initialized(self):
|
||||
sheerka, context, foo, the_concept = self.init_concepts("foo", Concept("the concept", ret="a").def_var("a"))
|
||||
|
||||
instance = sheerka.new("the concept")
|
||||
instance.set_value("a", sheerka.new("foo"))
|
||||
ret_value = self.tretval(sheerka, instance)
|
||||
|
||||
res = RetEvaluator().eval(context, ret_value)
|
||||
assert res.status
|
||||
assert sheerka.isinstance(res.body, "foo")
|
||||
|
||||
@pytest.mark.parametrize("instance, expected", [
|
||||
(Concept("with ret", ret="a").def_var("a", "foo"), "foo"),
|
||||
(Concept("with ret", ret="a", body="10").def_var("a", "foo"), "foo"),
|
||||
(Concept("with ret", ret="a").def_var("a", "bar"), "bar"),
|
||||
])
|
||||
def test_i_can_evaluate(self, instance, expected):
|
||||
sheerka, context, foo, bar = self.init_concepts("foo", Concept("bar", body="10"))
|
||||
|
||||
ret_value = self.tretval(sheerka, instance)
|
||||
|
||||
res = RetEvaluator().eval(context, ret_value)
|
||||
assert res.status
|
||||
assert sheerka.isinstance(res.body, expected)
|
||||
|
||||
# import pytest
|
||||
# from core.builtin_concepts import ReturnValueConcept
|
||||
# from core.concept import Concept
|
||||
# from evaluators.RetEvaluator import RetEvaluator
|
||||
#
|
||||
# from tests.BaseTest import BaseTest
|
||||
# from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
#
|
||||
#
|
||||
# class TestRetEvaluator(TestUsingMemoryBasedSheerka):
|
||||
#
|
||||
# @pytest.mark.parametrize("ret_val, expected", [
|
||||
# (ReturnValueConcept("who", True, Concept("foo", ret="bar")), True),
|
||||
# (ReturnValueConcept("who", False, Concept("foo", ret="bar")), False),
|
||||
# (ReturnValueConcept("who", True, Concept("foo")), False),
|
||||
# (BaseTest.pretval(Concept("foo", ret="bar")), False),
|
||||
# (ReturnValueConcept("who", True, "not even a concept"), False),
|
||||
# ])
|
||||
# def test_i_can_match(self, ret_val, expected):
|
||||
# context = self.get_context()
|
||||
# assert RetEvaluator().matches(context, ret_val) == expected
|
||||
#
|
||||
# def test_i_can_evaluate_fully_initialized(self):
|
||||
# sheerka, context, foo, the_concept = self.init_concepts("foo", Concept("the concept", ret="a").def_var("a"))
|
||||
#
|
||||
# instance = sheerka.new("the concept")
|
||||
# instance.set_value("a", sheerka.new("foo"))
|
||||
# ret_value = self.tretval(sheerka, instance)
|
||||
#
|
||||
# res = RetEvaluator().eval(context, ret_value)
|
||||
# assert res.status
|
||||
# assert sheerka.isinstance(res.body, "foo")
|
||||
#
|
||||
# @pytest.mark.parametrize("instance, expected", [
|
||||
# (Concept("with ret", ret="a").def_var("a", "foo"), "foo"),
|
||||
# (Concept("with ret", ret="a", body="10").def_var("a", "foo"), "foo"),
|
||||
# (Concept("with ret", ret="a").def_var("a", "bar"), "bar"),
|
||||
# ])
|
||||
# def test_i_can_evaluate(self, instance, expected):
|
||||
# sheerka, context, foo, bar = self.init_concepts("foo", Concept("bar", body="10"))
|
||||
#
|
||||
# ret_value = self.tretval(sheerka, instance)
|
||||
#
|
||||
# res = RetEvaluator().eval(context, ret_value)
|
||||
# assert res.status
|
||||
# assert sheerka.isinstance(res.body, expected)
|
||||
#
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import pytest
|
||||
|
||||
from core.builtin_concepts import ReturnValueConcept, BuiltinConcepts
|
||||
from core.concept import Concept
|
||||
from evaluators.TooManySuccessEvaluator import TooManySuccessEvaluator
|
||||
@@ -44,8 +43,8 @@ class TestTooManySuccessEvaluator(TestUsingMemoryBasedSheerka):
|
||||
def test_i_can_eval(self):
|
||||
context = self.get_context()
|
||||
|
||||
value1 = r("evaluators.a", value=Concept("c1", body="1"))
|
||||
value2 = r("evaluators.a", value=Concept("c2", body="2"))
|
||||
value1 = r("evaluators.a", value=Concept("c1", body="1").auto_init())
|
||||
value2 = r("evaluators.a", value=Concept("c2", body="2").auto_init())
|
||||
return_values = [
|
||||
value1,
|
||||
value2,
|
||||
@@ -80,6 +79,23 @@ class TestTooManySuccessEvaluator(TestUsingMemoryBasedSheerka):
|
||||
assert matches
|
||||
assert res is None
|
||||
|
||||
def test_i_do_not_eval_when_the_concepts_are_not_evaluated(self):
|
||||
context = self.get_context()
|
||||
|
||||
return_values = [
|
||||
r("evaluators.a", value=Concept("c1", body="1")),
|
||||
r("evaluators.a", value=Concept("c2", body="2")),
|
||||
r("other", False),
|
||||
reduce_requested
|
||||
]
|
||||
|
||||
evaluator = TooManySuccessEvaluator()
|
||||
matches = evaluator.matches(context, return_values)
|
||||
res = evaluator.eval(context, return_values)
|
||||
|
||||
assert matches
|
||||
assert res is None
|
||||
|
||||
def test_other_success_are_not_reduced(self):
|
||||
context = self.get_context()
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import pytest
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.concept import Concept, PROPERTIES_TO_SERIALIZE, simplec, CMV, NotInit
|
||||
from core.concept import Concept, PROPERTIES_TO_SERIALIZE, simplec, CMV, NotInit, CC
|
||||
from evaluators.MutipleSameSuccessEvaluator import MultipleSameSuccessEvaluator
|
||||
from evaluators.OneSuccessEvaluator import OneSuccessEvaluator
|
||||
from evaluators.PythonEvaluator import PythonEvalError
|
||||
@@ -236,16 +236,16 @@ as:
|
||||
self.create_and_add_in_cache_concept(sheerka, Concept(name="hello b", body="'hello you ' + b").def_var("b"))
|
||||
self.create_and_add_in_cache_concept(sheerka, Concept(name="foo", body="'another value'"))
|
||||
|
||||
res = sheerka.evaluate_user_input("hello foo")
|
||||
res = sheerka.evaluate_user_input("eval hello foo")
|
||||
assert len(res) == 1
|
||||
assert not res[0].status
|
||||
assert sheerka.isinstance(res[0].value, BuiltinConcepts.TOO_MANY_SUCCESS)
|
||||
|
||||
concepts = res[0].value.body
|
||||
assert len(concepts) == 2
|
||||
sorted_values = sorted(concepts, key=lambda x: x.value.body)
|
||||
assert sorted_values[0].value.body == "hello another value"
|
||||
assert sorted_values[1].value.body == "hello you another value"
|
||||
sorted_values = sorted(concepts, key=lambda x: x.value)
|
||||
assert sorted_values[0].value == "hello another value"
|
||||
assert sorted_values[1].value == "hello you another value"
|
||||
|
||||
def test_i_can_manage_concepts_with_the_same_key_when_values_are_the_same(self):
|
||||
sheerka = self.get_sheerka()
|
||||
@@ -254,10 +254,10 @@ as:
|
||||
sheerka.create_new_concept(context, Concept(name="hello a", body="'hello ' + a").def_var("a"))
|
||||
sheerka.create_new_concept(context, Concept(name="hello b", body="'hello ' + b").def_var("b"))
|
||||
|
||||
res = sheerka.evaluate_user_input("hello 'foo'")
|
||||
res = sheerka.evaluate_user_input("eval hello 'foo'")
|
||||
assert len(res) == 1
|
||||
assert res[0].status
|
||||
assert res[0].value.body == "hello foo" # I don't know yet the one to choose
|
||||
assert res[0].value == "hello foo" # I don't know yet the one to choose
|
||||
assert res[0].who == sheerka.get_evaluator_name(MultipleSameSuccessEvaluator.NAME)
|
||||
|
||||
def test_i_can_create_concepts_with_python_code_as_body(self):
|
||||
@@ -574,7 +574,6 @@ as:
|
||||
assert res[0].status
|
||||
assert res[0].body == 3
|
||||
|
||||
|
||||
def test_eval_does_not_break_valid_result(self):
|
||||
sheerka = self.get_sheerka()
|
||||
sheerka.evaluate_user_input("def concept one as 1")
|
||||
@@ -981,13 +980,13 @@ as:
|
||||
sheerka = self.init_scenario(init)
|
||||
the = sheerka.get_by_name("the a")
|
||||
|
||||
# res = sheerka.evaluate_user_input("one plus the one")
|
||||
# assert res[0].status
|
||||
# plus = res[0].body
|
||||
# assert isinstance(plus, Concept)
|
||||
# assert plus.name == "plus"
|
||||
# assert plus.compiled["a"] == sheerka.new("one")
|
||||
# assert plus.compiled["b"] == CC(the, a=sheerka.new("one"))
|
||||
res = sheerka.evaluate_user_input("one plus the one")
|
||||
assert res[0].status
|
||||
plus = res[0].body
|
||||
assert isinstance(plus, Concept)
|
||||
assert plus.name == "plus"
|
||||
assert plus.compiled["a"] == sheerka.new("one")
|
||||
assert plus.compiled["b"] == CC(the, a=sheerka.new("one"))
|
||||
|
||||
res = sheerka.evaluate_user_input("eval one plus the one")
|
||||
assert res[0].status
|
||||
@@ -996,7 +995,7 @@ as:
|
||||
def test_i_can_evaluate_command(self):
|
||||
init = [
|
||||
"def concept command as 'Executed !'",
|
||||
"set_isa(c:command:, __COMMAND)",
|
||||
"set_isa(c:command:, __AUTO_EVAL)",
|
||||
]
|
||||
|
||||
# Since command is a __COMMAND, the body is auto evaluated
|
||||
@@ -1144,6 +1143,22 @@ as:
|
||||
assert res[0].status
|
||||
assert sheerka.isinstance(res[0].body, BuiltinConcepts.NEW_CONCEPT)
|
||||
|
||||
def test_bnf_node_parsers_are_updated_when_concepts_are_modified(self):
|
||||
init = [
|
||||
"def concept number",
|
||||
"def concept one",
|
||||
"def concept twenties from bnf 'twenty' number as 20 + number",
|
||||
]
|
||||
sheerka = self.init_scenario(init)
|
||||
|
||||
res = sheerka.evaluate_user_input("twenty one")
|
||||
assert len(res) > 1
|
||||
|
||||
sheerka.evaluate_user_input("set_isa(one, number)")
|
||||
res = sheerka.evaluate_user_input("twenty one")
|
||||
assert len(res) == 1
|
||||
assert res[0].status
|
||||
|
||||
|
||||
class TestSheerkaNonRegFile(TestUsingFileBasedSheerka):
|
||||
def test_i_can_def_several_concepts(self):
|
||||
|
||||
@@ -174,3 +174,4 @@ class TestFunctionParser(TestUsingMemoryBasedSheerka):
|
||||
assert isinstance(concept.compiled["b"], list)
|
||||
for item in concept.compiled["b"]:
|
||||
assert sheerka.isinstance(item, BuiltinConcepts.RETURN_VALUE)
|
||||
|
||||
|
||||
@@ -1154,6 +1154,30 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
|
||||
assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
|
||||
assert lexer_nodes == expected_array
|
||||
|
||||
@pytest.mark.parametrize("text, expected_result", [
|
||||
("a plus b", [CN("plus", source="a plus b")]),
|
||||
("suffixed a plus b", [CN("suffixed", source="suffixed a plus b")]),
|
||||
])
|
||||
def test_i_can_almost_parse_concept_definition(self, text, expected_result):
|
||||
"""
|
||||
In these examples, 'a' and 'b' are not defined.
|
||||
So the status of the return value cannot be True
|
||||
:param text:
|
||||
:param expected_result:
|
||||
:return:
|
||||
"""
|
||||
sheerka, context, parser = self.init_parser()
|
||||
|
||||
res = parser.parse(context, ParserInput(text))
|
||||
|
||||
wrapper = res.body
|
||||
lexer_nodes = res.body.body
|
||||
|
||||
expected_array = compute_expected_array(cmap, text, expected_result)
|
||||
assert not res.status
|
||||
assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
|
||||
assert lexer_nodes == expected_array
|
||||
|
||||
@pytest.mark.parametrize("text, expected_concept, expected_unrecognized", [
|
||||
("x$!# prefixed", "prefixed", ["a"]),
|
||||
("suffixed x$!#", "suffixed", ["a"]),
|
||||
|
||||
Reference in New Issue
Block a user