257 lines
9.5 KiB
Python
257 lines
9.5 KiB
Python
from core.builtin_concepts import ConceptAlreadyInSet, BuiltinConcepts
|
|
from core.concept import Concept
|
|
|
|
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 test_i_can_add_concept_to_set(self):
|
|
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(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
|
|
|
|
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.add_concept_to_set(context, foo, all_foos)
|
|
res = sheerka.add_concept_to_set(context, foo, all_foos)
|
|
|
|
assert not res.status
|
|
assert res.body == ConceptAlreadyInSet(foo, all_foos)
|
|
|
|
all_entries = sheerka.sdp.get("All_" + all_foos.id, None, False)
|
|
assert len(all_entries) == 1
|
|
assert foo.id in all_entries
|
|
|
|
def test_i_get_elements_from_a_set(self):
|
|
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(context, number)
|
|
|
|
assert set(elements) == {one, two, three}
|
|
|
|
def test_i_cannot_get_elements_if_not_a_set(self):
|
|
sheerka, context, one = self.init_concepts(Concept("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):
|
|
sheerka, context, group, foo = self.init_concepts(Concept("group"), Concept("foo"))
|
|
|
|
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(context, group)
|
|
assert sheerka.isinset(foo, group)
|
|
|
|
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(
|
|
"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(
|
|
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)
|