Added set of set handling (thru concept ISA)
This commit is contained in:
@@ -457,3 +457,4 @@ class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
|
||||
evaluated = sheerka.evaluate_concept(self.get_context(sheerka), one)
|
||||
assert evaluated.key == one.key
|
||||
assert evaluated.body == 1
|
||||
|
||||
|
||||
@@ -6,41 +6,39 @@ from tests.TestUsingFileBasedSheerka import TestUsingFileBasedSheerka
|
||||
|
||||
class TestSheerkaSetsManager(TestUsingFileBasedSheerka):
|
||||
|
||||
def init(self, use_dict, *concepts):
|
||||
sheerka = self.get_sheerka(use_dict, True)
|
||||
for c in concepts:
|
||||
sheerka.set_id_if_needed(c, False)
|
||||
sheerka.add_in_cache(c)
|
||||
|
||||
context = self.get_context(sheerka)
|
||||
return sheerka, context
|
||||
# def init(self, use_dict, *concepts):
|
||||
# sheerka = self.get_sheerka(use_dict, True)
|
||||
# for c in concepts:
|
||||
# sheerka.set_id_if_needed(c, False)
|
||||
# sheerka.add_in_cache(c)
|
||||
#
|
||||
# context = self.get_context(sheerka)
|
||||
# return sheerka, context
|
||||
|
||||
def test_i_can_add_concept_to_set(self):
|
||||
foo = Concept("foo")
|
||||
all_foos = Concept("all_foos")
|
||||
sheerka, context = self.init(False, foo, all_foos)
|
||||
sheerka, context, foo, all_foos = self.init_concepts(Concept("foo"), Concept("all_foo"), use_dict=False)
|
||||
|
||||
res = sheerka.add_concept_to_set(context, foo, all_foos)
|
||||
|
||||
assert res.status
|
||||
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
|
||||
|
||||
all_entries = self.get_sheerka(False, True).sdp.get("All_" + all_foos.id, None, False)
|
||||
all_entries = self.get_sheerka(use_dict=False).sdp.get("All_" + all_foos.id, None, False)
|
||||
assert len(all_entries) == 1
|
||||
assert foo.id in all_entries
|
||||
|
||||
def test_i_can_add_several_concepts_to_set(self):
|
||||
foo1 = Concept("foo1")
|
||||
foo2 = Concept("foo2")
|
||||
all_foos = Concept("all_foos")
|
||||
sheerka, context = self.init(False, foo1, foo2, all_foos)
|
||||
sheerka, context, foo1, foo2, all_foos = self.init_concepts(
|
||||
Concept("foo1"),
|
||||
Concept("foo2"),
|
||||
Concept("all_foo"), use_dict=False)
|
||||
|
||||
res = sheerka.sets_handler.add_concepts_to_set(context, (foo1, foo2), all_foos)
|
||||
|
||||
assert res.status
|
||||
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
|
||||
|
||||
all_entries = self.get_sheerka(False, False).sdp.get("All_" + all_foos.id, None, False)
|
||||
all_entries = self.get_sheerka(use_dict=False).sdp.get("All_" + all_foos.id, None, False)
|
||||
assert len(all_entries) == 2
|
||||
assert foo1.id in all_entries
|
||||
assert foo2.id in all_entries
|
||||
@@ -57,7 +55,7 @@ class TestSheerkaSetsManager(TestUsingFileBasedSheerka):
|
||||
assert res.status
|
||||
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
|
||||
|
||||
all_entries = self.get_sheerka(False, False).sdp.get("All_" + all_foos.id, None, False)
|
||||
all_entries = self.get_sheerka(use_dict=False).sdp.get("All_" + all_foos.id, None, False)
|
||||
assert len(all_entries) == 4
|
||||
assert foo1.id in all_entries
|
||||
assert foo2.id in all_entries
|
||||
@@ -65,9 +63,7 @@ class TestSheerkaSetsManager(TestUsingFileBasedSheerka):
|
||||
assert foo4.id in all_entries
|
||||
|
||||
def test_i_cannot_add_the_same_concept_twice_in_a_set(self):
|
||||
foo = Concept("foo")
|
||||
all_foos = Concept("all_foos")
|
||||
sheerka, context = self.init(True, foo, all_foos)
|
||||
sheerka, context, foo, all_foos = self.init_concepts(Concept("foo"), Concept("all_foos"))
|
||||
|
||||
sheerka.add_concept_to_set(context, foo, all_foos)
|
||||
res = sheerka.add_concept_to_set(context, foo, all_foos)
|
||||
@@ -80,40 +76,179 @@ class TestSheerkaSetsManager(TestUsingFileBasedSheerka):
|
||||
assert foo.id in all_entries
|
||||
|
||||
def test_i_get_elements_from_a_set(self):
|
||||
one = Concept("one")
|
||||
two = Concept("two")
|
||||
three = Concept("three")
|
||||
number = Concept("number")
|
||||
sheerka, context = self.init(True, one, two, three, number)
|
||||
sheerka, context, one, two, three, number = self.init_concepts(
|
||||
Concept("one"), Concept("two"), Concept("three"), Concept("number"))
|
||||
|
||||
for c in [one, two, three]:
|
||||
sheerka.add_concept_to_set(context, c, number)
|
||||
|
||||
elements = sheerka.get_set_elements(number)
|
||||
elements = sheerka.get_set_elements(context, number)
|
||||
|
||||
assert set(elements) == {one, two, three}
|
||||
|
||||
def test_i_cannot_get_elements_if_not_a_set(self):
|
||||
one = Concept("one")
|
||||
sheerka, context = self.init(True, one)
|
||||
sheerka, context, one = self.init_concepts(Concept("one"))
|
||||
|
||||
error = sheerka.get_set_elements(one)
|
||||
error = sheerka.get_set_elements(context, one)
|
||||
|
||||
assert sheerka.isinstance(error, BuiltinConcepts.NOT_A_SET)
|
||||
assert error.body == one
|
||||
|
||||
def test_isa_and_isa_group(self):
|
||||
group = Concept("group")
|
||||
foo = Concept("foo")
|
||||
sheerka, context = self.init(True, group, foo)
|
||||
sheerka, context, group, foo = self.init_concepts(Concept("group"), Concept("foo"))
|
||||
|
||||
assert not sheerka.isaset(group)
|
||||
assert not sheerka.isa(foo, group)
|
||||
assert not sheerka.isaset(context, group)
|
||||
assert not sheerka.isinset(foo, group)
|
||||
|
||||
context = self.get_context(sheerka)
|
||||
sheerka.add_concept_to_set(context, foo, group)
|
||||
assert sheerka.isaset(group)
|
||||
assert sheerka.isa(foo, group)
|
||||
assert sheerka.isaset(context, group)
|
||||
assert sheerka.isinset(foo, group)
|
||||
|
||||
def test_i_can_a_multiples_concepts(self):
|
||||
pass
|
||||
def test_i_can_define_a_group_from_another_group(self):
|
||||
sheerka, context, foo, bar, group1, group2 = self.init_concepts(
|
||||
Concept("foo"), Concept("bar"), Concept("group1"), Concept("group2", body="group1"))
|
||||
|
||||
sheerka.sets_handler.add_concepts_to_set(context, [foo, bar], group1)
|
||||
|
||||
assert sheerka.isaset(context, group2)
|
||||
assert sheerka.get_set_elements(context, group2) == [foo, bar]
|
||||
|
||||
def test_i_can_define_subset_of_another_group(self):
|
||||
sheerka, context, one, two, three, four, five, number, sub_number = self.init_concepts(
|
||||
Concept("one", body="1"),
|
||||
Concept("two", body="2"),
|
||||
Concept("three", body="3"),
|
||||
Concept("four", body="4"),
|
||||
Concept("five", body="5"),
|
||||
Concept("number"),
|
||||
Concept("sub_number", body="number", where="number < 4")
|
||||
)
|
||||
sheerka.sets_handler.add_concepts_to_set(context, [one, two, three, four, five], number)
|
||||
|
||||
assert sheerka.isaset(context, sub_number)
|
||||
assert sheerka.get_set_elements(context, sub_number) == [one, two, three]
|
||||
|
||||
def test_i_can_define_subset_of_another_set_when_some_concept_dont_have_a_defined_body(self):
|
||||
"""
|
||||
Example
|
||||
def concept unit from number where number <10
|
||||
It implies that all elements in number have a (numeric) body
|
||||
What if its not the case ?
|
||||
:return:
|
||||
"""
|
||||
|
||||
sheerka, context, one, two, three, four, five, number, sub_number = self.init_concepts(
|
||||
Concept("one", body="1"),
|
||||
Concept("two"),
|
||||
Concept("three", body="3"),
|
||||
Concept("four"),
|
||||
Concept("five", body="5"),
|
||||
Concept("number"),
|
||||
Concept("sub_number", body="number", where="number < 4")
|
||||
)
|
||||
sheerka.sets_handler.add_concepts_to_set(context, [one, two, three, four, five], number)
|
||||
|
||||
assert sheerka.isaset(context, sub_number)
|
||||
assert sheerka.get_set_elements(context, sub_number) == [one, three]
|
||||
|
||||
def test_i_can_define_subset_of_another_set_when_some_concept_are_bnf(self):
|
||||
"""
|
||||
Other case:
|
||||
twenties isa number, but its body is a source code, not a constant numeric number
|
||||
is it included ?
|
||||
:return:
|
||||
"""
|
||||
|
||||
sheerka, context, one, two, twenty, twenties, number, sub_number = self.init_concepts(
|
||||
Concept("one", body="1"),
|
||||
Concept("two", body="2"),
|
||||
Concept("twenty", body="20"),
|
||||
Concept("twenties", definition="twenty (one|two)=unit", body="twenty + unit"),
|
||||
Concept("number"),
|
||||
Concept("sub_number", body="number", where="number >= 20")
|
||||
)
|
||||
|
||||
sheerka.sets_handler.add_concepts_to_set(context, [one, two, twenty, twenties], number)
|
||||
|
||||
assert sheerka.isaset(context, sub_number)
|
||||
assert sheerka.get_set_elements(context, sub_number) == [twenty] # what is expected ?
|
||||
|
||||
def test_bnf_elements_can_be_part_of_a_set(self):
|
||||
"""
|
||||
The purpose of this test is
|
||||
def concept twenties from bnf ...
|
||||
twenties isa number
|
||||
|
||||
assert twenty two isa number
|
||||
We want that all elements of the twenties are number
|
||||
:return:
|
||||
"""
|
||||
|
||||
sheerka, context, one, two, twenty, twenties, number = self.init_concepts(
|
||||
Concept("one", body="1"),
|
||||
Concept("two", body="2"),
|
||||
Concept("twenty", body="20"),
|
||||
Concept("twenties", definition="twenty (one|two)=unit", body="twenty + unit").def_prop("unit"),
|
||||
Concept("number"),
|
||||
)
|
||||
|
||||
sheerka.sets_handler.add_concepts_to_set(context, [one, two, twenty, twenties], number)
|
||||
assert sheerka.isinset(twenties, number)
|
||||
|
||||
twenty_one = sheerka.evaluate_user_input("twenty one", "")[0].body
|
||||
assert sheerka.isinset(twenty_one, number)
|
||||
|
||||
def test_i_can_set_isa(self):
|
||||
sheerka, context, foo, all_foos = self.init_concepts(Concept("foo"), Concept("all_foo"), use_dict=False)
|
||||
sheerka.create_new_concept(context, foo)
|
||||
|
||||
assert BuiltinConcepts.ISA not in foo.props
|
||||
|
||||
sheerka.set_isa(context, foo, all_foos)
|
||||
|
||||
assert foo.get_prop(BuiltinConcepts.ISA) == [all_foos]
|
||||
assert sheerka.isa(foo, all_foos)
|
||||
assert sheerka.isinset(foo, all_foos)
|
||||
assert sheerka.isaset(context, all_foos)
|
||||
|
||||
def test_a_concept_can_be_in_multiple_sets(self):
|
||||
sheerka, context, foo, all_foos, all_bars = self.init_concepts(
|
||||
Concept("foo"),
|
||||
Concept("all_foo"),
|
||||
Concept("all_bar"),
|
||||
use_dict=False)
|
||||
sheerka.create_new_concept(context, foo)
|
||||
|
||||
sheerka.set_isa(context, foo, all_foos)
|
||||
sheerka.set_isa(context, foo, all_bars)
|
||||
|
||||
assert foo.get_prop(BuiltinConcepts.ISA) == [all_foos, all_bars]
|
||||
assert sheerka.isa(foo, all_foos)
|
||||
assert sheerka.isa(foo, all_bars)
|
||||
assert sheerka.isinset(foo, all_foos)
|
||||
assert sheerka.isinset(foo, all_bars)
|
||||
assert sheerka.isaset(context, all_foos)
|
||||
assert sheerka.isaset(context, all_bars)
|
||||
|
||||
def test_i_can_manage_isa_transitivity(self):
|
||||
"""
|
||||
if foo isa bar and bar isa baz, then foo isa baz
|
||||
:return:
|
||||
"""
|
||||
|
||||
sheerka, context, foo, bar, baz = self.init_concepts(
|
||||
Concept("foo"),
|
||||
Concept("bar"),
|
||||
Concept("baz"),
|
||||
)
|
||||
sheerka.create_new_concept(context, foo)
|
||||
sheerka.create_new_concept(context, bar)
|
||||
sheerka.create_new_concept(context, 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)
|
||||
|
||||
+13
-13
@@ -42,7 +42,7 @@ def my_function(a,b):
|
||||
assert tree_as_concept.node_type == "Module"
|
||||
assert sheerka.isinstance(tree_as_concept.get_prop("body"), BuiltinConcepts.LIST)
|
||||
|
||||
def_func = tree_as_concept.get_prop("body")[0]
|
||||
def_func = tree_as_concept.get_prop("body").body[0]
|
||||
assert sheerka.isinstance(def_func, BuiltinConcepts.GENERIC_NODE)
|
||||
assert def_func.node_type == "FunctionDef"
|
||||
assert def_func.parent == NodeParent(tree_as_concept, "body")
|
||||
@@ -54,27 +54,27 @@ def my_function(a,b):
|
||||
|
||||
def_func_args_real_args = def_func_args.get_prop("args")
|
||||
assert sheerka.isinstance(def_func_args_real_args, BuiltinConcepts.LIST)
|
||||
assert len(def_func_args_real_args) == 2
|
||||
assert len(def_func_args_real_args.body) == 2
|
||||
|
||||
assert sheerka.isinstance(def_func_args_real_args[0], BuiltinConcepts.GENERIC_NODE)
|
||||
assert def_func_args_real_args[0].node_type == "arg"
|
||||
assert def_func_args_real_args[0].parent == NodeParent(def_func_args, "args")
|
||||
assert def_func_args_real_args[0].get_prop("arg") == "a"
|
||||
assert sheerka.isinstance(def_func_args_real_args[1], BuiltinConcepts.GENERIC_NODE)
|
||||
assert def_func_args_real_args[1].node_type == "arg"
|
||||
assert def_func_args_real_args[1].parent == NodeParent(def_func_args, "args")
|
||||
assert def_func_args_real_args[1].get_prop("arg") == "b"
|
||||
assert sheerka.isinstance(def_func_args_real_args.body[0], BuiltinConcepts.GENERIC_NODE)
|
||||
assert def_func_args_real_args.body[0].node_type == "arg"
|
||||
assert def_func_args_real_args.body[0].parent == NodeParent(def_func_args, "args")
|
||||
assert def_func_args_real_args.body[0].get_prop("arg") == "a"
|
||||
assert sheerka.isinstance(def_func_args_real_args.body[1], BuiltinConcepts.GENERIC_NODE)
|
||||
assert def_func_args_real_args.body[1].node_type == "arg"
|
||||
assert def_func_args_real_args.body[1].parent == NodeParent(def_func_args, "args")
|
||||
assert def_func_args_real_args.body[1].get_prop("arg") == "b"
|
||||
|
||||
def_fun_body = def_func.get_prop("body")
|
||||
assert sheerka.isinstance(def_fun_body, BuiltinConcepts.LIST)
|
||||
assert len(def_fun_body) == 2
|
||||
assert len(def_fun_body.body) == 2
|
||||
|
||||
def_fun_body_for = def_fun_body[0]
|
||||
def_fun_body_for = def_fun_body.body[0]
|
||||
assert sheerka.isinstance(def_fun_body_for, BuiltinConcepts.GENERIC_NODE)
|
||||
assert def_fun_body_for.node_type == "For"
|
||||
assert def_fun_body_for.parent == NodeParent(def_func, "body")
|
||||
|
||||
def_fun_body_return = def_fun_body[1]
|
||||
def_fun_body_return = def_fun_body.body[1]
|
||||
assert sheerka.isinstance(def_fun_body_return, BuiltinConcepts.GENERIC_NODE)
|
||||
assert def_fun_body_return.node_type == "Return"
|
||||
assert def_fun_body_return.parent == NodeParent(def_func, "body")
|
||||
|
||||
@@ -134,6 +134,7 @@ class TestBuiltinHelpers(TestUsingMemoryBasedSheerka):
|
||||
("a == 1 or b == 2", ["b"], [], ["b == 2"]),
|
||||
("a == 1 or b == 2", ["a", "b"], [], ["a == 1 or b == 2"]),
|
||||
("predicate(a,c) or predicate(b,c)", ["a", "b"], [], ["predicate(a,c) or predicate(b,c)"]),
|
||||
("a < 1 and a > b", ["a"], [], ["a < 1 and a > b"]),
|
||||
])
|
||||
def test_i_can_extract_predicates(self, expression, vars_to_include, vars_to_exclude, expected_expr):
|
||||
sheerka = self.get_sheerka()
|
||||
|
||||
@@ -38,12 +38,12 @@ class TestSheerka(TestUsingFileBasedSheerka):
|
||||
assert isinstance(sheerka.cache_by_key[key], concept_class)
|
||||
|
||||
def test_builtin_concepts_can_be_updated(self):
|
||||
sheerka = self.get_sheerka(False, False)
|
||||
sheerka = self.get_sheerka(use_dict=False, skip_builtins_in_db=False)
|
||||
loaded_sheerka = sheerka.get(BuiltinConcepts.SHEERKA)
|
||||
loaded_sheerka.metadata.desc = "I have a description"
|
||||
sheerka.sdp.modify("Test", sheerka.CONCEPTS_ENTRY, loaded_sheerka.key, loaded_sheerka)
|
||||
|
||||
sheerka = self.get_sheerka(False, False)
|
||||
sheerka = self.get_sheerka(use_dict=False, skip_builtins_in_db=False)
|
||||
loaded_sheerka = sheerka.get(BuiltinConcepts.SHEERKA)
|
||||
|
||||
assert loaded_sheerka.metadata.desc == "I have a description"
|
||||
@@ -233,7 +233,7 @@ class TestSheerka(TestUsingFileBasedSheerka):
|
||||
assert sheerka.value(concept, reduce_simple_list) == expected
|
||||
|
||||
def test_list_of_concept_is_sorted_by_id(self):
|
||||
sheerka = self.get_sheerka(False, False)
|
||||
sheerka = self.get_sheerka(use_dict=False, skip_builtins_in_db=False)
|
||||
concepts = sheerka.concepts()
|
||||
|
||||
assert concepts[0].id < concepts[-1].id
|
||||
|
||||
Reference in New Issue
Block a user