Fixed #100 : SheerkaAdmin: Add builtins() command

Fixed #99 : SheerkaQueryManager: I can manage contains predicate when filtering objects
Fixed #97 : ERROR: list indices must be integers or slices, not Concept
Fixed #96 : SequenceNodeParser: SequenceNodeParser must correctly handle concept definition
Fixed #95 : ResolveAmbiguity must not remove concepts that do not require evaluation
Fixed #94 : Concepts with the same key are lost when new ontology
Fixed #93 : Introduce BuiltinConcepts.EVAL_GLOBAL_TRUTH_REQUESTED
Fixed #92 : ExpressionParser: Implement compile_disjunctions()
Fixed #91 : Implement get_concepts_complexity(context, concepts, concept_parts)
Fixed #90 : ResolveAmbiguity : where predicate is not used to resolve ambiguity
Fixed #89 : ResolveAmbiguityEvaluator: Concepts embedded in ConceptNode are not resolved
Fixed #88: SyaNodeParser: Parse multiple parameters when some of the are not recognized
Fixed #87: SyaNodeParser : Parse the multiple parameters
This commit is contained in:
2021-07-31 08:52:00 +02:00
parent 7dcaa9c111
commit e69745adc8
70 changed files with 1561 additions and 455 deletions
+20 -16
View File
@@ -7,9 +7,9 @@ from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
class TestSheerkaConceptsAlgebra(TestUsingMemoryBasedSheerka):
def test_i_can_add_concepts(self):
sheerka, context, man, human, male, driver, licence, car = self.init_concepts(
sheerka, context, man, human, male, driver, licence, car = self.init_test(global_truth=True).with_concepts(
"man", "human", "male",
"driver", "licence", "car")
"driver", "licence", "car").unpack()
sheerka.set_isa(context, sheerka.new("man"), human)
sheerka.set_isa(context, sheerka.new("man"), male)
@@ -23,8 +23,8 @@ class TestSheerkaConceptsAlgebra(TestUsingMemoryBasedSheerka):
BuiltinConcepts.HASA: {car, licence}, }
def test_can_add_concepts_when_property_already_exist(self):
sheerka, context, man, human, king, male = self.init_concepts(
"man", "human", "king", "male")
sheerka, context, man, human, king, male = self.init_test(global_truth=True).with_concepts(
"man", "human", "king", "male").unpack()
sheerka.set_isa(context, sheerka.new("man"), human)
sheerka.set_isa(context, sheerka.new("king"), male)
@@ -35,9 +35,9 @@ class TestSheerkaConceptsAlgebra(TestUsingMemoryBasedSheerka):
assert res.get_metadata().props == {BuiltinConcepts.ISA: {male, human}}
def test_i_can_subtract_concepts(self):
sheerka, context, foo, bar, isa1, isa2, hasa1, hasa2 = self.init_concepts(
sheerka, context, foo, bar, isa1, isa2, hasa1, hasa2 = self.init_test(global_truth=True).with_concepts(
"foo", "bar",
"isa1", "isa2", "has1", "has2")
"isa1", "isa2", "has1", "has2").unpack()
new_foo = sheerka.new("foo")
sheerka.set_isa(context, new_foo, isa1)
@@ -61,9 +61,10 @@ class TestSheerkaConceptsAlgebra(TestUsingMemoryBasedSheerka):
BuiltinConcepts.HASA: {hasa2}, }
def test_i_can_recognize_myself_when_using_sdp_repository(self):
sheerka, context, foo, isa1, hasa1, = self.init_test(cache_only=False). \
with_concepts("foo", "isa1", "has1", create_new=True). \
unpack()
sheerka, context, foo, isa1, hasa1, = self.init_test(cache_only=False, global_truth=True).with_concepts(
"foo",
"isa1",
"has1", create_new=True).unpack()
sheerka.om.commit(context)
new_foo = sheerka.new("foo")
@@ -74,7 +75,10 @@ class TestSheerkaConceptsAlgebra(TestUsingMemoryBasedSheerka):
assert sheerka.recognize(new_foo, all_scores=True) == [ConceptScore(1, new_foo, new_foo)]
def test_i_can_recognize_myself_when_not_using_sdp_repository(self):
sheerka, context, foo, isa1, hasa1, = self.init_concepts("foo", "isa1", "has1")
sheerka, context, foo, isa1, hasa1, = self.init_test(global_truth=True).with_concepts(
"foo",
"isa1",
"has1").unpack()
new_foo = sheerka.new("foo")
sheerka.set_isa(context, new_foo, isa1)
@@ -100,9 +104,9 @@ class TestSheerkaConceptsAlgebra(TestUsingMemoryBasedSheerka):
assert sheerka.recognize(Concept(), all_scores=True) == []
def test_i_can_recognize_multiple_concepts_with_the_proper_score(self):
sheerka, context, foo, bar, isa1, isa2, hasa1, hasa2 = self.init_concepts(
sheerka, context, foo, bar, isa1, isa2, hasa1, hasa2 = self.init_test(global_truth=True).with_concepts(
"foo", "bar",
"isa1", "isa2", "has1", "has2")
"isa1", "isa2", "has1", "has2").unpack()
new_foo = sheerka.new("foo")
sheerka.set_isa(context, new_foo, isa1)
@@ -131,9 +135,9 @@ class TestSheerkaConceptsAlgebra(TestUsingMemoryBasedSheerka):
ConceptScore(0.5, sheerka.new("bar"), to_recognize)]
def test_i_can_recognize_if_all_scores_is_disabled(self):
sheerka, context, foo, bar, isa1, isa2, hasa1, hasa2 = self.init_concepts(
sheerka, context, foo, bar, isa1, isa2, hasa1, hasa2 = self.init_test(global_truth=True).with_concepts(
"foo", "bar",
"isa1", "isa2", "has1", "has2")
"isa1", "isa2", "has1", "has2").unpack()
new_foo = sheerka.new("foo")
sheerka.set_isa(context, new_foo, isa1)
@@ -153,9 +157,9 @@ class TestSheerkaConceptsAlgebra(TestUsingMemoryBasedSheerka):
assert res == ConceptScore(1.0, sheerka.new("bar"), to_recognize)
def test_i_can_recognize_if_all_scores_is_disabled_but_multiple_high_scores(self):
sheerka, context, foo, bar, isa1, hasa1 = self.init_concepts(
sheerka, context, foo, bar, isa1, hasa1 = self.init_test(global_truth=True).with_concepts(
"foo", "bar",
"isa1", "has1")
"isa1", "has1").unpack()
new_foo = sheerka.new("foo")
sheerka.set_isa(context, new_foo, isa1)
+12 -10
View File
@@ -1105,13 +1105,14 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
Concept("hundreds", definition="number hundred"),
)
sheerka.set_isa(context, sheerka.new("one"), number)
sheerka.set_isa(context, sheerka.new("two"), number)
sheerka.set_isa(context, sheerka.new("twenty"), number)
sheerka.set_isa(context, sheerka.new("thirty"), number)
sheerka.set_isa(context, sheerka.new("hundred"), number)
sheerka.set_isa(context, sheerka.new("twenties"), number)
sheerka.set_isa(context, sheerka.new("hundreds"), number)
global_truth_context = self.get_context(sheerka, global_truth=True)
sheerka.set_isa(global_truth_context, sheerka.new("one"), number)
sheerka.set_isa(global_truth_context, sheerka.new("two"), number)
sheerka.set_isa(global_truth_context, sheerka.new("twenty"), number)
sheerka.set_isa(global_truth_context, sheerka.new("thirty"), number)
sheerka.set_isa(global_truth_context, sheerka.new("hundred"), number)
sheerka.set_isa(global_truth_context, sheerka.new("twenties"), number)
sheerka.set_isa(global_truth_context, sheerka.new("hundreds"), number)
sheerka.clear_bnf_definition() # reset all the grammar to simulate Sheerka restart
@@ -1129,12 +1130,12 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
}
def test_i_can_resolve_when_concepts_have_multiple_levels_of_sets(self):
sheerka, context, adjective, color, red, qualified_table = self.init_concepts(
sheerka, context, adjective, color, red, qualified_table = self.init_test(global_truth=True).with_concepts(
"adjective",
"color",
"red",
Concept("qualified table", definition="adjective 'table'"),
)
).unpack()
sheerka.set_isa(context, color, adjective)
sheerka.set_isa(context, red, color)
@@ -1404,7 +1405,8 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
def test_i_cannot_smart_get_attr_when_attribute_does_not_exist(self):
sheerka, context, adjective, color, red, size, table = self.init_concepts("adjective",
"color",
Concept("red", body="red").auto_init(),
Concept("red",
body="red").auto_init(),
"size",
"table",
create_new=True)
+3 -3
View File
@@ -391,8 +391,8 @@ class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
:return:
"""
sheerka, context, predicate, foo = self.init_concepts(
Concept("Sometimes True", body="in_context('a')"),
Concept("foo", pre="c:Sometimes True:"))
Concept("Sometimes True", body="in_context('a')", pre="is_question()"),
Concept("foo", pre="Sometimes True"))
foo1 = sheerka.new("foo")
foo1 = sheerka.evaluate_concept(context, foo1) # 'a' is not in context, so it fails
@@ -400,7 +400,7 @@ class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
context2 = self.get_context(sheerka)
context2.add_to_protected_hints('a')
foo2 = sheerka.new("foo")
foo2 = sheerka.evaluate_concept(context2, foo2) # 'a' in context + new instance of 'Sometimes True'
foo2 = sheerka.evaluate_concept(context2, foo2) # 'a' is now in context
assert sheerka.isinstance(foo1, BuiltinConcepts.CONDITION_FAILED)
assert sheerka.isinstance(foo2, "foo")
+31 -2
View File
@@ -7,6 +7,33 @@ class TestSheerkaHasAManager(TestUsingMemoryBasedSheerka):
def test_i_can_set_hasa(self):
sheerka, context, king, kingdom = self.init_concepts("king", "kingdom")
king_instance = sheerka.new("king")
res = sheerka.set_hasa(context, king_instance, kingdom)
assert res.status
# when global truth is not activated, only the current instance is modified
another_king = sheerka.get_by_key("king")
assert not another_king.get_prop(BuiltinConcepts.HASA) == {kingdom}
assert not sheerka.hasa(another_king, kingdom)
def test_i_cannot_set_the_same_attribute_twice(self):
sheerka, context, king, kingdom = self.init_concepts("king", "kingdom")
king_instance = sheerka.new("king")
sheerka.set_hasa(context, king_instance, kingdom)
res = sheerka.set_hasa(context, king_instance, kingdom)
assert not res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.PROPERTY_ALREADY_DEFINED)
assert res.body.property_name == BuiltinConcepts.HASA
assert res.body.property_value == kingdom
assert res.body.concept == king_instance
def test_i_can_set_hasa_when_global_truth_is_activated(self):
sheerka, context, king, kingdom = self.init_test(global_truth=True).with_concepts(
"king",
"kingdom").unpack()
res = sheerka.set_hasa(context, sheerka.new("king"), kingdom)
assert res.status
@@ -16,8 +43,10 @@ class TestSheerkaHasAManager(TestUsingMemoryBasedSheerka):
# check that the definition of the concept has been updated
assert sheerka.hasa(sheerka.new("king"), kingdom)
def test_i_cannot_set_the_same_attribute_twice(self):
sheerka, context, king, kingdom = self.init_concepts("king", "kingdom")
def test_i_cannot_set_the_same_attribute_twice_when_global_truth_is_activated(self):
sheerka, context, king, kingdom = self.init_test(global_truth=True).with_concepts(
"king",
"kingdom").unpack()
sheerka.set_hasa(context, sheerka.new("king"), kingdom)
res = sheerka.set_hasa(context, sheerka.new("king"), kingdom)
+81 -13
View File
@@ -86,15 +86,46 @@ class TestSheerkaIsAManager(TestUsingMemoryBasedSheerka):
def test_isa(self):
sheerka, context, blue, color = self.init_concepts(Concept("blue"), Concept("color"))
assert not sheerka.isa(blue, color)
sheerka.set_isa(context, blue, color)
assert sheerka.isa(blue, color)
blue_instance = sheerka.new("blue")
assert not sheerka.isa(blue_instance, color)
sheerka.set_isa(context, blue_instance, color)
assert sheerka.isa(blue_instance, color)
# isa tests the id of a concept, not it's content
another_color_instance_but_with_a_body = sheerka.new(color, body="a body")
assert sheerka.isa(blue, another_color_instance_but_with_a_body)
assert sheerka.isa(blue_instance, another_color_instance_but_with_a_body)
# isa, when EVAL_GLOBAL_TRUTH_REQUESTED is not activated, only affect the current concept
another_blue_instance = sheerka.new("blue")
assert not sheerka.isa(another_blue_instance, color)
# when EVAL_GLOBAL_TRUTH_REQUESTED is not activated, color is not a set
assert not sheerka.isinset(blue_instance, color)
assert not sheerka.isaset(context, color)
def test_isa_global_truth(self):
sheerka, context, blue, color = self.init_concepts(Concept("blue"), Concept("color"))
blue_instance = sheerka.new("blue")
assert not sheerka.isa(blue_instance, color)
assert not sheerka.isaset(context, color)
assert not sheerka.isinset(blue_instance, color)
context.add_to_private_hints(BuiltinConcepts.EVAL_GLOBAL_TRUTH_REQUESTED)
sheerka.set_isa(context, blue_instance, color)
assert sheerka.isa(blue_instance, color)
assert sheerka.isaset(context, color)
assert sheerka.isinset(blue_instance, color)
# all blue instances are now a color
another_blue_instance = sheerka.new("blue")
assert sheerka.isa(another_blue_instance, color)
assert sheerka.isaset(context, color)
assert sheerka.isinset(another_blue_instance, color)
def test_isaset(self):
sheerka, context, group, foo = self.init_concepts(Concept("group"), Concept("foo"))
@@ -246,13 +277,28 @@ class TestSheerkaIsAManager(TestUsingMemoryBasedSheerka):
foo = sheerka.new(foo.key) # new instance
sheerka.set_isa(context, foo, all_foo)
sheerka.set_isa(context, foo, all_bar)
assert foo.get_prop(BuiltinConcepts.ISA) == {all_foo, all_bar}
assert sheerka.isa(foo, all_foo)
assert sheerka.isa(foo, all_bar)
def test_a_concept_can_be_in_multiple_sets_when_global_truth_is_activated(self):
sheerka, context, foo, all_foo, all_bar = self.init_test(global_truth=True).with_concepts(
Concept("foo"),
Concept("all_foo"),
Concept("all_bar"),
create_new=True).unpack()
foo = sheerka.new(foo.key) # new instance
sheerka.set_isa(context, foo, all_foo)
foo = sheerka.new(foo.key) # new instance
sheerka.set_isa(context, foo, all_bar)
assert foo.get_prop(BuiltinConcepts.ISA) == {all_foo, all_bar}
assert sheerka.isa(foo, all_foo)
assert sheerka.isa(foo, all_bar)
assert sheerka.isinset(foo, all_foo)
assert sheerka.isinset(foo, all_bar)
assert sheerka.isaset(context, all_foo)
@@ -270,6 +316,25 @@ class TestSheerkaIsAManager(TestUsingMemoryBasedSheerka):
Concept("baz"),
)
sheerka.set_isa(context, foo, bar)
sheerka.set_isa(context, bar, baz)
assert sheerka.isa(foo, bar)
assert sheerka.isa(bar, baz)
assert sheerka.isa(foo, baz)
def test_i_can_manage_isa_transitivity_when_global_truth_is_activated(self):
"""
if foo isa bar and bar isa baz, then foo isa baz
:return:
"""
sheerka, context, foo, bar, baz = self.init_test(global_truth=True).with_concepts(
Concept("foo"),
Concept("bar"),
Concept("baz"),
).unpack()
sheerka.set_isa(context, sheerka.new("foo"), bar)
sheerka.set_isa(context, sheerka.new("bar"), baz)
@@ -278,11 +343,11 @@ class TestSheerkaIsAManager(TestUsingMemoryBasedSheerka):
assert sheerka.isa(sheerka.new("foo"), baz)
def test_i_cannot_manage_isa_transitivity_when_using_body(self):
sheerka, context, one, another_one, number = self.init_concepts(
sheerka, context, one, another_one, number = self.init_test(global_truth=True).with_concepts(
"one",
Concept("another one", body="one"),
"number"
)
).unpack()
sheerka.set_isa(context, sheerka.new("one"), number)
@@ -290,7 +355,10 @@ class TestSheerkaIsAManager(TestUsingMemoryBasedSheerka):
assert not sheerka.isa(another_one, number) # Correct this misbehaviour when BuiltinConcepts.IS is implemented
def test_concepts_in_group_cache_is_updated(self):
sheerka, context, one, two, number = self.init_concepts("one", "two", "number")
sheerka, context, one, two, number = self.init_test(global_truth=True).with_concepts(
"one",
"two",
"number").unpack()
sheerka.set_isa(context, sheerka.new("one"), number)
@@ -315,12 +383,12 @@ class TestSheerkaIsAManager(TestUsingMemoryBasedSheerka):
assert number.id not in sheerka.get_concepts_bnf_definitions()
def test_i_can_get_and_set_isa_when_multiple_ontology_layers(self):
sheerka, context, foo, group1, group2 = self.init_concepts(
sheerka, context, foo, group1, group2 = self.init_test(global_truth=True).with_concepts(
Concept("foo"),
Concept("group1"),
Concept("group2"),
cache_only=False
)
).unpack()
sheerka.set_isa(context, foo, group1)
@@ -401,10 +469,10 @@ class TestSheerkaSetsManagerUsingFileBasedSheerka(TestUsingFileBasedSheerka):
}
def test_i_can_set_isa(self):
sheerka, context, foo, bar, group = self.init_test().with_concepts("foo",
"bar",
"group",
).unpack()
sheerka, context, foo, bar, group = self.init_test(global_truth=True).with_concepts("foo",
"bar",
"group",
).unpack()
# nothing was previously in ISA
foo = sheerka.new(foo.key)
+9
View File
@@ -116,6 +116,15 @@ class TestSheerkaMemory(TestUsingMemoryBasedSheerka):
assert sheerka.memory(context, "self.name == 'foo'") == foo
def test_i_can_use_memory_when_the_entry_does_not_exist(self):
sheerka, context, foo, bar = self.init_test().with_concepts("foo", "bar", create_new=True).unpack()
sheerka.add_to_memory(context, "foo", foo) # add at least one element
res = sheerka.memory(context, "bar")
assert sheerka.isinstance(res, BuiltinConcepts.NOT_FOUND)
assert res.body == {'#name': 'bar'}
def test_i_retrieve_the_last_entry_when_requesting_memory_with_a_query(self):
sheerka, context, foo, bar, foo2 = self.init_concepts("foo", "bar", Concept("foo", body="2"))
+4 -10
View File
@@ -52,22 +52,16 @@ class TestSheerkaQueryManager(TestUsingMemoryBasedSheerka):
assert sheerka.filter_objects(context, lst, prop2={"key": "value"}) == [lst[3]]
# assert sheerka.filter_objects(context, lst, prop1={1, "v"}) == [lst[2]] set are not supported
# complex properties
assert sheerka.filter_objects(context, lst, prop1_contains="a") == [lst[0], lst[1]]
assert sheerka.filter_objects(context, lst, prop1_contains=1) == [lst[2], lst[3]]
def test_i_can_filter_by_object_type(self):
sheerka, context = self.init_test().unpack()
lst = [A("a11", "a12"), Concept("foo", body="a").auto_init(), Concept("foo", body="b").auto_init()]
assert sheerka.filter_objects(context, lst, __type="foo") == [lst[1], lst[2]]
def test_i_can_filter_on_atomic_def(self):
sheerka, context, isa, plus, isa2 = self.init_concepts(
Concept('x is a y').def_var("x").def_var("y"),
Concept('a plus b').def_var("a").def_var("b"),
Concept('u is a v').def_var("u").def_var("v"),
)
lst = [isa, plus, isa2]
assert sheerka.filter_objects(context, lst, atomic_def="is a") == [lst[0], lst[2]]
def test_i_can_filter_on_as_bag_property(self):
sheerka, context = self.init_test().unpack()
lst = [B("a11", "a12"), B("a21", "a22"), B("a31", "a32")]
+22
View File
@@ -253,6 +253,28 @@ class TestBuiltinHelpers(TestUsingMemoryBasedSheerka):
# a second time, now that bar is already evaluated
assert core.builtin_helpers.ensure_evaluated(context, bar) == foo
@pytest.mark.parametrize("concept, concepts_parts, expected", [
(Concept("foo"), ["where"], 0),
(Concept("foo", where="x"), ["where"], 1),
(Concept("foo", where="x and y"), ["where"], 2),
(Concept("foo", where="x or y"), ["where"], 1),
(Concept("foo", where="x or y and z"), ["where"], 2),
(Concept("foo", where="not w"), ["where"], 1),
(Concept("foo", where=""), ["where"], 0),
(Concept("foo", pre="x"), ["pre", "where"], 101),
(Concept("foo", where="x"), ["pre", "where"], 1),
(Concept("foo", where="x and y", pre="z"), ["pre", "where"], 103),
(Concept("foo", pre="x"), ["pre|where"], 1),
(Concept("foo", where="x"), ["pre|where"], 1),
(Concept("foo", where="x and y", pre="z"), ["pre|where"], 3),
])
def test_i_can_get_concept_complexity(self, concept, concepts_parts, expected):
context = self.get_context()
assert core.builtin_helpers.get_concept_complexity(context, concept, concepts_parts) == expected
# @pytest.mark.parametrize("return_values", [
# None,
# []
+31
View File
@@ -559,6 +559,37 @@ class TestSheerkaUsingMemoryBasedSheerka(TestUsingMemoryBasedSheerka):
ret_val = ReturnValueConcept("Test", True, sheerka.err("an error"))
assert sheerka.get_errors(context, ret_val) == []
def test_i_can_add_and_remove_global_context_hints(self):
sheerka, context = self.init_test().unpack()
sheerka.add_to_context(BuiltinConcepts.EVAL_QUESTION_REQUESTED)
assert sheerka._global_context_hints == {BuiltinConcepts.EVAL_QUESTION_REQUESTED}
sheerka.remove_fom_context(BuiltinConcepts.EVAL_QUESTION_REQUESTED)
assert sheerka._global_context_hints == set()
sheerka.add_to_context(sheerka.new(BuiltinConcepts.EVAL_QUESTION_REQUESTED))
assert sheerka._global_context_hints == {BuiltinConcepts.EVAL_QUESTION_REQUESTED}
sheerka.remove_fom_context(sheerka.new(BuiltinConcepts.EVAL_QUESTION_REQUESTED))
assert sheerka._global_context_hints == set()
def test_global_context_hints_are_added_to_every_user_input_execution(self):
sheerka, context, foo = self.init_test().with_concepts(
Concept("foo", pre="in_context(BuiltinConcepts.EVAL_GLOBAL_TRUTH_REQUESTED)")
).unpack()
res = sheerka.evaluate_user_input("eval foo", "testing_user")
assert sheerka.isinstance(res[0].value, BuiltinConcepts.CONDITION_FAILED) # sanity check
sheerka.add_to_context(BuiltinConcepts.EVAL_GLOBAL_TRUTH_REQUESTED)
res = sheerka.evaluate_user_input("eval foo", "testing_user")
assert sheerka.isinstance(res[0].value, "foo")
sheerka.remove_fom_context(BuiltinConcepts.EVAL_GLOBAL_TRUTH_REQUESTED)
res = sheerka.evaluate_user_input("eval foo", "testing_user")
assert sheerka.isinstance(res[0].value, BuiltinConcepts.CONDITION_FAILED)
class TestSheerkaUsingFileBasedSheerka(TestUsingFileBasedSheerka):
+28 -16
View File
@@ -842,11 +842,9 @@ class TestSheerkaOntology(TestUsingMemoryBasedSheerka):
sheerka, context, foo = self.init_concepts("foo", cache_only=False)
manager = SheerkaOntologyManager(sheerka, sheerka.root_folder, sheerka.cache_only)
cache = Cache(default=lambda sdp, key: sdp.get("by_id", key),
extend_exists=lambda sdp, key: sdp.get("by_id", key))
cache = Cache().auto_configure("by_id")
manager.register_concept_cache("by_id", cache, lambda obj: obj.id, use_ref=True)
cache = ListIfNeededCache(default=lambda sdp, key: sdp.get("by_key", key),
extend_exists=lambda sdp, key: sdp.get("by_key", key))
cache = ListIfNeededCache().auto_configure("by_key")
manager.register_concept_cache("by_key", cache, lambda obj: obj.key, use_ref=True)
manager.freeze()
@@ -863,11 +861,9 @@ class TestSheerkaOntology(TestUsingMemoryBasedSheerka):
sheerka, context, foo = self.init_concepts("foo", cache_only=False)
manager = SheerkaOntologyManager(sheerka, sheerka.root_folder, sheerka.cache_only)
cache = Cache(default=lambda sdp, key: sdp.get("by_id", key),
extend_exists=lambda sdp, key: sdp.get("by_id", key))
cache = Cache().auto_configure("by_id")
manager.register_concept_cache("by_id", cache, lambda obj: obj.id, use_ref=True)
cache = ListIfNeededCache(default=lambda sdp, key: sdp.get("by_key", key),
extend_exists=lambda sdp, key: sdp.get("by_key", key))
cache = ListIfNeededCache().auto_configure("by_key")
manager.register_concept_cache("by_key", cache, lambda obj: obj.key, use_ref=True)
manager.freeze()
@@ -887,15 +883,33 @@ class TestSheerkaOntology(TestUsingMemoryBasedSheerka):
assert list(manager.ontologies[0].cache_manager.sdp.state.data.keys()) == ['by_id', 'by_key']
assert manager.ontologies[1].cache_manager.sdp.state.data == {}
def test_i_can_add_the_concepts_with_the_same_key_from_different_layers(self):
sheerka, context, foo_1, foo_2 = self.init_concepts(
Concept("foo x", body="x + 1").def_var("x"),
Concept("foo x", body="x + 2").def_var("x"),
cache_only=False)
manager = SheerkaOntologyManager(sheerka, sheerka.root_folder, sheerka.cache_only)
cache = ListIfNeededCache().auto_configure("by_key")
manager.register_concept_cache("by_key", cache, lambda obj: obj.key, use_ref=True)
manager.freeze()
manager.add_concept(foo_1)
manager.commit(context)
manager.push_ontology("new ontology")
manager.add_concept(foo_2)
manager.commit(context)
assert manager.current_sdp().get("by_key", foo_1.key) == [foo_1, foo_2]
def test_i_can_update_concept_in_default_layer(self):
sheerka, context, foo = self.init_concepts("foo", cache_only=False)
manager = SheerkaOntologyManager(sheerka, sheerka.root_folder, sheerka.cache_only)
cache = Cache(default=lambda sdp, key: sdp.get("by_id", key),
extend_exists=lambda sdp, key: sdp.get("by_id", key))
cache = Cache().auto_configure("by_id")
manager.register_concept_cache("by_id", cache, lambda obj: obj.id, use_ref=True)
cache = ListIfNeededCache(default=lambda sdp, key: sdp.get("by_key", key),
extend_exists=lambda sdp, key: sdp.get("by_key", key))
cache = ListIfNeededCache().auto_configure("by_key")
manager.register_concept_cache("by_key", cache, lambda obj: obj.key, use_ref=True)
manager.freeze()
@@ -953,11 +967,9 @@ class TestSheerkaOntology(TestUsingMemoryBasedSheerka):
sheerka, context, foo = self.init_concepts("foo", cache_only=False)
manager = SheerkaOntologyManager(sheerka, sheerka.root_folder, sheerka.cache_only)
cache = Cache(default=lambda sdp, key: sdp.get("by_id", key),
extend_exists=lambda sdp, key: sdp.get("by_id", key))
cache = Cache().auto_configure("by_id")
manager.register_concept_cache("by_id", cache, lambda obj: obj.id, use_ref=True)
cache = ListIfNeededCache(default=lambda sdp, key: sdp.get("by_key", key),
extend_exists=lambda sdp, key: sdp.get("by_key", key))
cache = ListIfNeededCache().auto_configure("by_key")
manager.register_concept_cache("by_key", cache, lambda obj: obj.key, use_ref=True)
manager.freeze()
+18
View File
@@ -193,6 +193,8 @@ def test_i_can_escape():
("c:|id:", None, "id"),
("c:key|:", "key", None),
("c:key|id:x", None, None),
("c:one: plus c:two:", None, None),
("c:one|id: plus c:two:", None, None),
])
def test_i_can_unstr_concept(text, expected_key, expected_id):
k, i = core.utils.unstr_concept(text)
@@ -494,3 +496,19 @@ def test_sheerka_hasattr_get_attr():
assert not core.utils.sheerka_hasattr(concept, "b")
with pytest.raises(AttributeError):
core.utils.sheerka_getattr(concept, "b")
def test_i_can_replace_after():
my_new_items = ["alpha", "beta", "gamma"]
my_list1 = ["a", "b", "c", "d"]
core.utils.replace_after(my_list1, "c", my_new_items)
assert my_list1 == ["a", "b", "alpha", "beta", "gamma"]
my_list2 = ["a", "b", "c", "d"]
core.utils.replace_after(my_list2, "a", my_new_items)
assert my_list2 == ["alpha", "beta", "gamma"]
with pytest.raises(KeyError):
my_list3 = ["a", "b", "c", "d"]
core.utils.replace_after(my_list3, "x", my_new_items)