from core.builtin_concepts import ConceptEvalError from core.builtin_helpers import only_successful from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka class TestSheerkaNonRegMemory2(TestUsingMemoryBasedSheerka): def test_i_can_select_the_correct_concept_when_ambiguity(self): init = [ "def concept foo", "def concept x is a y pre is_question() as isa(x,y)", "def concept x is a foo pre is_question() as isinstance(x, foo)", ] sheerka = self.init_scenario(init) res = sheerka.evaluate_user_input("question(foo is a foo)") assert len(res) == 1 assert res[0].status assert res[0].value # value is True since 'x is a foo' was chosen def test_i_can_select_the_correct_concept_vs_bnf(self): init = [ "def concept one as 1", "def concept two as 2", "def concept twenties from bnf 'twenty' (one|two)=unit as 20 + unit", "def concept twenty two as 'specific occurrence'", ] sheerka = self.init_scenario(init) res = sheerka.evaluate_user_input("eval twenty one") assert res[0].value == 21 res = sheerka.evaluate_user_input("eval twenty two") assert res[0].value == "specific occurrence" # thanks to ExactConceptParser def test_i_can_use_global_truth_when_calling_a_concept(self): init = [ "def concept one", "def concept number", "def concept x is a y as set_isa(x, y)", ] sheerka = self.init_scenario(init) res = sheerka.evaluate_user_input("global_truth(one is a number)") assert res[0].status assert sheerka.isa(sheerka.new("one"), sheerka.new("number")) def test_i_can_get_sequence_when_evaluation_plural(self): init = [ "def concept one", "def concept two", "def concept number", "global_truth(set_isa(one, number))", "global_truth(set_isa(two, number))", ] sheerka = self.init_scenario(init) res = sheerka.evaluate_user_input("eval numbers") assert res[0].status assert set(res[0].body) == {sheerka.new("one"), sheerka.new("two")} def test_i_can_use_list_comprehension(self): init = [ "def concept rex", "def concept rantanplan", "def concept dog", "def concept x is a y as set_isa(x, y)", ] sheerka = self.init_scenario(init) res = sheerka.evaluate_user_input("global_truth([ x is a dog for x in [rex, rantanplan]])") assert len(res) == 1 assert res[0].status rex = sheerka.new("rex") dog = sheerka.new("dog") assert sheerka.isa(rex, dog) def test_fixing_maximum_recursion_depth_exceeded(self): init = [ "def concept x and y pre is_question() as x and y", "def concept x or y pre is_question() as x or y", "def concept or from a or b as a or b", "def concept and", ] sheerka = self.init_scenario(init) context = self.get_context(sheerka) res = sheerka.evaluate_user_input("or or and") res = only_successful(context, res) assert len(res.body.body) == 3 # note that there will be only one result when the sya node parser will fix its duplicate results issue def test_121_plural_are_not_updated_when_new_elements_are_added(self): init = [ "def concept animal", "def concept dog", "def concept a x is an y as set_isa(x, y)", "eval animals", "global_truth(a dog is an animal)", ] sheerka = self.init_scenario(init) res = sheerka.evaluate_user_input("eval animals") assert len(res) == 1 assert res[0].status assert res[0].body == [sheerka.new("dog")] def test_105_TOO_MANY_ERROR_is_not_the_relevant_error_when_results_are_filtered(self): init = [ "def concept foo", "def concept bar", "def concept x is a y pre is_question() def_var x def_var y", "def concept x is a y as raise NotImplementedError() def_var x def_var y", ] sheerka = self.init_scenario(init) res = sheerka.evaluate_user_input("eval foo is a bar") assert len(res) == 1 assert not res[0].status assert isinstance(res[0].body, ConceptEvalError) def test_74_keyword_parameters_are_no_longer_recognized_when_a_concept_that_redefines_equality_is_created(self): init = [ "def concept a=b as a=b", ] sheerka = self.init_scenario(init) bag = {} def test_function(**kwargs): nonlocal bag bag.update(kwargs) sheerka.add_to_short_term_memory(None, "test_function", test_function) res = sheerka.evaluate_user_input("test_function(id=11)") assert len(res) == 1 assert res[0].status assert bag["id"] == 11