Implemented SheerkaOntology

This commit is contained in:
2021-01-11 15:36:03 +01:00
parent e3c2adb533
commit e26c83a825
119 changed files with 6876 additions and 2002 deletions
+10 -2
View File
@@ -1,9 +1,17 @@
from core.builtin_concepts_ids import BuiltinConcepts
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
class TestSheerkaAdmin(TestUsingMemoryBasedSheerka):
def test_i_can_get_last_ret(self):
pass
def test_i_can_get_concepts(self):
sheerka = self.get_sheerka(cache_only=False, singleton=False)
res = sheerka.concepts()
concepts = list(res.body)
assert sheerka.isinstance(res, BuiltinConcepts.TO_LIST)
assert concepts[0] == sheerka
assert concepts[1].id == "2"
# def test_i_can_get_last_error_ret(self):
# sheerka, context = self.init_concepts()
+78 -45
View File
@@ -1,7 +1,8 @@
import pytest
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept
from core.global_symbols import EVENT_CONCEPT_PRECEDENCE_MODIFIED, CONCEPT_COMPARISON_CONTEXT, EVENT_RULE_PRECEDENCE_MODIFIED, \
from core.global_symbols import EVENT_CONCEPT_PRECEDENCE_MODIFIED, CONCEPT_COMPARISON_CONTEXT, \
EVENT_RULE_PRECEDENCE_MODIFIED, \
RULE_COMPARISON_CONTEXT
from core.sheerka.services.SheerkaComparisonManager import SheerkaComparisonManager, ComparisonObj
@@ -33,36 +34,37 @@ class TestSheerkaGreaterThanManager(TestUsingMemoryBasedSheerka):
assert res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
in_cache = sheerka.cache_manager.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
in_cache = sheerka.om.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
assert in_cache == [ComparisonObj(context.event.get_digest(), "prop_name", two.str_id, one.str_id, ">", "#")]
weighted = sheerka.cache_manager.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
weighted = sheerka.om.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
assert weighted == {"c:one|1001:": 1, "c:two|1002:": 2}
# I can commit
sheerka.cache_manager.commit(context)
in_db = sheerka.sdp.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
sheerka.om.commit(context)
in_db = sheerka.om.current_sdp().get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
assert in_db == [ComparisonObj(context.event.get_digest(), "prop_name", two.str_id, one.str_id, ">", "#")]
def test_i_can_add_is_greater_than_for_rules(self):
sheerka, context, r1, r2 = self.init_format_rules(("True", "true"), ("False", "false"),
cache_only=False,
compile_rule=False)
sheerka, context, r1, r2 = self.init_test(cache_only=False).with_rules(("True", "true"),
("False", "false"),
compile_rule=False).unpack()
service = sheerka.services[SheerkaComparisonManager.NAME]
res = service.set_is_greater_than(context, "prop_name", r2, r1)
assert res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
in_cache = sheerka.cache_manager.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
in_cache = sheerka.om.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
assert in_cache == [ComparisonObj(context.event.get_digest(), "prop_name", r2.str_id, r1.str_id, ">", "#")]
weighted = sheerka.cache_manager.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
assert weighted == {"r:|1:": 1, "r:|2:": 2}
weighted = sheerka.om.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
assert weighted == {r1.str_id: 1, r2.str_id: 2}
# I can commit
sheerka.cache_manager.commit(context)
in_db = sheerka.sdp.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
sheerka.om.commit(context)
in_db = sheerka.om.current_sdp().get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
assert in_db == [ComparisonObj(context.event.get_digest(), "prop_name", r2.str_id, r1.str_id, ">", "#")]
def test_i_can_add_a_is_less_than(self):
@@ -73,36 +75,38 @@ class TestSheerkaGreaterThanManager(TestUsingMemoryBasedSheerka):
assert res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
in_cache = sheerka.cache_manager.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
in_cache = sheerka.om.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
assert in_cache == [ComparisonObj(context.event.get_digest(), "prop_name", one.str_id, two.str_id, "<", "#")]
weighted = sheerka.cache_manager.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
weighted = sheerka.om.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
assert weighted == {"c:one|1001:": 1, "c:two|1002:": 2}
# I can commit
sheerka.cache_manager.commit(context)
in_db = sheerka.sdp.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
sheerka.om.commit(context)
in_db = sheerka.om.current_sdp().get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
assert in_db == [ComparisonObj(context.event.get_digest(), "prop_name", one.str_id, two.str_id, "<", "#")]
def test_i_can_add_is_less_than_for_rules(self):
sheerka, context, r1, r2 = self.init_format_rules(("True", "true"), ("False", "false"),
cache_only=False,
compile_rule=False)
sheerka, context, r1, r2 = self.init_test(cache_only=False).with_rules(("True", "true"),
("False", "false"),
compile_rule=False,
create_new=True).unpack()
service = sheerka.services[SheerkaComparisonManager.NAME]
res = service.set_is_less_than(context, "prop_name", r1, r2)
assert res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
in_cache = sheerka.cache_manager.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
in_cache = sheerka.om.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
assert in_cache == [ComparisonObj(context.event.get_digest(), "prop_name", r1.str_id, r2.str_id, "<", "#")]
weighted = sheerka.cache_manager.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
assert weighted == {"r:|1:": 1, "r:|2:": 2}
weighted = sheerka.om.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
assert weighted == {r1.str_id: 1, r2.str_id: 2}
# I can commit
sheerka.cache_manager.commit(context)
in_db = sheerka.sdp.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
sheerka.om.commit(context)
in_db = sheerka.om.current_sdp().get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
assert in_db == [ComparisonObj(context.event.get_digest(), "prop_name", r1.str_id, r2.str_id, "<", "#")]
def test_i_can_add_multiples_constraints(self):
@@ -112,31 +116,31 @@ class TestSheerkaGreaterThanManager(TestUsingMemoryBasedSheerka):
service.set_is_greater_than(context, "prop_name", two, one)
service.set_is_greater_than(context, "prop_name", three, two)
in_cache = sheerka.cache_manager.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
in_cache = sheerka.om.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
assert in_cache == [
ComparisonObj(context.event.get_digest(), "prop_name", two.str_id, one.str_id, ">", "#"),
ComparisonObj(context.event.get_digest(), "prop_name", three.str_id, two.str_id, ">", "#")
]
# I can commit
sheerka.cache_manager.commit(context)
in_db = sheerka.sdp.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
sheerka.om.commit(context)
in_db = sheerka.om.current_sdp().get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
assert in_db == [
ComparisonObj(context.event.get_digest(), "prop_name", two.str_id, one.str_id, ">", "#"),
ComparisonObj(context.event.get_digest(), "prop_name", three.str_id, two.str_id, ">", "#")
]
sheerka.cache_manager.clear(SheerkaComparisonManager.COMPARISON_ENTRY) # reset the cache
sheerka.om.clear(SheerkaComparisonManager.COMPARISON_ENTRY) # reset the cache
service.set_is_greater_than(context, "prop_name", four, three)
in_cache = sheerka.cache_manager.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
in_cache = sheerka.om.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
assert in_cache == [
ComparisonObj(context.event.get_digest(), "prop_name", two.str_id, one.str_id, ">", "#"),
ComparisonObj(context.event.get_digest(), "prop_name", three.str_id, two.str_id, ">", "#"),
ComparisonObj(context.event.get_digest(), "prop_name", four.str_id, three.str_id, ">", "#"),
]
weighted = sheerka.cache_manager.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
weighted = sheerka.om.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
assert weighted == {"c:one|1001:": 1, "c:two|1002:": 2, "c:three|1003:": 3, "c:four|1004:": 4}
def test_i_can_add_multiple_constraints_2(self):
@@ -146,23 +150,54 @@ class TestSheerkaGreaterThanManager(TestUsingMemoryBasedSheerka):
service.set_is_greater_than(context, "prop_name", two, one)
service.set_is_greater_than(context, "prop_name", three, two)
weighted = sheerka.cache_manager.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
weighted = sheerka.om.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
assert weighted == {"c:one|1001:": 1, "c:two|1002:": 2, "c:three|1003:": 3}
service.set_is_greater_than(context, "prop_name", three, one) # should not change order
weighted = sheerka.cache_manager.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
weighted = sheerka.om.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
assert weighted == {"c:one|1001:": 1, "c:two|1002:": 2, "c:three|1003:": 3}
def test_i_lesser_than_and_opposite_greater_than(self):
def test_i_can_add_lesser_than_and_opposite_greater_than(self):
sheerka, context, one, two = self.init_concepts("one", "two")
service = sheerka.services[SheerkaComparisonManager.NAME]
service.set_is_greater_than(context, "prop_name", two, one)
service.set_is_less_than(context, "prop_name", one, two)
weighted = sheerka.cache_manager.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
weighted = sheerka.om.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
assert weighted == {"c:one|1001:": 1, "c:two|1002:": 2}
def test_i_can_support_multiple_ontology_layers(self):
sheerka, context, one, two, three = self.init_concepts("one", "two", "three", cache_only=False)
service = sheerka.services[SheerkaComparisonManager.NAME]
service.set_is_greater_than(context, "prop_name", two, one)
# sanity check
expected_in_cache = [ComparisonObj(context.event.get_digest(), "prop_name", two.str_id, one.str_id, ">", "#")]
assert sheerka.om.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#") == expected_in_cache
expected_weights = {"c:one|1001:": 1, "c:two|1002:": 2}
assert sheerka.om.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#") == expected_weights
# I still can access to the previous values
sheerka.push_ontology(context, "new ontology")
assert sheerka.om.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#") == expected_in_cache
assert sheerka.om.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#") == expected_weights
# I can modify
service.set_is_greater_than(context, "prop_name", three, two)
expected_in_cache2 = [
ComparisonObj(context.event.get_digest(), "prop_name", two.str_id, one.str_id, ">", "#"),
ComparisonObj(context.event.get_digest(), "prop_name", three.str_id, two.str_id, ">", "#")]
assert sheerka.om.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#") == expected_in_cache2
expected_weights2 = {"c:one|1001:": 1, "c:two|1002:": 2, "c:three|1003:": 3}
assert sheerka.om.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#") == expected_weights2
# I can retrieve the previous values
sheerka.pop_ontology()
assert sheerka.om.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#") == expected_in_cache
assert sheerka.om.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#") == expected_weights
@pytest.mark.parametrize("entries, expected", [
(["two > one"], {'c:one|1001:': 1, 'c:two|1002:': 2}),
(["one < two"], {'c:one|1001:': 1, 'c:two|1002:': 2}),
@@ -192,7 +227,7 @@ class TestSheerkaGreaterThanManager(TestUsingMemoryBasedSheerka):
service = sheerka.services[SheerkaComparisonManager.NAME]
service.set_is_lesser(context, "prop_name", one)
sheerka.cache_manager.clear(service.RESOLVED_COMPARISON_ENTRY)
sheerka.om.clear(service.RESOLVED_COMPARISON_ENTRY)
assert service.get_concepts_weights("prop_name") == {"c:one|1001:": 0}
@@ -244,7 +279,7 @@ class TestSheerkaGreaterThanManager(TestUsingMemoryBasedSheerka):
service.set_is_greater_than(context, "prop_name", two, one)
service.set_is_less_than(context, "prop_name", one, two)
weighted = sheerka.cache_manager.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
weighted = sheerka.om.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
assert weighted == {"c:one|1001:": 1, "c:two|1002:": 2}
def test_methods_are_correctly_bound(self):
@@ -263,8 +298,8 @@ class TestSheerkaGreaterThanManager(TestUsingMemoryBasedSheerka):
assert service.get_concepts_weights("prop_name") == {"c:one|1001:": 0, "c:two|1002:": 1, "c:three|1003:": 2}
# I can commit
sheerka.cache_manager.commit(context)
in_db = sheerka.sdp.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
sheerka.om.commit(context)
in_db = sheerka.om.current_sdp().get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
assert in_db == [
ComparisonObj(context.event.get_digest(), "prop_name", one.str_id, None, "<<", "#"),
ComparisonObj(context.event.get_digest(), "prop_name", three.str_id, two.str_id, ">", "#")
@@ -458,14 +493,12 @@ class TestSheerkaGreaterThanManager(TestUsingMemoryBasedSheerka):
assert event_received
def test_an_event_is_fired_when_modifying_rule_precedence(self):
sheerka, context, r1, r2 = self.init_format_rules(
("True", "True"),
("False", "False"),
compile_rule=False,
)
sheerka, context, r1, r2 = self.init_test(cache_only=False).with_rules(("True", "true"),
("False", "false"),
compile_rule=False).unpack()
foo = Concept("foo")
event_received = False
sheerka.cache_manager.clear(SheerkaComparisonManager.COMPARISON_ENTRY)
sheerka.om.clear(SheerkaComparisonManager.COMPARISON_ENTRY)
def receive_event(c):
nonlocal event_received
+5 -5
View File
@@ -61,15 +61,15 @@ class TestSheerkaConceptsAlgebra(TestUsingMemoryBasedSheerka):
BuiltinConcepts.HASA: {hasa2}, }
def test_i_can_recognize_myself_when_using_sdp_repository(self):
sheerka, context, foo, isa1, hasa1, = self.init_concepts("foo", "isa1", "has1",
cache_only=False,
create_new=True)
sheerka.cache_manager.commit(context)
sheerka, context, foo, isa1, hasa1, = self.init_test(cache_only=False). \
with_concepts("foo", "isa1", "has1", create_new=True). \
unpack()
sheerka.om.commit(context)
new_foo = sheerka.new("foo")
sheerka.set_isa(context, new_foo, isa1)
sheerka.set_hasa(context, new_foo, hasa1)
sheerka.cache_manager.commit(context)
sheerka.om.commit(context)
assert sheerka.recognize(new_foo, all_scores=True) == [ConceptScore(1, new_foo, new_foo)]
+143 -84
View File
@@ -1,8 +1,9 @@
import pytest
from cache.CacheManager import ConceptNotFound
from core.builtin_concepts import BuiltinConcepts
from core.concept import PROPERTIES_TO_SERIALIZE, Concept, DEFINITION_TYPE_DEF, get_concept_attrs, NotInit, \
from core.concept import PROPERTIES_TO_SERIALIZE, Concept, DEFINITION_TYPE_DEF, get_concept_attrs, \
DEFINITION_TYPE_BNF
from core.global_symbols import NotInit, NotFound
from core.sheerka.Sheerka import Sheerka
from core.sheerka.services.SheerkaConceptManager import SheerkaConceptManager, NoModificationFound, ForbiddenAttribute, \
UnknownAttribute, CannotRemoveMeta, ValueNotFound, ConceptIsReferenced
@@ -21,7 +22,7 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
service = sheerka.services[SheerkaConceptManager.NAME]
res = sheerka.create_new_concept(context, concept)
sheerka.cache_manager.commit(context)
sheerka.om.commit(context)
assert res.status
assert sheerka.isinstance(res.value, BuiltinConcepts.NEW_CONCEPT)
@@ -47,15 +48,15 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
assert sheerka.get_by_hash(concept.get_definition_hash()) == concept
# I can get by the first entry
assert sheerka.cache_manager.get(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "+") == [concept.id]
assert sheerka.cache_manager.get(sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "+") == [concept.id]
assert sheerka.om.get(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "+") == [concept.id]
assert sheerka.om.get(sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "+") == [concept.id]
# saved in sdp
assert sheerka.sdp.exists(service.CONCEPTS_BY_ID_ENTRY, concept.id)
assert sheerka.sdp.exists(service.CONCEPTS_BY_KEY_ENTRY, concept.key)
assert sheerka.sdp.exists(service.CONCEPTS_BY_NAME_ENTRY, concept.name)
assert sheerka.sdp.exists(service.CONCEPTS_BY_HASH_ENTRY, concept.get_definition_hash())
assert sheerka.sdp.exists(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "+")
assert sheerka.om.current_sdp().exists(service.CONCEPTS_BY_ID_ENTRY, concept.id)
assert sheerka.om.current_sdp().exists(service.CONCEPTS_BY_KEY_ENTRY, concept.key)
assert sheerka.om.current_sdp().exists(service.CONCEPTS_BY_NAME_ENTRY, concept.name)
assert sheerka.om.current_sdp().exists(service.CONCEPTS_BY_HASH_ENTRY, concept.get_definition_hash())
assert sheerka.om.current_sdp().exists(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "+")
def test_i_cannot_create_a_bnf_concept_that_references_a_concept_that_cannot_be_resolved(self):
sheerka, context, one_1, one_1_0 = self.init_concepts(Concept("one", body="1"), Concept("one", body="1.0"))
@@ -74,7 +75,7 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
service = sheerka.services[SheerkaConceptManager.NAME]
res = sheerka.create_new_concept(self.get_context(sheerka), concept)
sheerka.cache_manager.commit(context)
sheerka.om.commit(context)
assert res.status
assert sheerka.isinstance(res.value, BuiltinConcepts.NEW_CONCEPT)
@@ -99,11 +100,11 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
assert sheerka.get_by_hash(concept.get_definition_hash()) == concept
# saved in sdp
assert sheerka.sdp.exists(service.CONCEPTS_BY_ID_ENTRY, concept.id)
assert sheerka.sdp.exists(service.CONCEPTS_BY_KEY_ENTRY, concept.key)
assert sheerka.sdp.exists(service.CONCEPTS_BY_NAME_ENTRY, concept.name)
assert sheerka.sdp.exists(service.CONCEPTS_BY_HASH_ENTRY, concept.get_definition_hash())
assert sheerka.sdp.exists(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "hello")
assert sheerka.om.current_sdp().exists(service.CONCEPTS_BY_ID_ENTRY, concept.id)
assert sheerka.om.current_sdp().exists(service.CONCEPTS_BY_KEY_ENTRY, concept.key)
assert sheerka.om.current_sdp().exists(service.CONCEPTS_BY_NAME_ENTRY, concept.name)
assert sheerka.om.current_sdp().exists(service.CONCEPTS_BY_HASH_ENTRY, concept.get_definition_hash())
assert sheerka.om.current_sdp().exists(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "hello")
def test_i_cannot_add_the_same_concept_twice(self):
"""
@@ -184,55 +185,55 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
assert res.status
# I can get by the first entry
assert sheerka.cache_manager.get(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "-") == [concept.id]
assert sheerka.cache_manager.get(sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "-") == [concept.id]
assert sheerka.om.get(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "-") == [concept.id]
assert sheerka.om.get(sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "-") == [concept.id]
@pytest.mark.parametrize("expression", [
"--'filter' ('one' | 'two') ",
"'--filter' ('one' | 'two') ",
])
def test_i_can_get_first_token_when_bnf_concept_and_not_a_letter(self, expression):
sheerka, context, bnf_concept = self.init_concepts(
sheerka, context, bnf_concept = self.init_test().with_concepts(
Concept("foo", definition=expression),
create_new=True)
create_new=True).unpack()
# I can get by the first entry
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]
assert sheerka.om.get(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "-") == [bnf_concept.id]
assert sheerka.om.get(sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "-") == [bnf_concept.id]
def test_concept_references_are_updated_1(self):
sheerka, context, one, two, number, twenty, twenties = self.init_concepts(
sheerka, context, one, two, number, twenty, twenties = self.init_test().with_concepts(
"one",
"two",
"number",
"twenty",
Concept("twenties", definition="twenty one | two 'hundred'"),
create_new=True
)
).unpack()
service = sheerka.services[SheerkaConceptManager.NAME]
assert sheerka.cache_manager.get(service.CONCEPTS_REFERENCES_ENTRY, one.id) == {twenties.id}
assert sheerka.cache_manager.get(service.CONCEPTS_REFERENCES_ENTRY, two.id) == {twenties.id}
assert sheerka.cache_manager.get(service.CONCEPTS_REFERENCES_ENTRY, number.id) is None
assert sheerka.cache_manager.get(service.CONCEPTS_REFERENCES_ENTRY, twenty.id) == {twenties.id}
assert sheerka.cache_manager.get(service.CONCEPTS_REFERENCES_ENTRY, twenties.id) is None
assert sheerka.om.get(service.CONCEPTS_REFERENCES_ENTRY, one.id) == {twenties.id}
assert sheerka.om.get(service.CONCEPTS_REFERENCES_ENTRY, two.id) == {twenties.id}
assert sheerka.om.get(service.CONCEPTS_REFERENCES_ENTRY, number.id) is NotFound
assert sheerka.om.get(service.CONCEPTS_REFERENCES_ENTRY, twenty.id) == {twenties.id}
assert sheerka.om.get(service.CONCEPTS_REFERENCES_ENTRY, twenties.id) is NotFound
def test_concept_references_are_updated_2(self):
sheerka, context, one, two, number, twenty, twenties = self.init_concepts(
sheerka, context, one, two, number, twenty, twenties = self.init_test().with_concepts(
"one",
"two",
"number",
"twenty",
Concept("twenties", definition="twenty number"),
create_new=True
)
).unpack()
service = sheerka.services[SheerkaConceptManager.NAME]
assert sheerka.cache_manager.get(service.CONCEPTS_REFERENCES_ENTRY, one.id) is None
assert sheerka.cache_manager.get(service.CONCEPTS_REFERENCES_ENTRY, two.id) is None
assert sheerka.cache_manager.get(service.CONCEPTS_REFERENCES_ENTRY, number.id) == {twenties.id}
assert sheerka.cache_manager.get(service.CONCEPTS_REFERENCES_ENTRY, twenty.id) == {twenties.id}
assert sheerka.cache_manager.get(service.CONCEPTS_REFERENCES_ENTRY, twenties.id) is None
assert sheerka.om.get(service.CONCEPTS_REFERENCES_ENTRY, one.id) is NotFound
assert sheerka.om.get(service.CONCEPTS_REFERENCES_ENTRY, two.id) is NotFound
assert sheerka.om.get(service.CONCEPTS_REFERENCES_ENTRY, number.id) == {twenties.id}
assert sheerka.om.get(service.CONCEPTS_REFERENCES_ENTRY, twenty.id) == {twenties.id}
assert sheerka.om.get(service.CONCEPTS_REFERENCES_ENTRY, twenties.id) is NotFound
@pytest.mark.parametrize("attr", [
"name",
@@ -357,16 +358,18 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
assert sheerka.get_by_hash(new_concept.get_definition_hash()).get_metadata().body == "metadata value"
# sdp is updated
sheerka.cache_manager.commit(context)
from_sdp = sheerka.sdp.get(service.CONCEPTS_BY_ID_ENTRY, new_concept.id)
sheerka.om.commit(context)
from_sdp = sheerka.om.current_sdp().get(service.CONCEPTS_BY_ID_ENTRY, new_concept.id)
assert from_sdp.get_metadata().body == "metadata value"
assert from_sdp.get_metadata().variables == [("var_name", "default value")]
assert from_sdp.get_prop(BuiltinConcepts.ISA) == {bar}
assert sheerka.sdp.get(service.CONCEPTS_BY_NAME_ENTRY, new_concept.name).get_metadata().body == "metadata value"
assert sheerka.sdp.get(service.CONCEPTS_BY_KEY_ENTRY, new_concept.key).get_metadata().body == "metadata value"
assert sheerka.sdp.get(service.CONCEPTS_BY_HASH_ENTRY,
new_concept.get_definition_hash()).get_metadata().body == "metadata value"
assert sheerka.om.current_sdp().get(service.CONCEPTS_BY_NAME_ENTRY,
new_concept.name).get_metadata().body == "metadata value"
assert sheerka.om.current_sdp().get(service.CONCEPTS_BY_KEY_ENTRY,
new_concept.key).get_metadata().body == "metadata value"
assert sheerka.om.current_sdp().get(service.CONCEPTS_BY_HASH_ENTRY,
new_concept.get_definition_hash()).get_metadata().body == "metadata value"
def test_caches_are_update_when_i_modify_the_name(self):
sheerka, context, foo = self.init_concepts("foo", cache_only=False)
@@ -391,14 +394,15 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
assert not sheerka.is_known(sheerka.get_by_key(foo.key))
assert not sheerka.is_known(sheerka.get_by_hash(foo.get_definition_hash()))
sheerka.cache_manager.commit(context)
assert sheerka.sdp.get(service.CONCEPTS_BY_ID_ENTRY, new_concept.id).name == "bar"
assert sheerka.sdp.get(service.CONCEPTS_BY_KEY_ENTRY, new_concept.key).name == "bar"
assert sheerka.sdp.get(service.CONCEPTS_BY_NAME_ENTRY, new_concept.name).name == "bar"
assert sheerka.sdp.get(service.CONCEPTS_BY_HASH_ENTRY, new_concept.get_definition_hash()).name == "bar"
assert sheerka.sdp.get(service.CONCEPTS_BY_KEY_ENTRY, foo.key) is None
assert sheerka.sdp.get(service.CONCEPTS_BY_NAME_ENTRY, foo.name) is None
assert sheerka.sdp.get(service.CONCEPTS_BY_HASH_ENTRY, foo.get_definition_hash()) is None
sheerka.om.commit(context)
assert sheerka.om.current_sdp().get(service.CONCEPTS_BY_ID_ENTRY, new_concept.id).name == "bar"
assert sheerka.om.current_sdp().get(service.CONCEPTS_BY_KEY_ENTRY, new_concept.key).name == "bar"
assert sheerka.om.current_sdp().get(service.CONCEPTS_BY_NAME_ENTRY, new_concept.name).name == "bar"
assert sheerka.om.current_sdp().get(service.CONCEPTS_BY_HASH_ENTRY,
new_concept.get_definition_hash()).name == "bar"
assert sheerka.om.current_sdp().get(service.CONCEPTS_BY_KEY_ENTRY, foo.key) is NotFound
assert sheerka.om.current_sdp().get(service.CONCEPTS_BY_NAME_ENTRY, foo.name) is NotFound
assert sheerka.om.current_sdp().get(service.CONCEPTS_BY_HASH_ENTRY, foo.get_definition_hash()) is NotFound
def test_i_can_modify_a_concept_from_a_list_of_concepts(self):
sheerka, context, foo1, foo2 = self.init_concepts(
@@ -467,12 +471,12 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
assert new_concept.key == "__var__0 bar __var__1 __var__2"
def test_bnf_is_modified_when_modifying_the_definition(self):
sheerka, context, one, two, foo = self.init_concepts(
sheerka, context, one, two, foo = self.init_test().with_concepts(
"one",
"two",
Concept(name="foo", definition="'twenty' one"),
create_new=True
)
).unpack()
to_add = {"meta": {"definition": "'twenty' two"}}
@@ -484,17 +488,17 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
assert new_concept.get_bnf() == Sequence(StrMatch('twenty'), ConceptExpression(two, rule_name='two'))
def test_concept_by_first_keyword_is_updated_after_concept_modification(self):
sheerka, context, foo, bar, baz = self.init_concepts(
sheerka, context, foo, bar, baz = self.init_test().with_concepts(
Concept("foo"),
Concept("bar"),
Concept("baz", definition="foo"),
create_new=True)
create_new=True).unpack()
assert sheerka.cache_manager.copy(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == {
assert sheerka.om.copy(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == {
"foo": ["1001"],
"bar": ["1002"],
'c:|1001:': ['1003']}
assert sheerka.cache_manager.copy(sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == {
assert sheerka.om.copy(sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == {
'foo': ['1001', '1003'],
'bar': ['1002']}
@@ -502,18 +506,18 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
res = sheerka.modify_concept(context, foo, to_add)
assert res.status
assert sheerka.cache_manager.copy(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == {
assert sheerka.om.copy(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == {
"bar": ["1002", "1001"],
'c:|1001:': ['1003']}
assert sheerka.cache_manager.copy(sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == {
assert sheerka.om.copy(sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == {
'bar': ['1002', '1001', '1003']}
def test_references_are_updated_after_concept_modification(self):
sheerka, context, one, twenty_one = self.init_concepts(
sheerka, context, one, twenty_one = self.init_test().with_concepts(
"onz",
Concept("twenty one", definition="'twenty' onz"),
create_new=True
)
).unpack()
assert twenty_one.get_bnf() == Sequence(StrMatch('twenty'), ConceptExpression(one, rule_name='onz'))
@@ -530,6 +534,35 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
BaseNodeParser.ensure_bnf(context, twenty_one)
assert twenty_one.get_bnf() == Sequence(StrMatch('twenty'), ConceptExpression(modified, rule_name='one'))
def test_i_can_modify_on_top_of_a_new_ontology_layer(self):
sheerka, context, foo = self.init_concepts(Concept("foo").def_var("a").def_var("b"), cache_only=False)
sheerka.push_ontology(context, "new ontology")
to_add = {
"meta": {"body": "a body"},
"props": {BuiltinConcepts.ISA: "bar"},
"variables": {"c": "value"}
}
to_remove = {
"variables": ["b"]
}
res = sheerka.modify_concept(context, foo, to_add=to_add, to_remove=to_remove)
assert res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.NEW_CONCEPT)
assert res.body.body.get_metadata().body == "a body"
assert res.body.body.get_metadata().variables == [("a", None), ("c", "value")]
assert res.body.body.get_metadata().props == {BuiltinConcepts.ISA: {"bar"}}
# and correctly set in cache
updated = sheerka.get_by_id(foo.id)
assert updated.get_metadata().body == "a body"
assert updated.get_metadata().variables == [("a", None), ("c", "value")]
assert updated.get_metadata().props == {BuiltinConcepts.ISA: {"bar"}}
sheerka.pop_ontology()
def test_i_cannot_modify_without_any_modification(self):
sheerka, context, foo = self.init_concepts("foo")
service = sheerka.services[SheerkaConceptManager.NAME]
@@ -627,10 +660,10 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
assert sheerka.get_attr(foo, prop) == bar
def test_i_cannot_remove_a_concept_which_has_reference(self):
sheerka, context, one, twenty_one = self.init_concepts(
sheerka, context, one, twenty_one = self.init_test().with_concepts(
Concept("one"),
Concept("twenty one", definition="'twenty' one"),
create_new=True)
create_new=True).unpack()
res = sheerka.remove_concept(context, one)
@@ -639,9 +672,9 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
assert res.body.body == ConceptIsReferenced([twenty_one])
def test_i_can_remove_a_concept(self):
sheerka, context, one = self.init_concepts(
sheerka, context, one = self.init_test().with_concepts(
Concept("one"),
create_new=True)
create_new=True).unpack()
# sanity check
assert sheerka.get_by_id(one.id) == one
@@ -669,6 +702,34 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
assert sheerka.isinstance(res.body, BuiltinConcepts.ERROR)
assert res.body.body == ConceptNotFound(one)
def test_i_can_create_concepts_in_multiple_ontology_layers(self):
sheerka, context = self.init_concepts(cache_only=False)
res = sheerka.create_new_concept(context, Concept("foo"))
assert res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.NEW_CONCEPT)
sheerka.push_ontology(context, "new ontology")
res = sheerka.create_new_concept(context, Concept("bar"))
assert res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.NEW_CONCEPT)
# I cannot defined foo again, even if it's not the same layer
res = sheerka.create_new_concept(context, Concept("foo"))
assert not res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.CONCEPT_ALREADY_DEFINED)
# I cannot define bar again in this layer
res = sheerka.create_new_concept(context, Concept("bar"))
assert not res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.CONCEPT_ALREADY_DEFINED)
sheerka.pop_ontology()
# But I can if I remove the layer
res = sheerka.create_new_concept(context, Concept("bar"))
assert res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.NEW_CONCEPT)
class TestSheerkaConceptManagerUsingFileBasedSheerka(TestUsingFileBasedSheerka):
def test_i_can_add_several_concepts(self):
@@ -678,42 +739,40 @@ class TestSheerkaConceptManagerUsingFileBasedSheerka(TestUsingFileBasedSheerka):
hello = Concept("Hello world a").def_var("a")
res = sheerka.create_new_concept(context, hello)
sheerka.cache_manager.commit(context)
sheerka.om.commit(context)
assert res.status
sheerka = self.get_sheerka() # another instance
context = self.get_context(sheerka)
greeting = Concept("Greeting a").def_var("a")
res = sheerka.create_new_concept(context, greeting)
sheerka.cache_manager.commit(context)
sheerka.om.commit(context)
assert res.status
sheerka = self.get_sheerka() # another instance again
assert sheerka.sdp.exists(service.CONCEPTS_BY_KEY_ENTRY, hello.key)
assert sheerka.sdp.exists(service.CONCEPTS_BY_KEY_ENTRY, greeting.key)
assert sheerka.sdp.exists(service.CONCEPTS_BY_ID_ENTRY, hello.id)
assert sheerka.sdp.exists(service.CONCEPTS_BY_ID_ENTRY, greeting.id)
assert sheerka.sdp.exists(service.CONCEPTS_BY_NAME_ENTRY, "Hello world a")
assert sheerka.sdp.exists(service.CONCEPTS_BY_NAME_ENTRY, "Greeting a")
assert sheerka.sdp.exists(service.CONCEPTS_BY_HASH_ENTRY, hello.get_definition_hash())
assert sheerka.sdp.exists(service.CONCEPTS_BY_HASH_ENTRY, greeting.get_definition_hash())
assert sheerka.om.current_sdp().exists(service.CONCEPTS_BY_KEY_ENTRY, hello.key)
assert sheerka.om.current_sdp().exists(service.CONCEPTS_BY_KEY_ENTRY, greeting.key)
assert sheerka.om.current_sdp().exists(service.CONCEPTS_BY_ID_ENTRY, hello.id)
assert sheerka.om.current_sdp().exists(service.CONCEPTS_BY_ID_ENTRY, greeting.id)
assert sheerka.om.current_sdp().exists(service.CONCEPTS_BY_NAME_ENTRY, "Hello world a")
assert sheerka.om.current_sdp().exists(service.CONCEPTS_BY_NAME_ENTRY, "Greeting a")
assert sheerka.om.current_sdp().exists(service.CONCEPTS_BY_HASH_ENTRY, hello.get_definition_hash())
assert sheerka.om.current_sdp().exists(service.CONCEPTS_BY_HASH_ENTRY, greeting.get_definition_hash())
assert sheerka.sdp.exists(Sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "Hello")
assert sheerka.sdp.exists(Sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "Greeting")
assert sheerka.om.current_sdp().exists(Sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "Hello")
assert sheerka.om.current_sdp().exists(Sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY, "Greeting")
def test_i_cannot_add_the_same_concept_twice_using_sdp(self):
"""
Checks that duplicated concepts are managed by sheerka, not by sheerka.sdp
:return:
"""
sheerka = self.get_sheerka(cache_only=False)
context = self.get_context(sheerka)
concept = self.get_default_concept()
sheerka, context, concept = self.init_concepts("foo")
sheerka.create_new_concept(context, concept)
sheerka.cache_manager.commit(context)
sheerka.om.commit(context)
sheerka.cache_manager.clear()
sheerka.om.current_cache_manager().clear(set_is_cleared=False)
res = sheerka.create_new_concept(context, concept)
assert not res.status
@@ -727,13 +786,13 @@ class TestSheerkaConceptManagerUsingFileBasedSheerka(TestUsingFileBasedSheerka):
sheerka.create_new_concept(context, Concept("foo", body="1"))
sheerka.create_new_concept(context, Concept("foo", body="2"))
sheerka.cache_manager.commit(context)
sheerka.om.commit(context)
assert len(sheerka.sdp.get(service.CONCEPTS_BY_KEY_ENTRY, "foo")) == 2
assert len(sheerka.om.current_sdp().get(service.CONCEPTS_BY_KEY_ENTRY, "foo")) == 2
sheerka = self.get_sheerka() # new instance
context = self.get_context(sheerka)
sheerka.create_new_concept(context, Concept("foo", body="3"))
sheerka.cache_manager.commit(context)
sheerka.om.commit(context)
assert len(sheerka.sdp.get(service.CONCEPTS_BY_KEY_ENTRY, "foo")) == 3
assert len(sheerka.om.current_sdp().get(service.CONCEPTS_BY_KEY_ENTRY, "foo")) == 3
+67 -5
View File
@@ -1,6 +1,7 @@
import pytest
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept, NotInit
from core.concept import Concept
from core.global_symbols import NotInit, NotFound
from core.sheerka.ExecutionContext import ExecutionContext
from core.sheerka.services.SheerkaDebugManager import SheerkaDebugManager, DebugItem, ConceptDebugObj
from parsers.PythonParser import PythonNode
@@ -830,7 +831,7 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
dummy = DummyObj(foo, "value")
res = sheerka.inspect(context, dummy, "#type#", "fake", "a", "b")
assert res.body == {'#type#': 'DummyObj',
'fake': "** Not Found **",
'fake': NotFound,
'a': foo,
'b': 'value'}
@@ -850,19 +851,19 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
res = sheerka.inspect(context, 0)
assert res.body == {'#type#': 'NotFound',
assert res.body == {'#type#': 'NotFoundConcept',
'id': sheerka.concepts_ids[BuiltinConcepts.NOT_FOUND],
'key': '__NOT_FOUND',
'name': '__NOT_FOUND',
'body': 'no digest'}
def test_i_can_inspect_values(self):
sheerka, context, table, how, little = self.init_concepts(
sheerka, context, table, how, little = self.init_test().with_concepts(
"table",
Concept("how is x").def_var("x"),
Concept("little x").def_var("x"),
create_new=True
)
).unpack()
return_values = sheerka.evaluate_user_input("how is little table")
@@ -911,3 +912,64 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
assert str(ConceptDebugObj(foo)) == \
"(:foo|1001:meta.x='x_meta', meta.y='y_meta', compiled.x=(:bar|1002:meta.a='a_meta', value.a='a_value'), value.x='x_value', value.z='extra_value')"
def test_i_can_save_and_restore_state_to_default_state(self):
sheerka, context = self.init_concepts()
service = sheerka.services[SheerkaDebugManager.NAME]
sheerka.push_ontology(context, "new ontology")
service.set_debug(context)
service.set_explicit(context)
service.debug_var(context, "var_service.var_method.var_name", "1+", 1)
service.debug_rule(context, "rule_service.rule_method.rule_name", "2+", 2)
service.debug_concept(context, "concept_service.concept_method.concept_name", "3+", 3)
# sanity check
assert service.activated
assert service.explicit
assert service.debug_vars_settings != []
assert service.debug_rules_settings != []
assert service.debug_concepts_settings != []
sheerka.pop_ontology()
assert not service.activated
assert not service.explicit
assert service.context_cache == set()
assert service.variable_cache == set()
assert service.debug_vars_settings == []
assert service.debug_rules_settings == []
assert service.debug_concepts_settings == []
def test_i_can_save_and_restore_state_to_specific_state(self):
sheerka, context = self.init_concepts()
service = sheerka.services[SheerkaDebugManager.NAME]
service.set_debug(context)
service.set_explicit(context)
service.debug_var(context, "v_service.v_method.v_name", "1+", 1)
service.debug_rule(context, "r_service.r_method.r_name", "2+", 2)
service.debug_concept(context, "c_serv.c_method.c_name", "3+", 3)
sheerka.push_ontology(context, "new ontology")
# modify the state
service.set_debug(context, False)
service.set_explicit(context, False)
service.debug_var(context, "var_service2.var_method2.var_name2", "11+", 11)
service.debug_rule(context, "rule_service2.rule_method2.rule_name2", "22+", 22)
service.debug_concept(context, "concept_service2.concept_method2.concept_name2", "33+", 33)
# sanity
assert not service.activated
assert not service.explicit
assert len(service.debug_vars_settings) == 2
assert len(service.debug_rules_settings) == 2
assert len(service.debug_concepts_settings) == 2
sheerka.pop_ontology()
assert service.activated
assert service.explicit
assert service.debug_vars_settings == [DebugItem("v_name", "v_service", "v_method", 1, True, 1, False, True)]
assert service.debug_rules_settings == [DebugItem("r_name", "r_service", "r_method", 2, True, 2, False, True)]
assert service.debug_concepts_settings == [DebugItem("c_name", "c_serv", "c_method", 3, True, 3, False, True)]
+6 -4
View File
@@ -2,8 +2,9 @@ from dataclasses import dataclass
import pytest
from core.builtin_concepts import BuiltinConcepts, ReturnValueConcept, ParserResultConcept
from core.concept import Concept, DoNotResolve, ConceptParts, InfiniteRecursionResolved, CB, NotInit, \
from core.concept import Concept, DoNotResolve, ConceptParts, InfiniteRecursionResolved, CB, \
concept_part_value, DEFINITION_TYPE_DEF
from core.global_symbols import NotInit
from core.sheerka.services.SheerkaEvaluateConcept import SheerkaEvaluateConcept
from core.sheerka.services.SheerkaMemory import SheerkaMemory
from parsers.BaseParser import BaseParser
@@ -13,7 +14,6 @@ from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
from tests.evaluators.EvaluatorTestsUtils import pr_ret_val, python_ret_val
class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
@pytest.mark.parametrize("body, expected", [
@@ -27,7 +27,7 @@ class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
("1 > 2", False),
])
def test_i_can_evaluate_a_concept_with_simple_body(self, body, expected):
sheerka, context, concept = self.init_concepts(Concept("foo", body=body), eval_body=True)
sheerka, context, concept = self.init_test(eval_body=True).with_concepts(Concept("foo", body=body)).unpack()
evaluated = sheerka.evaluate_concept(context, concept)
@@ -99,7 +99,9 @@ class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
def test_i_can_evaluate_when_the_body_is_the_name_of_the_concept(self):
# to prove that I can distinguish from a string
sheerka, context, concept = self.init_concepts(Concept("foo", body="'foo'"), eval_body=True, create_new=True)
sheerka, context, concept = self.init_test(eval_body=True).with_concepts(
Concept("foo", body="'foo'"),
create_new=True).unpack()
evaluated = sheerka.evaluate_concept(context, concept)
+5 -5
View File
@@ -9,7 +9,7 @@ from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
class TestSheerkaEvaluateRules(TestUsingMemoryBasedSheerka):
def test_i_can_evaluate_python_rules(self):
sheerka, context, r1, r2, r3, r4, r5, r6, r7, r8, r9 = self.init_format_rules(
sheerka, context, r1, r2, r3, r4, r5, r6, r7, r8, r9 = self.init_test().with_rules(
Rule(predicate="a == 1", action="", priority=1), # r1
Rule(predicate="a == 2", action="", priority=1), # r2
Rule(predicate="a == 3", action="", priority=0), # r3
@@ -19,7 +19,7 @@ class TestSheerkaEvaluateRules(TestUsingMemoryBasedSheerka):
Rule(predicate="a == 7", action="", priority=1, is_enabled=False), # r7
Rule(predicate="a == 8", action="", priority=1), # r8
Rule(predicate="a == 9", action="", priority=2), # r9
)
).unpack()
service = sheerka.services[SheerkaEvaluateRules.NAME]
rules = sorted([r1, r2, r3, r4, r5, r6, r7, r8, r9], key=operator.attrgetter('priority'), reverse=True)
@@ -33,7 +33,8 @@ class TestSheerkaEvaluateRules(TestUsingMemoryBasedSheerka):
}
def test_i_can_evaluate_concept_rules(self):
sheerka, context, r1, r2, r3, r4, r5, r6, r7, r8, r9 = self.init_format_rules(
sheerka, context, concept, r1, r2, r3, r4, r5, r6, r7, r8, r9 = self.init_test().with_concepts(
Concept("x equals y", body="x == y").def_var("x").def_var("y"), create_new=True).with_rules(
Rule(predicate="a equals 1", action="", priority=1), # r1
Rule(predicate="a equals 2", action="", priority=1), # r2
Rule(predicate="a equals 3", action="", priority=0), # r3
@@ -43,8 +44,7 @@ class TestSheerkaEvaluateRules(TestUsingMemoryBasedSheerka):
Rule(predicate="a equals 7", action="", priority=1, is_enabled=False), # r7
Rule(predicate="a equals 8", action="", priority=1), # r8
Rule(predicate="a equals 9", action="", priority=2), # r9
concepts=[Concept("x equals y", body="x == y").def_var("x").def_var("y")],
)
).unpack()
service = sheerka.services[SheerkaEvaluateRules.NAME]
rules = sorted([r1, r2, r3, r4, r5, r6, r7, r8, r9], key=operator.attrgetter('priority'), reverse=True)
+21 -2
View File
@@ -1,3 +1,5 @@
import pytest
from core.sheerka.services.SheerkaEventManager import SheerkaEventManager
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
@@ -44,7 +46,7 @@ example_of_class_method. event=xxx
"""
service = sheerka.services[SheerkaEventManager.NAME]
service.reset_topic(topic)
service.test_only_reset_topic(topic)
def test_i_can_subscribe_and_publish_with_data(self, capsys):
sheerka, context = self.init_concepts()
@@ -63,4 +65,21 @@ example_of_class_method. event=xxx, data='42'
"""
service = sheerka.services[SheerkaEventManager.NAME]
service.reset_topic(topic)
service.test_only_reset_topic(topic)
def test_i_can_save_and_reset_state(self):
sheerka, context = self.init_concepts()
service = sheerka.services[SheerkaEventManager.NAME]
sheerka.subscribe("my first topic", self.example_of_class_method_with_data)
sheerka.push_ontology(context, "new ontology")
sheerka.subscribe("my second topic", self.example_of_class_method_with_data)
# I can access to the topic
assert "my first topic" in service.subscribers
assert "my second topic" in service.subscribers
sheerka.pop_ontology()
assert "my first topic" in service.subscribers
assert "my second topic" not in service.subscribers
@@ -1,5 +1,8 @@
import pytest
from core.sheerka.services.SheerkaFunctionsParametersHistory import SheerkaFunctionsParametersHistory, \
FunctionParametersObj
from core.utils import sheerka_deepcopy
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
@@ -7,13 +10,15 @@ from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
class TestSheerkaFunctionsParametersHistory(TestUsingMemoryBasedSheerka):
def test_i_can_add_a_parameter_value(self):
sheerka, context = self.init_concepts(cache_only=False)
service = SheerkaFunctionsParametersHistory(sheerka).initialize()
sheerka.om.test_only_unfreeze()
service = SheerkaFunctionsParametersHistory(sheerka).initialize() # since service is no longer auto init'ed
sheerka.om.freeze()
service.record_function_parameter(context, "function", 1, "10")
service.record_function_parameter(context, "function", 2, "True")
service.record_function_parameter(context, "function", 3, "'string value'")
assert service.cache.copy() == {"function": FunctionParametersObj(
assert sheerka.om.copy(service.FUNCTIONS_PARAMETERS_ENTRY) == {"function": FunctionParametersObj(
context.event.get_digest(),
"function",
{
@@ -23,8 +28,8 @@ class TestSheerkaFunctionsParametersHistory(TestUsingMemoryBasedSheerka):
})}
# and i can serialize
sheerka.cache_manager.commit(context)
from_db = sheerka.sdp.get(SheerkaFunctionsParametersHistory.FUNCTIONS_PARAMETERS_ENTRY, "function")
sheerka.om.commit(context)
from_db = sheerka.om.current_sdp().get(service.FUNCTIONS_PARAMETERS_ENTRY, "function")
assert from_db.event_id == context.event.get_digest()
assert from_db.name == "function"
assert from_db.params == {
@@ -35,14 +40,16 @@ class TestSheerkaFunctionsParametersHistory(TestUsingMemoryBasedSheerka):
def test_i_can_add_the_same_value_multiple_times(self):
sheerka, context = self.init_concepts(cache_only=True)
service = SheerkaFunctionsParametersHistory(sheerka)
sheerka.om.test_only_unfreeze()
service = SheerkaFunctionsParametersHistory(sheerka).initialize() # since service is no longer auto init'ed
sheerka.om.freeze()
service.record_function_parameter(context, "function", 1, "10")
service.record_function_parameter(context, "function", 1, "20")
service.record_function_parameter(context, "function", 2, "True")
service.record_function_parameter(context, "function", 1, "20")
assert service.cache.copy() == {"function": FunctionParametersObj(
assert sheerka.om.copy(service.FUNCTIONS_PARAMETERS_ENTRY) == {"function": FunctionParametersObj(
context.event.get_digest(),
"function",
{
@@ -52,12 +59,14 @@ class TestSheerkaFunctionsParametersHistory(TestUsingMemoryBasedSheerka):
def test_i_can_specify_parameter_in_any_order(self):
sheerka, context = self.init_concepts()
service = SheerkaFunctionsParametersHistory(sheerka)
sheerka.om.test_only_unfreeze()
service = SheerkaFunctionsParametersHistory(sheerka).initialize() # since service is no longer auto init'ed
sheerka.om.freeze()
service.record_function_parameter(context, "function", 3, "'string value'")
service.record_function_parameter(context, "function", 2, "True")
assert service.cache.copy() == {"function": FunctionParametersObj(
assert sheerka.om.copy(service.FUNCTIONS_PARAMETERS_ENTRY) == {"function": FunctionParametersObj(
context.event.get_digest(),
"function",
{
@@ -67,7 +76,9 @@ class TestSheerkaFunctionsParametersHistory(TestUsingMemoryBasedSheerka):
def test_no_value_is_managed(self):
sheerka, context = self.init_concepts()
service = SheerkaFunctionsParametersHistory(sheerka)
sheerka.om.test_only_unfreeze()
service = SheerkaFunctionsParametersHistory(sheerka).initialize() # since service is no longer auto init'ed
sheerka.om.freeze()
# no entry for the function
assert service.get_function_parameters("function", 2) == []
@@ -78,10 +89,45 @@ class TestSheerkaFunctionsParametersHistory(TestUsingMemoryBasedSheerka):
def test_i_can_get_sorted_parameters(self):
sheerka, context = self.init_concepts()
service = SheerkaFunctionsParametersHistory(sheerka)
sheerka.om.test_only_unfreeze()
service = SheerkaFunctionsParametersHistory(sheerka).initialize() # since service is no longer auto init'ed
sheerka.om.freeze()
service.record_function_parameter(context, "function", 2, "'string value'")
service.record_function_parameter(context, "function", 2, "True")
service.record_function_parameter(context, "function", 2, "True")
assert service.get_function_parameters("function", 2) == ["True", "'string value'"]
def test_i_can_add_and_retrieve_parameters_when_multiple_ontology_layers(self):
sheerka, context = self.init_concepts(cache_only=False)
sheerka.om.test_only_unfreeze()
service = SheerkaFunctionsParametersHistory(sheerka).initialize() # since service is no longer auto init'ed
sheerka.om.freeze()
service.record_function_parameter(context, "function", 1, "10")
service.record_function_parameter(context, "function", 2, "True")
service.record_function_parameter(context, "function", 3, "'string value'")
sheerka.push_ontology(context, "new ontology")
service.record_function_parameter(context, "function", 1, "20")
service.record_function_parameter(context, "function", 2, "True")
assert sheerka.om.copy(service.FUNCTIONS_PARAMETERS_ENTRY) == {"function": FunctionParametersObj(
context.event.get_digest(),
"function",
{
1: [('10', 1), ("20", 1)],
2: [('True', 2)],
3: [("'string value'", 1)]
})}
sheerka.pop_ontology()
assert sheerka.om.copy(service.FUNCTIONS_PARAMETERS_ENTRY) == {"function": FunctionParametersObj(
context.event.get_digest(),
"function",
{
1: [('10', 1)],
2: [('True', 1)],
3: [("'string value'", 1)]
})}
+3 -3
View File
@@ -1,10 +1,10 @@
from core.sheerka.services.SheerkaHistoryManager import hist
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
from tests.TestUsingFileBasedSheerka import TestUsingFileBasedSheerka
class TestSheerkaHistoryManager(TestUsingMemoryBasedSheerka):
class TestSheerkaHistoryManager(TestUsingFileBasedSheerka):
def test_i_can_retrieve_history(self):
sheerka = self.get_sheerka(singleton=False)
sheerka = self.get_sheerka()
sheerka.save_execution_context = True
sheerka.evaluate_user_input("def concept one as 1")
@@ -1,12 +1,14 @@
import pytest
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept
from core.sheerka.services.SheerkaSetsManager import SheerkaSetsManager
from core.sheerka.services.SheerkaIsAManager import SheerkaIsAManager
from tests.TestUsingFileBasedSheerka import TestUsingFileBasedSheerka
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
class TestSheerkaSetsManager(TestUsingMemoryBasedSheerka):
class TestSheerkaIsAManager(TestUsingMemoryBasedSheerka):
def test_i_can_add_a_concept_to_a_set(self):
sheerka, context, foo, group = self.init_concepts(
@@ -14,14 +16,18 @@ class TestSheerkaSetsManager(TestUsingMemoryBasedSheerka):
Concept("group"),
cache_only=False
)
assert sheerka.add_concept_to_set(context, foo, group).status
group_elements = sheerka.cache_manager.get(SheerkaSetsManager.CONCEPTS_GROUPS_ENTRY, group.id)
res = sheerka.add_concept_to_set(context, foo, group)
assert res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
group_elements = sheerka.om.get(SheerkaIsAManager.CONCEPTS_GROUPS_ENTRY, group.id)
assert group_elements == {foo.id}
# it can be persisted
sheerka.cache_manager.commit(context)
assert sheerka.sdp.get(SheerkaSetsManager.CONCEPTS_GROUPS_ENTRY, group.id) == {foo.id}
sheerka.om.commit(context)
assert sheerka.om.current_sdp().get(SheerkaIsAManager.CONCEPTS_GROUPS_ENTRY, group.id) == {foo.id}
def test_i_cannot_add_the_same_concept_twice_in_a_set(self):
sheerka, context, foo, group = self.init_concepts(Concept("foo"), Concept("group"))
@@ -35,7 +41,7 @@ class TestSheerkaSetsManager(TestUsingMemoryBasedSheerka):
assert res.body.body == foo
assert res.body.concept_set == group
all_entries = sheerka.cache_manager.get(SheerkaSetsManager.CONCEPTS_GROUPS_ENTRY, group.id)
all_entries = sheerka.om.get(SheerkaIsAManager.CONCEPTS_GROUPS_ENTRY, group.id)
assert all_entries == {foo.id}
def test_i_can_have_multiple_groups(self):
@@ -53,12 +59,12 @@ class TestSheerkaSetsManager(TestUsingMemoryBasedSheerka):
assert sheerka.add_concept_to_set(context, bar, group2).status
assert sheerka.add_concept_to_set(context, baz, group2).status
assert sheerka.cache_manager.get(SheerkaSetsManager.CONCEPTS_GROUPS_ENTRY, group1.id) == {foo.id, bar.id}
assert sheerka.cache_manager.get(SheerkaSetsManager.CONCEPTS_GROUPS_ENTRY, group2.id) == {baz.id, bar.id}
assert sheerka.om.get(SheerkaIsAManager.CONCEPTS_GROUPS_ENTRY, group1.id) == {foo.id, bar.id}
assert sheerka.om.get(SheerkaIsAManager.CONCEPTS_GROUPS_ENTRY, group2.id) == {baz.id, bar.id}
# I can save in db
sheerka.cache_manager.commit(context)
assert sheerka.sdp.get(SheerkaSetsManager.CONCEPTS_GROUPS_ENTRY) == {
sheerka.om.commit(context)
assert sheerka.om.current_sdp().get(SheerkaIsAManager.CONCEPTS_GROUPS_ENTRY) == {
'1004': {'1001', '1002'}, '1005': {'1002', '1003'}
}
@@ -96,7 +102,7 @@ class TestSheerkaSetsManager(TestUsingMemoryBasedSheerka):
sheerka, context, foo, bar, group1, group2 = self.init_concepts(
"foo", "bar", "group1", Concept("group2", body="group1"))
service = sheerka.services[SheerkaSetsManager.NAME]
service = sheerka.services[SheerkaIsAManager.NAME]
service.add_concepts_to_set(context, [foo, bar], group1)
assert sheerka.isaset(context, group2)
@@ -112,7 +118,7 @@ class TestSheerkaSetsManager(TestUsingMemoryBasedSheerka):
Concept("number"),
Concept("sub_number", body="number", where="number < 4")
)
service = sheerka.services[SheerkaSetsManager.NAME]
service = sheerka.services[SheerkaIsAManager.NAME]
service.add_concepts_to_set(context, [one, two, three, four, five], number)
assert sheerka.isaset(context, sub_number)
@@ -129,7 +135,7 @@ class TestSheerkaSetsManager(TestUsingMemoryBasedSheerka):
Concept("sub_number", body="number", where="number < 4"),
Concept("sub_sub_number", body="sub_number", where="sub_number > 2")
)
service = sheerka.services[SheerkaSetsManager.NAME]
service = sheerka.services[SheerkaIsAManager.NAME]
service.add_concepts_to_set(context, [one, two, three, four, five], number)
assert sheerka.isaset(context, sub_sub_number)
@@ -153,7 +159,7 @@ class TestSheerkaSetsManager(TestUsingMemoryBasedSheerka):
Concept("number"),
Concept("sub_number", body="number", where="number < 4")
)
service = sheerka.services[SheerkaSetsManager.NAME]
service = sheerka.services[SheerkaIsAManager.NAME]
service.add_concepts_to_set(context, [one, two, three, four, five], number)
assert sheerka.isaset(context, sub_number)
@@ -175,7 +181,7 @@ class TestSheerkaSetsManager(TestUsingMemoryBasedSheerka):
Concept("number"),
Concept("sub_number", body="number", where="number >= 20")
)
service = sheerka.services[SheerkaSetsManager.NAME]
service = sheerka.services[SheerkaIsAManager.NAME]
service.add_concepts_to_set(context, [one, two, twenty, twenties], number)
assert sheerka.isaset(context, sub_number)
@@ -192,15 +198,14 @@ class TestSheerkaSetsManager(TestUsingMemoryBasedSheerka):
:return:
"""
sheerka, context, one, two, twenty, twenties, number = self.init_concepts(
sheerka, context, one, two, twenty, twenties, number = self.init_test().with_concepts(
Concept("one", body="1"),
Concept("two", body="2"),
Concept("twenty", body="20"),
Concept("twenties", definition="twenty (one|two)=unit", body="twenty + unit").def_var("unit"),
Concept("number"),
create_new=True
)
service = sheerka.services[SheerkaSetsManager.NAME]
create_new=True).unpack()
service = sheerka.services[SheerkaIsAManager.NAME]
service.add_concepts_to_set(context, [one, two, twenty, twenties], number)
assert sheerka.isinset(twenties, number)
@@ -210,12 +215,11 @@ class TestSheerkaSetsManager(TestUsingMemoryBasedSheerka):
assert sheerka.isinset(res[0].body, number)
def test_a_concept_can_be_in_multiple_sets(self):
sheerka, context, foo, all_foo, all_bar = self.init_concepts(
sheerka, context, foo, all_foo, all_bar = self.init_test().with_concepts(
Concept("foo"),
Concept("all_foo"),
Concept("all_bar"),
create_new=True
)
create_new=True).unpack()
foo = sheerka.new(foo.key) # new instance
sheerka.set_isa(context, foo, all_foo)
@@ -270,7 +274,7 @@ class TestSheerkaSetsManager(TestUsingMemoryBasedSheerka):
elements = sheerka.get_set_elements(context, number)
assert [c.id for c in elements] == [one.id]
concepts_in_cache = sheerka.cache_manager.get(SheerkaSetsManager.CONCEPTS_IN_GROUPS_ENTRY, number.id)
concepts_in_cache = sheerka.om.get(SheerkaIsAManager.CONCEPTS_IN_GROUPS_ENTRY, number.id)
assert [c.id for c in concepts_in_cache] == [one.id]
# pretend that number has been updated in sheerka.concepts_grammar
@@ -281,23 +285,63 @@ class TestSheerkaSetsManager(TestUsingMemoryBasedSheerka):
elements = sheerka.get_set_elements(context, number)
assert {c.id for c in elements} == {one.id, two.id}
concepts_in_cache = sheerka.cache_manager.get(SheerkaSetsManager.CONCEPTS_IN_GROUPS_ENTRY, number.id)
concepts_in_cache = sheerka.om.get(SheerkaIsAManager.CONCEPTS_IN_GROUPS_ENTRY, number.id)
assert {c.id for c in concepts_in_cache} == {one.id, two.id}
# make sure the bnf definition is also updated
assert number.id not in sheerka.concepts_grammars
def test_i_can_get_and_set_isa_when_multiple_ontology_layers(self):
sheerka, context, foo, group1, group2 = self.init_concepts(
Concept("foo"),
Concept("group1"),
Concept("group2"),
cache_only=False
)
sheerka.set_isa(context, foo, group1)
assert sheerka.isaset(context, group1)
assert sheerka.isinset(foo, group1)
assert sheerka.isa(foo, group1)
sheerka.push_ontology(context, "new ontology")
assert sheerka.isaset(context, group1)
assert sheerka.isinset(foo, group1)
assert sheerka.isa(foo, group1)
assert not sheerka.isaset(context, group2)
assert not sheerka.isinset(foo, group2)
assert not sheerka.isa(foo, group2)
sheerka.set_isa(context, foo, group2)
assert sheerka.isaset(context, group1)
assert sheerka.isinset(foo, group1)
assert sheerka.isa(foo, group1)
assert sheerka.isaset(context, group2)
assert sheerka.isinset(foo, group2)
assert sheerka.isa(foo, group2)
# I can revert back
sheerka.pop_ontology()
assert sheerka.isaset(context, group1)
assert sheerka.isinset(foo, group1)
assert sheerka.isa(foo, group1)
assert not sheerka.isaset(context, group2)
assert not sheerka.isinset(foo, group2)
foo = sheerka.get_by_id(foo.id)
assert not sheerka.isa(foo, group2)
class TestSheerkaSetsManagerUsingFileBasedSheerka(TestUsingFileBasedSheerka):
def test_i_can_add_concept_to_set_and_retrieve_it_in_another_session(self):
sheerka, context, foo, bar, group = self.init_concepts(
Concept("foo"),
Concept("bar"),
Concept("group"),
create_new=True)
sheerka, context, foo, bar, group = self.init_test().with_concepts(Concept("foo"),
Concept("bar"),
Concept("group"),
create_new=True).unpack()
assert sheerka.add_concept_to_set(context, foo, group).status
sheerka.cache_manager.commit(context)
sheerka.om.commit(context)
sheerka = self.get_sheerka(reset_attrs=False) # another session
context = self.get_context(sheerka)
@@ -306,8 +350,8 @@ class TestSheerkaSetsManagerUsingFileBasedSheerka(TestUsingFileBasedSheerka):
# I can get the elements
assert set(sheerka.get_set_elements(context, group)) == {foo, bar}
sheerka.cache_manager.commit(context) # save in db
all_entries = sheerka.sdp.get(SheerkaSetsManager.CONCEPTS_GROUPS_ENTRY) # check the db
sheerka.om.commit(context) # save in db
all_entries = sheerka.om.current_sdp().get(SheerkaIsAManager.CONCEPTS_GROUPS_ENTRY) # check the db
assert all_entries == {
group.id: {foo.id, bar.id}
}
@@ -320,33 +364,31 @@ class TestSheerkaSetsManagerUsingFileBasedSheerka(TestUsingFileBasedSheerka):
for c in [foo3, foo4]:
sheerka.create_new_concept(context, c)
sets_handler = sheerka.services[SheerkaSetsManager.NAME]
sets_handler = sheerka.services[SheerkaIsAManager.NAME]
res = sets_handler.add_concepts_to_set(context, (foo3, foo4), group)
assert res.status
# I can get the elements
assert set(sheerka.get_set_elements(context, group)) == {foo, bar, foo3, foo4}
sheerka.cache_manager.commit(context) # save in db
all_entries = sheerka.sdp.get(SheerkaSetsManager.CONCEPTS_GROUPS_ENTRY) # check the db
sheerka.om.commit(context) # save in db
all_entries = sheerka.om.current_sdp().get(SheerkaIsAManager.CONCEPTS_GROUPS_ENTRY) # check the db
assert all_entries == {
group.id: {foo.id, bar.id, foo3.id, foo4.id}
}
def test_i_can_set_isa(self):
sheerka, context, foo, bar, group = self.init_concepts(
"foo",
"bar",
"group",
create_new=True, # needed by modify
)
sheerka, context, foo, bar, group = self.init_test().with_concepts("foo",
"bar",
"group",
).unpack()
# nothing was previously in ISA
foo = sheerka.new(foo.key)
assert BuiltinConcepts.ISA not in foo.get_metadata().props
res = sheerka.set_isa(context, foo, group)
assert res.status
sheerka.cache_manager.commit(context)
sheerka.om.commit(context)
sheerka = self.get_sheerka(reset_attrs=False)
assert foo.get_prop(BuiltinConcepts.ISA) == {group}
@@ -363,11 +405,11 @@ class TestSheerkaSetsManagerUsingFileBasedSheerka(TestUsingFileBasedSheerka):
assert sheerka.isinset(bar, group)
assert sheerka.isaset(context, group)
sheerka.cache_manager.commit(context)
sheerka.om.commit(context)
# they are both in the same group
sheerka = self.get_sheerka(reset_attrs=False)
all_entries = sheerka.sdp.get(SheerkaSetsManager.CONCEPTS_GROUPS_ENTRY)
all_entries = sheerka.om.current_sdp().get(SheerkaIsAManager.CONCEPTS_GROUPS_ENTRY)
assert all_entries == {
group.id: {foo.id, bar.id}
}
+22 -21
View File
@@ -1,5 +1,6 @@
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept
from core.global_symbols import NotFound
from core.sheerka.ExecutionContext import ExecutionContext
from core.sheerka.services.SheerkaMemory import SheerkaMemory, MemoryObject
@@ -19,7 +20,7 @@ class TestSheerkaMemory(TestUsingMemoryBasedSheerka):
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()
sheerka, context = self.init_test().unpack()
service = sheerka.services[SheerkaMemory.NAME]
foo = Concept("foo")
@@ -28,10 +29,10 @@ class TestSheerkaMemory(TestUsingMemoryBasedSheerka):
context_id = ExecutionContext.ids[context.event.get_digest()]
assert service.short_term_objects.copy() == {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
assert sheerka.get_from_short_term_memory(None, "a") is NotFound
def test_i_can_add_many(self):
sheerka, context = self.init_concepts()
sheerka, context = self.init_test().unpack()
bag = {"a": "foo", "b": "bar", }
context_id = ExecutionContext.ids[context.event.get_digest()]
service = sheerka.services[SheerkaMemory.NAME]
@@ -40,7 +41,7 @@ class TestSheerkaMemory(TestUsingMemoryBasedSheerka):
assert service.short_term_objects.copy() == {context_id: bag}
def test_i_can_get_obj_from_parents(self):
sheerka, context = self.init_concepts()
sheerka, context = self.init_test().unpack()
foo = Concept("foo")
sheerka.add_to_short_term_memory(None, "a", foo)
@@ -53,54 +54,54 @@ class TestSheerkaMemory(TestUsingMemoryBasedSheerka):
assert id(sheerka.get_from_short_term_memory(sub_context, "b")) == id(foo)
assert id(sheerka.get_from_short_term_memory(context, "b")) == id(foo)
assert sheerka.get_from_short_term_memory(None, "b") is None
assert sheerka.get_from_short_term_memory(None, "b") is NotFound
def test_short_term_memory_entries_are_removed_on_context_exit(self):
sheerka, context = self.init_concepts()
sheerka, context = self.init_test().unpack()
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
assert sheerka.get_from_short_term_memory(sub_context, "a") is NotFound
def test_short_term_memory_entries_are_removed_on_context_exit_2(self):
# this time we test the bulk insert
sheerka, context = self.init_concepts()
sheerka, context = self.init_test().unpack()
with context.push(BuiltinConcepts.TESTING, None) as sub_context:
foo = Concept("foo")
sheerka.add_many_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
assert sheerka.get_from_short_term_memory(sub_context, "a") is NotFound
def test_i_can_add_and_retrieve_from_memory(self):
sheerka, context = self.init_concepts()
service = sheerka.services[SheerkaMemory.NAME]
sheerka, context = self.init_test().unpack()
assert sheerka.get_from_memory(context, "a") is None
assert sheerka.get_from_memory(context, "a") is NotFound
foo = Concept("foo")
sheerka.add_to_memory(context, "a", foo)
assert service.memory_objects.copy() == {"a": MemoryObject(context.event.get_digest(), foo)}
assert sheerka.om.copy(SheerkaMemory.OBJECTS_ENTRY) == {"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()
sheerka, context = self.init_test(cache_only=False).unpack()
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)
sheerka.om.commit(context)
assert sheerka.memory(context) == {"foo": foo, "bar": bar}
def test_i_can_use_memory_with_a_string(self):
sheerka, context = self.init_concepts()
sheerka, context = self.init_test().unpack()
foo = Concept("foo")
sheerka.add_to_memory(context, "foo", foo)
@@ -108,7 +109,7 @@ class TestSheerkaMemory(TestUsingMemoryBasedSheerka):
assert sheerka.memory(context, "foo") == foo
def test_i_can_use_memory_with_a_concept(self):
sheerka, context = self.init_concepts()
sheerka, context = self.init_test().unpack()
foo = Concept("foo")
sheerka.add_to_memory(context, "foo", foo)
@@ -116,7 +117,7 @@ class TestSheerkaMemory(TestUsingMemoryBasedSheerka):
assert sheerka.memory(context, Concept("foo")) == foo
def test_concept_not_found_is_return_when_not_found(self):
sheerka, context = self.init_concepts()
sheerka, context = self.init_test().unpack()
res = sheerka.memory(context, "foo")
@@ -124,7 +125,7 @@ class TestSheerkaMemory(TestUsingMemoryBasedSheerka):
assert res.body == {"#name": "foo"}
def test_memory_only_returns_the_last_object(self):
sheerka, context = self.init_concepts()
sheerka, context = self.init_test().unpack()
foo = Concept("foo")
bar = Concept("bar")
@@ -135,17 +136,17 @@ class TestSheerkaMemory(TestUsingMemoryBasedSheerka):
assert sheerka.memory(context, "item") == bar
def test_object_are_not_added_in_memory_during_the_initialisation(self):
sheerka, context = self.init_concepts()
sheerka, context = self.init_test().unpack()
assert len(sheerka.memory(context)) == 0
class TestSheerkaMemoryUsingFileBase(TestUsingFileBasedSheerka):
def test_i_can_record_memory_objects(self):
sheerka, context = self.init_concepts()
sheerka, context = self.init_test().unpack()
sheerka.add_to_memory(context, "item", Concept("foo"))
sheerka.cache_manager.commit(context)
sheerka.om.commit(context)
sheerka = self.get_sheerka()
context = self.get_context(sheerka)
+117 -39
View File
@@ -3,8 +3,8 @@ import ast
import pytest
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept, CMV
from core.global_symbols import RULE_COMPARISON_CONTEXT
from core.rule import Rule
from core.global_symbols import RULE_COMPARISON_CONTEXT, NotFound
from core.rule import Rule, ACTION_TYPE_PRINT, ACTION_TYPE_EXEC
from core.sheerka.services.SheerkaRuleManager import SheerkaRuleManager, FormatRuleParser, \
FormatAstRawText, FormatAstVariable, FormatAstSequence, FormatAstFunction, \
FormatRuleSyntaxError, FormatAstList, UnexpectedEof, FormatAstColor, RulePredicate, FormatAstDict, FormatAstMulti
@@ -28,13 +28,12 @@ CONCEPT_EVALUATOR_NAME = "Concept"
class TestSheerkaRuleManager(TestUsingMemoryBasedSheerka):
@pytest.mark.parametrize("action_type, cache_entry", [
("print", SheerkaRuleManager.FORMAT_RULE_ENTRY),
("exec", SheerkaRuleManager.EXEC_RULE_ENTRY),
(ACTION_TYPE_PRINT, SheerkaRuleManager.FORMAT_RULE_ENTRY),
(ACTION_TYPE_EXEC, SheerkaRuleManager.EXEC_RULE_ENTRY),
])
def test_i_can_create_a_new_rule(self, action_type, cache_entry):
sheerka, context = self.init_concepts(cache_only=False)
previous_rules_number = sheerka.cache_manager.caches[sheerka.OBJECTS_IDS_ENTRY].cache.copy()[
SheerkaRuleManager.RULE_IDS]
previous_rules_number = sheerka.om.get_all(sheerka.OBJECTS_IDS_ENTRY)[SheerkaRuleManager.RULE_IDS]
rule = Rule(action_type, "name", "True", "Hello world")
@@ -52,33 +51,50 @@ class TestSheerkaRuleManager(TestUsingMemoryBasedSheerka):
assert created_rule.metadata.action == "Hello world"
# saved in cache
assert len(sheerka.cache_manager.caches[cache_entry].cache) > 0
from_cache = sheerka.cache_manager.get(cache_entry, expected_id)
assert len(sheerka.om.current_cache_manager().caches[cache_entry].cache) > 0
from_cache = sheerka.om.get(cache_entry, expected_id)
assert from_cache.metadata.id == expected_id
assert from_cache.metadata.name == "name"
assert from_cache.metadata.predicate == "True"
assert from_cache.metadata.action_type == action_type
assert from_cache.metadata.action == "Hello world"
sheerka.cache_manager.commit(context)
# the rule is also saved by name
by_name = sheerka.get_rule_by_name("name")
assert by_name.metadata.id == expected_id
assert by_name.metadata.name == "name"
assert by_name.metadata.predicate == "True"
assert by_name.metadata.action_type == action_type
assert by_name.metadata.action == "Hello world"
sheerka.om.commit(context)
# saved in sdp
from_sdp = sheerka.sdp.get(cache_entry, expected_id)
from_sdp = sheerka.om.current_sdp().get(cache_entry, expected_id)
assert from_sdp.metadata.id == expected_id
assert from_sdp.metadata.name == "name"
assert from_sdp.metadata.predicate == "True"
assert from_sdp.metadata.action_type == action_type
assert from_sdp.metadata.action == "Hello world"
by_name = sheerka.om.current_sdp().get(SheerkaRuleManager.RULES_BY_NAME_ENTRY, "name")
assert by_name.metadata.id == expected_id
assert by_name.metadata.name == "name"
assert by_name.metadata.predicate == "True"
assert by_name.metadata.action_type == action_type
assert by_name.metadata.action == "Hello world"
def test_i_can_create_multiple_rules(self):
sheerka, context = self.init_concepts(cache_only=False)
previous_rules_number = len(sheerka.cache_manager.caches[SheerkaRuleManager.FORMAT_RULE_ENTRY].cache)
previous_rules_number = len(
sheerka.om.current_cache_manager().caches[SheerkaRuleManager.FORMAT_RULE_ENTRY].cache)
sheerka.create_new_rule(context, Rule("print", "name1", "True", "Hello world"))
sheerka.create_new_rule(context, Rule("print", "name2", "value() is __EXPLANATION", "list(value())"))
assert len(
sheerka.cache_manager.caches[SheerkaRuleManager.FORMAT_RULE_ENTRY].cache) == 2 + previous_rules_number
sheerka.om.current_cache_manager().caches[
SheerkaRuleManager.FORMAT_RULE_ENTRY].cache) == 2 + previous_rules_number
@pytest.mark.parametrize("text, expected", [
("", FormatAstRawText("")),
@@ -186,7 +202,7 @@ class TestSheerkaRuleManager(TestUsingMemoryBasedSheerka):
assert res[0].concept is None
def test_i_can_compile_predicate_when_python_and_concept(self):
sheerka, context, *concepts = self.init_concepts(Concept("foo bar"), create_new=True)
sheerka, context, *concepts = self.init_test().with_concepts(Concept("foo bar"), create_new=True).unpack()
service = sheerka.services[SheerkaRuleManager.NAME]
text = "foo bar == 5"
ast_ = ast.parse("__C__foo0bar__1001__C__ == 5", "<source>", 'eval')
@@ -207,12 +223,12 @@ class TestSheerkaRuleManager(TestUsingMemoryBasedSheerka):
("cat is an b", ["cat", "b"]),
])
def test_i_can_compile_predicate_when_exact_concept(self, text, expected_variables):
sheerka, context, *concepts = self.init_concepts(
sheerka, context, *concepts = self.init_test().with_concepts(
Concept("x is an y", pre="is_question()", body="isinstance(x, y)").def_var("x").def_var("y"),
Concept("cat"),
Concept("animal"),
create_new=True
)
).unpack()
service = sheerka.services[SheerkaRuleManager.NAME]
expected = concepts[0]
@@ -232,12 +248,12 @@ class TestSheerkaRuleManager(TestUsingMemoryBasedSheerka):
("a cat is an b", ["a cat", "b"]),
])
def test_i_can_compile_predicate_when_sya_node_parser(self, text, expected_variables):
sheerka, context, *concepts = self.init_concepts(
sheerka, context, *concepts = self.init_test().with_concepts(
Concept("x is an y", pre="is_question()", body="isinstance(x, y)").def_var("x").def_var("y"),
Concept("a cat"),
Concept("animal"),
create_new=True
)
).unpack()
service = sheerka.services[SheerkaRuleManager.NAME]
expected = CMV(concepts[0], x=expected_variables[0], y=expected_variables[1])
@@ -251,11 +267,11 @@ class TestSheerkaRuleManager(TestUsingMemoryBasedSheerka):
assert res[0].concept == expected
def test_i_can_compile_predicate_when_bnf_node_parser(self):
sheerka, context, *concepts = self.init_concepts(
sheerka, context, *concepts = self.init_test().with_concepts(
Concept("animal"),
Concept("x is an y", pre="is_question()", definition="('cat'|'bird')=x 'is an' animal").def_var("x"),
create_new=True
)
).unpack()
service = sheerka.services[SheerkaRuleManager.NAME]
expected = concepts[1]
@@ -269,11 +285,11 @@ class TestSheerkaRuleManager(TestUsingMemoryBasedSheerka):
assert res[0].concept == expected
def test_i_can_compile_predicate_when_multiple_choices(self):
sheerka, context, *concepts = self.init_concepts(
sheerka, context, *concepts = self.init_test().with_concepts(
Concept("x is a y", pre="is_question()", body="isinstance(x, y)").def_var("x").def_var("y"),
Concept("x is a y", pre="is_question()", body="isa(x, y)").def_var("x").def_var("y"),
create_new=True
)
).unpack()
service = sheerka.services[SheerkaRuleManager.NAME]
res = service.compile_when(context, "test", "a is a b")
@@ -291,6 +307,34 @@ class TestSheerkaRuleManager(TestUsingMemoryBasedSheerka):
assert sheerka.objvalue(res[1].predicate)[0].concept == CMV(concepts[1], x="a", y="b")
assert res[1].concept == CMV(concepts[1], x="a", y="b")
def test_i_can_get_rule_priorities(self):
sheerka, context, rule_true, rule_false = self.init_test().with_rules(("True", "True"),
("False", "False")).unpack()
sheerka.set_is_greater_than(context, BuiltinConcepts.PRECEDENCE,
rule_true,
rule_false,
RULE_COMPARISON_CONTEXT)
rules_from_cache = sheerka.om.get_all(SheerkaRuleManager.FORMAT_RULE_ENTRY)
assert rules_from_cache[rule_true.id].priority == 2
assert rules_from_cache[rule_false.id].priority == 1
def test_i_can_get_and_retrieve_rules_when_multiple_ontology_layers(self):
sheerka, context, rule_true = self.init_test().with_rules(("true", "True", "True")).unpack()
sheerka.push_ontology(context, "new ontology")
rule_false = sheerka.create_new_rule(context, Rule(ACTION_TYPE_EXEC, "false", "False", "False")).body.body
# All rules are visible
assert sheerka.get_rule_by_id(rule_true.id) == rule_true
assert sheerka.get_rule_by_id(rule_false.id) == rule_false
sheerka.pop_ontology()
assert sheerka.get_rule_by_id(rule_true.id) == rule_true
assert not sheerka.is_known(sheerka.get_rule_by_id(rule_false.id))
# @pytest.mark.skip
# @pytest.mark.parametrize("text, expected", [
# ("cat is an animal", set()),
@@ -337,24 +381,58 @@ class TestSheerkaRuleManagerUsingFileBasedSheerka(TestUsingFileBasedSheerka):
rules[1],
RULE_COMPARISON_CONTEXT)
sheerka.cache_manager.commit(context)
assert len(sheerka.cache_manager.copy(SheerkaRuleManager.FORMAT_RULE_ENTRY)) == len(rules)
sheerka.om.commit(context)
expected_rules_by_id = sheerka.om.get_all(SheerkaRuleManager.FORMAT_RULE_ENTRY)
sheerka = self.get_sheerka() # new instance
assert len(sheerka.cache_manager.copy(SheerkaRuleManager.FORMAT_RULE_ENTRY)) == len(rules)
sheerka = self.new_sheerka_instance(False) # new instance
rules_by_id = sheerka.om.get_all(SheerkaRuleManager.FORMAT_RULE_ENTRY)
# manually update the rules (I need their new priorities)
service = sheerka.services[SheerkaRuleManager.NAME]
rules = [service.format_rule_cache.get(rule_id) for rule_id in service.format_rule_cache]
assert len(rules_by_id) == len(expected_rules_by_id)
# check if the rules are correctly initialized
rules_as_map = {rule.id: rule for rule in rules}
for rule_id in service.format_rule_cache:
actual = service.format_rule_cache.get(rule_id)
expected = rules_as_map[rule_id]
assert actual.metadata.is_compiled == expected.metadata.is_compiled
assert actual.metadata.is_enabled == expected.metadata.is_enabled
assert actual.compiled_action == expected.compiled_action
assert actual.compiled_predicate == expected.compiled_predicate
assert actual.priority is not None
assert actual.priority == expected.priority
for rule_id, rule in rules_by_id.items():
expected = expected_rules_by_id[rule_id]
assert rule.metadata.action_type == expected.metadata.action_type
assert rule.metadata.name == expected.metadata.name
assert rule.metadata.predicate == expected.metadata.predicate
assert rule.metadata.action == expected.metadata.action
assert rule.metadata.id == expected.metadata.id
assert rule.metadata.is_compiled == expected.metadata.is_compiled
assert rule.metadata.is_enabled == expected.metadata.is_enabled
assert rule.compiled_action == expected.compiled_action
assert rule.compiled_predicate == expected.compiled_predicate
assert rule.priority is not None
assert rule.priority == expected.priority
def test_rules_are_still_accessible_after_a_new_ontology_layer(self):
sheerka, context, *rules = self.init_format_rules(
Rule("print", "name1", "True", "Hello world"),
Rule("print", "name2", "value() is __EXPLANATION", "list(value())")
)
sheerka.set_is_greater_than(context, BuiltinConcepts.PRECEDENCE,
rules[0],
rules[1],
RULE_COMPARISON_CONTEXT)
sheerka.om.commit(context)
expected_rules_by_id = sheerka.om.get_all(SheerkaRuleManager.FORMAT_RULE_ENTRY)
sheerka.push_ontology(context, "new ontology")
rules_by_id = sheerka.om.get_all(SheerkaRuleManager.FORMAT_RULE_ENTRY)
assert len(rules_by_id) == len(expected_rules_by_id)
for rule_id, rule in rules_by_id.items():
expected = expected_rules_by_id[rule_id]
assert rule.metadata.action_type == expected.metadata.action_type
assert rule.metadata.name == expected.metadata.name
assert rule.metadata.predicate == expected.metadata.predicate
assert rule.metadata.action == expected.metadata.action
assert rule.metadata.id == expected.metadata.id
assert rule.metadata.is_compiled == expected.metadata.is_compiled
assert rule.metadata.is_enabled == expected.metadata.is_enabled
assert rule.compiled_action == expected.compiled_action
assert rule.compiled_predicate == expected.compiled_predicate
assert rule.priority is not None
assert rule.priority == expected.priority
+86 -16
View File
@@ -1,4 +1,7 @@
import pytest
from core.concept import Concept, ConceptParts
from core.global_symbols import NotFound
from core.sheerka.services.SheerkaVariableManager import SheerkaVariableManager
from tests.TestUsingFileBasedSheerka import TestUsingFileBasedSheerka
@@ -15,10 +18,11 @@ class TestSheerkaVariable(TestUsingMemoryBasedSheerka):
assert res == 1
# I can persist in db
sheerka.cache_manager.commit(context)
sheerka.om.commit(context)
assert sheerka.sdp.exists(SheerkaVariableManager.VARIABLES_ENTRY, "TestSheerkaVariable|my_variable")
loaded = sheerka.sdp.get(SheerkaVariableManager.VARIABLES_ENTRY, "TestSheerkaVariable|my_variable")
assert sheerka.om.current_sdp().exists(SheerkaVariableManager.VARIABLES_ENTRY,
"TestSheerkaVariable|my_variable")
loaded = sheerka.om.current_sdp().get(SheerkaVariableManager.VARIABLES_ENTRY, "TestSheerkaVariable|my_variable")
assert loaded.event_id == context.event.get_digest()
assert loaded.key == "my_variable"
assert loaded.value == 1
@@ -45,10 +49,10 @@ class TestSheerkaVariable(TestUsingMemoryBasedSheerka):
concept = Concept("foo")
sheerka.record_var(context, "TestSheerkaVariable", "my_variable", concept)
assert sheerka.load_var("TestSheerkaVariable", "my_variable") is not None
assert sheerka.load_var("TestSheerkaVariable", "my_variable") == concept
sheerka.delete_var(context, "TestSheerkaVariable", "my_variable")
assert sheerka.load_var("TestSheerkaVariable", "my_variable") is None
assert sheerka.load_var("TestSheerkaVariable", "my_variable") is NotFound
def test_i_can_set_and_get_a_value(self):
sheerka = self.get_sheerka(cache_only=False)
@@ -60,35 +64,101 @@ class TestSheerkaVariable(TestUsingMemoryBasedSheerka):
assert res == "my value"
# I can persist in db
sheerka.cache_manager.commit(context)
sheerka.om.commit(context)
assert sheerka.sdp.exists(SheerkaVariableManager.VARIABLES_ENTRY, "Test_user|my_variable")
loaded = sheerka.sdp.get(SheerkaVariableManager.VARIABLES_ENTRY, "Test_user|my_variable")
assert sheerka.om.current_sdp().exists(SheerkaVariableManager.VARIABLES_ENTRY, "Test_user|my_variable")
loaded = sheerka.om.current_sdp().get(SheerkaVariableManager.VARIABLES_ENTRY, "Test_user|my_variable")
assert loaded.event_id == context.event.get_digest()
assert loaded.key == "my_variable"
assert loaded.value == "my value"
assert loaded.who == "Test_user"
assert loaded.parents is None
def test_i_can_record_and_load_internal_variables(self):
sheerka = self.get_sheerka(cache_only=False)
context = self.get_context(sheerka)
sheerka.record_internal_var(context, "TestSheerkaVariable", "my_variable", 1)
res = sheerka.load_internal_var("TestSheerkaVariable", "my_variable")
assert res == 1
sheerka.record_internal_var(context, "TestSheerkaVariable", "lambda", lambda x: x + 1)
res = sheerka.load_internal_var("TestSheerkaVariable", "lambda")
assert res(10) == 11
def test_i_can_record_and_load_in_multiple_ontology_layers(self):
sheerka = self.get_sheerka(cache_only=False)
context = self.get_context(sheerka)
sheerka.record_var(context, "TestSheerkaVariable", "my_variable", 1)
sheerka.record_internal_var(context, "TestSheerkaVariable", "lambda", lambda x: x + 1)
sheerka.om.commit(context)
sheerka.push_ontology(context, "new ontology")
sheerka.push_ontology(context, "another ontology")
# I can still access to the previous values
assert sheerka.load_var("TestSheerkaVariable", "my_variable") == 1
assert sheerka.load_internal_var("TestSheerkaVariable", "lambda")(10) == 11
# I can modify the current values
sheerka.record_var(context, "TestSheerkaVariable", "my_variable", 2)
sheerka.record_internal_var(context, "TestSheerkaVariable", "lambda", lambda x: x + 2)
sheerka.om.commit(context)
assert sheerka.load_var("TestSheerkaVariable", "my_variable") == 2
assert sheerka.load_internal_var("TestSheerkaVariable", "lambda")(10) == 12
# I can revert back
sheerka.pop_ontology()
assert sheerka.load_var("TestSheerkaVariable", "my_variable") == 1
assert sheerka.load_internal_var("TestSheerkaVariable", "lambda")(10) == 11
sheerka.pop_ontology()
assert sheerka.load_var("TestSheerkaVariable", "my_variable") == 1
assert sheerka.load_internal_var("TestSheerkaVariable", "lambda")(10) == 11
class TestSheerkaVariableUsingFileBasedSdp(TestUsingFileBasedSheerka):
def test_i_can_bound_variables_to_sheerka(self):
sheerka, context = self.init_concepts()
sheerka, context = self.init_test().unpack()
old_value = sheerka.enable_process_return_values
new_value = not old_value
sheerka.record_var(context, "TestSheerkaVariable", "sheerka.enable_process_return_values", new_value)
sheerka.cache_manager.commit(context)
sheerka.record_var(context, sheerka.name, "enable_process_return_values", new_value)
sheerka.om.commit(context)
assert sheerka.enable_process_return_values == new_value
# the modification is persisted upon new Sheerka creation
sheerka = self.get_sheerka()
sheerka = self.new_sheerka_instance(False)
assert sheerka.load_var(sheerka.name, "enable_process_return_values") == new_value
assert sheerka.enable_process_return_values == new_value
# reset old value
sheerka.enable_process_return_values = old_value
sheerka.record_var(context, sheerka.name, "enable_process_return_values", old_value)
def test_i_can_bound_variables_to_sheerka_when_ontology_layer(self):
sheerka, context = self.init_test().unpack()
old_value = sheerka.enable_process_return_values
new_value = not old_value
sheerka.record_var(context, sheerka.name, "enable_process_return_values", new_value)
sheerka.om.commit(context)
# sanity
assert sheerka.enable_process_return_values == new_value
assert sheerka.load_var(sheerka.name, "enable_process_return_values") == new_value
# the modification is seen even when there is an ontology layer
sheerka.push_ontology(context, "new ontology")
sheerka = self.new_sheerka_instance(False)
assert sheerka.load_var(sheerka.name, "enable_process_return_values") == new_value
assert sheerka.enable_process_return_values == new_value
# reset old value
sheerka.record_var(context, sheerka.name, "enable_process_return_values", old_value)
# def test_i_can_get_the_parent_when_modified(self):
# sheerka = self.get_sheerka()
@@ -99,14 +169,14 @@ class TestSheerkaVariableUsingFileBasedSdp(TestUsingFileBasedSheerka):
# res = sheerka.load_var("TestSheerkaVariable", "my_variable")
# assert res == 2
#
# loaded = sheerka.sdp.get(SheerkaVariableManager.VARIABLES_ENTRY, "TestSheerkaVariable.my_variable")
# loaded = sheerka.om.current_sdp().get(SheerkaVariableManager.VARIABLES_ENTRY, "TestSheerkaVariable.my_variable")
# assert loaded.event_id == context.event.get_digest()
# assert loaded.key == "my_variable"
# assert loaded.value == 2
# assert loaded.who == "TestSheerkaVariable"
# assert loaded.parents == ['8c9ada7bf488d84229f6539f76042431638f16d600fe3b7ec7e7161043a40d59']
#
# parent = sheerka.sdp.load_obj(loaded.parents[0])
# parent = sheerka.om.current_sdp().load_obj(loaded.parents[0])
# assert parent.event_id == context.event.get_digest()
# assert parent.key == "my_variable"
# assert parent.value == 1
@@ -120,7 +190,7 @@ class TestSheerkaVariableUsingFileBasedSdp(TestUsingFileBasedSheerka):
# sheerka.record_var(context, "TestSheerkaVariable", "my_variable", 1)
# sheerka.record_var(context, "TestSheerkaVariable", "my_variable", 1)
#
# loaded = sheerka.sdp.get(SheerkaVariableManager.VARIABLES_ENTRY, "TestSheerkaVariable.my_variable")
# loaded = sheerka.om.current_sdp().get(SheerkaVariableManager.VARIABLES_ENTRY, "TestSheerkaVariable.my_variable")
# assert loaded.event_id == context.event.get_digest()
# assert loaded.key == "my_variable"
# assert loaded.value == 1
+178 -58
View File
@@ -1,10 +1,14 @@
import os
import pytest
from conftest import SHEERKA_TEST_FOLDER
from core.builtin_concepts import BuiltinConcepts, ReturnValueConcept, UserInputConcept
from core.builtin_concepts_ids import AllBuiltinConcepts
from core.concept import Concept, PROPERTIES_TO_SERIALIZE, ConceptParts, NotInit
from core.concept import Concept, PROPERTIES_TO_SERIALIZE, ConceptParts, get_concept_attrs
from core.global_symbols import NotInit
from core.sheerka.Sheerka import Sheerka, BASE_NODE_PARSER_CLASS
from core.sheerka.SheerkaOntologyManager import OntologyAlreadyExists
from core.sheerka.services.SheerkaConceptManager import SheerkaConceptManager
from core.tokenizer import Token, TokenKind
@@ -107,7 +111,7 @@ class TestSheerkaUsingMemoryBasedSheerka(TestUsingMemoryBasedSheerka):
assert ret.body == "fake_concept"
def test_i_can_instantiate_a_concept(self):
sheerka, context, concept = self.init_concepts(self.get_default_concept(), create_new=True)
sheerka, context, concept = self.init_test().with_concepts(self.get_default_concept(), create_new=True).unpack()
new = sheerka.new(concept.key, a=10, b="value")
@@ -119,10 +123,10 @@ class TestSheerkaUsingMemoryBasedSheerka(TestUsingMemoryBasedSheerka):
assert new.get_value("b") == "value"
def test_i_can_instantiate_multiple_when_same_key(self):
sheerka, context, *concepts = self.init_concepts(
sheerka, context, *concepts = self.init_test().with_concepts(
Concept("foo", body="foo1"),
Concept("foo", body="foo2"),
create_new=True)
create_new=True).unpack()
# when no id, i get two instances
concepts = sheerka.new("foo")
@@ -141,7 +145,7 @@ class TestSheerkaUsingMemoryBasedSheerka(TestUsingMemoryBasedSheerka):
assert foo2.get_metadata().body == "foo2"
def test_instances_are_different_when_asking_for_new(self):
sheerka, context, concept = self.init_concepts(self.get_default_concept(), create_new=True)
sheerka, context, concept = self.init_test().with_concepts(self.get_default_concept(), create_new=True).unpack()
new1 = sheerka.new(concept.key, a=10, b="value")
new2 = sheerka.new(concept.key, a=10, b="value")
@@ -150,7 +154,7 @@ class TestSheerkaUsingMemoryBasedSheerka(TestUsingMemoryBasedSheerka):
assert id(new1) != id(new2)
def test_new_instance_does_not_impact_each_others(self):
sheerka, context, foo, bar = self.init_concepts("foo", "bar", create_new=True)
sheerka, context, foo, bar = self.init_test().with_concepts("foo", "bar", create_new=True).unpack()
new_foo = sheerka.new("foo")
new_foo.get_metadata().body = "metadata value" # modify metadata
@@ -167,7 +171,8 @@ class TestSheerkaUsingMemoryBasedSheerka(TestUsingMemoryBasedSheerka):
assert new_foo.get_compiled() != foo.get_compiled()
def test_i_get_the_same_instance_when_is_unique_is_true(self):
sheerka, context, concept = self.init_concepts(Concept(name="unique", is_unique=True), create_new=True)
sheerka, context, concept = self.init_test(). \
with_concepts(Concept(name="unique", is_unique=True), create_new=True).unpack()
new1 = sheerka.new(concept.key)
new2 = sheerka.new(concept.key, a=10, b="value") # not that variables are simply discareded
@@ -176,10 +181,8 @@ class TestSheerkaUsingMemoryBasedSheerka(TestUsingMemoryBasedSheerka):
assert id(new1) == id(new2)
def test_values_are_reset_when_asking_for_a_new_instance(self):
sheerka, context, template = self.init_concepts(
Concept("foo", body="'foo body'"),
create_new=True,
eval_body=True)
sheerka, context, template = self.init_test(eval_body=True).with_concepts(Concept("foo", body="'foo body'"),
create_new=True).unpack()
sheerka.evaluate_concept(context, sheerka.get_by_id(template.id))
assert template.get_metadata().is_evaluated
@@ -202,10 +205,9 @@ class TestSheerkaUsingMemoryBasedSheerka(TestUsingMemoryBasedSheerka):
assert new.body == ('key', 'fake_concept')
def test_i_cannot_instantiate_with_invalid_id(self):
sheerka, context, *concepts = self.init_concepts(
Concept("foo", body="foo1"),
Concept("foo", body="foo2"),
create_new=True)
sheerka, context, *concepts = self.init_test().with_concepts(Concept("foo", body="foo1"),
Concept("foo", body="foo2"),
create_new=True).unpack()
new = sheerka.new(("foo", "invalid_id"))
@@ -213,10 +215,9 @@ class TestSheerkaUsingMemoryBasedSheerka(TestUsingMemoryBasedSheerka):
assert new.body == [('key', 'foo'), ('id', 'invalid_id')]
def test_i_cannot_instantiate_with_invalid_key(self):
sheerka, context, *concepts = self.init_concepts(
Concept("foo", body="foo1"),
Concept("foo", body="foo2"),
create_new=True)
sheerka, context, *concepts = self.init_test().with_concepts(Concept("foo", body="foo1"),
Concept("foo", body="foo2"),
create_new=True).unpack()
new = sheerka.new(("invalid_key", "1001"))
@@ -224,9 +225,8 @@ class TestSheerkaUsingMemoryBasedSheerka(TestUsingMemoryBasedSheerka):
assert new.body == [('key', 'invalid_key'), ('id', '1001')]
def test_concept_id_is_irrelevant_when_only_one_concept(self):
sheerka, context, *concepts = self.init_concepts(
Concept("foo", body="foo1"),
create_new=True)
sheerka, context, *concepts = self.init_test().with_concepts(Concept("foo", body="foo1"),
create_new=True).unpack()
new = sheerka.new(("foo", "invalid_id"))
@@ -234,7 +234,8 @@ class TestSheerkaUsingMemoryBasedSheerka(TestUsingMemoryBasedSheerka):
assert new.get_metadata().body == "foo1"
def test_i_cannot_instantiate_when_properties_are_not_recognized(self):
sheerka, context, concept = self.init_concepts(self.get_default_concept(), create_new=True)
sheerka, context, concept = self.init_test().with_concepts(self.get_default_concept(),
create_new=True).unpack()
new = sheerka.new(concept.key, a=10, c="value")
@@ -326,46 +327,138 @@ class TestSheerkaUsingMemoryBasedSheerka(TestUsingMemoryBasedSheerka):
assert sheerka.resolve(concept) == real_expected
def test_i_can_resolve_when_searching_by_definition(self):
sheerka, context, plus = self.init_concepts(
self.from_def_concept("plus", "a plus b", ["a", "b"]),
create_new=True
)
sheerka, context, plus = self.init_test().with_concepts(self.from_def_concept("plus", "a plus b", ["a", "b"]),
create_new=True).unpack()
assert sheerka.resolve("a plus b") == plus
def test_i_can_reset_global_concept_definition_when_adding_and_removing_layers(self):
sheerka, context, foo = self.init_concepts(Concept("foo").def_var("a").def_var("b"))
assert get_concept_attrs(foo) == ["a", "b"]
sheerka.push_ontology(context, "another ontology")
sheerka.modify_concept(context, foo, to_add={"variables": {"c": None}}, to_remove={"variables": ["b"]})
assert get_concept_attrs(foo) == ["a", "c"]
sheerka.pop_ontology()
assert get_concept_attrs(foo) == ["a", "b"]
def test_i_can_manage_concepts_ids_on_multiple_ontology_layers(self):
sheerka, context = self.init_concepts()
res = sheerka.create_new_concept(context, Concept("foo"))
assert res.body.body.id == "1001"
sheerka.push_ontology(context, "new ontology")
res = sheerka.create_new_concept(context, Concept("bar"))
assert res.body.body.id == "1002"
res = sheerka.create_new_concept(context, Concept("baz"))
assert res.body.body.id == "1003"
sheerka.pop_ontology()
res = sheerka.create_new_concept(context, Concept("baz"))
assert res.body.body.id == "1002"
def test_i_can_add_ontology(self):
sheerka, context = self.init_test().unpack()
# Create an ontology an set some values
res = sheerka.push_ontology(context, "new ontology")
assert res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
foo = sheerka.create_new_concept(context, Concept("foo").def_var("a").def_var("b")).body.body
sheerka.locals = {"key1": "value1"}
# sanity check
assert sheerka.get_by_name("foo") == foo
assert not sheerka.is_known(sheerka.get_by_name("bar"))
assert sheerka.locals == {"key1": "value1"}
assert get_concept_attrs(foo) == ["a", "b"]
# record the ontology
ontology = sheerka.get_ontology(context)
sheerka.pop_ontology()
# Create another ontology with some other values
sheerka.push_ontology(context, "another ontology")
foo2 = sheerka.create_new_concept(context, Concept("foo").def_var("a").def_var("b").def_var("c")).body.body
bar = sheerka.create_new_concept(context, Concept("bar")).body.body
sheerka.locals = {"key2": "value2"}
# sanity check
assert sheerka.get_by_name("foo") == foo2
assert sheerka.get_by_name("bar") == bar
assert sheerka.locals == {"key2": "value2"}
assert get_concept_attrs(foo) == ["a", "b", "c"]
# put pack the previous ontology
sheerka.add_ontology(context, ontology)
assert sheerka.get_by_name("foo") == foo # not [foo, foo2], foo2 is not seen !!!
assert sheerka.get_by_name("bar") == bar
assert sheerka.locals == {"key1": "value1"}
assert get_concept_attrs(foo) == ["a", "b"]
# sanity check
sheerka.pop_ontology()
assert sheerka.get_by_name("foo") == foo2
assert sheerka.get_by_name("bar") == bar
assert sheerka.locals == {"key2": "value2"}
assert get_concept_attrs(foo) == ["a", "b", "c"]
def test_adding_the_same_ontology_twice_has_no_effect(self):
sheerka, context = self.init_test().unpack()
sheerka.push_ontology(context, "#unit_test#")
assert len(sheerka.om.ontologies) == 2
def test_i_cannot_add_an_ontology_that_already_exists(self):
sheerka, context = self.init_test().unpack()
sheerka.push_ontology(context, "new ontology")
res = sheerka.push_ontology(context, "#unit_test#")
assert not res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.ONTOLOGY_ALREADY_DEFINED)
class TestSheerkaUsingFileBasedSheerka(TestUsingFileBasedSheerka):
def test_root_folder_is_created_after_initialization(self):
return_value = Sheerka().initialize(self.root_folder)
return_value = Sheerka().initialize(SHEERKA_TEST_FOLDER)
assert return_value.status, "initialisation should be successful"
assert os.path.exists(self.root_folder), "init folder should be created"
assert os.path.exists(SHEERKA_TEST_FOLDER), "init folder should be created"
def test_builtin_concepts_are_initialized(self):
sheerka = self.get_sheerka()
sheerka, context = self.init_test().unpack()
service = sheerka.services[SheerkaConceptManager.NAME]
for concept_name in AllBuiltinConcepts:
assert service.has_key(str(concept_name))
assert sheerka.sdp.get(service.CONCEPTS_BY_KEY_ENTRY, str(concept_name)) is not None
# check that the concept is already in cache
assert sheerka.om.ontologies[-1].cache_manager.has(service.CONCEPTS_BY_KEY_ENTRY, concept_name)
# check that we can access it
assert sheerka.om.get(service.CONCEPTS_BY_KEY_ENTRY, concept_name) is not None
# I can get back data from the sdp when the cache is empty
sheerka.cache_manager.clear()
sheerka.push_ontology(context, 'new ontology')
# caches are empty
assert not service.has_id("1")
assert not service.has_key(str(BuiltinConcepts.SHEERKA))
assert not service.has_key(BuiltinConcepts.SHEERKA)
assert sheerka.get_by_id("1") == sheerka # use sdp
# assert sheerka.has_key(str(BuiltinConcepts.SHEERKA)) # auto update the other caches
def test_builtin_concepts_can_be_updated(self):
sheerka = self.get_sheerka()
service = sheerka.services[SheerkaConceptManager.NAME]
before_parsing = sheerka.get_by_key(BuiltinConcepts.BEFORE_PARSING)
before_parsing.get_metadata().desc = "I have a description"
before_parsing.get_metadata().full_serialization = True
with sheerka.sdp.get_transaction("Test") as transac:
with sheerka.om.current_sdp().get_transaction("Test") as transac:
transac.add(service.CONCEPTS_BY_KEY_ENTRY, before_parsing.key, before_parsing, use_ref=True)
sheerka = self.get_sheerka() # another fresh new instance
@@ -374,8 +467,8 @@ class TestSheerkaUsingFileBasedSheerka(TestUsingFileBasedSheerka):
assert before_parsing.get_metadata().desc == "I have a description"
def test_i_first_look_in_local_cache(self):
sheerka, context, concept = self.init_concepts("foo", create_new=True)
sheerka.cache_manager.commit(context)
sheerka, context, concept = self.init_test().with_concepts("foo", create_new=True).unpack()
sheerka.om.commit(context)
sheerka.get_by_key(concept.key).new_property = "I have modified the concept in cache"
@@ -385,7 +478,7 @@ class TestSheerkaUsingFileBasedSheerka(TestUsingFileBasedSheerka):
assert from_cache.new_property == "I have modified the concept in cache"
# sdp instance is not modified
sheerka.cache_manager.clear()
sheerka.om.clear()
from_sdp = sheerka.get_by_key(concept.key)
assert from_sdp is not None
assert from_sdp.key == concept.key
@@ -394,42 +487,42 @@ class TestSheerkaUsingFileBasedSheerka(TestUsingFileBasedSheerka):
def test_i_can_retrieve_from_sdp_when_cache_is_reset(self):
sheerka, context, concept = self.init_concepts(Concept("foo", body="1"))
service = sheerka.services[SheerkaConceptManager.NAME]
sheerka.cache_manager.commit(context)
sheerka.om.commit(context)
sheerka.cache_manager.clear()
sheerka.om.clear()
sheerka.get_by_key("foo")
assert service.has_key("foo")
# It's also updated when sdp returns more than one element
concept2 = Concept("foo", body="2")
sheerka.create_new_concept(context, concept2)
sheerka.cache_manager.commit(context)
sheerka.om.commit(context)
sheerka.cache_manager.clear()
sheerka.om.clear()
assert len(sheerka.get_by_key("foo")) == 2
assert service.has_key("foo")
# updated when by_id
sheerka.cache_manager.clear()
sheerka.om.clear()
assert sheerka.get_by_id("1001") == concept
assert service.has_id("1001")
sheerka.cache_manager.clear()
sheerka.om.clear()
assert sheerka.get_by_name("foo") == [concept, concept2]
assert service.has_name("foo")
sheerka.cache_manager.clear()
sheerka.om.clear()
assert sheerka.get_by_hash(concept.get_definition_hash()) == concept
assert service.has_hash(concept.get_definition_hash())
def test_get_by_key_retrieve_all_elements(self):
sheerka, context, *concepts = self.init_concepts(
sheerka, context, *concepts = self.init_test().with_concepts(
Concept("foo", body="1"),
Concept("foo", body="2"),
create_new=True)
sheerka.cache_manager.commit(context)
create_new=True).unpack()
sheerka.om.commit(context)
sheerka.cache_manager.clear()
sheerka.om.clear()
sheerka.get_by_key("foo", "1001") # I ask only for the one with id = "1001"
# but the two keys are returned
@@ -439,28 +532,55 @@ class TestSheerkaUsingFileBasedSheerka(TestUsingFileBasedSheerka):
assert concepts[1].id == "1002"
def test_concept_node_parsing_is_initialized_at_startup(self):
sheerka, context, foo, bar, baz = self.init_concepts(
sheerka, context, foo, bar, baz = self.init_test().with_concepts(
"foo",
"bar",
Concept("baz", definition="foo"),
create_new=True)
sheerka.cache_manager.commit(context)
create_new=True).unpack()
sheerka.om.commit(context)
assert sheerka.cache_manager.copy(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == {
assert sheerka.om.copy(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == {
'bar': ['1002'],
'c:|1001:': ['1003'],
'foo': ['1001']}
assert sheerka.cache_manager.copy(sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == {
assert sheerka.om.copy(sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == {
'bar': ['1002'],
'foo': ['1001', '1003']
}
sheerka = self.get_sheerka() # another instance
assert sheerka.cache_manager.copy(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == {
assert sheerka.om.copy(sheerka.CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == {
'bar': ['1002'],
'c:|1001:': ['1003'],
'foo': ['1001']}
assert sheerka.cache_manager.copy(sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == {
assert sheerka.om.copy(sheerka.RESOLVED_CONCEPTS_BY_FIRST_KEYWORD_ENTRY) == {
'bar': ['1002'],
'foo': ['1001', '1003']
}
def test_i_can_remember_ontologies_when_sheerka_is_recreated(self):
sheerka, context = self.init_test().unpack()
sheerka.push_ontology(context, "to remove")
sheerka.pop_ontology()
sheerka.push_ontology(context, "new ontology")
sheerka.push_ontology(context, "another ontology")
# sanity check
ontologies_names = [o.name for o in sheerka.om.ontologies]
assert ontologies_names == ['another ontology', 'new ontology', '#unit_test#', '__default__']
# get new instance
sheerka = self.new_sheerka_instance(False)
# make sure that ontologies are recreated
ontologies_names = [o.name for o in sheerka.om.ontologies]
assert ontologies_names == ['another ontology', 'new ontology', '#unit_test#', '__default__']
def test_adding_the_same_ontology_twice_has_no_effect(self):
sheerka, context = self.init_test().unpack()
sheerka = self.new_sheerka_instance(False) # new instance that remembers the top layer ontology
sheerka.push_ontology(context, "#unit_test#")
assert len(sheerka.om.ontologies) == 2
+52 -46
View File
@@ -3,6 +3,7 @@ import types
import pytest
from core.builtin_concepts import BuiltinConcepts
from core.sheerka.ExecutionContext import ExecutionContext
from core.sheerka.SheerkaOntologyManager import SheerkaOntologyManager
from core.sheerka.services.SheerkaResultManager import SheerkaResultConcept
from sdp.sheerkaDataProvider import Event
@@ -10,26 +11,27 @@ from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
class TestSheerkaResultManager(TestUsingMemoryBasedSheerka):
io_cache = None
@classmethod
def setup_class(cls):
sheerka = cls().get_sheerka(cache_only=False)
sheerka = cls().get_sheerka(cache_only=False, ontology="#TestSheerkaResultManager#")
sheerka.save_execution_context = True
cls.root_ontology_name = "#TestSheerkaResultManager#"
@classmethod
def teardown_class(cls):
sheerka = cls().get_sheerka()
sheerka.save_execution_context = False
cls.sheerka.pop_ontology()
cls.root_ontology_name = SheerkaOntologyManager.ROOT_ONTOLOGY_NAME
def init_test(self):
sheerka, context = self.init_concepts()
def init_service(self):
sheerka, context = self.init_test().unpack()
service = sheerka.services[SheerkaResultConcept.NAME]
return sheerka, context, service
def test_i_can_record_execution_contexts(self):
sheerka, context, service = self.init_test()
sheerka, context, service = self.init_service()
service.test_only_reset()
sheerka.evaluate_user_input("foo")
@@ -40,7 +42,7 @@ class TestSheerkaResultManager(TestUsingMemoryBasedSheerka):
assert execution_context.desc == "Evaluating 'foo'"
executions_contexts_in_db = sheerka.sdp.load_result(event_id)
executions_contexts_in_db = sheerka.om.current_sdp().load_result(event_id)
assert executions_contexts_in_db is not None
assert executions_contexts_in_db.desc == "Evaluating 'foo'"
@@ -48,7 +50,7 @@ class TestSheerkaResultManager(TestUsingMemoryBasedSheerka):
assert service.last_execution.desc == "Evaluating 'foo'"
def test_i_can_get_the_result_by_digest_using_cache(self):
sheerka, context, service = self.init_test()
sheerka, context, service = self.init_service()
sheerka.evaluate_user_input("def concept one as 1")
digest = service.last_execution.event.get_digest()
@@ -77,10 +79,10 @@ class TestSheerkaResultManager(TestUsingMemoryBasedSheerka):
assert list(res.body) == previous_results
def test_i_can_get_result_by_digest_using_db(self):
sheerka, context, service = self.init_test()
sheerka, context, service = self.init_service()
sheerka.evaluate_user_input("def concept one as 1")
digest = service.last_execution.event.get_digest()
service.reset()
service.test_only_reset()
res = sheerka.get_results_by_digest(context, digest)
@@ -106,18 +108,18 @@ class TestSheerkaResultManager(TestUsingMemoryBasedSheerka):
assert list(res.body) == previous_results
def test_i_cannot_get_result_by_digest_if_the_digest_does_not_exist(self):
sheerka, context = self.init_concepts()
sheerka, context = self.init_test().unpack()
res = sheerka.get_results_by_digest(context, "fake digest")
assert sheerka.isinstance(res, BuiltinConcepts.NOT_FOUND)
assert res.body == {'digest': 'fake digest'}
def test_i_cannot_get_results_if_no_previous_digest(self):
sheerka, context = self.init_concepts()
sheerka, context = self.init_test().unpack()
assert sheerka.get_results(context) is None
def test_i_can_get_the_result_by_command_name(self):
sheerka, context, service = self.init_test()
sheerka, context, service = self.init_service()
sheerka.evaluate_user_input("def concept one as 1")
digest = service.last_execution.event.get_digest()
@@ -130,16 +132,16 @@ class TestSheerkaResultManager(TestUsingMemoryBasedSheerka):
assert isinstance(res.body, types.GeneratorType)
def test_i_can_get_the_result_by_command_name_using_db(self):
sheerka, context, service = self.init_test()
sheerka, context, service = self.init_service()
sheerka.evaluate_user_input("def concept one as 1")
sheerka.evaluate_user_input("one") # another command
service.reset()
service.test_only_reset()
res = sheerka.get_results_by_command(context, "def concept")
assert res.command == "def concept one as 1"
def test_i_can_get_the_result_by_command_when_not_in_the_same_page_size(self):
sheerka, context, service = self.init_test()
sheerka, context, service = self.init_service()
sheerka.evaluate_user_input("def concept one as 1")
sheerka.evaluate_user_input("one")
@@ -152,14 +154,17 @@ class TestSheerkaResultManager(TestUsingMemoryBasedSheerka):
assert res.command == "def concept one as 1"
def test_i_cannot_get_results_from_command_if_the_command_does_not_exist(self):
sheerka, context = self.init_concepts()
sheerka, context, service = self.init_service()
service.test_only_reset()
res = sheerka.get_results_by_command(context, "def concept")
assert sheerka.isinstance(res, BuiltinConcepts.NOT_FOUND)
assert res.body == {'command': 'def concept'}
def test_i_cannot_get_result_from_command_if_the_command_does_not_exists_multiple_pages(self):
sheerka, context, service = self.init_test()
sheerka, context, service = self.init_service()
service.test_only_reset()
sheerka.evaluate_user_input("def concept one as 1")
sheerka.evaluate_user_input("one")
@@ -172,7 +177,7 @@ class TestSheerkaResultManager(TestUsingMemoryBasedSheerka):
assert res.body == {'command': 'fake command'}
def test_i_can_get_last_results(self):
sheerka, context, service = self.init_test()
sheerka, context, service = self.init_service()
sheerka.evaluate_user_input("def concept one as 1")
sheerka.evaluate_user_input("one")
@@ -182,21 +187,21 @@ class TestSheerkaResultManager(TestUsingMemoryBasedSheerka):
assert res.command == "one"
def test_i_can_get_last_results_using_db(self):
sheerka, context, service = self.init_test()
sheerka, context, service = self.init_service()
sheerka.evaluate_user_input("def concept one as 1")
sheerka.evaluate_user_input("one")
service.reset()
service.test_only_reset()
res = sheerka.get_last_results(context)
assert sheerka.isinstance(res, BuiltinConcepts.EXPLANATION)
assert res.command == "one"
def test_i_can_get_last_results_when_event_with_no_result(self):
sheerka, context = self.init_concepts()
sheerka, context = self.init_test().unpack()
sheerka.sdp.save_event(Event("event 1"))
sheerka.sdp.save_event(Event("event 2"))
sheerka.sdp.save_event(Event("event 3"))
sheerka.om.save_event(Event("event 1"))
sheerka.om.save_event(Event("event 2"))
sheerka.om.save_event(Event("event 3"))
sheerka.evaluate_user_input("def concept one as 1")
res = sheerka.get_last_results(context)
@@ -204,18 +209,20 @@ class TestSheerkaResultManager(TestUsingMemoryBasedSheerka):
assert res.command == "def concept one as 1"
def test_i_cannot_get_last_results_when_no_result(self):
sheerka, context = self.init_concepts()
sheerka, context, service = self.init_service()
service.test_only_reset()
res = sheerka.get_last_results(context)
assert sheerka.isinstance(res, BuiltinConcepts.NOT_FOUND)
assert res.body == {'query': 'last'}
def test_i_cannot_get_last_results_when_only_events(self):
sheerka, context = self.init_concepts()
sheerka, context, service = self.init_service()
service.test_only_reset()
sheerka.sdp.save_event(Event("event 1"))
sheerka.sdp.save_event(Event("event 2"))
sheerka.sdp.save_event(Event("event 3"))
sheerka.om.save_event(Event("event 1"))
sheerka.om.save_event(Event("event 2"))
sheerka.om.save_event(Event("event 3"))
res = sheerka.get_last_results(context)
assert sheerka.isinstance(res, BuiltinConcepts.NOT_FOUND)
@@ -227,7 +234,7 @@ class TestSheerkaResultManager(TestUsingMemoryBasedSheerka):
{"desc": "Evaluating 'def concept one as 1'", "id": 0}
])
def test_i_can_get_last_results_using_kwarg(self, kwargs):
sheerka, context, service = self.init_test()
sheerka, context, service = self.init_service()
sheerka.evaluate_user_input("def concept one as 1")
ExecutionContext.ids.clear()
@@ -244,7 +251,7 @@ class TestSheerkaResultManager(TestUsingMemoryBasedSheerka):
("'def concept one as 1' in desc and id == 0", {"desc": "Evaluating 'def concept one as 1'", "id": 0})
])
def test_i_can_get_last_results_using_filter(self, predicate, expected):
sheerka, context, service = self.init_test()
sheerka, context, service = self.init_service()
sheerka.evaluate_user_input("def concept one as 1")
ExecutionContext.ids.clear()
@@ -261,7 +268,7 @@ class TestSheerkaResultManager(TestUsingMemoryBasedSheerka):
("'def concept one as 1' in desc and id == 0", {"desc": "Evaluating 'def concept one as 1'", "id": 0})
])
def test_i_can_get_last_results_using_the_first_argument_to_filter(self, predicate, expected):
sheerka, context, service = self.init_test()
sheerka, context, service = self.init_service()
sheerka.evaluate_user_input("def concept one as 1")
ExecutionContext.ids.clear()
@@ -278,7 +285,7 @@ class TestSheerkaResultManager(TestUsingMemoryBasedSheerka):
{"desc": "Evaluating 'def concept one as 1'", "id": 0}
])
def test_i_can_get_results_using_kwarg(self, kwargs):
sheerka, context, service = self.init_test()
sheerka, context, service = self.init_service()
sheerka.evaluate_user_input("def concept one as 1")
ExecutionContext.ids.clear()
sheerka.get_last_results(context)
@@ -296,7 +303,7 @@ class TestSheerkaResultManager(TestUsingMemoryBasedSheerka):
("'def concept one as 1' in desc and id == 0", {"desc": "Evaluating 'def concept one as 1'", "id": 0})
])
def test_i_can_get_results_using_filter(self, predicate, expected):
sheerka, context, service = self.init_test()
sheerka, context, service = self.init_service()
sheerka.evaluate_user_input("def concept one as 1")
ExecutionContext.ids.clear()
sheerka.get_last_results(context)
@@ -314,7 +321,7 @@ class TestSheerkaResultManager(TestUsingMemoryBasedSheerka):
("'def concept one as 1' in desc and id == 0", {"desc": "Evaluating 'def concept one as 1'", "id": 0})
])
def test_i_can_get_results_using_the_first_argument_to_filter(self, predicate, expected):
sheerka, context, service = self.init_test()
sheerka, context, service = self.init_service()
sheerka.evaluate_user_input("def concept one as 1")
ExecutionContext.ids.clear()
sheerka.get_last_results(context)
@@ -332,33 +339,32 @@ class TestSheerkaResultManager(TestUsingMemoryBasedSheerka):
SheerkaResultConcept.get_predicate(**predicate)
def test_i_can_get_last_return_value(self):
sheerka, context, service = self.init_test()
sheerka, context, service = self.init_service()
sheerka.evaluate_user_input("def concept one as 1")
ret = sheerka.last_ret(context)
ret = sheerka.get_last_return_value(context)
assert sheerka.isinstance(ret[0].body, BuiltinConcepts.NEW_CONCEPT)
sheerka.evaluate_user_input("eval one")
ret = sheerka.last_ret(context)
ret = sheerka.get_last_return_value(context)
assert ret[0].body == 1
def test_i_can_track_new_concept(self):
sheerka, context, service = self.init_test()
sheerka, context, service = self.init_service()
res = sheerka.evaluate_user_input("def concept one as 1")
new_concept = res[0].body.body
assert sheerka.last_created_concept(context) == new_concept
assert sheerka.get_last_created_concept(context) == new_concept
assert service.last_created_concept_id == new_concept.id
def test_last_created_concept_is_recorded(self):
sheerka, context, service = self.init_test()
sheerka, context, service = self.init_service()
res = sheerka.evaluate_user_input("def concept one as 1")
new_concept = res[0].body.body
service.reset()
service.test_only_reset()
service.initialize_deferred(context, False)
assert service.last_created_concept_id == new_concept.id
assert sheerka.last_created_concept(context) == new_concept
assert sheerka.get_last_created_concept(context) == new_concept
+1 -1
View File
@@ -252,7 +252,7 @@ class TestSheerkaExecuteEvaluators(TestUsingMemoryBasedSheerka):
def teardown_class(cls):
# At the end of the tests, sheerka singleton instance will be corrupted
# Ask for a new one
TestUsingMemoryBasedSheerka.singleton_instance = None
TestUsingMemoryBasedSheerka.sheerka = None
def test_i_can_get_evaluators_when_context_is_not_altered(self):
sheerka, context = self.init_concepts()
+1 -1
View File
@@ -140,7 +140,7 @@ class TestSheerkaExecuteParsers(TestUsingMemoryBasedSheerka):
def teardown_class(cls):
# At the end of the tests, sheerka singleton instance will be corrupted
# Ask for a new one
TestUsingMemoryBasedSheerka.singleton_instance = None
TestUsingMemoryBasedSheerka.sheerka = None
def test_i_can_get_parser_when_context_is_not_altered(self):
sheerka, context = self.init_concepts()
File diff suppressed because it is too large Load Diff
+21 -1
View File
@@ -2,7 +2,8 @@ from dataclasses import dataclass
import pytest
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept, ConceptParts
from core.concept import Concept, ConceptParts, freeze_concept_attrs
from core.simple_debug import my_debug
from printer.Formatter import Formatter, BraceToken
from printer.SheerkaPrinter import FormatInstructions
@@ -154,10 +155,13 @@ class TestSheerkaPrinter(TestUsingMemoryBasedSheerka):
# for all obj of a given type
sheerka = self.get_sheerka()
sheerka.printer_handler.reset()
foo = Concept("foo a b").def_var("a").def_var("b").init_key()
foo.set_value("a", "value a").set_value("b", "value b")
foo.set_value(ConceptParts.BODY, "body")
sheerka.set_id_if_needed(foo, False)
freeze_concept_attrs(foo)
sheerka.printer_handler.register_format_l(foo, "DEFAULT:{id}-{name}-{key}-{body}-{a}-{b}")
@@ -170,10 +174,13 @@ class TestSheerkaPrinter(TestUsingMemoryBasedSheerka):
# for all obj of a given type in the current print call
sheerka = self.get_sheerka()
sheerka.printer_handler.reset()
foo = Concept("foo a b").def_var("a").def_var("b").init_key()
foo.set_value("a", "value a").set_value("b", "value b")
foo.set_value(ConceptParts.BODY, "body")
sheerka.set_id_if_needed(foo, False)
freeze_concept_attrs(foo)
sheerka.printer_handler.register_format_l(foo, "DEFAULT:{id}-{name}-{key}-{body}-{a}-{b}")
context_instructions = FormatInstructions().set_format_l(foo, "CONTEXT:{id}-{name}-{key}-{body}-{a}-{b}")
@@ -187,10 +194,13 @@ class TestSheerkaPrinter(TestUsingMemoryBasedSheerka):
# for the item only
sheerka = self.get_sheerka()
sheerka.printer_handler.reset()
foo = Concept("foo a b").def_var("a").def_var("b").init_key()
foo.set_value("a", "value a").set_value("b", "value b")
foo.set_value(ConceptParts.BODY, "body")
sheerka.set_id_if_needed(foo, False)
freeze_concept_attrs(foo)
sheerka.printer_handler.register_format_l(foo, "{id}-{name}-{key}-{body}-{a}-{b}")
context_instructions = FormatInstructions().set_format_l(foo, "CONTEXT:{id}-{name}-{key}-{body}-{a}-{b}")
@@ -242,6 +252,8 @@ class TestSheerkaPrinter(TestUsingMemoryBasedSheerka):
def test_i_can_format_d_concepts_using_default_definition(self, capsys):
sheerka, context, foo = self.init_concepts(Concept("foo a b").def_var("a").def_var("b"))
sheerka.printer_handler.reset()
foo_1 = sheerka.new(foo.key, a="value a", b="value b")
foo_2 = sheerka.new(foo.key, a="value c", b="value d")
lst = [foo_1, foo_2]
@@ -259,6 +271,8 @@ b: DEFAULT:'value d'
def test_i_can_format_d_concepts_using_context_definition(self, capsys):
sheerka, context, foo = self.init_concepts(Concept("foo a b").def_var("a").def_var("b"))
sheerka.printer_handler.reset()
foo_1 = sheerka.new(foo.key, a="value a", b="value b")
foo_2 = sheerka.new(foo.key, a="value c", b="value d")
lst = [foo_1, foo_2]
@@ -277,6 +291,8 @@ b: CONTEXT:'value d'
def test_i_can_format_d_concepts_using_item_definition(self, capsys):
sheerka, context, foo = self.init_concepts(Concept("foo a b").def_var("a").def_var("b"))
sheerka.printer_handler.reset()
item_instructions = FormatInstructions().set_format_d(foo, {"a": "ITEM:{a}", "b": "ITEM:{b}"})
foo.set_format_instructions(item_instructions)
foo_1 = sheerka.new(foo.key, a="value a", b="value b")
@@ -371,6 +387,8 @@ self: (1001)foo a b
def test_i_can_format_d_when_dictionary(self, capsys):
sheerka, context, foo = self.init_concepts(Concept("foo a b").def_var("a").def_var("b"))
sheerka.printer_handler.reset()
dict_value = {
"a": "value a",
"beta": {"b1": 10, "b2": Obj("10", 15), "b3": ["items", "in", "a", "list"]},
@@ -436,6 +454,8 @@ bar: *name 'bar' is not defined*
])
def test_i_can_concat_print_instruction_and_register_format(self, capsys, template, expected):
sheerka = self.get_sheerka()
sheerka.printer_handler.reset()
foo = Obj("value a", "value b")
sheerka.printer_handler.register_format_l("tests.core.test_sheerka_printer.Obj", "{a}")
+10 -2
View File
@@ -3,7 +3,9 @@ from dataclasses import dataclass
import core.utils
import pytest
from core.builtin_concepts import BuiltinConcepts
from core.builtin_helpers import evaluate_expression
from core.concept import Concept
from core.global_symbols import NotFound, NotInit, Removed
from core.tokenizer import Token, TokenKind, Tokenizer, Keywords
@@ -306,7 +308,7 @@ def test_i_can_make_unique():
("a.prop2.bar", {"a": Obj2(None, {'foo': 'dict-first', 'bar': 'dict-second'})}, 'dict-second'),
])
def test_i_can_evaluate_expression(expression, bag, expected):
assert core.utils.evaluate_expression(expression, bag) == expected
assert evaluate_expression(expression, bag) == expected
@pytest.mark.parametrize("expression, bag, expected_error, prop_name", [
@@ -315,7 +317,7 @@ def test_i_can_evaluate_expression(expression, bag, expected):
])
def test_i_cannot_evaluate_expression(expression, bag, expected_error, prop_name):
with pytest.raises(expected_error) as e:
core.utils.evaluate_expression(expression, bag)
evaluate_expression(expression, bag)
assert e.value.args == (prop_name,)
@@ -417,3 +419,9 @@ def test_i_can_deep_copy_a_concept():
concept_props = sorted(list(concept.get_prop(BuiltinConcepts.ISA)), key=lambda o: o.id)
for copied_prop, concept_prop in zip(copied_props, concept_props):
check_are_the_same(copied_prop, concept_prop)
def test_i_can_deep_copy_a_custom_type():
assert core.utils.sheerka_deepcopy(NotInit) is NotInit
assert core.utils.sheerka_deepcopy(NotFound) is NotFound
assert core.utils.sheerka_deepcopy(Removed) is Removed