Fixed memory() and RET usage
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user