Introduced ConceptsAlgebra

This commit is contained in:
2020-09-27 20:28:50 +02:00
parent 978e5a5939
commit d100b7e8b3
18 changed files with 541 additions and 50 deletions
+1 -1
View File
@@ -334,4 +334,4 @@ class TestSheerkaGreaterThanManager(TestUsingMemoryBasedSheerka):
res = self.execution_definition(context, service, concepts_map, definition)
assert not res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.ALREADY_DEFINED)
assert sheerka.isinstance(res.body, BuiltinConcepts.CONCEPT_ALREADY_DEFINED)
+175
View File
@@ -0,0 +1,175 @@
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept
from core.sheerka.services.SheerkaConceptsAlgebra import ConceptScore
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
class TestSheerkaConceptsAlgebra(TestUsingMemoryBasedSheerka):
def test_i_can_add_concepts(self):
sheerka, context, man, human, male, driver, licence, car = self.init_concepts(
"man", "human", "male",
"driver", "licence", "car")
sheerka.set_isa(context, sheerka.new("man"), human)
sheerka.set_isa(context, sheerka.new("man"), male)
sheerka.set_hasa(context, sheerka.new("driver"), licence)
sheerka.set_hasa(context, sheerka.new("driver"), car)
res = sheerka.cadd(context, sheerka.new("man"), sheerka.new("driver"))
assert isinstance(res, Concept)
assert res.metadata.props == {BuiltinConcepts.ISA: {male, human},
BuiltinConcepts.HASA: {car, licence}, }
def test_can_add_concepts_when_property_already_exist(self):
sheerka, context, man, human, king, male = self.init_concepts(
"man", "human", "king", "male")
sheerka.set_isa(context, sheerka.new("man"), human)
sheerka.set_isa(context, sheerka.new("king"), male)
res = sheerka.cadd(context, sheerka.new("man"), sheerka.new("king"))
assert isinstance(res, Concept)
assert res.metadata.props == {BuiltinConcepts.ISA: {male, human}}
def test_i_can_subtract_concepts(self):
sheerka, context, foo, bar, isa1, isa2, hasa1, hasa2 = self.init_concepts(
"foo", "bar",
"isa1", "isa2", "has1", "has2")
new_foo = sheerka.new("foo")
sheerka.set_isa(context, new_foo, isa1)
sheerka.set_isa(context, new_foo, isa2)
sheerka.set_hasa(context, new_foo, hasa1)
sheerka.set_hasa(context, new_foo, hasa2)
new_bar = sheerka.new("bar")
sheerka.set_isa(context, new_bar, isa1)
sheerka.set_hasa(context, new_bar, hasa1)
assert sheerka.csub(context) == Concept()
res = sheerka.csub(context, new_foo)
assert isinstance(res, Concept)
assert res.metadata.props == res.metadata.props
res = sheerka.csub(context, new_foo, new_bar)
assert isinstance(res, Concept)
assert res.metadata.props == {BuiltinConcepts.ISA: {isa2},
BuiltinConcepts.HASA: {hasa2}, }
def test_i_can_recognize_myself_when_cache_only_is_not_set(self):
sheerka, context, foo, isa1, hasa1, = self.init_concepts("foo", "isa1", "has1",
cache_only=False,
create_new=True)
sheerka.cache_manager.commit(context)
new_foo = sheerka.new("foo")
sheerka.set_isa(context, new_foo, isa1)
sheerka.set_hasa(context, new_foo, hasa1)
sheerka.cache_manager.commit(context)
assert sheerka.recognize(new_foo, all_scores=True) == [ConceptScore(1, new_foo, new_foo)]
def test_i_can_recognize_myself_when_cache_only_is_set(self):
sheerka, context, foo, isa1, hasa1, = self.init_concepts("foo", "isa1", "has1")
new_foo = sheerka.new("foo")
sheerka.set_isa(context, new_foo, isa1)
sheerka.set_hasa(context, new_foo, hasa1)
assert sheerka.recognize(new_foo, all_scores=True) == [ConceptScore(1, new_foo, new_foo)]
def test_unknown_prop_is_returned_when_nothing_is_recognized(self):
sheerka, context, isa1, hasa1, = self.init_concepts("isa1", "has1")
foo = Concept()
foo.add_prop(BuiltinConcepts.ISA, isa1)
foo.add_prop(BuiltinConcepts.HASA, hasa1)
res = sheerka.recognize(foo, all_scores=True)
assert sheerka.isinstance(res, BuiltinConcepts.UNKNOWN_CONCEPT)
assert res.body == [(BuiltinConcepts.ISA, {isa1}), (BuiltinConcepts.HASA, {hasa1})]
def test_empty_list_is_return_when_there_is_noting_to_recognize(self):
sheerka, context = self.init_concepts()
assert sheerka.recognize(Concept(), all_scores=True) == []
def test_i_can_recognize_multiple_concepts_with_the_proper_score(self):
sheerka, context, foo, bar, isa1, isa2, hasa1, hasa2 = self.init_concepts(
"foo", "bar",
"isa1", "isa2", "has1", "has2")
new_foo = sheerka.new("foo")
sheerka.set_isa(context, new_foo, isa1)
sheerka.set_isa(context, new_foo, isa2)
sheerka.set_hasa(context, new_foo, hasa1)
sheerka.set_hasa(context, new_foo, hasa2)
new_bar = sheerka.new("bar")
sheerka.set_isa(context, new_bar, isa1)
sheerka.set_hasa(context, new_bar, hasa1)
to_recognize = Concept()
to_recognize.add_prop(BuiltinConcepts.ISA, isa1)
to_recognize.add_prop(BuiltinConcepts.HASA, hasa1)
res = sheerka.recognize(to_recognize, all_scores=True)
assert res == [ConceptScore(1.0, sheerka.new("bar"), to_recognize),
ConceptScore(0.5, sheerka.new("foo"), to_recognize)]
# add the other properties, to match foo
to_recognize.add_prop(BuiltinConcepts.ISA, isa2)
to_recognize.add_prop(BuiltinConcepts.HASA, hasa2)
res = sheerka.recognize(to_recognize, all_scores=True)
assert res == [ConceptScore(1.0, sheerka.new("foo"), to_recognize),
ConceptScore(0.5, sheerka.new("bar"), to_recognize)]
def test_i_can_recognize_if_all_scores_is_disabled(self):
sheerka, context, foo, bar, isa1, isa2, hasa1, hasa2 = self.init_concepts(
"foo", "bar",
"isa1", "isa2", "has1", "has2")
new_foo = sheerka.new("foo")
sheerka.set_isa(context, new_foo, isa1)
sheerka.set_isa(context, new_foo, isa2)
sheerka.set_hasa(context, new_foo, hasa1)
sheerka.set_hasa(context, new_foo, hasa2)
new_bar = sheerka.new("bar")
sheerka.set_isa(context, new_bar, isa1)
sheerka.set_hasa(context, new_bar, hasa1)
to_recognize = Concept()
to_recognize.add_prop(BuiltinConcepts.ISA, isa1)
to_recognize.add_prop(BuiltinConcepts.HASA, hasa1)
res = sheerka.recognize(to_recognize, all_scores=False)
assert res == ConceptScore(1.0, sheerka.new("bar"), to_recognize)
def test_i_can_recognize_if_all_scores_is_disabled_but_multiple_high_scores(self):
sheerka, context, foo, bar, isa1, hasa1 = self.init_concepts(
"foo", "bar",
"isa1", "has1")
new_foo = sheerka.new("foo")
sheerka.set_isa(context, new_foo, isa1)
sheerka.set_hasa(context, new_foo, hasa1)
new_bar = sheerka.new("bar")
sheerka.set_isa(context, new_bar, isa1)
sheerka.set_hasa(context, new_bar, hasa1)
to_recognize = Concept()
to_recognize.add_prop(BuiltinConcepts.ISA, isa1)
to_recognize.add_prop(BuiltinConcepts.HASA, hasa1)
res = sheerka.recognize(to_recognize, all_scores=False)
assert len(res) == 2
assert ConceptScore(1.0, sheerka.new("foo"), to_recognize) in res
assert ConceptScore(1.0, sheerka.new("bar"), to_recognize) in res
+2 -2
View File
@@ -99,7 +99,7 @@ class TestSheerkaCreateNewConcept(TestUsingMemoryBasedSheerka):
res = sheerka.create_new_concept(self.get_context(sheerka), concept)
assert not res.status
assert sheerka.isinstance(res.value, BuiltinConcepts.ALREADY_DEFINED)
assert sheerka.isinstance(res.value, BuiltinConcepts.CONCEPT_ALREADY_DEFINED)
assert res.value.body == concept
def test_i_can_get_a_newly_created_concept(self):
@@ -260,7 +260,7 @@ class TestSheerkaCreateNewConceptFileBased(TestUsingFileBasedSheerka):
res = sheerka.create_new_concept(context, concept)
assert not res.status
assert sheerka.isinstance(res.value, BuiltinConcepts.ALREADY_DEFINED)
assert sheerka.isinstance(res.value, BuiltinConcepts.CONCEPT_ALREADY_DEFINED)
assert res.value.body == concept
def test_new_entry_does_not_override_the_previous_ones(self):
+17
View File
@@ -0,0 +1,17 @@
from core.builtin_concepts import BuiltinConcepts
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
class TestSheerkaHasAManager(TestUsingMemoryBasedSheerka):
def test_i_can_set_hasa(self):
sheerka, context, king, kingdom = self.init_concepts("king", "kingdom")
res = sheerka.set_hasa(context, sheerka.new("king"), kingdom)
assert res.status
another_king = sheerka.get_by_key("king")
assert another_king.get_prop(BuiltinConcepts.HASA) == {kingdom}
# check that the definition of the concept has been updated
assert sheerka.hasa(sheerka.new("king"), kingdom)
+1 -1
View File
@@ -73,7 +73,7 @@ class TestSheerkaModifyConcept(TestUsingMemoryBasedSheerka):
res = sheerka.modify_concept(context, foo)
assert not res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.ALREADY_DEFINED)
assert sheerka.isinstance(res.body, BuiltinConcepts.CONCEPT_ALREADY_DEFINED)
def test_i_can_modify_a_concept_that_is_in_a_list(self):
sheerka, context, foo1, foo2 = self.init_concepts(
+7 -7
View File
@@ -241,12 +241,12 @@ class TestSheerkaSetsManager(TestUsingMemoryBasedSheerka):
Concept("baz"),
)
sheerka.set_isa(context, foo, bar)
sheerka.set_isa(context, bar, baz)
sheerka.set_isa(context, sheerka.new("foo"), bar)
sheerka.set_isa(context, sheerka.new("bar"), baz)
assert sheerka.isa(foo, bar)
assert sheerka.isa(bar, baz)
assert sheerka.isa(foo, baz)
assert sheerka.isa(sheerka.new("foo"), bar)
assert sheerka.isa(sheerka.new("bar"), baz)
assert sheerka.isa(sheerka.new("foo"), baz)
def test_i_cannot_manage_isa_transitivity_when_using_body(self):
sheerka, context, one, another_one, number = self.init_concepts(
@@ -255,9 +255,9 @@ class TestSheerkaSetsManager(TestUsingMemoryBasedSheerka):
"number"
)
sheerka.set_isa(context, one, number)
sheerka.set_isa(context, sheerka.new("one"), number)
assert sheerka.isa(one, number) # sanity
assert sheerka.isa(sheerka.new("one"), number) # sanity
assert not sheerka.isa(another_one, number) # Correct this misbehaviour when BuiltinConcepts.IS is implemented
def test_concept_expression_recurse_id_is_updated(self):
+1 -1
View File
@@ -167,7 +167,7 @@ as:
assert len(res) == 1
assert not res[0].status
assert sheerka.isinstance(res[0].value, BuiltinConcepts.ALREADY_DEFINED)
assert sheerka.isinstance(res[0].value, BuiltinConcepts.CONCEPT_ALREADY_DEFINED)
@pytest.mark.parametrize("text", [
"",