Refactored Caching, Refactored BnfNodeParser, Introduced Sphinx

This commit is contained in:
2020-05-12 17:21:10 +02:00
parent 7d3a490bc5
commit 6e343ba996
110 changed files with 13865 additions and 7540 deletions
+175 -101
View File
@@ -1,79 +1,65 @@
from core.builtin_concepts import ConceptAlreadyInSet, BuiltinConcepts
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept
from tests.TestUsingFileBasedSheerka import TestUsingFileBasedSheerka
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
class TestSheerkaSetsManager(TestUsingFileBasedSheerka):
class TestSheerkaSetsManager(TestUsingMemoryBasedSheerka):
# 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_a_concept_to_a_set(self):
sheerka, context, foo, group = self.init_concepts(
Concept("foo"),
Concept("group"),
cache_only=False
)
assert sheerka.add_concept_to_set(context, foo, group).status
def test_i_can_add_concept_to_set(self):
sheerka, context, foo, all_foos = self.init_concepts(Concept("foo"), Concept("all_foo"), use_dict=False)
group_elements = sheerka.cache_manager.get(sheerka.CONCEPTS_GROUPS_ENTRY, group.id)
assert group_elements == {foo.id}
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(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):
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(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
# I can add another elements
foo3 = Concept("foo3")
foo4 = Concept("foo4")
for c in [foo3, foo4]:
sheerka.set_id_if_needed(c, False)
sheerka.add_in_cache(c)
res = sheerka.sets_handler.add_concepts_to_set(context, (foo3, foo4), all_foos)
assert res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
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
assert foo3.id in all_entries
assert foo4.id in all_entries
# it can be persisted
sheerka.cache_manager.commit(context)
assert sheerka.sdp.get(sheerka.CONCEPTS_GROUPS_ENTRY, group.id) == {foo.id}
def test_i_cannot_add_the_same_concept_twice_in_a_set(self):
sheerka, context, foo, all_foos = self.init_concepts(Concept("foo"), Concept("all_foos"))
sheerka, context, foo, group = self.init_concepts(Concept("foo"), Concept("group"))
sheerka.add_concept_to_set(context, foo, group)
sheerka.add_concept_to_set(context, foo, all_foos)
res = sheerka.add_concept_to_set(context, foo, all_foos)
# add again
res = sheerka.add_concept_to_set(context, foo, group)
assert not res.status
assert res.body == ConceptAlreadyInSet(foo, all_foos)
assert sheerka.isinstance(res.body, BuiltinConcepts.CONCEPT_ALREADY_IN_SET)
assert res.body.body == foo
assert res.body.concept_set == group
all_entries = sheerka.sdp.get("All_" + all_foos.id, None, False)
assert len(all_entries) == 1
assert foo.id in all_entries
all_entries = sheerka.cache_manager.get(sheerka.CONCEPTS_GROUPS_ENTRY, group.id)
assert all_entries == {foo.id}
def test_i_can_have_multiple_groups(self):
sheerka, context, foo, bar, baz, group1, group2 = self.init_concepts(
Concept("foo"),
Concept("bar"),
Concept("baz"),
Concept("group1"),
Concept("group2"),
cache_only=False
)
assert sheerka.add_concept_to_set(context, foo, group1).status
assert sheerka.add_concept_to_set(context, bar, group1).status
assert sheerka.add_concept_to_set(context, bar, group2).status
assert sheerka.add_concept_to_set(context, baz, group2).status
assert sheerka.cache_manager.get(sheerka.CONCEPTS_GROUPS_ENTRY, group1.id) == {foo.id, bar.id}
assert sheerka.cache_manager.get(sheerka.CONCEPTS_GROUPS_ENTRY, group2.id) == {baz.id, bar.id}
# I can save in db
sheerka.cache_manager.commit(context)
assert sheerka.sdp.get(sheerka.CONCEPTS_GROUPS_ENTRY) == {
'1004': {'1001', '1002'}, '1005': {'1002', '1003'}
}
def test_i_get_elements_from_a_set(self):
sheerka, context, one, two, three, number = self.init_concepts(
@@ -83,7 +69,6 @@ class TestSheerkaSetsManager(TestUsingFileBasedSheerka):
sheerka.add_concept_to_set(context, c, 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):
@@ -100,8 +85,9 @@ class TestSheerkaSetsManager(TestUsingFileBasedSheerka):
assert not sheerka.isaset(context, group)
assert not sheerka.isinset(foo, group)
context = self.get_context(sheerka)
context = self.get_context(sheerka) # another context ?
sheerka.add_concept_to_set(context, foo, group)
assert sheerka.isaset(context, group)
assert sheerka.isinset(foo, group)
@@ -112,7 +98,7 @@ class TestSheerkaSetsManager(TestUsingFileBasedSheerka):
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]
assert set(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(
@@ -122,15 +108,30 @@ class TestSheerkaSetsManager(TestUsingFileBasedSheerka):
Concept("four", body="4"),
Concept("five", body="5"),
Concept("number"),
Concept("sub_number", body="number", where="number < 4"),
create_new=True
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]
assert set(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):
def test_i_can_define_subset_of_subset(self):
sheerka, context, one, two, three, four, five, number, sub_number, sub_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"),
Concept("sub_sub_number", body="sub_number", where="sub_number > 2")
)
sheerka.sets_handler.add_concepts_to_set(context, [one, two, three, four, five], number)
assert sheerka.isaset(context, sub_sub_number)
assert set(sheerka.get_set_elements(context, sub_sub_number)) == {three}
def test_i_can_define_subset_of_another_set_when_some_concept_do_not_have_a_defined_body(self):
"""
Example
def concept unit from number where number <10
@@ -151,7 +152,7 @@ class TestSheerkaSetsManager(TestUsingFileBasedSheerka):
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]
assert set(sheerka.get_set_elements(context, sub_number)) == {one, three}
def test_i_can_define_subset_of_another_set_when_some_concept_are_bnf(self):
"""
@@ -190,8 +191,9 @@ class TestSheerkaSetsManager(TestUsingFileBasedSheerka):
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("twenties", definition="twenty (one|two)=unit", body="twenty + unit").def_var("unit"),
Concept("number"),
create_new=True
)
sheerka.sets_handler.add_concepts_to_set(context, [one, two, twenty, twenties], number)
@@ -200,39 +202,27 @@ class TestSheerkaSetsManager(TestUsingFileBasedSheerka):
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(
"foo", "all_foo",
create_new=True,
use_dict=False)
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(
sheerka, context, foo, all_foo, all_bar = self.init_concepts(
Concept("foo"),
Concept("all_foo"),
Concept("all_bar"),
use_dict=False)
sheerka.create_new_concept(context, foo)
create_new=True
)
sheerka.set_isa(context, foo, all_foos)
sheerka.set_isa(context, foo, all_bars)
foo = sheerka.new(foo.key) # new instance
sheerka.set_isa(context, foo, all_foo)
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)
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)
assert sheerka.isaset(context, all_bar)
def test_i_can_manage_isa_transitivity(self):
"""
@@ -244,10 +234,9 @@ class TestSheerkaSetsManager(TestUsingFileBasedSheerka):
Concept("foo"),
Concept("bar"),
Concept("baz"),
create_new=True
)
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)
@@ -255,3 +244,88 @@ class TestSheerkaSetsManager(TestUsingFileBasedSheerka):
assert sheerka.isa(foo, bar)
assert sheerka.isa(bar, baz)
assert sheerka.isa(foo, baz)
class TestSheerkaSetsManagerUsingFileBasedSheerka(TestUsingFileBasedSheerka):
def test_i_can_add_concept_to_set_and_retrieve_it_in_another_session(self):
sheerka, context, foo, bar, group = self.init_concepts(
Concept("foo"),
Concept("bar"),
Concept("group"),
create_new=True)
assert sheerka.add_concept_to_set(context, foo, group).status
sheerka.cache_manager.commit(context)
sheerka = self.get_sheerka() # another session
assert sheerka.add_concept_to_set(context, bar, group).status
# I can get the elements
assert set(sheerka.get_set_elements(context, group)) == {foo, bar}
sheerka.cache_manager.commit(context) # save in db
all_entries = sheerka.sdp.get(sheerka.CONCEPTS_GROUPS_ENTRY) # check the db
assert all_entries == {
group.id: {foo.id, bar.id}
}
# I can also add a group another elements
sheerka = self.get_sheerka()
foo3 = Concept("foo3")
foo4 = Concept("foo4")
for c in [foo3, foo4]:
sheerka.create_new_concept(context, c)
res = sheerka.sets_handler.add_concepts_to_set(context, (foo3, foo4), group)
assert res.status
# I can get the elements
assert set(sheerka.get_set_elements(context, group)) == {foo, bar, foo3, foo4}
sheerka.cache_manager.commit(context) # save in db
all_entries = sheerka.sdp.get(sheerka.CONCEPTS_GROUPS_ENTRY) # check the db
assert all_entries == {
group.id: {foo.id, bar.id, foo3.id, foo4.id}
}
def test_i_can_set_isa(self):
sheerka, context, foo, bar, group = self.init_concepts(
"foo",
"bar",
"group",
create_new=True, # needed by modify
)
# nothing was previously in ISA
foo = sheerka.new(foo.key)
assert BuiltinConcepts.ISA not in foo.metadata.props
res = sheerka.set_isa(context, foo, group)
assert res.status
sheerka.cache_manager.commit(context)
sheerka = self.get_sheerka()
assert foo.get_prop(BuiltinConcepts.ISA) == {group}
assert sheerka.isa(foo, group)
assert sheerka.isinset(foo, group)
assert sheerka.isaset(context, group)
# I can do the same for bar
sheerka = self.get_sheerka()
res = sheerka.set_isa(context, bar, group)
assert res.status
assert bar.get_prop(BuiltinConcepts.ISA) == {group}
assert sheerka.isa(bar, group)
assert sheerka.isinset(bar, group)
assert sheerka.isaset(context, group)
sheerka.cache_manager.commit(context)
# they are both in the same group
sheerka = self.get_sheerka()
all_entries = sheerka.sdp.get(sheerka.CONCEPTS_GROUPS_ENTRY)
assert all_entries == {
group.id: {foo.id, bar.id}
}
elements = sheerka.get_set_elements(context, group)
assert set(elements) == {foo, bar}