Fixed #101 : Implement PLURIAL

Fixed #103 : Implement PlurialNodeParser
Fixed #104 : Implement dynamic concept
Fixed #107 : PrepareEvalxxxEvaluator: context hints are lost on a second evaluation
This commit is contained in:
2021-08-05 19:07:21 +02:00
parent c798c2c570
commit 71d1b1d1ca
31 changed files with 600 additions and 105 deletions
+8
View File
@@ -1537,6 +1537,14 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
weights = sheerka.get_weights(BuiltinConcepts.PRECEDENCE, comparison_context=CONCEPT_COMPARISON_CONTEXT)
assert weights == {'c:one|1001:': 2, 'c:two|1002:': 1}
def test_i_can_get_a_dynamic_concept_by_id_when_allow_dynamic(self):
sheerka, context, foo = self.init_concepts("foo")
dynamic_foo = sheerka.new_dynamic(foo, "SUFFIX")
assert sheerka.isinstance(sheerka.get_by_id(dynamic_foo.id), BuiltinConcepts.UNKNOWN_CONCEPT)
assert sheerka.isinstance(sheerka.get_by_id(dynamic_foo.id, allow_dynamic=True), foo)
class TestSheerkaConceptManagerUsingFileBasedSheerka(TestUsingFileBasedSheerka):
def test_i_can_add_several_concepts(self):
+28 -1
View File
@@ -518,6 +518,29 @@ class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
evaluated = sheerka.evaluate_concept(self.get_context(sheerka, False, False), concept)
assert evaluated.key == concept.key
def test_i_can_evaluate_context_hint_multiple_times(self):
"""
Previous behaviour (that we want to change)
When def concept foo as global_truth(xxx) pre yyy
the context hint for the global truth is set during the initialisation of the ast
if the body is computed straight away it's ok,
But if the concept is evaluated a second time, the asts is not computed again, so the context hint is not set
:return:
"""
sheerka, context, foo = self.init_concepts(
Concept("foo", body="global_truth(in_context(BuiltinConcepts.EVAL_GLOBAL_TRUTH_REQUESTED))")
)
foo_instance = sheerka.new("foo")
evaluated = sheerka.evaluate_concept(context, foo_instance, eval_body=False)
assert ConceptParts.BODY in evaluated.get_compiled()
assert evaluated.body == NotInit
assert not evaluated.get_hints().is_evaluated
evaluated = sheerka.evaluate_concept(context, foo_instance, eval_body=True) # evaluate the body this time
assert isinstance(evaluated.body, bool) and evaluated.body
def test_i_can_apply_intermediate_where_condition_using_python(self):
sheerka, context, one_1, one_str, plus = self.init_concepts(
Concept("one", body="1"),
@@ -818,10 +841,14 @@ class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
(Concept("foo"), False, []),
(Concept("foo", pre="pre", post="post", ret="ret", where="where"), False, ["#pre#", "#post#"]),
(Concept("foo", pre="pr", post="p", ret="r", where="w"), True,
["#pre#", "#ret#", "#post#", "variables", "#body#"]),
["#pre#", "variables", "#body#", "#ret#", "#post#"]),
(Concept("foo", pre="pre", body="body"), False, ["#pre#"]),
(Concept("foo", pre="pre", body="body"), True, ["#pre#", "variables", "#body#"]),
(Concept("foo", pre="a").def_var("a"), False, ["variables", "#pre#"]),
(Concept("foo", pre="self"), False, ["#body#", "#pre#"]),
(Concept("foo", pre="self + a").def_var("a"), False, ["variables", "#body#", "#pre#"]),
(Concept("foo", pre="self + a", ret="ret").def_var("a"), False, ["variables", "#body#", "#pre#"]),
(Concept("foo", pre="self + a", ret="ret").def_var("a"), True, ["variables", "#body#", "#pre#", "#ret#"]),
(Concept("foo", body="body"), False, [])
+17
View File
@@ -9,7 +9,10 @@ class TestSheerkaHasAManager(TestUsingMemoryBasedSheerka):
king_instance = sheerka.new("king")
res = sheerka.set_hasa(context, king_instance, kingdom)
assert res.status
assert king_instance.get_prop(BuiltinConcepts.HASA) == {kingdom}
assert sheerka.hasa(king_instance, kingdom)
# when global truth is not activated, only the current instance is modified
another_king = sheerka.get_by_key("king")
@@ -56,3 +59,17 @@ class TestSheerkaHasAManager(TestUsingMemoryBasedSheerka):
assert res.body.property_name == BuiltinConcepts.HASA
assert res.body.property_value == kingdom
assert res.body.concept == sheerka.new("king")
def test_i_can_set_hase_twice_when_global_truth_is_true_the_second_time(self):
sheerka, context, king, kingdom = self.init_concepts("king", "kingdom")
global_truth_context = self.get_context(sheerka, global_truth=True)
king_instance = sheerka.new("king")
sheerka.set_hasa(context, king_instance, kingdom)
# set it again with global_truth = True
res = sheerka.set_hasa(global_truth_context, king_instance, kingdom)
assert res.status
assert sheerka.hasa(king_instance, kingdom)
assert sheerka.hasa(sheerka.new("king"), kingdom) # try another instance
+16 -1
View File
@@ -116,7 +116,8 @@ class TestSheerkaIsAManager(TestUsingMemoryBasedSheerka):
context.add_to_private_hints(BuiltinConcepts.EVAL_GLOBAL_TRUTH_REQUESTED)
sheerka.set_isa(context, blue_instance, color)
res = sheerka.set_isa(context, blue_instance, color)
assert res.status
assert sheerka.isa(blue_instance, color)
assert sheerka.isaset(context, color)
assert sheerka.isinset(blue_instance, color)
@@ -423,6 +424,20 @@ class TestSheerkaIsAManager(TestUsingMemoryBasedSheerka):
foo = sheerka.get_by_id(foo.id)
assert not sheerka.isa(foo, group2)
def test_i_can_set_isa_twice_when_global_truth_is_true_the_second_time(self):
sheerka, context, blue, color = self.init_concepts(Concept("blue"), Concept("color"))
global_truth_context = self.get_context(sheerka, global_truth=True)
blue_instance = sheerka.new("blue")
sheerka.set_isa(context, blue_instance, color)
# set it again with global_truth = True
res = sheerka.set_isa(global_truth_context, blue_instance, color)
assert res.status
assert sheerka.isa(blue_instance, color)
assert sheerka.isa(sheerka.new("blue"), color) # try another instance
class TestSheerkaSetsManagerUsingFileBasedSheerka(TestUsingFileBasedSheerka):
def test_i_can_add_concept_to_set_and_retrieve_it_in_another_session(self):
+76
View File
@@ -0,0 +1,76 @@
from core.builtin_concepts_ids import BuiltinConcepts
from core.global_symbols import NotFound
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
class TestSheerkaPluralManager(TestUsingMemoryBasedSheerka):
def test_i_can_set_plural(self):
sheerka, context, man, men = self.init_concepts("man", "men")
men_instance = sheerka.new("men")
res = sheerka.set_plural(context, men_instance, man)
assert res.status
assert men_instance.get_prop(BuiltinConcepts.PLURAL) == man
assert sheerka.is_plural(men_instance)
assert sheerka.is_plural(men_instance, man)
# global truth is not set
another_instance = sheerka.new("men")
assert another_instance.get_prop(BuiltinConcepts.PLURAL) is None
def test_i_can_set_plural_when_global_truth_is_set(self):
sheerka, context, man, men = self.init_concepts("man", "men", global_truth=True)
assert sheerka.known_plural(man) == NotFound
res = sheerka.set_plural(context, men, man)
assert res.status
assert sheerka.is_plural(men)
another_instance = sheerka.new("men")
assert sheerka.is_plural(another_instance)
assert sheerka.known_plural(man) == men.id
def test_i_cannot_set_plural_twice(self):
sheerka, context, man, men = self.init_concepts("man", "men")
men_instance = sheerka.new("men")
sheerka.set_plural(context, men_instance, man)
res = sheerka.set_plural(context, men_instance, man)
assert not res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.PROPERTY_ALREADY_DEFINED)
assert res.body.concept == men_instance
assert res.body.property_name == BuiltinConcepts.PLURAL
assert res.body.property_value == man
def test_i_cannot_set_plural_twice_when_global_truth(self):
sheerka, context, man, men = self.init_concepts("man", "men", global_truth=True)
sheerka.set_plural(context, men, man)
another_instance = sheerka.new("men")
res = sheerka.set_plural(context, another_instance, man)
assert not res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.PROPERTY_ALREADY_DEFINED)
assert res.body.concept == another_instance
assert res.body.property_name == BuiltinConcepts.PLURAL
assert res.body.property_value == man
def test_i_can_set_plural_twice_when_global_truth_is_true_the_second_time(self):
sheerka, context, man, men = self.init_concepts("man", "men")
global_truth_context = self.get_context(sheerka, global_truth=True)
men_instance = sheerka.new("men")
sheerka.set_plural(context, men_instance, man)
# set it again with global_truth = True
res = sheerka.set_plural(global_truth_context, men_instance, man)
assert res.status
assert men_instance.get_prop(BuiltinConcepts.PLURAL) == man
# and it's now true for all newly created context
assert sheerka.is_plural(sheerka.new("men"), man)