Fixed #61 : SheerkaDebugManager: Add get_value()

Fixed #60 : Hash error when ReturnValue is a list of list
Fixed #59 : Implement smart_get()
Fixed #58 : SheerkaPromptCompleter: Cannot parse concept token
Fixed #57 : SheerkaPrompt: Add concept autocompletion
Fixed #56 : automatically backup command
Fixed #54 : I can record execution status
Fixed #53 : ConceptManager: modify_concept fails
This commit is contained in:
2021-04-09 15:47:32 +02:00
parent 6cda2686fb
commit dd3d8f4abe
37 changed files with 1055 additions and 191 deletions
+169 -1
View File
@@ -840,7 +840,8 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
assert sheerka.get_property(foo_instance, BuiltinConcepts.ASSOCIATIVITY) == SyaAssociativity.Left
res = sheerka.set_property(context, foo_instance, sheerka.new(BuiltinConcepts.ASSOCIATIVITY), SyaAssociativity.Right)
res = sheerka.set_property(context, foo_instance, sheerka.new(BuiltinConcepts.ASSOCIATIVITY),
SyaAssociativity.Right)
assert res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
assert sheerka.get_property(foo_instance, BuiltinConcepts.ASSOCIATIVITY) == SyaAssociativity.Right
@@ -1235,6 +1236,173 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
assert sheerka.chicken_and_eggs.get(two.id) == {one.id, two.id, three.id}
assert sheerka.chicken_and_eggs.get(three.id) == {one.id, two.id, three.id}
def test_i_can_smart_get_attr_when_the_value_is_known(self):
sheerka, context = self.init_concepts()
foo = Concept("foo")
prop = Concept("property")
bar = Concept("bar")
sheerka.set_attr(foo, prop, bar)
assert sheerka.smart_get_attr(foo, prop) == bar
def test_i_can_smart_get_attr_when_simple_isa(self):
sheerka, context, adjective, color, red, table = self.init_concepts("adjective",
"color",
"red",
"table",
create_new=True)
sheerka.set_isa(context, color, adjective)
sheerka.set_isa(context, red, color)
color_instance = sheerka.new(color, body=red)
adjective_instance = sheerka.new(adjective, body=color_instance)
table_instance = sheerka.new(table)
sheerka.set_attr(table_instance, adjective, adjective_instance)
assert sheerka.smart_get_attr(table_instance, color) == color_instance
assert sheerka.objvalue(sheerka.smart_get_attr(table_instance, color)) == red
def test_i_can_smart_get_when_multiple_levels_of_isa(self):
sheerka, context, adjective, color, reddish, red, table = self.init_concepts("adjective",
"color",
"reddish",
"red",
"table",
create_new=True)
sheerka.set_isa(context, color, adjective)
sheerka.set_isa(context, reddish, color)
sheerka.set_isa(context, red, reddish)
reddish_instance = sheerka.new(reddish, body=red)
color_instance = sheerka.new(color, body=reddish_instance)
adjective_instance = sheerka.new(adjective, body=color_instance)
table_instance = sheerka.new(table)
sheerka.set_attr(table_instance, adjective, adjective_instance)
assert sheerka.smart_get_attr(table_instance, reddish_instance) == reddish_instance
assert sheerka.objvalue(sheerka.smart_get_attr(table_instance, color)) == red
def test_i_can_smart_get_when_multiple_values(self):
sheerka, context, adjective, color, red, blue, table = self.init_concepts("adjective",
"color",
"red",
"blue",
"table",
create_new=True)
sheerka.set_isa(context, color, adjective)
sheerka.set_isa(context, red, color)
sheerka.set_isa(context, blue, color)
table_instance = sheerka.new(table)
# add red color
red_color_instance = sheerka.new(color, body=red)
red_adjective_instance = sheerka.new(adjective, body=red_color_instance)
sheerka.set_attr(table_instance, adjective, red_adjective_instance)
# add blue color
blue_color_instance = sheerka.new(color, body=blue)
blue_adjective_instance = sheerka.new(adjective, body=blue_color_instance)
sheerka.set_attr(table_instance, adjective, blue_adjective_instance)
res = sheerka.smart_get_attr(table_instance, color)
assert res == [red_color_instance, blue_color_instance]
def test_i_can_smart_get_when_ancestor_is_asked(self):
sheerka, context, adjective, color, red, table = self.init_concepts("adjective",
"color",
"red",
"table",
create_new=True)
sheerka.set_isa(context, color, adjective)
sheerka.set_isa(context, red, color)
color_instance = sheerka.new(color, body=red)
table_instance = sheerka.new(table)
sheerka.set_attr(table_instance, color, color_instance)
assert sheerka.smart_get_attr(table_instance, adjective) == color_instance
def test_i_can_smart_get_when_ancestor_is_asked_and_multiple_values(self):
sheerka, context, adjective, color, red, size, large, table = self.init_concepts("adjective",
"color",
"red",
"size",
"large",
"table",
create_new=True)
sheerka.set_isa(context, color, adjective)
sheerka.set_isa(context, red, color)
sheerka.set_isa(context, size, adjective)
sheerka.set_isa(context, large, size)
table_instance = sheerka.new(table)
color_instance = sheerka.new(color, body=red)
sheerka.set_attr(table_instance, color, color_instance)
size_instance = sheerka.new(size, body=large)
sheerka.set_attr(table_instance, size, size_instance)
assert sheerka.smart_get_attr(table_instance, adjective) == [color_instance, size_instance]
def test_i_can_smart_get_when_direct_value_takes_precedence_over_child_value(self):
sheerka, context, adjective, color, red, blue, table = self.init_concepts("adjective",
"color",
"red",
"blue",
"table",
create_new=True)
sheerka.set_isa(context, color, adjective)
sheerka.set_isa(context, red, color)
color_instance = sheerka.new(color, body=red)
adjective_instance = sheerka.new(adjective, body=color_instance)
table_instance = sheerka.new(table)
sheerka.set_attr(table_instance, adjective, adjective_instance)
sheerka.set_attr(table_instance, color, blue) # set direct color value
assert sheerka.smart_get_attr(table_instance, color) == blue
def test_i_can_smart_get_when_direct_value_takes_precedence_over_ancestor_value(self):
sheerka, context, adjective, color, red, blue, table = self.init_concepts("adjective",
"color",
"red",
"blue",
"table",
create_new=True)
sheerka.set_isa(context, color, adjective)
sheerka.set_isa(context, red, color)
color_instance = sheerka.new(color, body=red)
table_instance = sheerka.new(table)
sheerka.set_attr(table_instance, color, color_instance)
sheerka.set_attr(table_instance, adjective, blue) # set direct color value
assert sheerka.smart_get_attr(table_instance, adjective) == blue
def test_i_cannot_smart_get_attr_if_value_is_unknown_and_attribute_not_a_concept(self):
sheerka, context = self.init_concepts()
foo = Concept("foo")
res = sheerka.smart_get_attr(foo, "attribute")
assert sheerka.isinstance(res, BuiltinConcepts.NOT_FOUND)
def test_i_cannot_smart_get_attr_if_value_is_unknown_and_no_isa(self):
sheerka, context, adjective, color, red, table = self.init_concepts("adjective",
"color",
"red",
"table",
create_new=True)
# sheerka.set_isa(context, color, adjective) # color is not defined as an adjective
sheerka.set_isa(context, red, color)
color_instance = sheerka.new(color, body=red)
adjective_instance = sheerka.new(adjective, body=color_instance)
table_instance = sheerka.new(table)
sheerka.set_attr(table_instance, adjective, adjective_instance)
res = sheerka.smart_get_attr(table_instance, color)
assert sheerka.isinstance(res, BuiltinConcepts.NOT_FOUND)
class TestSheerkaConceptManagerUsingFileBasedSheerka(TestUsingFileBasedSheerka):
def test_i_can_add_several_concepts(self):
+12
View File
@@ -15,3 +15,15 @@ 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")
sheerka.set_hasa(context, sheerka.new("king"), kingdom)
res = sheerka.set_hasa(context, sheerka.new("king"), 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 == sheerka.new("king")
+49
View File
@@ -3,6 +3,10 @@ import types
import pytest
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept
from core.global_symbols import EVENT_CONCEPT_CREATED, EVENT_CONCEPT_MODIFIED, EVENT_CONCEPT_PRECEDENCE_MODIFIED, \
EVENT_RULE_PRECEDENCE_MODIFIED
from core.rule import Rule, ACTION_TYPE_EXEC
from core.sheerka.ExecutionContext import ExecutionContext
from core.sheerka.SheerkaOntologyManager import SheerkaOntologyManager
from core.sheerka.services.SheerkaResultManager import SheerkaResultManager
@@ -400,6 +404,51 @@ class TestSheerkaResultManager(TestUsingMemoryBasedSheerka):
assert service.last_error_event_id is None
assert sheerka.get_last_error() == self.sheerka.new(BuiltinConcepts.NOT_FOUND, body={"query": "get_last_error"})
@pytest.mark.parametrize("event_id, event_args", [
(EVENT_CONCEPT_CREATED, Concept("foo")),
(EVENT_CONCEPT_MODIFIED, {"old": Concept("foo"), "new": Concept("bar")}),
(EVENT_CONCEPT_PRECEDENCE_MODIFIED, None),
(EVENT_RULE_PRECEDENCE_MODIFIED, None),
])
def test_i_can_detect_when_the_global_state_has_change(self, event_id, event_args):
sheerka, context = self.init_test().unpack()
assert not context.is_state_modified()
if event_args:
sheerka.publish(context, event_id, event_args)
else:
sheerka.publish(context, event_id)
assert context.is_state_modified()
def test_i_can_detect_when_the_global_state_has_change_when_a_concept_is_deleted(self):
sheerka, context, foo = self.init_concepts("foo", create_new=True)
del context.values["is_state_modified"]
assert not context.is_state_modified()
with context.push("Testing state", None) as sub_context:
sheerka.remove_concept(sub_context, foo)
assert context.is_state_modified()
def test_i_can_detect_when_the_global_state_has_change_when_a_rule_is_created(self):
sheerka, context = self.init_test().unpack()
rule = Rule(ACTION_TYPE_EXEC, "testing state has change", "True", "'hello world'")
with context.push("Testing state", None) as sub_context:
sheerka.create_new_rule(sub_context, rule)
assert context.is_state_modified()
def test_i_can_detect_when_the_global_state_has_change_when_a_rule_is_deleted(self):
sheerka, context, rule = self.init_exec_rules(("testing state has change", "True", "'hello world'"))
del context.values["is_state_modified"]
assert not context.is_state_modified()
with context.push("Testing state", None) as sub_context:
sheerka.remove_rule(sub_context, rule)
assert context.is_state_modified()
class TestSheerkaResultManagerFileBased(TestUsingFileBasedSheerka):
@classmethod
+13
View File
@@ -430,6 +430,19 @@ def test_i_can_deep_copy_a_custom_type():
assert core.utils.sheerka_deepcopy(Removed) is Removed
def test_i_can_deep_copy_concepts_than_look_the_same():
foo = Concept("foo")
foo2 = Concept().update_from(foo)
foo2.set_value("prop_name", "prop_value")
assert foo != foo2
copied_foo = core.utils.sheerka_deepcopy(foo)
copied_foo2 = core.utils.sheerka_deepcopy(foo2)
assert copied_foo != copied_foo2
assert len({copied_foo, copied_foo2}) == 2
@pytest.mark.parametrize("expression1, expression2, expected", [
("foo bar baz", "foo bar baz", True),
("foo()", " foo ( ) ", True),